mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 05:17:13 +02:00
format support lib
This commit is contained in:
2
slsSupportLib/src/ClientSocket.cpp
Executable file → Normal file
2
slsSupportLib/src/ClientSocket.cpp
Executable file → Normal file
@ -72,7 +72,7 @@ void ClientSocket::readReply(int &ret, void *retval, size_t retval_size) {
|
||||
|
||||
try {
|
||||
Receive(&ret, sizeof(ret));
|
||||
}
|
||||
}
|
||||
// debugging
|
||||
catch (sls::SocketError &e) {
|
||||
if (socketType == "Receiver") {
|
||||
|
4
slsSupportLib/src/DataSocket.cpp
Executable file → Normal file
4
slsSupportLib/src/DataSocket.cpp
Executable file → Normal file
@ -136,8 +136,6 @@ void DataSocket::shutDownSocket() {
|
||||
close();
|
||||
}
|
||||
|
||||
void DataSocket::shutdown(){
|
||||
::shutdown(sockfd_, SHUT_RDWR);
|
||||
}
|
||||
void DataSocket::shutdown() { ::shutdown(sockfd_, SHUT_RDWR); }
|
||||
|
||||
} // namespace sls
|
||||
|
@ -5,7 +5,7 @@
|
||||
namespace sls {
|
||||
|
||||
int ServerInterface::sendResult(int ret, void *retval, int retvalSize,
|
||||
char *mess) {
|
||||
char *mess) {
|
||||
|
||||
write(&ret, sizeof(ret));
|
||||
if (ret == defs::FAIL) {
|
||||
@ -13,7 +13,7 @@ int ServerInterface::sendResult(int ret, void *retval, int retvalSize,
|
||||
write(mess, MAX_STR_LENGTH);
|
||||
} else {
|
||||
LOG(logERROR) << "No error message provided for this "
|
||||
"failure. Will mess up TCP\n";
|
||||
"failure. Will mess up TCP\n";
|
||||
}
|
||||
} else {
|
||||
write(retval, retvalSize);
|
||||
|
11
slsSupportLib/src/ServerSocket.cpp
Executable file → Normal file
11
slsSupportLib/src/ServerSocket.cpp
Executable file → Normal file
@ -1,5 +1,5 @@
|
||||
#include "ServerInterface.h"
|
||||
#include "ServerSocket.h"
|
||||
#include "ServerInterface.h"
|
||||
|
||||
#include "DataSocket.h"
|
||||
#include "logger.h"
|
||||
@ -13,8 +13,8 @@
|
||||
#include <stdexcept>
|
||||
#include <unistd.h>
|
||||
#define DEFAULT_PACKET_SIZE 1286
|
||||
#define SOCKET_BUFFER_SIZE (100 * 1024 * 1024) // 100 MB
|
||||
#define DEFAULT_BACKLOG 5
|
||||
#define SOCKET_BUFFER_SIZE (100 * 1024 * 1024) // 100 MB
|
||||
#define DEFAULT_BACKLOG 5
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -29,7 +29,8 @@ ServerSocket::ServerSocket(int port)
|
||||
if (bind(getSocketId(), (struct sockaddr *)&serverAddr,
|
||||
sizeof(serverAddr)) != 0) {
|
||||
close();
|
||||
throw sls::SocketError("Server ERROR: cannot bind socket. Please check if another instance is running.");
|
||||
throw sls::SocketError("Server ERROR: cannot bind socket. Please check "
|
||||
"if another instance is running.");
|
||||
}
|
||||
if (listen(getSocketId(), DEFAULT_BACKLOG) != 0) {
|
||||
close();
|
||||
@ -38,7 +39,7 @@ ServerSocket::ServerSocket(int port)
|
||||
}
|
||||
|
||||
ServerInterface ServerSocket::accept() {
|
||||
lastClient = thisClient; //update from previous connection
|
||||
lastClient = thisClient; // update from previous connection
|
||||
struct sockaddr_in clientAddr;
|
||||
socklen_t addr_size = sizeof clientAddr;
|
||||
int newSocket =
|
||||
|
@ -403,7 +403,6 @@ template <> defs::burstMode StringTo(const std::string &s) {
|
||||
throw sls::RuntimeError("Unknown burst mode " + s);
|
||||
}
|
||||
|
||||
|
||||
template <> defs::timingSourceType StringTo(const std::string &s) {
|
||||
if (s == "internal")
|
||||
return defs::TIMING_INTERNAL;
|
||||
|
@ -56,22 +56,21 @@ UdpRxSocket::UdpRxSocket(int port, ssize_t packet_size, const char *hostname,
|
||||
UdpRxSocket::~UdpRxSocket() { Shutdown(); }
|
||||
ssize_t UdpRxSocket::getPacketSize() const noexcept { return packet_size_; }
|
||||
|
||||
bool UdpRxSocket::ReceivePacket(char *dst) noexcept{
|
||||
bool UdpRxSocket::ReceivePacket(char *dst) noexcept {
|
||||
auto bytes_received =
|
||||
recvfrom(sockfd_, dst, packet_size_, 0, nullptr, nullptr);
|
||||
return bytes_received == packet_size_;
|
||||
}
|
||||
|
||||
ssize_t UdpRxSocket::ReceiveDataOnly(char *dst) noexcept {
|
||||
auto r = recvfrom(sockfd_, dst, packet_size_, 0, nullptr, nullptr);
|
||||
constexpr ssize_t eiger_header_packet =
|
||||
40; // only detector that has this
|
||||
if (r == eiger_header_packet) {
|
||||
LOG(logWARNING) << "Got header pkg";
|
||||
r = recvfrom(sockfd_, dst, packet_size_, 0, nullptr, nullptr);
|
||||
}
|
||||
return r;
|
||||
auto r = recvfrom(sockfd_, dst, packet_size_, 0, nullptr, nullptr);
|
||||
constexpr ssize_t eiger_header_packet = 40; // only detector that has this
|
||||
if (r == eiger_header_packet) {
|
||||
LOG(logWARNING) << "Got header pkg";
|
||||
r = recvfrom(sockfd_, dst, packet_size_, 0, nullptr, nullptr);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
size_t UdpRxSocket::getBufferSize() const {
|
||||
size_t ret = 0;
|
||||
@ -87,10 +86,10 @@ void UdpRxSocket::setBufferSize(ssize_t size) {
|
||||
}
|
||||
|
||||
void UdpRxSocket::Shutdown() {
|
||||
shutdown(sockfd_, SHUT_RDWR);
|
||||
if (sockfd_ >= 0) {
|
||||
close(sockfd_);
|
||||
sockfd_ = -1;
|
||||
}
|
||||
shutdown(sockfd_, SHUT_RDWR);
|
||||
if (sockfd_ >= 0) {
|
||||
close(sockfd_);
|
||||
sockfd_ = -1;
|
||||
}
|
||||
}
|
||||
} // namespace sls
|
@ -1,12 +1,12 @@
|
||||
#include "ZmqSocket.h"
|
||||
#include <zmq.h>
|
||||
#include <vector>
|
||||
#include <arpa/inet.h> //inet_ntoa
|
||||
#include <errno.h>
|
||||
#include <iostream>
|
||||
#include <netdb.h> //gethostbyname()
|
||||
#include <netdb.h> //gethostbyname()
|
||||
#include <string.h>
|
||||
#include <unistd.h> //usleep in some machines
|
||||
#include <vector>
|
||||
#include <zmq.h>
|
||||
|
||||
using namespace rapidjson;
|
||||
ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
|
||||
@ -145,8 +145,7 @@ int ZmqSocket::ConvertInternetAddresstoIpString(struct addrinfo *res, char *ip,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ZmqSocket::SendHeader(
|
||||
int index, zmqHeader header) {
|
||||
int ZmqSocket::SendHeader(int index, zmqHeader header) {
|
||||
|
||||
/** Json Header Format */
|
||||
const char jsonHeaderFormat[] = "{"
|
||||
@ -183,44 +182,25 @@ int ZmqSocket::SendHeader(
|
||||
|
||||
; //"}\n";
|
||||
char buf[MAX_STR_LENGTH] = "";
|
||||
sprintf(buf, jsonHeaderFormat,
|
||||
header.jsonversion,
|
||||
header.dynamicRange,
|
||||
header.fileIndex,
|
||||
header.ndetx,
|
||||
header.ndety,
|
||||
header.npixelsx,
|
||||
header.npixelsy,
|
||||
header.imageSize,
|
||||
header.acqIndex,
|
||||
header.frameIndex,
|
||||
header.progress,
|
||||
header.fname.c_str(),
|
||||
header.data ? 1 : 0,
|
||||
header.completeImage ? 1 : 0,
|
||||
sprintf(buf, jsonHeaderFormat, header.jsonversion, header.dynamicRange,
|
||||
header.fileIndex, header.ndetx, header.ndety, header.npixelsx,
|
||||
header.npixelsy, header.imageSize, header.acqIndex,
|
||||
header.frameIndex, header.progress, header.fname.c_str(),
|
||||
header.data ? 1 : 0, header.completeImage ? 1 : 0,
|
||||
|
||||
header.frameNumber,
|
||||
header.expLength,
|
||||
header.packetNumber,
|
||||
header.bunchId,
|
||||
header.timestamp,
|
||||
header.modId,
|
||||
header.row,
|
||||
header.column,
|
||||
header.reserved,
|
||||
header.debug,
|
||||
header.roundRNumber,
|
||||
header.detType,
|
||||
header.version,
|
||||
header.frameNumber, header.expLength, header.packetNumber,
|
||||
header.bunchId, header.timestamp, header.modId, header.row,
|
||||
header.column, header.reserved, header.debug, header.roundRNumber,
|
||||
header.detType, header.version,
|
||||
|
||||
// additional stuff
|
||||
header.flippedDataX,
|
||||
header.quad);
|
||||
header.flippedDataX, header.quad);
|
||||
|
||||
if (header.addJsonHeader.size() > 0) {
|
||||
strcat(buf, ", ");
|
||||
strcat(buf, "\"addJsonHeader\": {");
|
||||
for (auto it = header.addJsonHeader.begin(); it != header.addJsonHeader.end(); ++it) {
|
||||
for (auto it = header.addJsonHeader.begin();
|
||||
it != header.addJsonHeader.end(); ++it) {
|
||||
if (it != header.addJsonHeader.begin()) {
|
||||
strcat(buf, ", ");
|
||||
}
|
||||
@ -260,7 +240,7 @@ int ZmqSocket::SendData(char *buf, int length) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ZmqSocket::ReceiveHeader(const int index, zmqHeader& zHeader,
|
||||
int ZmqSocket::ReceiveHeader(const int index, zmqHeader &zHeader,
|
||||
uint32_t version) {
|
||||
std::vector<char> buffer(MAX_STR_LENGTH);
|
||||
int len =
|
||||
@ -292,7 +272,7 @@ int ZmqSocket::ReceiveHeader(const int index, zmqHeader& zHeader,
|
||||
};
|
||||
|
||||
int ZmqSocket::ParseHeader(const int index, int length, char *buff,
|
||||
zmqHeader& zHeader, uint32_t version) {
|
||||
zmqHeader &zHeader, uint32_t version) {
|
||||
Document document;
|
||||
if (document.Parse(buff, length).HasParseError()) {
|
||||
LOG(logERROR) << index << " Could not parse. len:" << length
|
||||
@ -318,7 +298,7 @@ int ZmqSocket::ParseHeader(const int index, int length, char *buff,
|
||||
// parse
|
||||
zHeader.data = ((document["data"].GetUint()) == 0) ? false : true;
|
||||
zHeader.dynamicRange = document["bitmode"].GetUint();
|
||||
zHeader.fileIndex = document["fileIndex"].GetUint64();
|
||||
zHeader.fileIndex = document["fileIndex"].GetUint64();
|
||||
zHeader.ndetx = document["detshape"][0].GetUint();
|
||||
zHeader.ndety = document["detshape"][1].GetUint();
|
||||
zHeader.npixelsx = document["shape"][0].GetUint();
|
||||
@ -348,10 +328,12 @@ int ZmqSocket::ParseHeader(const int index, int length, char *buff,
|
||||
zHeader.completeImage = document["completeImage"].GetUint();
|
||||
|
||||
if (document.HasMember("addJsonHeader")) {
|
||||
const Value& V = document["addJsonHeader"];
|
||||
const Value &V = document["addJsonHeader"];
|
||||
zHeader.addJsonHeader.clear();
|
||||
for (Value::ConstMemberIterator iter = V.MemberBegin(); iter != V.MemberEnd(); ++iter){
|
||||
zHeader.addJsonHeader[iter->name.GetString()] = iter->value.GetString();
|
||||
for (Value::ConstMemberIterator iter = V.MemberBegin();
|
||||
iter != V.MemberEnd(); ++iter) {
|
||||
zHeader.addJsonHeader[iter->name.GetString()] =
|
||||
iter->value.GetString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -451,7 +433,7 @@ void ZmqSocket::PrintError() {
|
||||
}
|
||||
}
|
||||
|
||||
//Nested class to do RAII handling of socket descriptors
|
||||
// Nested class to do RAII handling of socket descriptors
|
||||
ZmqSocket::mySocketDescriptors::mySocketDescriptors()
|
||||
: server(false), contextDescriptor(0), socketDescriptor(0){};
|
||||
ZmqSocket::mySocketDescriptors::~mySocketDescriptors() {
|
||||
|
128
slsSupportLib/src/file_utils.cpp
Executable file → Normal file
128
slsSupportLib/src/file_utils.cpp
Executable file → Normal file
@ -2,85 +2,79 @@
|
||||
#include "logger.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
|
||||
int readDataFile(std::ifstream &infile, short int *data, int nch, int offset) {
|
||||
int ichan, iline=0;
|
||||
short int idata;
|
||||
int interrupt=0;
|
||||
std::string str;
|
||||
while (infile.good() and interrupt==0) {
|
||||
getline(infile,str);
|
||||
std::istringstream ssstr(str);
|
||||
ssstr >> ichan >> idata;
|
||||
if (ssstr.fail() || ssstr.bad()) {
|
||||
interrupt=1;
|
||||
break;
|
||||
}
|
||||
if (iline<nch) {
|
||||
if (ichan>=offset) {
|
||||
data[iline]=idata;
|
||||
iline++;
|
||||
}
|
||||
} else {
|
||||
interrupt=1;
|
||||
break;
|
||||
}
|
||||
return iline;
|
||||
};
|
||||
return iline;
|
||||
int ichan, iline = 0;
|
||||
short int idata;
|
||||
int interrupt = 0;
|
||||
std::string str;
|
||||
while (infile.good() and interrupt == 0) {
|
||||
getline(infile, str);
|
||||
std::istringstream ssstr(str);
|
||||
ssstr >> ichan >> idata;
|
||||
if (ssstr.fail() || ssstr.bad()) {
|
||||
interrupt = 1;
|
||||
break;
|
||||
}
|
||||
if (iline < nch) {
|
||||
if (ichan >= offset) {
|
||||
data[iline] = idata;
|
||||
iline++;
|
||||
}
|
||||
} else {
|
||||
interrupt = 1;
|
||||
break;
|
||||
}
|
||||
return iline;
|
||||
};
|
||||
return iline;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int readDataFile(std::string fname, short int *data, int nch) {
|
||||
std::ifstream infile;
|
||||
int iline=0;
|
||||
std::string str;
|
||||
infile.open(fname.c_str(), std::ios_base::in);
|
||||
if (infile.is_open()) {
|
||||
iline=readDataFile(infile, data, nch, 0);
|
||||
infile.close();
|
||||
} else {
|
||||
LOG(logERROR) << "Could not read file " << fname;
|
||||
return -1;
|
||||
}
|
||||
return iline;
|
||||
std::ifstream infile;
|
||||
int iline = 0;
|
||||
std::string str;
|
||||
infile.open(fname.c_str(), std::ios_base::in);
|
||||
if (infile.is_open()) {
|
||||
iline = readDataFile(infile, data, nch, 0);
|
||||
infile.close();
|
||||
} else {
|
||||
LOG(logERROR) << "Could not read file " << fname;
|
||||
return -1;
|
||||
}
|
||||
return iline;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int writeDataFile(std::ofstream &outfile,int nch, short int *data, int offset) {
|
||||
if (data==nullptr)
|
||||
return slsDetectorDefs::FAIL;
|
||||
for (int ichan=0; ichan<nch; ichan++)
|
||||
outfile << ichan+offset << " " << *(data+ichan) << std::endl;
|
||||
return slsDetectorDefs::OK;
|
||||
int writeDataFile(std::ofstream &outfile, int nch, short int *data,
|
||||
int offset) {
|
||||
if (data == nullptr)
|
||||
return slsDetectorDefs::FAIL;
|
||||
for (int ichan = 0; ichan < nch; ichan++)
|
||||
outfile << ichan + offset << " " << *(data + ichan) << std::endl;
|
||||
return slsDetectorDefs::OK;
|
||||
}
|
||||
|
||||
|
||||
int writeDataFile(std::string fname,int nch, short int *data) {
|
||||
std::ofstream outfile;
|
||||
if (data==nullptr)
|
||||
return slsDetectorDefs::FAIL;
|
||||
outfile.open (fname.c_str(),std::ios_base::out);
|
||||
if (outfile.is_open()) {
|
||||
writeDataFile(outfile, nch, data, 0);
|
||||
outfile.close();
|
||||
return slsDetectorDefs::OK;
|
||||
} else {
|
||||
LOG(logERROR) << "Could not open file " << fname << "for writing";
|
||||
return slsDetectorDefs::FAIL;
|
||||
}
|
||||
int writeDataFile(std::string fname, int nch, short int *data) {
|
||||
std::ofstream outfile;
|
||||
if (data == nullptr)
|
||||
return slsDetectorDefs::FAIL;
|
||||
outfile.open(fname.c_str(), std::ios_base::out);
|
||||
if (outfile.is_open()) {
|
||||
writeDataFile(outfile, nch, data, 0);
|
||||
outfile.close();
|
||||
return slsDetectorDefs::OK;
|
||||
} else {
|
||||
LOG(logERROR) << "Could not open file " << fname << "for writing";
|
||||
return slsDetectorDefs::FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void mkdir_p(const std::string& path, std::string dir) {
|
||||
void mkdir_p(const std::string &path, std::string dir) {
|
||||
if (path.length() == 0)
|
||||
return;
|
||||
|
||||
@ -90,7 +84,7 @@ void mkdir_p(const std::string& path, std::string dir) {
|
||||
if (path[i] == '/')
|
||||
break;
|
||||
}
|
||||
if(mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0){
|
||||
if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
|
||||
if (errno != EEXIST)
|
||||
throw sls::RuntimeError("Could not create: " + dir);
|
||||
}
|
||||
@ -98,7 +92,3 @@ void mkdir_p(const std::string& path, std::string dir) {
|
||||
if (i + 1 < path.length())
|
||||
mkdir_p(path.substr(i + 1), dir);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
7
slsSupportLib/src/network_utils.cpp
Executable file → Normal file
7
slsSupportLib/src/network_utils.cpp
Executable file → Normal file
@ -1,5 +1,6 @@
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
#include "network_utils.h"
|
||||
#include <algorithm>
|
||||
#include <arpa/inet.h>
|
||||
#include <cassert>
|
||||
@ -15,7 +16,6 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "network_utils.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -85,7 +85,8 @@ IpAddr HostnameToIp(const char *hostname) {
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if (getaddrinfo(hostname, nullptr, &hints, &result)) {
|
||||
freeaddrinfo(result);
|
||||
throw RuntimeError("Could not convert hostname (" + std::string(hostname) + ") to ip");
|
||||
throw RuntimeError("Could not convert hostname (" +
|
||||
std::string(hostname) + ") to ip");
|
||||
}
|
||||
uint32_t ip = ((sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
|
||||
freeaddrinfo(result);
|
||||
@ -133,7 +134,7 @@ IpAddr InterfaceNameToIp(const std::string &ifn) {
|
||||
continue;
|
||||
|
||||
auto s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
|
||||
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
||||
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
||||
|
||||
if ((strcmp(ifa->ifa_name, ifn.c_str()) == 0) &&
|
||||
(ifa->ifa_addr->sa_family == AF_INET)) {
|
||||
|
Reference in New Issue
Block a user