CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/IOPool/Streamer/src/ConsRegMessage.cc

Go to the documentation of this file.
00001 
00009 #include "IOPool/Streamer/interface/ConsRegMessage.h"
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 #include <cassert>
00012 
00016 ConsRegRequestBuilder::ConsRegRequestBuilder(void* buf, uint32 bufSize,
00017                                              std::string const& consumerName,
00018                                              std::string const& consumerPriority,
00019                                              std::string const& requestParamSet):
00020   buf_((uint8*)buf),bufSize_(bufSize)
00021 {
00022   uint8* bufPtr;
00023   uint32 len;
00024 
00025   // update the buffer pointer to just beyond the header
00026   bufPtr = buf_ + sizeof(Header);
00027   //std::cout << "bufPtr = 0x" << hex << ((uint32) bufPtr) << dec << std::endl;
00028   //std::cout << "buf_ = 0x" << hex << ((uint32) buf_) << dec << std::endl;
00029   //std::cout << "bufSize_ = " << bufSize_ << std::endl;
00030   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00031 
00032   // copy the consumer name into the message
00033   len = consumerName.length();
00034   assert(((uint32) (bufPtr + len + sizeof(uint32) - buf_)) <= bufSize_);
00035   convert(len, bufPtr);
00036   bufPtr += sizeof(uint32);
00037   consumerName.copy((char *) bufPtr, len);
00038   bufPtr += len;
00039 
00040   // copy the consumer priority into the message
00041   len = consumerPriority.length();
00042   assert(((uint32) (bufPtr + len + sizeof(uint32) - buf_)) <= bufSize_);
00043   convert(len, bufPtr);
00044   bufPtr += sizeof(uint32);
00045   consumerPriority.copy((char *) bufPtr, len);
00046   bufPtr += len;
00047 
00048   // copy the request parameter set into the message
00049   len = requestParamSet.length();
00050   assert(((uint32) (bufPtr + len + sizeof(uint32) - buf_)) <= bufSize_);
00051   convert(len, bufPtr);
00052   bufPtr += sizeof(uint32);
00053   requestParamSet.copy((char *) bufPtr, len);
00054   bufPtr += len;
00055 
00056   // create the message header now that we now the full size
00057   //std::cout << "bufPtr = 0x" << hex << ((uint32) bufPtr) << dec << std::endl;
00058   //std::cout << "buf_ = 0x" << hex << ((uint32) buf_) << dec << std::endl;
00059   new (buf_) Header(Header::CONS_REG_REQUEST, (bufPtr - buf_));
00060 }
00061 
00065 uint32 ConsRegRequestBuilder::size() const
00066 {
00067   HeaderView hview(buf_);
00068   return hview.size();
00069 }
00070 
00074 ConsRegRequestView::ConsRegRequestView(void* buf):
00075   buf_((uint8*)buf),head_(buf)
00076 {
00077   uint8* bufPtr;
00078   uint32 len;
00079 
00080   // verify that the buffer actually contains a registration request
00081   if (this->code() != Header::CONS_REG_REQUEST)
00082     {
00083       throw cms::Exception("MessageDecoding","ConsRegRequestView")
00084         << "Invalid consumer registration request message code ("
00085         << this->code() << "). Should be " << Header::CONS_REG_REQUEST << "\n";
00086     }
00087 
00088   // update the buffer pointer to just beyond the header
00089   bufPtr = buf_ + sizeof(Header);
00090 
00091   // determine the consumer name
00092   len = convert32(bufPtr);
00093   bufPtr += sizeof(uint32);
00094   if (len <= 256) // len >= 0, since len is unsigned
00095   {
00096     consumerName_.append((char *) bufPtr, len);
00097   }
00098   bufPtr += len;
00099 
00100   // determine the consumer priority
00101   len = convert32(bufPtr);
00102   bufPtr += sizeof(uint32);
00103   if (len <= 64) // len >= 0, since len is unsigned
00104   {
00105     consumerPriority_.append((char *) bufPtr, len);
00106   }
00107   bufPtr += len;
00108 
00109   // determine the request parameter set (maintain backward compatibility
00110   // with sources of registration requests that don't have the param set)
00111   if (bufPtr < (buf_ + this->size()))
00112     {
00113       len = convert32(bufPtr);
00114       bufPtr += sizeof(uint32);
00115       // what is a reasonable limit?  This is just to prevent
00116       // a bogus, really large value from being used...
00117       if (len <= 65000) // len >= 0, since len is unsigned
00118       {
00119         requestParameterSet_.append((char *) bufPtr, len);
00120       }
00121       bufPtr += len;
00122     }
00123 }
00124 
00128 ConsRegResponseBuilder::ConsRegResponseBuilder(void* buf, uint32 bufSize,
00129                                                uint32 status,
00130                                                uint32 consumerId):
00131   buf_((uint8*)buf),bufSize_(bufSize)
00132 {
00133   uint8* bufPtr;
00134 
00135   // update the buffer pointer to just beyond the header
00136   bufPtr = buf_ + sizeof(Header);
00137   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00138 
00139   // encode the status
00140   assert(((uint32) (bufPtr + sizeof(uint32) - buf_)) <= bufSize_);
00141   convert (status, bufPtr);
00142   bufPtr += sizeof(uint32);
00143 
00144   // encode the consumer ID
00145   assert(((uint32) (bufPtr + sizeof(uint32) - buf_)) <= bufSize_);
00146   convert (consumerId, bufPtr);
00147   bufPtr += sizeof(uint32);
00148 
00149   // create the message header now that we now the full size
00150   new (buf_) Header(Header::CONS_REG_RESPONSE, (bufPtr - buf_));
00151 }
00152 
00156 uint32 ConsRegResponseBuilder::size() const
00157 {
00158   HeaderView hview(buf_);
00159   return hview.size();
00160 }
00161 
00166 void ConsRegResponseBuilder::
00167 setStreamSelectionTable(std::map<std::string, Strings> const& selTable)
00168 {
00169   // add the table just beyond the existing data
00170   uint8* bufPtr = buf_ + size();
00171 
00172   // add the number of entries in the table to the message
00173   convert (static_cast<uint32>(selTable.size()), bufPtr);
00174   bufPtr += sizeof(uint32);
00175   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00176 
00177   // add each entry in the table to the message
00178   std::map<std::string, Strings>::const_iterator mapIter;
00179   for (mapIter = selTable.begin(); mapIter != selTable.end(); mapIter++)
00180     {
00181       // create a new string list with the map key as the last entry
00182       Strings workList = mapIter->second;
00183       workList.push_back(mapIter->first);
00184 
00185       // copy the string list into the message
00186       bufPtr = MsgTools::fillNames(workList, bufPtr);
00187       assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00188     }
00189 
00190   // update the message header with the new full size
00191   new (buf_) Header(Header::CONS_REG_RESPONSE, (bufPtr - buf_));
00192 }
00193 
00197 ConsRegResponseView::ConsRegResponseView(void* buf):
00198   buf_((uint8*)buf),head_(buf)
00199 {
00200   uint8* bufPtr;
00201 
00202   // verify that the buffer actually contains a registration response
00203   if (this->code() != Header::CONS_REG_RESPONSE)
00204     {
00205       throw cms::Exception("MessageDecoding","ConsRegResponseView")
00206         << "Invalid consumer registration response message code ("
00207         << this->code() << "). Should be " << Header::CONS_REG_RESPONSE << "\n";
00208     }
00209 
00210   // update the buffer pointer to just beyond the header
00211   bufPtr = buf_ + sizeof(Header);
00212 
00213   // decode the status
00214   status_ = convert32(bufPtr);
00215   bufPtr += sizeof(uint32);
00216 
00217   // decode the consumer ID
00218   consumerId_ = convert32(bufPtr);
00219   bufPtr += sizeof(uint32);
00220 }
00221 
00226 std::map<std::string, Strings> ConsRegResponseView::getStreamSelectionTable()
00227 {
00228   std::map<std::string, Strings> selTable;
00229 
00230   // check if there is more than just the status code and consumer id
00231   if (size() >= (3 * sizeof(uint32)))
00232     {
00233       // initialize the data pointer to the start of the map data
00234       uint8* bufPtr = buf_ + sizeof(Header);
00235       bufPtr += (2 * sizeof(uint32));
00236 
00237       // decode the number of streams in the table
00238       uint32 streamCount = convert32(bufPtr);
00239       bufPtr += sizeof(uint32);
00240 
00241       // loop over each stream
00242       for (uint32 idx = 0; idx < streamCount; idx++)
00243         {
00244           // decode the vector of strings for the stream
00245           Strings workList;
00246           //uint32 listCount = convert32(bufPtr);
00247           bufPtr += sizeof(uint32);
00248           uint32 listLen = convert32(bufPtr);
00249           bufPtr += sizeof(uint32);
00250           MsgTools::getNames(bufPtr, listLen, workList);
00251 
00252           // pull the map key off the end of the list 
00253           std::string streamLabel = workList.back();
00254           workList.pop_back();
00255           selTable[streamLabel] = workList;
00256 
00257           // move on to the next entry in the message
00258           bufPtr += listLen;
00259         }
00260     }
00261 
00262   return selTable;
00263 }