CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SMProxy.cc
Go to the documentation of this file.
1 //
3 // SMProxy
4 // -------
5 //
6 // 03/20/2007 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
9 
11 
12 #include "xdaq/Application.h"
13 
14 #include "toolbox/mem/Reference.h"
15 #include "toolbox/mem/MemoryPoolFactory.h"
16 #include "toolbox/mem/exception/Exception.h"
17 
18 #include "i2o/Method.h"
19 #include "i2o/utils/AddressMap.h"
20 
21 #include "xcept/tools.h"
22 
23 #include <iostream>
24 #include <cmath>
25 
26 using namespace std;
27 using namespace evf;
28 
30 // construction/destruction
32 
33 //______________________________________________________________________________
34 SMProxy::SMProxy(xdaq::ApplicationDescriptor *fuAppDesc,
35  xdaq::ApplicationDescriptor *smAppDesc,
36  xdaq::ApplicationContext *fuAppContext, toolbox::mem::Pool *i2oPool) :
37  log_(fuAppContext->getLogger()), fuAppDesc_(fuAppDesc),
38  smAppDesc_(smAppDesc), fuAppContext_(fuAppContext),
39  i2oPool_(i2oPool),
40  initHeaderSize_(sizeof(I2O_SM_PREAMBLE_MESSAGE_FRAME)),
41  dataHeaderSize_(sizeof(I2O_SM_DATA_MESSAGE_FRAME)),
42  dqmHeaderSize_(sizeof(I2O_SM_DQM_MESSAGE_FRAME)) {
43  fuUrl_ = fuAppDesc_->getContextDescriptor()->getURL();
44  if (fuUrl_.size() >= MAX_I2O_SM_URLCHARS)
45  fuUrl_ = fuUrl_.substr(0, MAX_I2O_SM_URLCHARS - 1);
46 
47  fuClassName_ = fuAppDesc_->getClassName();
48  if (fuClassName_.size() >= MAX_I2O_SM_URLCHARS)
50 }
51 
52 //______________________________________________________________________________
54 
55 }
56 
58 // implementation of member functions
60 
61 //______________________________________________________________________________
63  UInt_t fuProcessId, UInt_t fuGuid, UChar_t*data, UInt_t dataSize,
64  UInt_t nExpectedEPs) throw (evf::Exception) {
65  UInt_t totalSize = 0;
66  MemRef_t* bufRef = createFragmentChain(I2O_SM_PREAMBLE, initHeaderSize_,
67  data, dataSize, totalSize);
68 
70  MemRef_t* next = bufRef;
71  do {
72  msg = (I2O_SM_PREAMBLE_MESSAGE_FRAME*) next->getDataLocation();
73  msg->rbBufferID = fuResourceId;
74  msg->outModID = outModId;
75  msg->fuProcID = fuProcessId;
76  msg->fuGUID = fuGuid;
77  msg->nExpectedEPs = nExpectedEPs;
78  } while ((next = next->getNextReference()));
79 
80  try {
81  fuAppContext_->postFrame(bufRef, fuAppDesc_, smAppDesc_);
82  } catch (xdaq::exception::Exception &e) {
83  string msg = "Failed to post INIT Message.";
84  XCEPT_RETHROW(evf::Exception, msg, e);
85  }
86 
87  return totalSize;
88 }
89 
90 //______________________________________________________________________________
92  UInt_t evtNumber, UInt_t outModId, UInt_t fuProcessId, UInt_t fuGuid,
93  UChar_t *data, UInt_t dataSize) throw (evf::Exception) {
94  UInt_t totalSize = 0;
95  MemRef_t* bufRef = createFragmentChain(I2O_SM_DATA, dataHeaderSize_, data,
96  dataSize, totalSize);
97 
99  MemRef_t* next = bufRef;
100  do {
101  msg = (I2O_SM_DATA_MESSAGE_FRAME*) next->getDataLocation();
102  msg->rbBufferID = fuResourceId;
103  msg->runID = runNumber;
104  msg->eventID = evtNumber;
105  msg->outModID = outModId;
106  msg->fuProcID = fuProcessId;
107  msg->fuGUID = fuGuid;
108  } while ((next = next->getNextReference()));
109 
110  try {
111  fuAppContext_->postFrame(bufRef, fuAppDesc_, smAppDesc_);
112  } catch (xdaq::exception::Exception &e) {
113  string errmsg = "Failed to post DATA Message.";
114  LOG4CPLUS_FATAL(log_, errmsg);
115  XCEPT_RETHROW(evf::Exception, errmsg, e);
116  }
117 
118  return totalSize;
119 }
120 
121 //______________________________________________________________________________
123  UInt_t evtNumber, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data,
124  UInt_t dataSize) throw (evf::Exception) {
125  UInt_t totalSize = 0;
126  MemRef_t* bufRef = createFragmentChain(I2O_SM_ERROR, dataHeaderSize_, data,
127  dataSize, totalSize);
128 
130  MemRef_t* next = bufRef;
131  do {
132  msg = (I2O_SM_DATA_MESSAGE_FRAME*) next->getDataLocation();
133  msg->rbBufferID = fuResourceId;
134  msg->runID = runNumber;
135  msg->eventID = evtNumber;
136  msg->outModID = 0xffffffff;
137  msg->fuProcID = fuProcessId;
138  msg->fuGUID = fuGuid;
139  } while ((next = next->getNextReference()));
140 
141  try {
142  fuAppContext_->postFrame(bufRef, fuAppDesc_, smAppDesc_);
143  } catch (xdaq::exception::Exception &e) {
144  string errmsg = "Failed to post ERROR Message.";
145  LOG4CPLUS_FATAL(log_, errmsg);
146  XCEPT_RETHROW(evf::Exception, errmsg, e);
147  }
148 
149  return totalSize;
150 }
151 
152 //______________________________________________________________________________
154  UInt_t evtAtUpdate, UInt_t folderId, UInt_t fuProcessId, UInt_t fuGuid,
155  UChar_t*data, UInt_t dataSize) throw (evf::Exception) {
156  UInt_t totalSize = 0;
157  MemRef_t* bufRef = createFragmentChain(I2O_SM_DQM, dqmHeaderSize_, data,
158  dataSize, totalSize);
159 
161  MemRef_t* next = bufRef;
162  do {
163  msg = (I2O_SM_DQM_MESSAGE_FRAME*) next->getDataLocation();
164  msg->rbBufferID = fuDqmId;
165  msg->runID = runNumber;
166  msg->eventAtUpdateID = evtAtUpdate;
167  msg->folderID = folderId;
168  msg->fuProcID = fuProcessId;
169  msg->fuGUID = fuGuid;
170  } while ((next = next->getNextReference()));
171 
172  try {
173  fuAppContext_->postFrame(bufRef, fuAppDesc_, smAppDesc_);
174  } catch (xdaq::exception::Exception &e) {
175  string errmsg = "Failed to post DQM Message.";
176  LOG4CPLUS_FATAL(log_, errmsg);
177  XCEPT_RETHROW(evf::Exception, errmsg, e);
178  }
179 
180  return totalSize;
181 }
182 
184 // implementation of private member functions
186 
187 //______________________________________________________________________________
189  UInt_t headerSize, UChar_t *data, UInt_t dataSize, UInt_t &totalSize)
190  throw (evf::Exception) {
191  totalSize = 0;
192 
193  UInt_t fragmentDataSizeMax = I2O_MAX_SIZE - headerSize;
194  UInt_t fragmentCount = (dataSize / fragmentDataSizeMax);
195  if (dataSize % fragmentDataSizeMax)
196  ++fragmentCount;
197 
198  UInt_t currentPosition = 0;
199  UInt_t remainingDataSize = dataSize;
200 
201  MemRef_t *head(0);
202  MemRef_t *tail(0);
203 
204  try {
205 
206  for (UInt_t iFragment = 0; iFragment < fragmentCount; iFragment++) {
207 
208  UInt_t fragmentDataSize = fragmentDataSizeMax;
209  UInt_t fragmentSize = fragmentDataSize + headerSize;
210 
211  if (remainingDataSize < fragmentDataSizeMax) {
212  fragmentDataSize = remainingDataSize;
213  fragmentSize = fragmentDataSize + headerSize;
214  if (fragmentSize & 0x7)
215  fragmentSize = ((fragmentSize >> 3) + 1) << 3;
216  }
217 
218  // allocate the fragment buffer from the pool
219  toolbox::mem::Reference *bufRef =
220  toolbox::mem::getMemoryPoolFactory()->getFrame(i2oPool_,
221  fragmentSize);
222 
223  // set up pointers to the allocated message buffer
224  I2O_MESSAGE_FRAME *stdMsg;
225  I2O_PRIVATE_MESSAGE_FRAME *pvtMsg;
227 
228  stdMsg = (I2O_MESSAGE_FRAME*) bufRef->getDataLocation();
229  pvtMsg = (I2O_PRIVATE_MESSAGE_FRAME*) stdMsg;
230  msg = (I2O_SM_MULTIPART_MESSAGE_FRAME*) stdMsg;
231 
232  stdMsg->VersionOffset = 0;
233  stdMsg->MsgFlags = 0; // normal message (not multicast)
234  stdMsg->MessageSize = fragmentSize >> 2;
235  stdMsg->Function = I2O_PRIVATE_MESSAGE;
236  stdMsg->InitiatorAddress = i2o::utils::getAddressMap()->getTid(
237  fuAppDesc_);
238  stdMsg->TargetAddress = i2o::utils::getAddressMap()->getTid(
239  smAppDesc_);
240 
241  pvtMsg->XFunctionCode = i2oFunctionCode;
242  pvtMsg->OrganizationID = XDAQ_ORGANIZATION_ID;
243 
244  msg->dataSize = fragmentDataSize;
245  msg->hltLocalId = fuAppDesc_->getLocalId();
246  msg->hltInstance = fuAppDesc_->getInstance();
247  msg->hltTid = i2o::utils::getAddressMap()->getTid(fuAppDesc_);
248  msg->numFrames = fragmentCount;
249  msg->frameCount = iFragment;
250  msg->originalSize = dataSize;
251 
252  for (UInt_t i = 0; i < fuUrl_.size(); i++)
253  msg->hltURL[i] = fuUrl_[i];
254  msg->hltURL[fuUrl_.size()] = '\0';
255 
256  for (UInt_t i = 0; i < fuClassName_.size(); i++)
257  msg->hltClassName[i] = fuClassName_[i];
258  msg->hltClassName[fuClassName_.size()] = '\0';
259 
260  if (iFragment == 0) {
261  head = bufRef;
262  tail = bufRef;
263  } else {
264  tail->setNextReference(bufRef);
265  tail = bufRef;
266  }
267 
268  if (fragmentDataSize != 0) {
269  UChar_t* targetAddr = (UChar_t*) msg + headerSize;
270  std::copy(data + currentPosition,
271  data + currentPosition + fragmentDataSize, targetAddr);
272  }
273 
274  bufRef->setDataSize(fragmentSize);
275  remainingDataSize -= fragmentDataSize;
276  currentPosition += fragmentDataSize;
277  totalSize += fragmentSize;
278 
279  } // for (iFragment ...)
281  if (0 != head)
282  head->release();
283  totalSize = 0;
284  string errmsg = "Failed to allocate buffer reference.";
285  LOG4CPLUS_FATAL(log_, errmsg);
286  XCEPT_RETHROW(evf::Exception, errmsg, e);
287  } catch (xdaq::exception::ApplicationDescriptorNotFound& e) {
288  if (0 != head)
289  head->release();
290  totalSize = 0;
291  string errmsg = "Failed to get tid.";
292  LOG4CPLUS_FATAL(log_, errmsg);
293  XCEPT_RETHROW(evf::Exception, errmsg, e);
294  }
295 
296  return head;
297 }
int i
Definition: DBlmapReader.cc:9
#define I2O_SM_ERROR
Definition: i2oEvfMsgs.h:23
unsigned short UShort_t
Definition: FUTypes.h:13
xdaq::ApplicationDescriptor * fuAppDesc_
Definition: SMProxy.h:81
std::string fuUrl_
Definition: SMProxy.h:90
#define I2O_SM_DQM
Definition: i2oEvfMsgs.h:25
#define I2O_SM_PREAMBLE
Definition: i2oEvfMsgs.h:21
toolbox::mem::Reference MemRef_t
Definition: FUTypes.h:10
std::string fuClassName_
Definition: SMProxy.h:91
UInt_t sendDqmEvent(UInt_t fuDqmId, UInt_t runNumber, UInt_t evtAtUpdate, UInt_t folderId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data, UInt_t dataSize)
Definition: SMProxy.cc:153
unsigned char UChar_t
Definition: FUTypes.h:14
MemRef_t * createFragmentChain(UShort_t i2oFunctionCode, UInt_t headerSize, UChar_t *data, UInt_t dataSize, UInt_t &totalSize)
Definition: SMProxy.cc:188
UInt_t sendErrorEvent(UInt_t fuResourceId, UInt_t runNumber, UInt_t evtNumber, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data, UInt_t dataSize)
Definition: SMProxy.cc:122
unsigned int UInt_t
Definition: FUTypes.h:12
UInt_t sendDataEvent(UInt_t fuResourceId, UInt_t runNumber, UInt_t evtNumber, UInt_t outModId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data, UInt_t dataSize)
Definition: SMProxy.cc:91
#define I2O_MAX_SIZE
Definition: i2oEvfMsgs.h:46
char hltURL[MAX_I2O_SM_URLCHARS]
Definition: i2oEvfMsgs.h:64
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
char hltClassName[MAX_I2O_SM_URLCHARS]
Definition: i2oEvfMsgs.h:65
#define MAX_I2O_SM_URLCHARS
Definition: i2oEvfMsgs.h:53
UInt_t sendInitMessage(UInt_t fuResourceId, UInt_t outModId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data, UInt_t dataSize, UInt_t nExpectedEPs)
Definition: SMProxy.cc:62
#define I2O_SM_DATA
Definition: i2oEvfMsgs.h:22
virtual ~SMProxy()
Definition: SMProxy.cc:53