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 
27 using namespace std;
28 using namespace evf;
29 
30 
32 // construction/destruction
34 
35 //______________________________________________________________________________
36 SMProxy::SMProxy(xdaq::ApplicationDescriptor *fuAppDesc,
37  xdaq::ApplicationDescriptor *smAppDesc,
38  xdaq::ApplicationContext *fuAppContext,
39  toolbox::mem::Pool *i2oPool)
40  : log_(fuAppContext->getLogger())
41  , fuAppDesc_(fuAppDesc)
42  , smAppDesc_(smAppDesc)
43  , fuAppContext_(fuAppContext)
44  , i2oPool_(i2oPool)
45  , initHeaderSize_(sizeof(I2O_SM_PREAMBLE_MESSAGE_FRAME))
46  , dataHeaderSize_(sizeof(I2O_SM_DATA_MESSAGE_FRAME))
47  , dqmHeaderSize_(sizeof(I2O_SM_DQM_MESSAGE_FRAME))
48 {
49  fuUrl_=fuAppDesc_->getContextDescriptor()->getURL();
50  if (fuUrl_.size()>=MAX_I2O_SM_URLCHARS)
51  fuUrl_=fuUrl_.substr(0,MAX_I2O_SM_URLCHARS-1);
52 
53  fuClassName_=fuAppDesc_->getClassName();
56 }
57 
58 
59 //______________________________________________________________________________
61 {
62 
63 }
64 
65 
67 // implementation of member functions
69 
70 //______________________________________________________________________________
72  UInt_t outModId,
73  UInt_t fuProcessId,
74  UInt_t fuGuid,
75  UChar_t*data,
76  UInt_t dataSize)
77  throw (evf::Exception)
78 {
79  UInt_t totalSize=0;
80  MemRef_t* bufRef =createFragmentChain(I2O_SM_PREAMBLE,
81  initHeaderSize_,
82  data,
83  dataSize,
84  totalSize);
85 
87  MemRef_t* next=bufRef;
88  do {
89  msg=(I2O_SM_PREAMBLE_MESSAGE_FRAME*)next->getDataLocation();
90  msg->rbBufferID=fuResourceId;
91  msg->outModID=outModId;
92  msg->fuProcID=fuProcessId;
93  msg->fuGUID=fuGuid;
94  }
95  while ((next=next->getNextReference()));
96 
97  try {
98  fuAppContext_->postFrame(bufRef,fuAppDesc_,smAppDesc_);
99  }
100  catch (xdaq::exception::Exception &e) {
101  string msg="Failed to post INIT Message.";
102  XCEPT_RETHROW(evf::Exception,msg,e);
103  }
104 
105  return totalSize;
106 }
107 
108 
109 //______________________________________________________________________________
112  UInt_t evtNumber,
113  UInt_t outModId,
114  UInt_t fuProcessId,
115  UInt_t fuGuid,
116  UChar_t *data,
117  UInt_t dataSize)
118  throw (evf::Exception)
119 {
120  UInt_t totalSize=0;
121  MemRef_t* bufRef =createFragmentChain(I2O_SM_DATA,
122  dataHeaderSize_,
123  data,
124  dataSize,
125  totalSize);
126 
128  MemRef_t* next=bufRef;
129  do {
130  msg=(I2O_SM_DATA_MESSAGE_FRAME*)next->getDataLocation();
131  msg->rbBufferID=fuResourceId;
132  msg->runID =runNumber;
133  msg->eventID =evtNumber;
134  msg->outModID=outModId;
135  msg->fuProcID=fuProcessId;
136  msg->fuGUID=fuGuid;
137  }
138  while ((next=next->getNextReference()));
139 
140  try {
141  fuAppContext_->postFrame(bufRef,fuAppDesc_,smAppDesc_);
142  }
143  catch (xdaq::exception::Exception &e) {
144  string errmsg="Failed to post DATA Message.";
145  LOG4CPLUS_FATAL(log_,errmsg);
146  XCEPT_RETHROW(evf::Exception,errmsg,e);
147  }
148 
149  return totalSize;
150 }
151 
152 
153 //______________________________________________________________________________
156  UInt_t evtNumber,
157  UInt_t fuProcessId,
158  UInt_t fuGuid,
159  UChar_t *data,
160  UInt_t dataSize)
161  throw (evf::Exception)
162 {
163  UInt_t totalSize=0;
164  MemRef_t* bufRef =createFragmentChain(I2O_SM_ERROR,
165  dataHeaderSize_,
166  data,
167  dataSize,
168  totalSize);
169 
171  MemRef_t* next=bufRef;
172  do {
173  msg=(I2O_SM_DATA_MESSAGE_FRAME*)next->getDataLocation();
174  msg->rbBufferID=fuResourceId;
175  msg->runID =runNumber;
176  msg->eventID =evtNumber;
177  msg->outModID=0xffffffff;
178  msg->fuProcID=fuProcessId;
179  msg->fuGUID=fuGuid;
180  }
181  while ((next=next->getNextReference()));
182 
183  try {
184  fuAppContext_->postFrame(bufRef,fuAppDesc_,smAppDesc_);
185  }
186  catch (xdaq::exception::Exception &e) {
187  string errmsg="Failed to post ERROR Message.";
188  LOG4CPLUS_FATAL(log_,errmsg);
189  XCEPT_RETHROW(evf::Exception,errmsg,e);
190  }
191 
192  return totalSize;
193 }
194 
195 
196 //______________________________________________________________________________
199  UInt_t evtAtUpdate,
200  UInt_t folderId,
201  UInt_t fuProcessId,
202  UInt_t fuGuid,
203  UChar_t*data,
204  UInt_t dataSize)
205  throw (evf::Exception)
206 {
207  UInt_t totalSize=0;
208  MemRef_t* bufRef =createFragmentChain(I2O_SM_DQM,
209  dqmHeaderSize_,
210  data,
211  dataSize,
212  totalSize);
213 
215  MemRef_t* next=bufRef;
216  do {
217  msg=(I2O_SM_DQM_MESSAGE_FRAME*)next->getDataLocation();
218  msg->rbBufferID =fuDqmId;
219  msg->runID =runNumber;
220  msg->eventAtUpdateID=evtAtUpdate;
221  msg->folderID =folderId;
222  msg->fuProcID =fuProcessId;
223  msg->fuGUID =fuGuid;
224  }
225  while ((next=next->getNextReference()));
226 
227  try {
228  fuAppContext_->postFrame(bufRef,fuAppDesc_,smAppDesc_);
229  }
230  catch (xdaq::exception::Exception &e) {
231  string errmsg="Failed to post DQM Message.";
232  LOG4CPLUS_FATAL(log_,errmsg);
233  XCEPT_RETHROW(evf::Exception,errmsg,e);
234  }
235 
236  return totalSize;
237 }
238 
239 
241 // implementation of private member functions
243 
244 //______________________________________________________________________________
246  UInt_t headerSize,
247  UChar_t *data,
248  UInt_t dataSize,
249  UInt_t &totalSize)
250  throw (evf::Exception)
251 {
252  totalSize=0;
253 
254  UInt_t fragmentDataSizeMax=I2O_MAX_SIZE-headerSize;
255  UInt_t fragmentCount=(dataSize/fragmentDataSizeMax);
256  if (dataSize%fragmentDataSizeMax) ++fragmentCount;
257 
258  UInt_t currentPosition =0;
259  UInt_t remainingDataSize=dataSize;
260 
261  MemRef_t *head(0);
262  MemRef_t *tail(0);
263 
264  try {
265 
266  for (UInt_t iFragment=0;iFragment<fragmentCount;iFragment++) {
267 
268  UInt_t fragmentDataSize=fragmentDataSizeMax;
269  UInt_t fragmentSize =fragmentDataSize+headerSize;
270 
271  if (remainingDataSize<fragmentDataSizeMax) {
272  fragmentDataSize=remainingDataSize;
273  fragmentSize=fragmentDataSize+headerSize;
274  if (fragmentSize&0x7) fragmentSize = ((fragmentSize >> 3) + 1) << 3;
275  }
276 
277  // allocate the fragment buffer from the pool
278  toolbox::mem::Reference *bufRef =
279  toolbox::mem::getMemoryPoolFactory()->getFrame(i2oPool_,fragmentSize);
280 
281  // set up pointers to the allocated message buffer
282  I2O_MESSAGE_FRAME *stdMsg;
283  I2O_PRIVATE_MESSAGE_FRAME *pvtMsg;
285 
286  stdMsg=(I2O_MESSAGE_FRAME*)bufRef->getDataLocation();
287  pvtMsg=(I2O_PRIVATE_MESSAGE_FRAME*)stdMsg;
288  msg =(I2O_SM_MULTIPART_MESSAGE_FRAME*)stdMsg;
289 
290  stdMsg->VersionOffset =0;
291  stdMsg->MsgFlags =0; // normal message (not multicast)
292  stdMsg->MessageSize =fragmentSize >> 2;
293  stdMsg->Function =I2O_PRIVATE_MESSAGE;
294  stdMsg->InitiatorAddress=i2o::utils::getAddressMap()->getTid(fuAppDesc_);
295  stdMsg->TargetAddress =i2o::utils::getAddressMap()->getTid(smAppDesc_);
296 
297  pvtMsg->XFunctionCode =i2oFunctionCode;
298  pvtMsg->OrganizationID =XDAQ_ORGANIZATION_ID;
299 
300  msg->dataSize =fragmentDataSize;
301  msg->hltLocalId =fuAppDesc_->getLocalId();
302  msg->hltInstance =fuAppDesc_->getInstance();
303  msg->hltTid =i2o::utils::getAddressMap()->getTid(fuAppDesc_);
304  msg->numFrames =fragmentCount;
305  msg->frameCount =iFragment;
306  msg->originalSize =dataSize;
307 
308  for (UInt_t i=0;i<fuUrl_.size();i++)
309  msg->hltURL[i]=fuUrl_[i];
310  msg->hltURL[fuUrl_.size()]='\0';
311 
312  for (UInt_t i=0;i<fuClassName_.size();i++)
313  msg->hltClassName[i]=fuClassName_[i];
314  msg->hltClassName[fuClassName_.size()]='\0';
315 
316  if (iFragment==0) {
317  head=bufRef;
318  tail=bufRef;
319  }
320  else {
321  tail->setNextReference(bufRef);
322  tail=bufRef;
323  }
324 
325  if (fragmentDataSize!=0) {
326  UChar_t* targetAddr=(UChar_t*)msg+headerSize;
327  std::copy(data+currentPosition,
328  data+currentPosition+fragmentDataSize,
329  targetAddr);
330  }
331 
332  bufRef->setDataSize(fragmentSize);
333  remainingDataSize-=fragmentDataSize;
334  currentPosition +=fragmentDataSize;
335  totalSize +=fragmentSize;
336 
337  } // for (iFragment ...)
338  }
340  if (0!=head) head->release();
341  totalSize=0;
342  string errmsg="Failed to allocate buffer reference.";
343  LOG4CPLUS_FATAL(log_,errmsg);
344  XCEPT_RETHROW(evf::Exception,errmsg,e);
345  }
346  catch(xdaq::exception::ApplicationDescriptorNotFound& e) {
347  if (0!=head) head->release();
348  totalSize=0;
349  string errmsg="Failed to get tid.";
350  LOG4CPLUS_FATAL(log_,errmsg);
351  XCEPT_RETHROW(evf::Exception,errmsg,e);
352  }
353 
354  return head;
355 }
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:197
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:245
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:154
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:110
#define I2O_MAX_SIZE
Definition: i2oEvfMsgs.h:46
UInt_t sendInitMessage(UInt_t fuResourceId, UInt_t outModId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t *data, UInt_t dataSize)
Definition: SMProxy.cc:71
char hltURL[MAX_I2O_SM_URLCHARS]
Definition: i2oEvfMsgs.h:64
char hltClassName[MAX_I2O_SM_URLCHARS]
Definition: i2oEvfMsgs.h:65
#define MAX_I2O_SM_URLCHARS
Definition: i2oEvfMsgs.h:53
#define I2O_SM_DATA
Definition: i2oEvfMsgs.h:22
virtual ~SMProxy()
Definition: SMProxy.cc:60