00001
00002
00003 #include "ace/Name_Proxy.h"
00004 #include "ace/Log_Msg.h"
00005 #include "ace/os_include/arpa/os_inet.h"
00006
00007 ACE_RCSID(ace, Name_Proxy, "Name_Proxy.cpp,v 4.18 2005/10/28 16:14:53 ossama Exp")
00008
00009 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00010
00011 void
00012 ACE_Name_Proxy::dump (void) const
00013 {
00014 #if defined (ACE_HAS_DUMP)
00015 ACE_TRACE ("ACE_Name_Proxy::dump");
00016
00017 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00018 this->connector_.dump ();
00019 this->peer_.dump ();
00020 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("reactor_ = %x"), this->reactor_));
00021 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00022 #endif
00023 }
00024
00025
00026
00027 ACE_Name_Proxy::ACE_Name_Proxy (void)
00028 : reactor_ (0)
00029 {
00030 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
00031 }
00032
00033
00034
00035 int
00036 ACE_Name_Proxy::open (const ACE_INET_Addr &remote_addr,
00037 ACE_Synch_Options& options)
00038 {
00039 ACE_TRACE ("ACE_Name_Proxy::open");
00040 ACE_Time_Value *timeout = 0;
00041
00042 if (options[ACE_Synch_Options::USE_TIMEOUT])
00043 timeout = const_cast<ACE_Time_Value *> (options.time_value ());
00044
00045
00046 return this->connector_.connect (this->peer_,
00047 remote_addr,
00048 timeout);
00049 }
00050
00051
00052
00053 ACE_Name_Proxy::ACE_Name_Proxy (
00054 const ACE_INET_Addr &remote_addr,
00055 ACE_Synch_Options& options)
00056 : reactor_ (0)
00057 {
00058 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
00059 if (this->open (remote_addr, options) == -1
00060 && options[ACE_Synch_Options::USE_TIMEOUT] && errno != EWOULDBLOCK)
00061 ACE_ERROR ((LM_ERROR,
00062 ACE_LIB_TEXT ("%p\n"),
00063 ACE_LIB_TEXT ("ACE_Name_Proxy::ACE_Name_Proxy")));
00064 }
00065
00066
00067
00068 ACE_HANDLE
00069 ACE_Name_Proxy::get_handle (void) const
00070 {
00071 ACE_TRACE ("ACE_Name_Proxy::get_handle");
00072 return this->peer_.get_handle ();
00073 }
00074
00075 int
00076 ACE_Name_Proxy::request_reply (ACE_Name_Request &request)
00077 {
00078 ACE_TRACE ("ACE_Name_Proxy::request_reply");
00079 void *buffer;
00080 ssize_t length = request.encode (buffer);
00081
00082 if (length == -1)
00083 ACE_ERROR_RETURN ((LM_ERROR,
00084 ACE_LIB_TEXT ("%p\n"),
00085 ACE_LIB_TEXT ("encode failed")),
00086 -1);
00087
00088
00089
00090 if (this->peer_.send_n (buffer, length) != length)
00091 ACE_ERROR_RETURN ((LM_ERROR,
00092 ACE_LIB_TEXT ("%p\n"),
00093 ACE_LIB_TEXT ("send_n failed")),
00094 -1);
00095 else
00096 {
00097 ACE_Name_Reply reply;
00098
00099
00100
00101 if (this->peer_.recv_n (&reply,
00102 sizeof reply) == -1)
00103 ACE_ERROR_RETURN ((LM_ERROR,
00104 ACE_LIB_TEXT ("%p\n"),
00105 ACE_LIB_TEXT ("recv failed")),
00106 -1);
00107 else if (reply.decode () == -1)
00108 ACE_ERROR_RETURN ((LM_ERROR,
00109 ACE_LIB_TEXT ("%p\n"),
00110 ACE_LIB_TEXT ("decode failed")),
00111 -1);
00112 errno = int (reply.errnum ());
00113 return reply.status ();
00114 }
00115 }
00116
00117 int
00118 ACE_Name_Proxy::send_request (ACE_Name_Request &request)
00119 {
00120 ACE_TRACE ("ACE_Name_Proxy::send_request");
00121 void *buffer;
00122 ssize_t length = request.encode (buffer);
00123
00124 if (length == -1)
00125 ACE_ERROR_RETURN ((LM_ERROR,
00126 ACE_LIB_TEXT ("%p\n"),
00127 ACE_LIB_TEXT ("encode failed")),
00128 -1);
00129
00130
00131
00132 else if (this->peer_.send_n (buffer, length) != length)
00133 ACE_ERROR_RETURN ((LM_ERROR,
00134 ACE_LIB_TEXT ("%p\n"),
00135 ACE_LIB_TEXT ("send_n failed")),
00136 -1);
00137 return 0;
00138 }
00139
00140 int
00141 ACE_Name_Proxy::recv_reply (ACE_Name_Request &reply)
00142 {
00143 ACE_TRACE ("ACE_Name_Proxy::recv_reply");
00144
00145
00146
00147 ssize_t n = this->peer_.recv ((void *) &reply, sizeof (ACE_UINT32));
00148
00149 switch (n)
00150 {
00151 case -1:
00152
00153 ACE_DEBUG ((LM_DEBUG,
00154 ACE_LIB_TEXT ("****************** recv_reply returned -1\n")));
00155 default:
00156 ACE_ERROR ((LM_ERROR,
00157 ACE_LIB_TEXT ("%p got %d bytes, expected %d bytes\n"),
00158 ACE_LIB_TEXT ("recv failed"),
00159 n,
00160 sizeof (ACE_UINT32)));
00161
00162 case 0:
00163
00164 return -1;
00165
00166 case sizeof (ACE_UINT32):
00167 {
00168
00169 ssize_t length = ntohl (reply.length ());
00170
00171
00172
00173 n = this->peer_.recv ((void *) (((char *) &reply)
00174 + sizeof (ACE_UINT32)),
00175 length - sizeof (ACE_UINT32));
00176
00177
00178 if (n != ssize_t (length - sizeof (ACE_UINT32)))
00179 {
00180 ACE_ERROR ((LM_ERROR,
00181 ACE_LIB_TEXT ("%p expected %d, got %d\n"),
00182 ACE_LIB_TEXT ("invalid length"),
00183 length,
00184 n));
00185 return -1;
00186 }
00187
00188
00189 if (reply.decode () == -1)
00190 {
00191 ACE_ERROR ((LM_ERROR,
00192 ACE_LIB_TEXT ("%p\n"),
00193 ACE_LIB_TEXT ("decode failed")));
00194 return -1;
00195 }
00196 }
00197 }
00198 return 0;
00199 }
00200
00201
00202
00203 ACE_Name_Proxy::~ACE_Name_Proxy (void)
00204 {
00205 ACE_TRACE ("ACE_Name_Proxy::~ACE_Name_Proxy");
00206 this->peer_.close ();
00207 }
00208
00209 ACE_END_VERSIONED_NAMESPACE_DECL