From e3044689ddbcf2a4021d05dd7e86196bd0adec2e Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Tue, 28 Apr 2020 14:26:59 +0200 Subject: [PATCH] WIP --- slsReceiverSoftware/src/BinaryFile.cpp | 62 +++++++++++---- slsReceiverSoftware/src/BinaryFile.h | 41 +--------- slsReceiverSoftware/src/BinaryFileStatic.h | 92 ---------------------- 3 files changed, 48 insertions(+), 147 deletions(-) diff --git a/slsReceiverSoftware/src/BinaryFile.cpp b/slsReceiverSoftware/src/BinaryFile.cpp index 28929e309..55f36cad6 100755 --- a/slsReceiverSoftware/src/BinaryFile.cpp +++ b/slsReceiverSoftware/src/BinaryFile.cpp @@ -9,7 +9,7 @@ #include "receiver_defs.h" #include - +#include FILE* BinaryFile::masterfd = nullptr; @@ -20,7 +20,8 @@ BinaryFile::BinaryFile(int ind, uint32_t* maxf, File(ind, maxf, nd, fname, fpath, findex, owenable, dindex, nunits, nf, dr, portno, smode), filefd(nullptr), numFramesInFile(0), - numActualPacketsInFile(0) + numActualPacketsInFile(0), + maxMasterFileSize(2000) { #ifdef VERBOSE PrintMembers(); @@ -41,15 +42,27 @@ slsDetectorDefs::fileFormat BinaryFile::GetFileType() { return BINARY; } - void BinaryFile::CreateFile() { numFramesInFile = 0; numActualPacketsInFile = 0; - currentFileName = BinaryFileStatic::CreateFileName(*filePath, *fileNamePrefix, *fileIndex, - subFileIndex, *detIndex, *numUnitsPerDetector, index); + std::ostringstream os; + os << *filePath << "/" << *fileNamePrefix << "_d" + << (*detIndex * (*numUnitsPerDetector) + index) << "_f" << subFileIndex << '_' + << *fileIndex << ".raw"; + currentFileName = os.str(); - BinaryFileStatic::CreateDataFile(filefd, *overWriteEnable, currentFileName); + if (!(*overWriteEnable)){ + if (NULL == (filefd = fopen((const char *) currentFileName.c_str(), "wx"))){ + filefd = 0; + throw sls::RuntimeError("Could not create/overwrite file " + currentFileName); + } + } else if (NULL == (filefd = fopen((const char *) currentFileName.c_str(), "w"))){ + filefd = 0; + throw sls::RuntimeError("Could not create file " + currentFileName); + } + //setting to no file buffering + setvbuf(filefd, NULL, _IONBF, 0); if(!(*silentMode)) { LOG(logINFO) << "[" << *udpPortNumber << "]: Binary File created: " << currentFileName; @@ -57,15 +70,27 @@ void BinaryFile::CreateFile() { } void BinaryFile::CloseCurrentFile() { - BinaryFileStatic::CloseDataFile(filefd); + if (filefd) + fclose(filefd); + filefd = 0; } void BinaryFile::CloseAllFiles() { - BinaryFileStatic::CloseDataFile(filefd); - if (master && (*detIndex==0)) - BinaryFileStatic::CloseDataFile(masterfd); + CloseCurrentFile(); + if (master && (*detIndex==0)) { + if (masterfd) + fclose(masterfd); + masterfd = 0; + } } +int BinaryFile::WriteData(char* buf, int bsize) { + if (!filefd) + return 0; + return fwrite(buf, 1, bsize, filefd); +} + + void BinaryFile::WriteToFile(char* buffer, int buffersize, uint64_t fnum, uint32_t nump) { // check if maxframesperfile = 0 for infinite if ((*maxFramesPerFile) && (numFramesInFile >= (*maxFramesPerFile))) { @@ -81,13 +106,13 @@ void BinaryFile::WriteToFile(char* buffer, int buffersize, uint64_t fnum, uint32 // contiguous bitset if (sizeof(sls_bitset) == sizeof(bitset_storage)) { - ret = BinaryFileStatic::WriteDataFile(filefd, buffer, buffersize); + ret = BinaryFileStatic::WriteData(buffer, buffersize); } // not contiguous bitset else { // write detector header - ret = BinaryFileStatic::WriteDataFile(filefd, buffer, sizeof(sls_detector_header)); + ret = BinaryFileStatic::WriteData(buffer, sizeof(sls_detector_header)); // get contiguous representation of bit mask bitset_storage storage; @@ -96,11 +121,11 @@ void BinaryFile::WriteToFile(char* buffer, int buffersize, uint64_t fnum, uint32 for (int i = 0; i < MAX_NUM_PACKETS; ++i) storage[i >> 3] |= (bits[i] << (i & 7)); // write bitmask - ret += BinaryFileStatic::WriteDataFile(filefd, (char*)storage, sizeof(bitset_storage)); + ret += BinaryFileStatic::WriteData((char*)storage, sizeof(bitset_storage)); // write data - ret += BinaryFileStatic::WriteDataFile(filefd, - buffer + sizeof(sls_detector_header), buffersize - sizeof(sls_receiver_header)); + ret += BinaryFileStatic::WriteData(buffer + sizeof(sls_detector_header), + buffersize - sizeof(sls_receiver_header)); } // if write error @@ -116,8 +141,11 @@ void BinaryFile::CreateMasterFile(bool mfwenable, masterAttributes& attr) { numActualPacketsInFile = 0; if (mfwenable && master && (*detIndex==0)) { - masterFileName = BinaryFileStatic::CreateMasterFileName(*filePath, - *fileNamePrefix, *fileIndex); + + std::ostringstream os; + os << *filePath << "/" << *fileNamePrefix << "_master" + << "_" << *fileIndex << ".raw"; + masterFileName = os.str(); if(!(*silentMode)) { LOG(logINFO) << "Master File: " << masterFileName; } diff --git a/slsReceiverSoftware/src/BinaryFile.h b/slsReceiverSoftware/src/BinaryFile.h index a5337bbaf..a3f54ad6c 100755 --- a/slsReceiverSoftware/src/BinaryFile.h +++ b/slsReceiverSoftware/src/BinaryFile.h @@ -9,7 +9,6 @@ */ #include "File.h" -#include "BinaryFileStatic.h" #include @@ -38,39 +37,18 @@ class BinaryFile : private virtual slsDetectorDefs, public File, public BinaryFi int* nd, std::string* fname, std::string* fpath, uint64_t* findex, bool* owenable, int* dindex, int* nunits, uint64_t* nf, uint32_t* dr, uint32_t* portno, bool* smode); - - /** - * Destructor - */ ~BinaryFile(); - /** - * Print all member values - */ void PrintMembers(TLogLevel level = logDEBUG1) override; - - /** - * Create file - */ void CreateFile() override; - /** * Create master file * @param mfwenable master file write enable * @param attr master file attributes */ void CreateMasterFile(bool mfwenable, masterAttributes& attr) override; - - /** - * Close Current File - */ void CloseCurrentFile() override; - - /** - * Close all Files - */ void CloseAllFiles() override; - /** * Write data to file * @param buffer buffer to write from @@ -80,29 +58,16 @@ class BinaryFile : private virtual slsDetectorDefs, public File, public BinaryFi */ void WriteToFile(char* buffer, int buffersize, uint64_t fnum, uint32_t nump) override; - - private: - /** - * Get Type - * @return type - */ - fileFormat GetFileType() override; + fileFormat GetFileType() override; + int WriteData(char* buf, int bsize); - - - /** File Descriptor */ FILE* filefd; - - /** Master File Descriptor */ static FILE* masterfd; - - /** Number of frames in file */ uint32_t numFramesInFile; - - /** Number of actual packets caught in file */ uint64_t numActualPacketsInFile; + const int maxMasterFileSize; }; diff --git a/slsReceiverSoftware/src/BinaryFileStatic.h b/slsReceiverSoftware/src/BinaryFileStatic.h index 80f86fbc2..bbe212710 100755 --- a/slsReceiverSoftware/src/BinaryFileStatic.h +++ b/slsReceiverSoftware/src/BinaryFileStatic.h @@ -15,80 +15,12 @@ #include #include -#define MAX_MASTER_FILE_LENGTH 2000 class BinaryFileStatic { public: - /** Constructor */ - BinaryFileStatic(){}; - /** Destructor */ - virtual ~BinaryFileStatic(){}; - - /** - * Create File Name in format fpath/fnameprefix_fx_dy_z.raw, - * where x is fnum, y is (dindex * numunits + unitindex) and z is findex - * @param fpath file path - * @param fnameprefix file name prefix - * @param findex file index - * @param subfindex sub file index - * @param dindex readout index - * @param numunits number of units per readout. eg. eiger has 2 udp units per readout - * @param unitindex unit index - * @returns complete file name created - */ - static std::string CreateFileName(std::string fpath, std::string fprefix, - uint64_t findex, uint64_t subfindex, - int dindex, int numunits = 1, - int unitindex = 0) { - std::ostringstream os; - os << fpath << "/" << fprefix << "_d" - << (dindex * numunits + unitindex) << "_f" << subfindex << '_' - << findex << ".raw"; - return os.str(); - } - - /** - * Create file names for master file - * @param fpath file path - * @param fnameprefix file name prefix - * @param findex file index - * @returns master file name - */ - static std::string CreateMasterFileName(std::string fpath, std::string fnameprefix, - uint64_t findex) { - std::ostringstream os; - os << fpath << "/" << fnameprefix << "_master" - << "_" << findex << ".raw"; - return os.str(); - } - - /** - * Close File - * @param fd file pointer - */ - static void CloseDataFile(FILE*& fd) - { - if (fd) - fclose(fd); - fd = 0; - } - - /** - * Write data to file - * @param fd file pointer - * @param buf buffer to write from - * @param bsize size of buffer - * @returns number of elements written - */ - static int WriteDataFile(FILE* fd, char* buf, int bsize) - { - if (!fd) - return 0; - return fwrite(buf, 1, bsize, fd); - } /** @@ -188,27 +120,3 @@ class BinaryFileStatic { } - /** - * Create File - * @param fd file pointer - * @param owenable overwrite enable - * @param fname complete file name - * @returns 0 for success and 1 for fail - */ - static void CreateDataFile(FILE*& fd, bool owenable, std::string fname) - { - if(!owenable){ - if (NULL == (fd = fopen((const char *) fname.c_str(), "wx"))){ - fd = 0; - throw sls::RuntimeError("Could not create/overwrite file " + fname); - } - } else if (NULL == (fd = fopen((const char *) fname.c_str(), "w"))){ - fd = 0; - throw sls::RuntimeError("Could not create file " + fname); - } - //setting to no file buffering - setvbuf(fd, NULL, _IONBF, 0); - } - -}; -