GIOP_Message_State.cpp

Go to the documentation of this file.
00001 // $Id: GIOP_Message_State.cpp 79012 2007-07-24 14:21:05Z johnnyw $
00002 
00003 #include "tao/GIOP_Message_State.h"
00004 #include "tao/debug.h"
00005 #include "tao/GIOP_Message_Base.h"
00006 
00007 #include "ace/Log_Msg.h"
00008 
00009 #if !defined (__ACE_INLINE__)
00010 # include "tao/GIOP_Message_State.inl"
00011 #endif /* __ACE_INLINE__ */
00012 
00013 ACE_RCSID (tao,
00014            GIOP_Message_State,
00015            "$Id: GIOP_Message_State.cpp 79012 2007-07-24 14:21:05Z johnnyw $")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 int
00020 TAO_GIOP_Message_State::parse_message_header (ACE_Message_Block &incoming)
00021 {
00022   if (incoming.length () >= TAO_GIOP_MESSAGE_HEADER_LEN)
00023     {
00024       // Parse the GIOP header
00025       return this->parse_message_header_i (incoming);
00026     }
00027 
00028   // One indicates that we didn't have enough data in the message to
00029   // parse the header
00030   return 1;
00031 }
00032 
00033 int
00034 TAO_GIOP_Message_State::parse_message_header_i (ACE_Message_Block &incoming)
00035 {
00036   if (TAO_debug_level > 8)
00037     {
00038       ACE_DEBUG ((LM_DEBUG,
00039                   ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::parse_message_header_i\n")
00040                   ));
00041     }
00042 
00043   // Grab the rd_ptr_ from the message block..
00044   char *buf = incoming.rd_ptr ();
00045 
00046   // Parse the magic bytes first
00047   if (this->parse_magic_bytes (buf) == -1)
00048     {
00049       return -1;
00050     }
00051 
00052   // Get the version information
00053   if (this->get_version_info (buf) == -1)
00054     return -1;
00055 
00056   // Get the byte order information...
00057   if (this->get_byte_order_info (buf) == -1)
00058     return -1;
00059 
00060   // Get the message type
00061   this->message_type_ = this->message_type (buf[TAO_GIOP_MESSAGE_TYPE_OFFSET]);
00062 
00063   // Get the size of the message..
00064   this->get_payload_size (buf);
00065 
00066   if (this->payload_size_ == 0)
00067     {
00068       switch (this->message_type_)
00069         {
00070         case TAO_PLUGGABLE_MESSAGE_MESSAGERROR:
00071         case TAO_PLUGGABLE_MESSAGE_CLOSECONNECTION:
00072           if (TAO_debug_level > 0)
00073             {
00074               const char* which =
00075                 (this->message_type_ == TAO_PLUGGABLE_MESSAGE_CLOSECONNECTION) ? "CloseConnection" :
00076                 (this->message_type_ == TAO_PLUGGABLE_MESSAGE_MESSAGERROR) ? "MessageError" : "unknown";
00077               ACE_DEBUG ((LM_DEBUG,
00078                           ACE_TEXT ("TAO (%P|%t) - GIOP %s received \n"), which));
00079             }
00080           return 0;
00081         default:
00082           if (TAO_debug_level > 0)
00083             ACE_DEBUG ((LM_DEBUG,
00084                         ACE_TEXT ("TAO (%P|%t) - ")
00085                         ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
00086                         ACE_TEXT ("Message of size zero recd. \n")));
00087           return -1;
00088         }
00089     }
00090 
00091   return 0; // success
00092 }
00093 
00094 TAO_Pluggable_Message_Type
00095 TAO_GIOP_Message_State::message_type (CORBA::Octet type) const
00096 {
00097   // Convert to the right type of Pluggable Messaging message type.
00098 
00099   switch (type)
00100     {
00101     case TAO_GIOP_REQUEST:
00102       return TAO_PLUGGABLE_MESSAGE_REQUEST;
00103     case TAO_GIOP_LOCATEREQUEST:
00104       return TAO_PLUGGABLE_MESSAGE_LOCATEREQUEST;
00105 
00106     case TAO_GIOP_LOCATEREPLY:
00107       return TAO_PLUGGABLE_MESSAGE_LOCATEREPLY;
00108 
00109     case TAO_GIOP_REPLY:
00110       return TAO_PLUGGABLE_MESSAGE_REPLY;
00111 
00112     case TAO_GIOP_CLOSECONNECTION:
00113       return TAO_PLUGGABLE_MESSAGE_CLOSECONNECTION;
00114 
00115     case TAO_GIOP_FRAGMENT:
00116       return TAO_PLUGGABLE_MESSAGE_FRAGMENT;
00117 
00118     case TAO_GIOP_MESSAGERROR:
00119       return TAO_PLUGGABLE_MESSAGE_MESSAGERROR;
00120 
00121     case TAO_GIOP_CANCELREQUEST:
00122       return TAO_PLUGGABLE_MESSAGE_CANCELREQUEST;
00123 
00124     default:
00125         if (TAO_debug_level > 0)
00126           {
00127             ACE_ERROR ((LM_ERROR,
00128                     ACE_TEXT ("TAO (%P|%t) %N:%l        message_type : ")
00129                     ACE_TEXT ("wrong message.\n")));
00130            }
00131     }
00132 
00133   return TAO_PLUGGABLE_MESSAGE_MESSAGERROR;
00134 }
00135 
00136 int
00137 TAO_GIOP_Message_State::parse_magic_bytes (char *buf)
00138 {
00139   // The values are hard-coded to support non-ASCII platforms.
00140   if (!(buf [0] == 0x47      // 'G'
00141         && buf [1] == 0x49   // 'I'
00142         && buf [2] == 0x4f   // 'O'
00143         && buf [3] == 0x50)) // 'P'
00144     {
00145       if (TAO_debug_level > 0)
00146         ACE_DEBUG ((LM_DEBUG,
00147                     ACE_TEXT ("TAO (%P|%t) - ")
00148                     ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
00149                     ACE_TEXT ("bad header: ")
00150                     ACE_TEXT ("magic word [%02x,%02x,%02x,%02x]\n"),
00151                     buf[0],
00152                     buf[1],
00153                     buf[2],
00154                     buf[3]));
00155       return -1;
00156     }
00157 
00158  return 0;
00159 }
00160 
00161 int
00162 TAO_GIOP_Message_State::get_version_info (char *buf)
00163 {
00164   if (TAO_debug_level > 8)
00165     {
00166       ACE_DEBUG ((LM_DEBUG,
00167                   "TAO (%P|%t) - GIOP_Message_State::get_version_info\n"));
00168     }
00169 
00170   // We have a GIOP message on hand. Get its revision numbers
00171   CORBA::Octet incoming_major = buf[TAO_GIOP_VERSION_MAJOR_OFFSET];
00172   CORBA::Octet incoming_minor = buf[TAO_GIOP_VERSION_MINOR_OFFSET];
00173 
00174   // Check the revision information
00175   if (TAO_GIOP_Message_Generator_Parser_Impl::check_revision (
00176           incoming_major,
00177           incoming_minor) == 0)
00178     {
00179       if (TAO_debug_level > 0)
00180         {
00181           ACE_DEBUG ((LM_DEBUG,
00182                       ACE_TEXT ("TAO (%P|%t) - bad version <%d.%d>\n"),
00183                       incoming_major, incoming_minor));
00184         }
00185 
00186       return -1;
00187     }
00188 
00189   // Set the version
00190   this->giop_version_.minor = incoming_minor;
00191   this->giop_version_.major = incoming_major;
00192 
00193   return 0;
00194 }
00195 
00196 int
00197 TAO_GIOP_Message_State::get_byte_order_info (char *buf)
00198 {
00199   if (TAO_debug_level > 8)
00200     {
00201       ACE_DEBUG ((LM_DEBUG,
00202                   ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info\n") ));
00203     }
00204 
00205     // Let us be specific that this is for 1.0
00206   if (this->giop_version_.minor == 0 &&
00207       this->giop_version_.major == 1)
00208     {
00209       this->byte_order_ =
00210         buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET];
00211 
00212       if (this->byte_order_ != 0 &&
00213           this->byte_order_ != 1)
00214         {
00215           if (TAO_debug_level > 2)
00216             {
00217               ACE_DEBUG ((LM_DEBUG,
00218                           ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info, ")
00219                           ACE_TEXT ("invalid byte order <%d> for version <1.0>\n"),
00220                           this->byte_order_));
00221             }
00222           return -1;
00223         }
00224     }
00225   else
00226     {
00227       // Read the byte ORDER
00228       this->byte_order_ =
00229         (CORBA::Octet) (buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x01);
00230 
00231       // Read the fragment bit
00232       this->more_fragments_ =
00233         (CORBA::Octet) (buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x02);
00234 
00235       if ((buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET] & ~0x3) != 0)
00236         {
00237           if (TAO_debug_level > 2)
00238             {
00239               ACE_DEBUG ((LM_DEBUG,
00240                           ACE_TEXT ("TAO (%P|%t) - invalid flags for <%d> ")
00241                           ACE_TEXT ("for version <%d %d> \n"),
00242                           buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET],
00243                           this->giop_version_.major,
00244                           this->giop_version_.minor));
00245             }
00246           return -1;
00247         }
00248     }
00249 
00250   return 0;
00251 }
00252 
00253 void
00254 TAO_GIOP_Message_State::get_payload_size (char *rd_ptr)
00255 {
00256   // Move the read pointer
00257   rd_ptr += TAO_GIOP_MESSAGE_SIZE_OFFSET;
00258 
00259   this->payload_size_ =  this->read_ulong (rd_ptr);
00260 }
00261 
00262 CORBA::ULong
00263 TAO_GIOP_Message_State::read_ulong (const char *rd_ptr) const
00264 {
00265   CORBA::ULong x = 0;
00266 
00267   // We don't need to do this sort of copy. But some compilers (read it
00268   // as SunCC) have a problem in deferencing from the
00269   // reinterpret_cast pointer of the <rd_ptr>, as the <rd_ptr> can be
00270   // on stack. So let us go ahead with this copying...
00271   char buf[] =
00272     {
00273       *rd_ptr,
00274       *(rd_ptr + 1),
00275       *(rd_ptr + 2),
00276       *(rd_ptr + 3)
00277     };
00278 
00279 #if !defined (ACE_DISABLE_SWAP_ON_READ)
00280   if (!(this->byte_order_ != ACE_CDR_BYTE_ORDER))
00281     {
00282       x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
00283     }
00284   else
00285     {
00286       ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (&x));
00287     }
00288 #else
00289   x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
00290 #endif /* ACE_DISABLE_SWAP_ON_READ */
00291 
00292   return x;
00293 }
00294 
00295 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:07:32 2008 for TAO by doxygen 1.3.6