CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/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& requestParamSet):
00019   buf_((uint8*)buf),bufSize_(bufSize)
00020 {
00021   // update the buffer pointer to just beyond the header
00022   uint8* bufPtr = buf_ + sizeof(Header);
00023   //std::cout << "bufPtr = 0x" << hex << ((uint32) bufPtr) << dec << std::endl;
00024   //std::cout << "buf_ = 0x" << hex << ((uint32) buf_) << dec << std::endl;
00025   //std::cout << "bufSize_ = " << bufSize_ << std::endl;
00026   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00027 
00028   // copy the consumer name into the message
00029   uint32 len = consumerName.length();
00030   assert(((uint32) (bufPtr + len + sizeof(uint32) - buf_)) <= bufSize_);
00031   convert(len, bufPtr);
00032   bufPtr += sizeof(uint32);
00033   consumerName.copy((char *) bufPtr, len);
00034   bufPtr += len;
00035 
00036   // copy the request parameter set into the message
00037   len = requestParamSet.length();
00038   assert(((uint32) (bufPtr + len + sizeof(uint32) - buf_)) <= bufSize_);
00039   convert(len, bufPtr);
00040   bufPtr += sizeof(uint32);
00041   requestParamSet.copy((char *) bufPtr, len);
00042   bufPtr += len;
00043 
00044   // create the message header now that we now the full size
00045   //std::cout << "bufPtr = 0x" << hex << ((uint32) bufPtr) << dec << std::endl;
00046   //std::cout << "buf_ = 0x" << hex << ((uint32) buf_) << dec << std::endl;
00047   new (buf_) Header(Header::CONS_REG_REQUEST, (bufPtr - buf_));
00048 }
00049 
00053 uint32 ConsRegRequestBuilder::size() const
00054 {
00055   HeaderView hview(buf_);
00056   return hview.size();
00057 }
00058 
00062 ConsRegRequestView::ConsRegRequestView(void* buf):
00063   buf_((uint8*)buf),head_(buf)
00064 {
00065   // verify that the buffer actually contains a registration request
00066   if (this->code() != Header::CONS_REG_REQUEST)
00067     {
00068       throw cms::Exception("MessageDecoding","ConsRegRequestView")
00069         << "Invalid consumer registration request message code ("
00070         << this->code() << "). Should be " << Header::CONS_REG_REQUEST << "\n";
00071     }
00072 
00073   // update the buffer pointer to just beyond the header
00074   uint8* bufPtr = buf_ + sizeof(Header);
00075 
00076   // determine the consumer name
00077   uint32 len = convert32(bufPtr);
00078   bufPtr += sizeof(uint32);
00079   if (len <= 256) // len >= 0, since len is unsigned
00080   {
00081     consumerName_.append((char *) bufPtr, len);
00082   }
00083   bufPtr += len;
00084 
00085   // determine the request parameter set (maintain backward compatibility
00086   // with sources of registration requests that don't have the param set)
00087   if (bufPtr < (buf_ + this->size()))
00088   {
00089       len = convert32(bufPtr);
00090       bufPtr += sizeof(uint32);
00091       // what is a reasonable limit?  This is just to prevent
00092       // a bogus, really large value from being used...
00093       if (len <= 65000) // len >= 0, since len is unsigned
00094       {
00095         requestParameterSet_.append((char *) bufPtr, len);
00096       }
00097       bufPtr += len;
00098       assert(bufPtr); // silence clang static analyzer
00099   }
00100 }
00101 
00105 ConsRegResponseBuilder::ConsRegResponseBuilder(void* buf, uint32 bufSize,
00106                                                uint32 status,
00107                                                uint32 consumerId):
00108   buf_((uint8*)buf),bufSize_(bufSize)
00109 {
00110   // update the buffer pointer to just beyond the header
00111   uint8* bufPtr = buf_ + sizeof(Header);
00112   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00113 
00114   // encode the status
00115   assert(((uint32) (bufPtr + sizeof(uint32) - buf_)) <= bufSize_);
00116   convert (status, bufPtr);
00117   bufPtr += sizeof(uint32);
00118 
00119   // encode the consumer ID
00120   assert(((uint32) (bufPtr + sizeof(uint32) - buf_)) <= bufSize_);
00121   convert (consumerId, bufPtr);
00122   bufPtr += sizeof(uint32);
00123 
00124   // create the message header now that we now the full size
00125   new (buf_) Header(Header::CONS_REG_RESPONSE, (bufPtr - buf_));
00126 }
00127 
00131 uint32 ConsRegResponseBuilder::size() const
00132 {
00133   HeaderView hview(buf_);
00134   return hview.size();
00135 }
00136 
00141 void ConsRegResponseBuilder::
00142 setStreamSelectionTable(std::map<std::string, Strings> const& selTable)
00143 {
00144   // add the table just beyond the existing data
00145   uint8* bufPtr = buf_ + size();
00146 
00147   // add the number of entries in the table to the message
00148   convert (static_cast<uint32>(selTable.size()), bufPtr);
00149   bufPtr += sizeof(uint32);
00150   assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00151 
00152   // add each entry in the table to the message
00153   std::map<std::string, Strings>::const_iterator mapIter;
00154   for (mapIter = selTable.begin(); mapIter != selTable.end(); mapIter++)
00155     {
00156       // create a new string list with the map key as the last entry
00157       Strings workList = mapIter->second;
00158       workList.push_back(mapIter->first);
00159 
00160       // copy the string list into the message
00161       bufPtr = MsgTools::fillNames(workList, bufPtr);
00162       assert(((uint32) (bufPtr - buf_)) <= bufSize_);
00163     }
00164 
00165   // update the message header with the new full size
00166   new (buf_) Header(Header::CONS_REG_RESPONSE, (bufPtr - buf_));
00167 }
00168 
00172 ConsRegResponseView::ConsRegResponseView(void* buf):
00173   buf_((uint8*)buf),head_(buf)
00174 {
00175   // verify that the buffer actually contains a registration response
00176   if (this->code() != Header::CONS_REG_RESPONSE)
00177     {
00178       throw cms::Exception("MessageDecoding","ConsRegResponseView")
00179         << "Invalid consumer registration response message code ("
00180         << this->code() << "). Should be " << Header::CONS_REG_RESPONSE << "\n";
00181     }
00182 
00183   // update the buffer pointer to just beyond the header
00184   uint8* bufPtr = buf_ + sizeof(Header);
00185 
00186   // decode the status
00187   status_ = convert32(bufPtr);
00188   bufPtr += sizeof(uint32);
00189 
00190   // decode the consumer ID
00191   consumerId_ = convert32(bufPtr);
00192   bufPtr += sizeof(uint32);
00193 
00194   assert(bufPtr); // silence clang static analyzer
00195 }
00196 
00201 std::map<std::string, Strings> ConsRegResponseView::getStreamSelectionTable()
00202 {
00203   std::map<std::string, Strings> selTable;
00204 
00205   // check if there is more than just the status code and consumer id
00206   if (size() >= (3 * sizeof(uint32)))
00207     {
00208       // initialize the data pointer to the start of the map data
00209       uint8* bufPtr = buf_ + sizeof(Header);
00210       bufPtr += (2 * sizeof(uint32));
00211 
00212       // decode the number of streams in the table
00213       uint32 streamCount = convert32(bufPtr);
00214       bufPtr += sizeof(uint32);
00215 
00216       // loop over each stream
00217       for (uint32 idx = 0; idx < streamCount; idx++)
00218         {
00219           // decode the vector of strings for the stream
00220           Strings workList;
00221           //uint32 listCount = convert32(bufPtr);
00222           bufPtr += sizeof(uint32);
00223           uint32 listLen = convert32(bufPtr);
00224           bufPtr += sizeof(uint32);
00225           MsgTools::getNames(bufPtr, listLen, workList);
00226 
00227           // pull the map key off the end of the list 
00228           std::string streamLabel = workList.back();
00229           workList.pop_back();
00230           selTable[streamLabel] = workList;
00231 
00232           // move on to the next entry in the message
00233           bufPtr += listLen;
00234         }
00235     }
00236 
00237   return selTable;
00238 }