Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phMessage.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <phission.h>
00013 #include <phClientHandler.h>
00014 
00015 /* -------------------------------------------------------------------------- *
00016  * phMessage constructor:
00017  * -------------------------------------------------------------------------- */
00018 phMessage::phMessage()
00019 {
00020     phFUNCTION("phMessage::phMessage")
00021 
00022     this->m_buf         = NULL;
00023     this->m_buf_size    = 0;
00024     
00025     this->reset();
00026     
00027     //this->setName("phMessage");
00028 }
00029 /* -------------------------------------------------------------------------- *
00030  * phMessage copy constructor:
00031  * -------------------------------------------------------------------------- */
00032 phMessage::phMessage( phMessage &message )
00033 {
00034     this->m_buf         = NULL;
00035     this->m_buf_size    = 0;
00036 
00037     this->reset();
00038     
00039     //this->setName("phMessage");
00040 
00041     this->set(message);
00042 }
00043 /* -------------------------------------------------------------------------- *
00044  * phMessage dstructor:
00045  * -------------------------------------------------------------------------- */
00046 phMessage::~phMessage()
00047 {
00048     this->reset();
00049     
00050     phFree(this->m_buf);
00051     this->m_buf_size    = 0;
00052 }
00053 
00054 /* PRIVATE STUFF */
00055 
00056 /* -------------------------------------------------------------------------- */
00057 void phMessage::reset()
00058 {
00059     this->m_src_id      = phID_INVALID;
00060     this->m_dst_id      = phID_INVALID;
00061     this->m_src_class   = phCLASS_INVALID;
00062     this->m_dst_class   = phCLASS_INVALID;
00063     this->m_message_id  = phMESSAGE_INVALID;
00064     this->m_mutex_id    = phMUTEX_INVALID;
00065     this->m_timestamp   = phTIME_INVALID;
00066 }
00067 
00068 /* -------------------------------------------------------------------------- *
00069  * 
00070  * -------------------------------------------------------------------------- */
00071 uint32_t phMessage::getDataSize()
00072 {
00073     return sizeof(uint32_t) * phMessage_nfields();
00074 }
00075 
00076 /* -------------------------------------------------------------------------- *
00077  * 
00078  * -------------------------------------------------------------------------- */
00079 int phMessage::alloc_buffer( uint8_t **pbuf, uint32_t *psize )
00080 {
00081     phFUNCTION("phMessage::alloc_buffer")
00082     size_t   data_size      = this->getDataSize();
00083 
00084     phCHECK_NULLPTR(psize,NULL,"Bad parameter: psize");
00085     phCHECK_NULLPTR(pbuf, NULL,"Bad parameter: pbuf");
00086     
00087     /* Check the size value, if it's not 0... */
00088     if ((*psize > 0) && (*pbuf != NULL))
00089     {
00090         /* Check the size of the buffer if 
00091          * it's allocated */
00092         if (*psize != data_size)
00093         {
00094             void *tmp = (void *)*pbuf;
00095             
00096             *pbuf = (uint8_t *)phRealloc(*pbuf,data_size);
00097                             
00098             phCHECK_NULLPTR(*pbuf,"phRealloc",
00099                     "phRealloc(%p,%d) failed.",
00100                     tmp, data_size );
00101 
00102             *psize  = data_size;
00103         }
00104     }
00105     /* Allocate the buffer if needed */
00106     else
00107     {
00108         *pbuf = (uint8_t *)phMalloc(data_size);
00109         phCHECK_NULLPTR(*pbuf,"phMalloc","phMalloc(%d) failed.",
00110                       data_size);
00111     
00112         *psize = data_size;
00113     }
00114     
00115     return phSUCCESS;
00116 error:
00117     if (psize != NULL)
00118     {
00119         *psize = 0;
00120     }
00121         
00122     return phFAIL;
00123 }
00124 
00125 /* -------------------------------------------------------------------------- *
00126  * 
00127  * -------------------------------------------------------------------------- */
00128 int phMessage::getData( uint8_t **pbuf, uint32_t *psize )
00129 {
00130     phFUNCTION("phMessage::getData")
00131     uint32_t srcid          = 0;
00132     uint32_t dstid          = 0;
00133     uint32_t srcclass       = 0;
00134     uint32_t dstclass       = 0;
00135     uint32_t message_id     = 0;
00136     uint32_t mutex_id       = 0;
00137     uint32_t timestamp      = 0;
00138     uint32_t field          = 0;
00139     size_t   element_size   = sizeof(uint32_t);
00140     
00141     rc = this->alloc_buffer(pbuf,psize);
00142     phCHECK_RC(rc,NULL,"alloc_buffer(%p,%p) failed.",
00143             pbuf,psize);
00144     
00145     srcid       = phSocket::hton32(this->m_src_id);
00146     dstid       = phSocket::hton32(this->m_dst_id);
00147     srcclass    = phSocket::hton32(this->m_src_class);
00148     dstclass    = phSocket::hton32(this->m_dst_class);
00149     message_id  = phSocket::hton32(this->m_message_id);
00150     mutex_id    = phSocket::hton32(this->m_mutex_id);
00151     timestamp   = phSocket::hton32(this->m_timestamp);
00152     
00153     memcpy(&((*pbuf)[element_size*field++]),&srcid,     element_size);
00154     memcpy(&((*pbuf)[element_size*field++]),&dstid,     element_size);
00155     memcpy(&((*pbuf)[element_size*field++]),&srcclass,  element_size);
00156     memcpy(&((*pbuf)[element_size*field++]),&dstclass,  element_size);
00157     memcpy(&((*pbuf)[element_size*field++]),&message_id,element_size);
00158     memcpy(&((*pbuf)[element_size*field++]),&mutex_id,  element_size);
00159     memcpy(&((*pbuf)[element_size*field++]),&timestamp, element_size);
00160 
00161     return phSUCCESS;
00162 error:
00163     return phFAIL;
00164 }
00165 
00166 /* -------------------------------------------------------------------------- *
00167  * 
00168  * -------------------------------------------------------------------------- */
00169 int phMessage::setData( uint8_t *buf, uint32_t size )
00170 {
00171     phFUNCTION("phMessage::setData")
00172     uint32_t srcid          = 0;
00173     uint32_t dstid          = 0;
00174     uint32_t srcclass       = 0;
00175     uint32_t dstclass       = 0;
00176     uint32_t message_id     = 0;
00177     uint32_t mutex_id       = 0;
00178     uint32_t timestamp      = 0;
00179     uint32_t field          = 0;
00180     size_t   element_size   = sizeof(uint32_t);
00181     size_t   data_size      = this->getDataSize();
00182     
00183     phCHECK_NULLPTR(buf,NULL,"Bad parameter: buf == NULL");
00184     
00185     if (size != data_size)
00186     {
00187         phCHECK_RC(-1,NULL,
00188             "Data sizes don't match - size:%u != DataSize:%u",
00189             size, data_size);
00190     }
00191 
00192     memcpy(&srcid,      &(buf[element_size*field++]),element_size);
00193     memcpy(&dstid,      &(buf[element_size*field++]),element_size);
00194     memcpy(&srcclass,   &(buf[element_size*field++]),element_size);
00195     memcpy(&dstclass,   &(buf[element_size*field++]),element_size);
00196     memcpy(&message_id, &(buf[element_size*field++]),element_size);
00197     memcpy(&mutex_id,   &(buf[element_size*field++]),element_size);
00198     memcpy(&timestamp,  &(buf[element_size*field++]),element_size);
00199 
00200     this->m_src_id      = phSocket::ntoh32(srcid);
00201     this->m_dst_id      = phSocket::ntoh32(dstid);
00202     this->m_src_class   = phSocket::ntoh32(srcclass);
00203     this->m_dst_class   = phSocket::ntoh32(dstclass);
00204     this->m_message_id  = phSocket::ntoh32(message_id);
00205     this->m_mutex_id    = phSocket::ntoh32(mutex_id);
00206     this->m_timestamp   = phSocket::ntoh32(timestamp);
00207     
00208     return phSUCCESS;
00209 error:
00210     return phFAIL;
00211 }
00212 
00213 /* PUBLIC STUFF */
00214  
00215 /* -------------------------------------------------------------------------- *
00216  * 
00217  * -------------------------------------------------------------------------- */
00218 phMessage &phMessage::operator =( phMessage &m )
00219 {
00220     this->reset();
00221     
00222     phFree(this->m_buf);
00223     this->m_buf_size    = 0;
00224 
00225     this->set(m);
00226 
00227     return *this;
00228 }
00229 
00230 /* -------------------------------------------------------------------------- *
00231  * 
00232  * -------------------------------------------------------------------------- */
00233 int phMessage::set( uint32_t src_id, 
00234                     uint32_t dst_id,
00235                     uint32_t src_class, 
00236                     uint32_t dst_class,
00237                     uint32_t message_id, 
00238                     uint32_t mutex_id,
00239                     uint32_t timestamp )
00240 {
00241     phFUNCTION("phMessage::set")
00242 
00243     rc = this->setSrcId(src_id);
00244     rc = this->setDstId(dst_id);
00245     rc = this->setSrcClass(src_class);
00246     rc = this->setDstClass(dst_class);
00247     rc = this->setMessageId(message_id);
00248     rc = this->setMutexId(mutex_id);
00249     rc = this->setTimestamp(timestamp);
00250     
00251     return phSUCCESS;
00252 }
00253 
00254 /* -------------------------------------------------------------------------- *
00255  * 
00256  * -------------------------------------------------------------------------- */
00257 int phMessage::set( phMessage &message )
00258 {
00259     return this->set(message.getSrcId(),
00260                      message.getDstId(),
00261                      message.getSrcClass(),
00262                      message.getDstClass(),
00263                      message.getMessageId(),
00264                      message.getMutexId(),
00265                      message.getTimestamp());
00266 }
00267 /* -------------------------------------------------------------------------- *
00268  * 
00269  * -------------------------------------------------------------------------- */
00270 int phMessage::setSrcId(uint32_t src_id)
00271 {
00272     this->m_src_id = src_id;
00273     
00274     return phSUCCESS;
00275 }
00276 /* -------------------------------------------------------------------------- *
00277  * 
00278  * -------------------------------------------------------------------------- */
00279 int phMessage::setDstId(uint32_t dst_id)
00280 {
00281     this->m_dst_id = dst_id;
00282     
00283     return phSUCCESS;
00284 }
00285 /* -------------------------------------------------------------------------- *
00286  * 
00287  * -------------------------------------------------------------------------- */
00288 int phMessage::setSrcClass(uint32_t src_class)
00289 {
00290     this->m_src_class = src_class;
00291     
00292     return phSUCCESS;
00293 }
00294 /* -------------------------------------------------------------------------- *
00295  * 
00296  * -------------------------------------------------------------------------- */
00297 int phMessage::setDstClass(uint32_t dst_class)
00298 {
00299     this->m_dst_class = dst_class;
00300     
00301     return phSUCCESS;
00302 }
00303 /* -------------------------------------------------------------------------- *
00304  * 
00305  * -------------------------------------------------------------------------- */
00306 int phMessage::setMessageId(uint32_t message_id)
00307 {
00308     this->m_message_id = message_id;
00309     
00310     return phSUCCESS;
00311 }
00312 /* -------------------------------------------------------------------------- *
00313  * 
00314  * -------------------------------------------------------------------------- */
00315 int phMessage::setMutexId( uint32_t mutex_id )
00316 {
00317     this->m_mutex_id = mutex_id;
00318     
00319     return phSUCCESS;
00320 }
00321 /* -------------------------------------------------------------------------- *
00322  * 
00323  * -------------------------------------------------------------------------- */
00324 int phMessage::setTimestamp( uint32_t timestamp )
00325 {
00326     this->m_timestamp = timestamp;
00327     
00328     return phSUCCESS;
00329 }
00330 
00331 /* -------------------------------------------------------------------------- *
00332  * 
00333  * -------------------------------------------------------------------------- */
00334 uint32_t phMessage::getSrcId() 
00335 { 
00336     return this->m_src_id; 
00337 }
00338 /* -------------------------------------------------------------------------- *
00339  * 
00340  * -------------------------------------------------------------------------- */
00341 uint32_t phMessage::getDstId() 
00342 { 
00343     return this->m_dst_id; 
00344 }
00345 
00346 /* -------------------------------------------------------------------------- *
00347  * 
00348  * -------------------------------------------------------------------------- */
00349 uint32_t phMessage::getSrcClass() 
00350 { 
00351     return this->m_src_class; 
00352 }
00353 /* -------------------------------------------------------------------------- *
00354  * 
00355  * -------------------------------------------------------------------------- */
00356 uint32_t phMessage::getDstClass() 
00357 { 
00358     return this->m_dst_class; 
00359 }
00360 
00361 /* -------------------------------------------------------------------------- *
00362  * 
00363  * -------------------------------------------------------------------------- */
00364 uint32_t phMessage::getMessageId() 
00365 { 
00366     return this->m_message_id; 
00367 }
00368 
00369 /* -------------------------------------------------------------------------- *
00370  * 
00371  * -------------------------------------------------------------------------- */
00372 uint32_t phMessage::getMutexId() 
00373 { 
00374     return this->m_mutex_id; 
00375 }
00376 
00377 /* -------------------------------------------------------------------------- *
00378  * 
00379  * -------------------------------------------------------------------------- */
00380 uint32_t phMessage::getTimestamp() 
00381 { 
00382     return this->m_timestamp; 
00383 }
00384 /* -------------------------------------------------------------------------- *
00385  * 
00386  * -------------------------------------------------------------------------- */
00387 int phMessage::receive( phSocket *sock )
00388 {
00389     phFUNCTION("phMessage::receive")
00390     int retrc = phSUCCESS;
00391     
00392     phCHECK_NULLPTR(sock,NULL,"Bad parameter: sock == NULL");
00393 
00394     rc = this->alloc_buffer(&this->m_buf,&this->m_buf_size);
00395     phCHECK_RC(rc,NULL,"alloc_buffer failed.");
00396  
00397     this->reset();
00398     
00399     /* Receive all or part of the request from the 
00400      * client process */
00401     rc = sock->recv(this->m_buf, this->m_buf_size);
00402     if (sock->isConnected())
00403     {
00404         phPRINT_RC(rc,NULL,"this->m_sock->recv()");
00405 
00406         rc = this->setData(this->m_buf,this->m_buf_size);
00407         phCHECK_RC(rc,NULL,"setData failed.");
00408     }
00409     else
00410     {
00411         retrc = 1;
00412     }
00413     return retrc;
00414 error:
00415     return phFAIL;
00416 }
00417 
00418 /* -------------------------------------------------------------------------- *
00419  * 
00420  * -------------------------------------------------------------------------- */
00421 int phMessage::send( phSocket *sock )
00422 {
00423     phFUNCTION("phMessage::send")
00424     int retrc = phSUCCESS;
00425     
00426     phCHECK_NULLPTR(sock,NULL,"Bad parameter: sock == NULL");
00427 
00428     rc = this->getData(&this->m_buf,&this->m_buf_size);
00429     phCHECK_RC(rc,NULL,"getData failed.");
00430     
00431     rc = sock->send(this->m_buf,this->m_buf_size);
00432     if (sock->isConnected())
00433     {
00434         phCHECK_RC(rc,NULL,"this->send");
00435     }
00436     else
00437     {
00438         retrc = 1;
00439     }
00440 
00441     return retrc;
00442 error:
00443     return phFAIL;
00444 }
00445 
00446 /* -------------------------------------------------------------------------- *
00447  * 
00448  * -------------------------------------------------------------------------- */
00449 int phMessage::print( FILE *fd,
00450                       int short_print )
00451 {
00452     phFUNCTION("phMessage::sock")
00453     char *message_str   = NULL;
00454     char *message_str_short   = NULL;
00455     char *src_class     = NULL;
00456     char *dst_class     = NULL;
00457     char *src_class_short     = NULL;
00458     char *dst_class_short     = NULL;
00459     uint32_t timestamp  = 0;
00460 
00461     phCHECK_NULLPTR(fd,NULL,"Bad parameter: fd == NULL");
00462 
00463     if (this->m_src_class == phCLASS_CLIENT)
00464     {
00465         src_class = "Client";
00466         src_class_short = "C";
00467     }
00468     else if (this->m_src_class == phCLASS_NODE)
00469     {
00470         src_class = "Node";
00471         src_class_short = "N";
00472     }
00473     else
00474     {
00475         src_class = "Unknown";
00476         src_class_short = "U";
00477     }
00478 
00479     if (this->m_dst_class == phCLASS_CLIENT)
00480     {
00481         dst_class = "Client";
00482         dst_class_short = "C";
00483     }
00484     else if (this->m_dst_class == phCLASS_NODE)
00485     {
00486         dst_class = "Node";
00487         dst_class_short = "N";
00488     }
00489     else
00490     {
00491         dst_class = "Unknown";
00492         dst_class_short = "U";
00493     }
00494         
00495     if (this->m_message_id == phCLIENT_LOCK_MSG)
00496     {
00497         message_str = "phCLIENT_REQUEST";
00498         message_str_short = "CREQ";
00499     }
00500     else if (this->m_message_id == phCLIENT_UNLOCK_MSG)
00501     {
00502         message_str = "phCLIENT_RELEASE";
00503         message_str_short = "CREL";
00504     }
00505     else if (this->m_message_id == phCLIENT_SUCCESS)
00506     {
00507         message_str = "phCLIENT_SUCCESS";
00508         message_str_short = "CSUC";
00509     }
00510     else if (this->m_message_id == phCLIENT_FAIL)
00511     {
00512         message_str = "phCLIENT_FAIL";
00513         message_str_short = "CFAL";
00514     }
00515     else if (this->m_message_id == phCLIENT_HELLO)
00516     {
00517         message_str = "phCLIENT_HELLO";
00518         message_str_short = "CHEL";
00519     }
00520     else if (this->m_message_id == phCLIENT_GOODBYE)
00521     {
00522         message_str = "phCLIENT_GOODBYE";
00523         message_str_short = "CGDB";
00524     }
00525     else if (this->m_message_id == phNODE_REQUEST)
00526     {
00527         message_str = "phNODE_REQUEST";
00528         message_str_short = "NREQ";
00529     }
00530     else if (this->m_message_id == phNODE_REPLY)
00531     {
00532         message_str = "phNODE_REPLY";
00533         message_str_short = "NREP";
00534     }
00535     else if (this->m_message_id == phNODE_RELEASE)
00536     {
00537         message_str = "phNODE_RELEASE";
00538         message_str_short = "NREL";
00539     }
00540     else
00541     {
00542         message_str = "UNKNOWN";
00543         message_str_short = "UNKN";
00544     }
00545 
00546     timestamp = (this->m_timestamp == phTIME_INVALID ? 
00547                     0 : this->m_timestamp);
00548    
00549     if (short_print == 1)
00550     {
00551         fprintf(fd,"phMessage([t %2u][s|%1u:%-1s]->[d|%1u:%-1s][mg|%4u:%-5s][m|%3u])\n",
00552             timestamp,
00553             this->m_src_id,src_class_short, 
00554             this->m_dst_id,dst_class_short, 
00555             this->m_message_id,
00556             message_str_short,
00557             this->m_mutex_id);
00558     }
00559     else if (short_print == 2)
00560     {
00561         fprintf(fd,"phMessage(%2u:[%1u:%-1s]:[%1u:%-1s]:[%-4s]:%2u)\n",
00562             timestamp,
00563             this->m_src_id,src_class_short, 
00564             this->m_dst_id,dst_class_short, 
00565             message_str_short,
00566             this->m_mutex_id);
00567     }
00568     else
00569     {
00570         fprintf(fd,"phMessage([t %6u][s|%3u:%-8s]->[d|%3u:%-8s][msg|%5u:%-20s][mutex|%3u])\n",
00571             timestamp,
00572             this->m_src_id,src_class, 
00573             this->m_dst_id,dst_class, 
00574             this->m_message_id,
00575             message_str,
00576             this->m_mutex_id);
00577     }
00578     fflush(fd);
00579     
00580     return phSUCCESS;
00581 error:
00582     return phFAIL;
00583 }
00584 
00585 
00586 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:05 2007 for phission by  doxygen 1.4.4