Name_Request_Reply.cpp

Go to the documentation of this file.
00001 #include "ace/Name_Request_Reply.h"
00002 #include "ace/Basic_Types.h"
00003 #include "ace/CDR_Base.h"
00004 #include "ace/Log_Msg.h"
00005 #include "ace/Time_Value.h"
00006 #include "ace/Truncate.h"
00007 #include "ace/OS_NS_string.h"
00008 #include "ace/os_include/arpa/os_inet.h"
00009 
00010 ACE_RCSID (ace,
00011            Name_Request_Reply,
00012            "$Id: Name_Request_Reply.cpp 80826 2008-03-04 14:51:23Z wotte $")
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 // Default "do nothing" constructor.
00017 
00018 ACE_Name_Request::ACE_Name_Request (void)
00019 {
00020   ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
00021 }
00022 
00023 // Create a ACE_Name_Request message.
00024 
00025 ACE_Name_Request::ACE_Name_Request (
00026   ACE_INT32 t, // Type of request.
00027   const ACE_WCHAR_T name[], // Name
00028   const ACE_UINT32 name_length, // size in bytes
00029   const ACE_WCHAR_T value[], //
00030   const ACE_UINT32 value_length, // size in bytes
00031   const char type[], //
00032   const ACE_UINT32 type_length, // size in bytes
00033   ACE_Time_Value *timeout) // Max time waiting for request.
00034 {
00035   ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
00036   this->msg_type (t);
00037   this->name_len (name_length);
00038   this->value_len (value_length);
00039   this->type_len (type_length);
00040 
00041   // If timeout is a NULL pointer, then block forever...
00042   if (timeout == 0)
00043     {
00044       this->transfer_.block_forever_ = 1;
00045       this->transfer_.sec_timeout_   = 0;
00046       this->transfer_.usec_timeout_  = 0;
00047     }
00048   else // Do a "timed wait."
00049     {
00050       this->block_forever (0);
00051       // Keep track of how long client is willing to wait.
00052       this->transfer_.sec_timeout_ = timeout->sec ();
00053       this->transfer_.usec_timeout_ = timeout->usec ();
00054     }
00055 
00056   // Set up pointers and copy name value and type into request.
00057   this->name_ = this->transfer_.data_;
00058   this->value_  = &this->name_[name_length / sizeof (ACE_WCHAR_T) ];
00059   this->type_  = (char *)(&this->value_[value_length / sizeof (ACE_WCHAR_T)]); //
00060 
00061   (void) ACE_OS::memcpy (this->name_,
00062                          name,
00063                          name_length);
00064   (void) ACE_OS::memcpy (this->value_,
00065                          value,
00066                          value_length);
00067   (void) ACE_OS::memcpy (this->type_,
00068                          type,
00069                          type_length);
00070 
00071   // Compute size of the fixed portion of the message...
00072   size_t len = sizeof this->transfer_ - sizeof this->transfer_.data_;
00073 
00074   // ... then add in the amount of the variable-sized portion.
00075   len += name_length + value_length + type_length ;
00076 
00077   this->length (static_cast<ACE_UINT32> (len));
00078 }
00079 
00080 // Initialize length_ in order to avoid problems with byte-ordering.
00081 
00082 void
00083 ACE_Name_Request::init (void)
00084 {
00085   ACE_TRACE ("ACE_Name_Request::init");
00086   this->length (sizeof this->transfer_);
00087 }
00088 
00089 // = Set/get the length of the encoded/decoded message.
00090 
00091 ACE_UINT32
00092 ACE_Name_Request::length (void) const
00093 {
00094   ACE_TRACE ("ACE_Name_Request::length");
00095   return this->transfer_.length_;
00096 }
00097 
00098 void
00099 ACE_Name_Request::length (ACE_UINT32 l)
00100 {
00101   ACE_TRACE ("ACE_Name_Request::length");
00102   this->transfer_.length_ = l;
00103 }
00104 
00105 // = Set/get the type of the message.
00106 
00107 ACE_INT32
00108 ACE_Name_Request::msg_type (void) const
00109 {
00110   ACE_TRACE ("ACE_Name_Request::msg_type");
00111   return this->transfer_.msg_type_;
00112 }
00113 
00114 void
00115 ACE_Name_Request::msg_type (ACE_INT32 t)
00116 {
00117   ACE_TRACE ("ACE_Name_Request::msg_type");
00118   this->transfer_.msg_type_ = t;
00119 }
00120 
00121 // = Set/get the len of the name
00122 
00123 ACE_UINT32
00124 ACE_Name_Request::name_len (void) const
00125 {
00126   ACE_TRACE ("ACE_Name_Request::name_len");
00127   return this->transfer_.name_len_;
00128 }
00129 
00130 void
00131 ACE_Name_Request::name_len (ACE_UINT32 t)
00132 {
00133   ACE_TRACE ("ACE_Name_Request::name_len");
00134   this->transfer_.name_len_ = t;
00135 }
00136 
00137 // = Set/get the len of the value
00138 
00139 ACE_UINT32
00140 ACE_Name_Request::value_len (void) const
00141 {
00142   ACE_TRACE ("ACE_Name_Request::value_len");
00143   return this->transfer_.value_len_;
00144 }
00145 
00146 void
00147 ACE_Name_Request::value_len (ACE_UINT32 t)
00148 {
00149   ACE_TRACE ("ACE_Name_Request::value_len");
00150   this->transfer_.value_len_ = t;
00151 }
00152 
00153 // = Set/get the len of the type
00154 
00155 ACE_UINT32
00156 ACE_Name_Request::type_len (void) const
00157 {
00158   ACE_TRACE ("ACE_Name_Request::type_len");
00159   return this->transfer_.type_len_;
00160 }
00161 
00162 void
00163 ACE_Name_Request::type_len (ACE_UINT32 t)
00164 {
00165   ACE_TRACE ("ACE_Name_Request::type_len");
00166   this->transfer_.type_len_ = t;
00167 }
00168 
00169 // = Set/get the blocking semantics.
00170 
00171 ACE_UINT32
00172 ACE_Name_Request::block_forever (void) const
00173 {
00174   ACE_TRACE ("ACE_Name_Request::block_forever");
00175   return this->transfer_.block_forever_;
00176 }
00177 
00178 void
00179 ACE_Name_Request::block_forever (ACE_UINT32 bs)
00180 {
00181   ACE_TRACE ("ACE_Name_Request::block_forever");
00182   this->transfer_.block_forever_ = bs;
00183 }
00184 
00185 // = Set/get the timeout.
00186 
00187 ACE_Time_Value
00188 ACE_Name_Request::timeout (void) const
00189 {
00190   ACE_TRACE ("ACE_Name_Request::timeout");
00191   time_t sec = ACE_Utils::truncate_cast<time_t> (this->transfer_.sec_timeout_);
00192   return ACE_Time_Value (sec, this->transfer_.usec_timeout_);
00193 }
00194 
00195 void
00196 ACE_Name_Request::timeout (const ACE_Time_Value timeout)
00197 {
00198   ACE_TRACE ("ACE_Name_Request::timeout");
00199   this->transfer_.sec_timeout_ = timeout.sec ();
00200   this->transfer_.usec_timeout_ = timeout.usec ();
00201 }
00202 
00203 // = Set/get the name
00204 
00205 const ACE_WCHAR_T *
00206 ACE_Name_Request::name (void) const
00207 {
00208   ACE_TRACE ("ACE_Name_Request::name");
00209   return this->name_;
00210 }
00211 
00212 void
00213 ACE_Name_Request::name (const ACE_WCHAR_T *t)
00214 {
00215   ACE_TRACE ("ACE_Name_Request::name");
00216   (void) ACE_OS::memcpy (this->name_,
00217                          t,
00218                          this->name_len ());
00219 }
00220 
00221 // = Set/get the value
00222 
00223 const ACE_WCHAR_T *
00224 ACE_Name_Request::value (void) const
00225 {
00226   ACE_TRACE ("ACE_Name_Request::value");
00227   return this->value_;
00228 }
00229 
00230 void
00231 ACE_Name_Request::value (const ACE_WCHAR_T *c)
00232 {
00233   ACE_TRACE ("ACE_Name_Request::value");
00234 
00235   (void) ACE_OS::memcpy (this->value_,
00236                          c,
00237                          this->value_len());
00238 }
00239 
00240 // = Set/get the type
00241 
00242 const char *
00243 ACE_Name_Request::type (void) const
00244 {
00245   ACE_TRACE ("ACE_Name_Request::type");
00246   return this->type_;
00247 }
00248 
00249 void
00250 ACE_Name_Request::type (const char *c)
00251 {
00252   ACE_TRACE ("ACE_Name_Request::type");
00253   ACE_OS::strsncpy (this->type_,
00254                     c,
00255                     sizeof this->type_);
00256 }
00257 
00258 // Encode the transfer buffer into network byte order so that it can
00259 // be sent to the server.
00260 
00261 int
00262 ACE_Name_Request::encode (void *&buf)
00263 {
00264   ACE_TRACE ("ACE_Name_Request::encode");
00265   // Compute the length *before* doing the marshaling.
00266 
00267   ACE_UINT32 len = this->length ();
00268 
00269   size_t nv_data_len =
00270     (this->transfer_.name_len_ + this->transfer_.value_len_)
00271     / sizeof (ACE_WCHAR_T);
00272 
00273   for (size_t i = 0; i < nv_data_len; i++)
00274     this->transfer_.data_[i] =
00275       ACE_HTONS (this->transfer_.data_[i]);
00276 
00277   buf = (void *) &this->transfer_;
00278   this->transfer_.block_forever_ = ACE_HTONL (this->transfer_.block_forever_);
00279   this->transfer_.usec_timeout_  = ACE_HTONL (this->transfer_.usec_timeout_);
00280 #if defined (ACE_LITTLE_ENDIAN)
00281   ACE_UINT64 secs = this->transfer_.sec_timeout_;
00282   ACE_CDR::swap_8 ((const char *)&secs, (char *)&this->transfer_.sec_timeout_);
00283 #endif
00284   this->transfer_.length_ = ACE_HTONL (this->transfer_.length_);
00285   this->transfer_.msg_type_ = ACE_HTONL (this->transfer_.msg_type_);
00286   this->transfer_.name_len_ = ACE_HTONL (this->transfer_.name_len_);
00287   this->transfer_.value_len_ = ACE_HTONL (this->transfer_.value_len_);
00288   this->transfer_.type_len_ = ACE_HTONL (this->transfer_.type_len_);
00289 
00290   return len;
00291 }
00292 
00293 // Decode the transfer buffer into host byte byte order so that it can
00294 // be used by the server.
00295 
00296 int
00297 ACE_Name_Request::decode (void)
00298 {
00299   ACE_TRACE ("ACE_Name_Request::decode");
00300   // Decode the fixed-sized portion first.
00301   this->transfer_.block_forever_ = ACE_NTOHL (this->transfer_.block_forever_);
00302   this->transfer_.usec_timeout_  = ACE_NTOHL (this->transfer_.usec_timeout_);
00303 #if defined (ACE_LITTLE_ENDIAN)
00304   ACE_UINT64 secs = this->transfer_.sec_timeout_;
00305   ACE_CDR::swap_8 ((const char *)&secs, (char *)&this->transfer_.sec_timeout_);
00306 #endif
00307   this->transfer_.length_ = ACE_NTOHL (this->transfer_.length_);
00308   this->transfer_.msg_type_ = ACE_NTOHL (this->transfer_.msg_type_);
00309   this->transfer_.name_len_ = ACE_NTOHL (this->transfer_.name_len_);
00310   this->transfer_.value_len_ = ACE_NTOHL (this->transfer_.value_len_);
00311   this->transfer_.type_len_ = ACE_NTOHL (this->transfer_.type_len_);
00312 
00313   size_t nv_data_len =
00314     (this->transfer_.name_len_ + this->transfer_.value_len_)
00315     / sizeof (ACE_WCHAR_T);
00316 
00317   for (size_t i = 0; i < nv_data_len; i++)
00318     this->transfer_.data_[i] =
00319       ACE_NTOHS (this->transfer_.data_[i]);
00320 
00321   this->name_ = this->transfer_.data_;
00322   this->value_ = &this->name_[this->transfer_.name_len_ / sizeof (ACE_WCHAR_T)];
00323   this->type_ = (char *)(&this->value_[this->transfer_.value_len_ / sizeof (ACE_WCHAR_T)]);
00324   this->type_[this->transfer_.type_len_] = '\0';
00325 
00326   // Decode the variable-sized portion.
00327   return 0;
00328 }
00329 
00330 // Print out the current values of the ACE_Name_Request.
00331 
00332 void
00333 ACE_Name_Request::dump (void) const
00334 {
00335 #if defined (ACE_HAS_DUMP)
00336   ACE_TRACE ("ACE_Name_Request::dump");
00337   ACE_DEBUG ((LM_DEBUG,
00338               ACE_TEXT ("*******\nlength = %d\n"),
00339               this->length ()));
00340   ACE_DEBUG ((LM_DEBUG,
00341               ACE_TEXT ("message-type = ")));
00342 
00343   switch (this->msg_type ())
00344     {
00345     case ACE_Name_Request::BIND:
00346       ACE_DEBUG ((LM_DEBUG,
00347                   ACE_TEXT ("BIND\n")));
00348       break;
00349     case ACE_Name_Request::REBIND:
00350       ACE_DEBUG ((LM_DEBUG,
00351                   ACE_TEXT ("REBIND\n")));
00352       break;
00353     case ACE_Name_Request::RESOLVE:
00354       ACE_DEBUG ((LM_DEBUG,
00355                   ACE_TEXT ("RESOLVE\n")));
00356       break;
00357     case ACE_Name_Request::UNBIND:
00358       ACE_DEBUG ((LM_DEBUG,
00359                   ACE_TEXT ("UNBIND\n")));
00360       break;
00361     case ACE_Name_Request::LIST_NAMES:
00362       ACE_DEBUG ((LM_DEBUG,
00363                   ACE_TEXT ("LIST_NAMES\n")));
00364       break;
00365     case ACE_Name_Request::LIST_VALUES:
00366       ACE_DEBUG ((LM_DEBUG,
00367                   ACE_TEXT ("LIST_VALUES\n")));
00368       break;
00369     case ACE_Name_Request::LIST_TYPES:
00370       ACE_DEBUG ((LM_DEBUG,
00371                   ACE_TEXT ("LIST_TYPES\n")));
00372       break;
00373     case ACE_Name_Request::LIST_NAME_ENTRIES:
00374       ACE_DEBUG ((LM_DEBUG,
00375                   ACE_TEXT ("LIST_NAME_ENTRIES\n")));
00376       break;
00377     case ACE_Name_Request::LIST_VALUE_ENTRIES:
00378       ACE_DEBUG ((LM_DEBUG,
00379                   ACE_TEXT ("LIST_VALUE_ENTRIES\n")));
00380       break;
00381     case ACE_Name_Request::LIST_TYPE_ENTRIES:
00382       ACE_DEBUG ((LM_DEBUG,
00383                   ACE_TEXT ("LIST_TYPE_ENTRIES\n")));
00384       break;
00385     default:
00386       ACE_DEBUG ((LM_DEBUG,
00387                   ACE_TEXT ("<unknown type> = %d\n"),
00388                   this->msg_type ()));
00389       break;
00390     }
00391 
00392   if (this->block_forever ())
00393     ACE_DEBUG ((LM_DEBUG,
00394                 ACE_TEXT ("blocking forever\n")));
00395   else
00396     {
00397 #if !defined (ACE_NLOGGING)
00398       ACE_Time_Value tv = this->timeout ();
00399 #endif /* ! ACE_NLOGGING */
00400       ACE_DEBUG ((LM_DEBUG,
00401                   ACE_TEXT ("waiting for %d secs and %d usecs\n"),
00402                   tv.sec (),
00403                   tv.usec ()));
00404     }
00405   ACE_DEBUG ((LM_DEBUG,
00406               ACE_TEXT ("*******\nname_len = %d\n"),
00407               this->name_len ()));
00408   ACE_DEBUG ((LM_DEBUG,
00409               ACE_TEXT ("*******\nvalue_len = %d\n"),
00410               this->value_len ()));
00411 
00412   ACE_DEBUG ((LM_DEBUG,
00413               ACE_TEXT ("+++++++\n")));
00414 #endif /* ACE_HAS_DUMP */
00415 }
00416 
00417 // Default constructor.
00418 
00419 ACE_Name_Reply::ACE_Name_Reply (void)
00420 {
00421   ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply");
00422 
00423   // Initialize to a known quantity.
00424   this->msg_type (0);
00425   this->errnum (0);
00426   this->length (sizeof this->transfer_);
00427 }
00428 
00429 // Create a ACE_Name_Reply message.
00430 
00431 ACE_Name_Reply::ACE_Name_Reply (ACE_UINT32 t, ACE_UINT32 err) // Type of reply.
00432 {
00433   ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply");
00434   this->msg_type (t);
00435   this->errnum (err);
00436   this->length (sizeof this->transfer_);
00437 }
00438 
00439 // Initialize length_ to avoid problems with byte-ordering.
00440 
00441 void
00442 ACE_Name_Reply::init (void)
00443 {
00444   ACE_TRACE ("ACE_Name_Reply::init");
00445   this->length (sizeof this->transfer_);
00446 }
00447 
00448 // = Set/get the length of the encoded/decoded message.
00449 
00450 ACE_UINT32
00451 ACE_Name_Reply::length (void) const
00452 {
00453   ACE_TRACE ("ACE_Name_Reply::length");
00454   return this->transfer_.length_;
00455 }
00456 
00457 void
00458 ACE_Name_Reply::length (ACE_UINT32 l)
00459 {
00460   ACE_TRACE ("ACE_Name_Reply::length");
00461   this->transfer_.length_ = l;
00462 }
00463 
00464 // = Set/get the type of the message.
00465 
00466 ACE_INT32
00467 ACE_Name_Reply::msg_type (void) const
00468 {
00469   ACE_TRACE ("ACE_Name_Reply::msg_type");
00470   return this->transfer_.type_;
00471 }
00472 
00473 void
00474 ACE_Name_Reply::msg_type (ACE_INT32 t)
00475 {
00476   ACE_TRACE ("ACE_Name_Reply::msg_type");
00477   this->transfer_.type_ = t;
00478 }
00479 
00480 // Get the status of the reply (0 == success, -1 == failure).
00481 
00482 ACE_INT32
00483 ACE_Name_Reply::status (void) const
00484 {
00485   ACE_TRACE ("ACE_Name_Reply::status");
00486   return this->transfer_.type_;
00487 }
00488 
00489 // Set the status of the reply (0 == success, -1 == failure).
00490 
00491 void
00492 ACE_Name_Reply::status (ACE_INT32 s)
00493 {
00494   ACE_TRACE ("ACE_Name_Reply::status");
00495   if (s == -1)
00496     this->transfer_.type_ = -1;
00497   else
00498     this->transfer_.type_ = 0;
00499 }
00500 
00501 // = Set/get the errno of a failed reply.
00502 ACE_UINT32
00503 ACE_Name_Reply::errnum (void) const
00504 {
00505   ACE_TRACE ("ACE_Name_Reply::errnum");
00506   return this->transfer_.errno_;
00507 }
00508 
00509 void
00510 ACE_Name_Reply::errnum (ACE_UINT32 e)
00511 {
00512   ACE_TRACE ("ACE_Name_Reply::errnum");
00513   this->transfer_.errno_ = e;
00514 }
00515 
00516 // Encode the transfer buffer into network byte order
00517 // so that it can be sent to the client.
00518 
00519 int
00520 ACE_Name_Reply::encode (void *&buf)
00521 {
00522   ACE_TRACE ("ACE_Name_Reply::encode");
00523   int len = this->length (); // Get length *before* marshaling.
00524 
00525   this->transfer_.length_ = ACE_HTONL (this->transfer_.length_);
00526   this->transfer_.type_ = ACE_HTONL (this->transfer_.type_);
00527   this->transfer_.errno_ = ACE_HTONL (this->transfer_.errno_);
00528   buf = (void *) &this->transfer_;
00529   return len;
00530 }
00531 
00532 // Decode the transfer buffer into host byte order so that it can be
00533 // used by the client.
00534 
00535 int
00536 ACE_Name_Reply::decode (void)
00537 {
00538   ACE_TRACE ("ACE_Name_Reply::decode");
00539   this->transfer_.length_ = ACE_NTOHL (this->transfer_.length_);
00540   this->transfer_.type_ = ACE_NTOHL (this->transfer_.type_);
00541   this->transfer_.errno_ = ACE_NTOHL (this->transfer_.errno_);
00542   return 0;
00543 }
00544 
00545 // Print out current values of the ACE_Name_Reply object.
00546 
00547 void
00548 ACE_Name_Reply::dump (void) const
00549 {
00550 #if defined (ACE_HAS_DUMP)
00551   ACE_TRACE ("ACE_Name_Reply::dump");
00552   ACE_DEBUG ((LM_DEBUG,
00553               ACE_TEXT ("*******\nlength = %d\nerrnum = %d"),
00554               this->length (),
00555               this->errnum ()));
00556   ACE_DEBUG ((LM_DEBUG,
00557               ACE_TEXT ("type = ")));
00558   switch (this->msg_type ())
00559     {
00560     case 0:
00561       ACE_DEBUG ((LM_DEBUG,
00562                   ACE_TEXT ("SUCCESS\n")));
00563       break;
00564     case -1:
00565       ACE_DEBUG ((LM_DEBUG,
00566                   ACE_TEXT ("FAILURE\n")));
00567       break;
00568     default:
00569       ACE_DEBUG ((LM_DEBUG,
00570                   ACE_TEXT ("<unknown type> = %d\n"),
00571                   this->msg_type ()));
00572       break;
00573     }
00574 #endif /* ACE_HAS_DUMP */
00575 }
00576 
00577 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:41 2010 for ACE by  doxygen 1.4.7