Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Name_Proxy.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // Name_Proxy.cpp
00003 // $Id: Name_Proxy.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:21 chad Exp $
00004 
00005 #include "ace/Name_Proxy.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 ACE_RCSID(ace, Name_Proxy, "$Id: Name_Proxy.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:21 chad Exp $")
00009 
00010 void
00011 ACE_Name_Proxy::dump (void) const
00012 {
00013   ACE_TRACE ("ACE_Name_Proxy::dump");
00014 
00015   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00016   this->connector_.dump ();
00017   this->peer_.dump ();
00018   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("reactor_ = %x"), this->reactor_));
00019   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00020 }
00021 
00022 // Default constructor.
00023 
00024 ACE_Name_Proxy::ACE_Name_Proxy (void)
00025   : reactor_ (0)
00026 {
00027   ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
00028 }
00029 
00030 // Establish binding with the ACE_Name Server at remote_addr.
00031 
00032 int
00033 ACE_Name_Proxy::open (const ACE_INET_Addr &remote_addr,
00034                       ACE_Synch_Options& options)
00035 {
00036   ACE_TRACE ("ACE_Name_Proxy::open");
00037   ACE_Time_Value *timeout = 0;
00038 
00039   if (options[ACE_Synch_Options::USE_TIMEOUT])
00040     timeout = ACE_const_cast (ACE_Time_Value *, options.time_value ());
00041 
00042   // Initiate the connection.
00043   return this->connector_.connect (this->peer_,
00044                                    remote_addr,
00045                                    timeout);
00046 }
00047 
00048 // Establish binding with the ACE_Name Server at remote_addr.
00049 
00050 ACE_Name_Proxy::ACE_Name_Proxy (const ACE_INET_Addr &remote_addr,
00051                                 ACE_Synch_Options& options)
00052 {
00053   ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
00054   if (this->open (remote_addr, options) == -1
00055       && options[ACE_Synch_Options::USE_TIMEOUT] && errno != EWOULDBLOCK)
00056     ACE_ERROR ((LM_ERROR,
00057                 ACE_LIB_TEXT ("%p\n"),
00058                 ACE_LIB_TEXT ("ACE_Name_Proxy::ACE_Name_Proxy")));
00059 }
00060 
00061 // Obtain underlying handle.
00062 
00063 /* VIRTUAL */ ACE_HANDLE
00064 ACE_Name_Proxy::get_handle (void) const
00065 {
00066   ACE_TRACE ("ACE_Name_Proxy::get_handle");
00067   return this->peer_.get_handle ();
00068 }
00069 
00070 int
00071 ACE_Name_Proxy::request_reply (ACE_Name_Request &request)
00072 {
00073   ACE_TRACE ("ACE_Name_Proxy::request_reply");
00074   void *buffer;
00075   ssize_t length = request.encode (buffer);
00076 
00077   if (length == -1)
00078     ACE_ERROR_RETURN ((LM_ERROR,
00079                        ACE_LIB_TEXT ("%p\n"),
00080                        ACE_LIB_TEXT ("encode failed")),
00081                       -1);
00082 
00083   // Transmit request via a blocking send.
00084 
00085   if (this->peer_.send_n (buffer, length) != length)
00086     ACE_ERROR_RETURN ((LM_ERROR,
00087                        ACE_LIB_TEXT ("%p\n"),
00088                        ACE_LIB_TEXT ("send_n failed")),
00089                       -1);
00090   else
00091     {
00092       ACE_Name_Reply reply;
00093 
00094       // Receive reply via blocking read.
00095 
00096       if (this->peer_.recv_n (&reply,
00097                               sizeof reply) == -1)
00098         ACE_ERROR_RETURN ((LM_ERROR,
00099                            ACE_LIB_TEXT ("%p\n"),
00100                            ACE_LIB_TEXT ("recv failed")),
00101                           -1);
00102       else if (reply.decode () == -1)
00103         ACE_ERROR_RETURN ((LM_ERROR,
00104                            ACE_LIB_TEXT ("%p\n"),
00105                            ACE_LIB_TEXT ("decode failed")),
00106                           -1);
00107       errno = int (reply.errnum ());
00108       return reply.status ();
00109     }
00110 }
00111 
00112 int
00113 ACE_Name_Proxy::send_request (ACE_Name_Request &request)
00114 {
00115   ACE_TRACE ("ACE_Name_Proxy::send_request");
00116   void *buffer;
00117   ssize_t length = request.encode (buffer);
00118 
00119   if (length == -1)
00120     ACE_ERROR_RETURN ((LM_ERROR,
00121                        ACE_LIB_TEXT ("%p\n"),
00122                        ACE_LIB_TEXT ("encode failed")),
00123                       -1);
00124 
00125   // Transmit request via a blocking send.
00126 
00127   else if (this->peer_.send_n (buffer, length) != length)
00128     ACE_ERROR_RETURN ((LM_ERROR,
00129                        ACE_LIB_TEXT ("%p\n"),
00130                        ACE_LIB_TEXT ("send_n failed")),
00131                       -1);
00132   return 0;
00133 }
00134 
00135 int
00136 ACE_Name_Proxy::recv_reply (ACE_Name_Request &reply)
00137 {
00138   ACE_TRACE ("ACE_Name_Proxy::recv_reply");
00139   // Read the first 4 bytes to get the length of the message This
00140   // implementation assumes that the first 4 bytes are the length of
00141   // the message.
00142   ssize_t n = this->peer_.recv ((void *) &reply, sizeof (ACE_UINT32));
00143 
00144   switch (n)
00145     {
00146     case -1:
00147       // FALLTHROUGH
00148       ACE_DEBUG ((LM_DEBUG,
00149                   ACE_LIB_TEXT ("****************** recv_reply returned -1\n")));
00150     default:
00151       ACE_ERROR ((LM_ERROR,
00152                   ACE_LIB_TEXT ("%p got %d bytes, expected %d bytes\n"),
00153                   ACE_LIB_TEXT ("recv failed"),
00154                   n,
00155                   sizeof (ACE_UINT32)));
00156       // FALLTHROUGH
00157     case 0:
00158       // We've shutdown unexpectedly
00159       return -1;
00160       // NOTREACHED
00161     case sizeof (ACE_UINT32):
00162       {
00163         // Transform the length into host byte order.
00164         ssize_t length = ntohl (reply.length ());
00165 
00166         // Receive the rest of the request message.
00167         // @@ beware of blocking read!!!.
00168         n = this->peer_.recv ((void *) (((char *) &reply)
00169                                         + sizeof (ACE_UINT32)),
00170                               length - sizeof (ACE_UINT32));
00171 
00172         // Subtract off the size of the part we skipped over...
00173         if (n != ssize_t (length - sizeof (ACE_UINT32)))
00174           {
00175             ACE_ERROR ((LM_ERROR,
00176                         ACE_LIB_TEXT ("%p expected %d, got %d\n"),
00177                         ACE_LIB_TEXT ("invalid length"),
00178                         length,
00179                         n));
00180             return -1;
00181           }
00182 
00183         // Decode the request into host byte order.
00184         if (reply.decode () == -1)
00185           {
00186             ACE_ERROR ((LM_ERROR,
00187                         ACE_LIB_TEXT ("%p\n"),
00188                         ACE_LIB_TEXT ("decode failed")));
00189             return -1;
00190           }
00191       }
00192     }
00193   return 0;
00194 }
00195 
00196 // Close down the connection to the server.
00197 
00198 ACE_Name_Proxy::~ACE_Name_Proxy (void)
00199 {
00200   ACE_TRACE ("ACE_Name_Proxy::~ACE_Name_Proxy");
00201   this->peer_.close ();
00202 }

Generated on Mon Jun 16 11:20:27 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002