CMS 3D CMS Logo

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