00001
00002
00003
00004
00005
00006
00008 #include "Rotorooter/RotoClientBinaryFile.h"
00009
00010 #include <iostream>
00011 #include <fstream>
00012
00013
00014 #include "TString.h"
00015
00016 #include "MessageService/MsgService.h"
00017 #include "Conventions/Detector.h"
00018
00019 ClassImp(RotoClientBinaryFile)
00020 CVSID("$Id: RotoClientBinaryFile.cxx,v 1.8 2005/08/26 19:09:35 rhatcher Exp $");
00021
00022
00023 RotoClientBinaryFile::RotoClientBinaryFile(const Char_t* ipaddress, Int_t port,
00024 MinosRooterEntity whom)
00025 : RotoClient(ipaddress,port,whom), fFileName(""),
00026 #ifdef BINARYVIAOSTREAM
00027 fStream(0)
00028 #else
00029 fFile(0)
00030 #endif
00031 {
00032
00033 }
00034
00035
00036 RotoClientBinaryFile::~RotoClientBinaryFile()
00037 {
00038 #ifdef BINARYVIAOSTREAM
00039 if (fStream) {
00040 fStream->close();
00041 delete fStream;
00042 fStream = 0;
00043 }
00044 #else
00045 if (fFile) {
00046 fclose(fFile);
00047 delete fFile;
00048 fFile = 0;
00049 }
00050 #endif
00051
00052 }
00053
00054
00055 Bool_t RotoClientBinaryFile::Connected()
00056 {
00057 return true;
00058 }
00059
00060
00061 Bool_t RotoClientBinaryFile::OpenDAQFile(Int_t detector,
00062 Int_t run, Int_t subrun)
00063 {
00064 return OpenFile(BuildDAQFileName(detector,run,subrun));
00065 }
00066
00067 Bool_t RotoClientBinaryFile::CloseDAQFile(Int_t detector,
00068 Int_t run, Int_t subrun)
00069 {
00070 return CloseFile(BuildDAQFileName(detector,run,subrun));
00071 }
00072
00073 Bool_t RotoClientBinaryFile::OpenDCSFile(Int_t detector,
00074 Int_t sec, Int_t nanosec)
00075 {
00076 return OpenFile(BuildDCSFileName(detector,sec,nanosec));
00077 }
00078
00079 Bool_t RotoClientBinaryFile::CloseDCSFile(Int_t detector,
00080 Int_t sec, Int_t nanosec)
00081 {
00082 return CloseFile(BuildDCSFileName(detector,sec,nanosec));
00083 }
00084
00085 Bool_t RotoClientBinaryFile::OpenBeamMonFile(Int_t detector,
00086 Int_t sec, Int_t nanosec)
00087 {
00088 return OpenFile(BuildBeamMonFileName(detector,sec,nanosec));
00089 }
00090
00091 Bool_t RotoClientBinaryFile::CloseBeamMonFile(Int_t detector,
00092 Int_t sec, Int_t nanosec)
00093 {
00094 return CloseFile(BuildBeamMonFileName(detector,sec,nanosec));
00095 }
00096
00097
00098 string RotoClientBinaryFile::BuildDAQFileName(Int_t detector,
00099 Int_t run, Int_t subrun)
00100 {
00101
00102
00103
00104
00105
00106 return fIPAddress;
00107
00108 Detector::Detector_t det = (Detector::Detector_t)(detector);
00109 Char_t detchar = Detector::AsString(det)[0];
00110
00111
00112
00113
00114 return string(Form("%c%8.8d_%4.4d.mdaq.dat",detchar,run,subrun));
00115 }
00116
00117
00118 string RotoClientBinaryFile::BuildDCSFileName(Int_t detector,
00119 Int_t sec, Int_t nanosec)
00120 {
00121 Detector::Detector_t det = (Detector::Detector_t)(detector);
00122 Char_t detchar = Detector::AsString(det)[0];
00123 return string(Form("%c%8.8x_%8.8x.mdcs.dat",detchar,sec,nanosec));
00124 }
00125
00126
00127 string RotoClientBinaryFile::BuildBeamMonFileName(Int_t detector,
00128 Int_t sec, Int_t nanosec)
00129 {
00130 Detector::Detector_t det = (Detector::Detector_t)(detector);
00131 Char_t detchar = Detector::AsString(det)[0];
00132 return string(Form("%c%8.8x_%8.8x.mbeam.dat",detchar,sec,nanosec));
00133 }
00134
00135
00136 Bool_t RotoClientBinaryFile::OpenFile(const string& fname)
00137 {
00138 if (fFileName != "") {
00139 MSG("Roto",Msg::kWarning)
00140 << "RotoClientBinaryFile::OpenFile called while "
00141 << fFileName << " was still open" << endl;
00142 CloseFile(fFileName);
00143 }
00144
00145 fFileName = fname;
00146 #ifdef BINARYVIAOSTREAM
00147 fStream = new ofstream(fname.c_str(),ios::out|ios::binary|ios::trunc);
00148 #else
00149 fFile = fopen(fname.c_str(),"wb");
00150 #endif
00151
00152 MSG("Roto",Msg::kInfo) << "OpenFile \"" << fFileName << "\"" << endl;
00153
00154 return true;
00155 }
00156
00157
00158 Bool_t RotoClientBinaryFile::CloseFile(const string& fname)
00159 {
00160
00161 if (fname == fFileName) {
00162 if (fFileName != "") {
00163 #ifdef BINARYVIAOSTREAM
00164 if (fStream) {
00165 MSG("Roto",Msg::kInfo) << "CloseFile \"" << fFileName <<"\""<< endl;
00166 fStream->close();
00167 delete fStream;
00168 fStream = 0;
00169 }
00170 #else
00171 if (fFile) {
00172 MSG("Roto",Msg::kInfo) << "CloseFile \"" << fFileName <<"\""<< endl;
00173 fclose(fFile);
00174 fFile = 0;
00175 }
00176 #endif
00177 fFileName = "";
00178 }
00179 return true;
00180 } else {
00181 MSG("Roto",Msg::kWarning)
00182 << "RotoClientBinaryFile::CloseFile name mismatch" << endl
00183 << fFileName << " is open" << endl
00184 << fname << " was requested to be closed " << endl;
00185 return false;
00186 }
00187 }
00188
00189
00190 Bool_t RotoClientBinaryFile::GetRotoStatus(Char_t* statusBuffer, Int_t maxBytes)
00191 {
00192 snprintf(statusBuffer,maxBytes,"RotoClientBinaryFile:: status");
00193 return true;
00194 }
00195
00196
00197 Bool_t RotoClientBinaryFile::SendRecordBuffer(const void* buffer, Int_t nbytes)
00198 {
00199 MSG("Roto",Msg::kVerbose)
00200 << "RotoClientBinaryFile::SendRecordBuffer" << endl;
00201
00202 #ifdef BINARYVIAOSTREAM
00203 if (!fStream) {
00204 #else
00205 if (!fFile) {
00206 #endif
00207 MSG("Roto",Msg::kWarning)
00208 << "RotoClientBinaryFile::SendRecordBuffer - no open file" << endl;
00209 return false;
00210 }
00211
00212
00213
00214
00215 Int_t nrecbytes = nbytes + sizeof(Int_t);
00216 Int_t* nrb_addr = &nrecbytes;
00217 #ifdef BINARYVIAOSTREAM
00218 fStream->write((const char*)nrb_addr,sizeof(Int_t));
00219
00220
00221 fStream->write((const char*)buffer,nbytes);
00222
00223 fStream->flush();
00224 #else
00225 fwrite(nrb_addr,sizeof(Int_t),1,fFile);
00226 fwrite(buffer,1,nbytes,fFile);
00227 fflush(fFile);
00228 #endif
00229
00230 return true;
00231 }
00232
00233
00234 Bool_t RotoClientBinaryFile::SendShutdownMessage()
00235 {
00236
00237 MSG("Roto",Msg::kInfo)
00238 << "RotoClientBinaryFile::SendShutdownMessage" << endl;
00239
00240 CloseFile(fFileName);
00241 return true;
00242 }
00243
00244