Fixed typos
[ACE_TAO.git] / ACE / ace / Name_Proxy.cpp
blobfc99cb6aa0c82bfb30e201069a9f91d431277c4e
1 #include "ace/Name_Proxy.h"
2 #include "ace/Log_Category.h"
3 #include "ace/os_include/arpa/os_inet.h"
7 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
9 void
10 ACE_Name_Proxy::dump (void) const
12 #if defined (ACE_HAS_DUMP)
13 ACE_TRACE ("ACE_Name_Proxy::dump");
15 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
16 this->connector_.dump ();
17 this->peer_.dump ();
18 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("reactor_ = %x"), this->reactor_));
19 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
20 #endif /* ACE_HAS_DUMP */
23 // Default constructor.
25 ACE_Name_Proxy::ACE_Name_Proxy (void)
26 : reactor_ (0)
28 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
31 // Establish binding with the ACE_Name Server at remote_addr.
33 int
34 ACE_Name_Proxy::open (const ACE_INET_Addr &remote_addr,
35 ACE_Synch_Options& options)
37 ACE_TRACE ("ACE_Name_Proxy::open");
38 ACE_Time_Value *timeout = 0;
40 if (options[ACE_Synch_Options::USE_TIMEOUT])
41 timeout = const_cast<ACE_Time_Value *> (options.time_value ());
43 // Initiate the connection.
44 return this->connector_.connect (this->peer_,
45 remote_addr,
46 timeout);
49 // Establish binding with the ACE_Name Server at remote_addr.
51 ACE_Name_Proxy::ACE_Name_Proxy (
52 const ACE_INET_Addr &remote_addr,
53 ACE_Synch_Options& options)
54 : reactor_ (0)
56 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
57 if (this->open (remote_addr, options) == -1
58 && options[ACE_Synch_Options::USE_TIMEOUT] && errno != EWOULDBLOCK)
59 ACELIB_ERROR ((LM_ERROR,
60 ACE_TEXT ("%p\n"),
61 ACE_TEXT ("ACE_Name_Proxy::ACE_Name_Proxy")));
64 // Obtain underlying handle.
66 /* VIRTUAL */ ACE_HANDLE
67 ACE_Name_Proxy::get_handle (void) const
69 ACE_TRACE ("ACE_Name_Proxy::get_handle");
70 return this->peer_.get_handle ();
73 int
74 ACE_Name_Proxy::request_reply (ACE_Name_Request &request)
76 ACE_TRACE ("ACE_Name_Proxy::request_reply");
77 void *buffer;
78 ssize_t length = request.encode (buffer);
80 if (length == -1)
81 ACELIB_ERROR_RETURN ((LM_ERROR,
82 ACE_TEXT ("%p\n"),
83 ACE_TEXT ("encode failed")),
84 -1);
86 // Transmit request via a blocking send.
88 if (this->peer_.send_n (buffer, length) != length)
89 ACELIB_ERROR_RETURN ((LM_ERROR,
90 ACE_TEXT ("%p\n"),
91 ACE_TEXT ("send_n failed")),
92 -1);
93 else
95 ACE_Name_Reply reply;
97 // Receive reply via blocking read.
99 if (this->peer_.recv_n (&reply,
100 sizeof reply) == -1)
101 ACELIB_ERROR_RETURN ((LM_ERROR,
102 ACE_TEXT ("%p\n"),
103 ACE_TEXT ("recv failed")),
104 -1);
105 else if (reply.decode () == -1)
106 ACELIB_ERROR_RETURN ((LM_ERROR,
107 ACE_TEXT ("%p\n"),
108 ACE_TEXT ("decode failed")),
109 -1);
110 errno = int (reply.errnum ());
111 return reply.status ();
116 ACE_Name_Proxy::send_request (ACE_Name_Request &request)
118 ACE_TRACE ("ACE_Name_Proxy::send_request");
119 void *buffer;
120 ssize_t length = request.encode (buffer);
122 if (length == -1)
123 ACELIB_ERROR_RETURN ((LM_ERROR,
124 ACE_TEXT ("%p\n"),
125 ACE_TEXT ("encode failed")),
126 -1);
128 // Transmit request via a blocking send.
130 else if (this->peer_.send_n (buffer, length) != length)
131 ACELIB_ERROR_RETURN ((LM_ERROR,
132 ACE_TEXT ("%p\n"),
133 ACE_TEXT ("send_n failed")),
134 -1);
135 return 0;
139 ACE_Name_Proxy::recv_reply (ACE_Name_Request &reply)
141 ACE_TRACE ("ACE_Name_Proxy::recv_reply");
142 // Read the first 4 bytes to get the length of the message This
143 // implementation assumes that the first 4 bytes are the length of
144 // the message.
145 ssize_t n = this->peer_.recv ((void *) &reply, sizeof (ACE_UINT32));
147 switch (n)
149 case -1:
150 ACELIB_DEBUG ((LM_DEBUG,
151 ACE_TEXT ("****************** recv_reply returned -1\n")));
152 // FALLTHROUGH
153 default:
154 ACELIB_ERROR ((LM_ERROR,
155 ACE_TEXT ("%p got %d bytes, expected %d bytes\n"),
156 ACE_TEXT ("recv failed"),
158 sizeof (ACE_UINT32)));
159 // FALLTHROUGH
160 case 0:
161 // We've shutdown unexpectedly
162 return -1;
163 // NOTREACHED
164 case sizeof (ACE_UINT32):
166 // Transform the length into host byte order.
167 ssize_t length = ACE_NTOHL (reply.length ());
169 // Receive the rest of the request message.
170 // @@ beware of blocking read!!!.
171 n = this->peer_.recv ((void *) (((char *) &reply)
172 + sizeof (ACE_UINT32)),
173 length - sizeof (ACE_UINT32));
175 // Subtract off the size of the part we skipped over...
176 if (n != ssize_t (length - sizeof (ACE_UINT32)))
178 ACELIB_ERROR ((LM_ERROR,
179 ACE_TEXT ("%p expected %d, got %d\n"),
180 ACE_TEXT ("invalid length"),
181 length,
182 n));
183 return -1;
186 // Decode the request into host byte order.
187 if (reply.decode () == -1)
189 ACELIB_ERROR ((LM_ERROR,
190 ACE_TEXT ("%p\n"),
191 ACE_TEXT ("decode failed")));
192 return -1;
196 return 0;
199 // Close down the connection to the server.
201 ACE_Name_Proxy::~ACE_Name_Proxy (void)
203 ACE_TRACE ("ACE_Name_Proxy::~ACE_Name_Proxy");
204 this->peer_.close ();
207 ACE_END_VERSIONED_NAMESPACE_DECL