1 #include "ace/Name_Request_Reply.h"
2 #include "ace/Basic_Types.h"
3 #include "ace/CDR_Base.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/Time_Value.h"
6 #include "ace/Truncate.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/os_include/arpa/os_inet.h"
12 "$Id: Name_Request_Reply.cpp 80826 2008-03-04 14:51:23Z wotte $")
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 // Default "do nothing" constructor.
18 ACE_Name_Request::ACE_Name_Request (void)
20 ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
23 // Create a ACE_Name_Request message.
25 ACE_Name_Request::ACE_Name_Request (
26 ACE_INT32 t
, // Type of request.
27 const ACE_WCHAR_T name
[], // Name
28 const ACE_UINT32 name_length
, // size in bytes
29 const ACE_WCHAR_T value
[], //
30 const ACE_UINT32 value_length
, // size in bytes
32 const ACE_UINT32 type_length
, // size in bytes
33 ACE_Time_Value
*timeout
) // Max time waiting for request.
35 ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
37 this->name_len (name_length
);
38 this->value_len (value_length
);
39 this->type_len (type_length
);
41 // If timeout is a NULL pointer, then block forever...
44 this->transfer_
.block_forever_
= 1;
45 this->transfer_
.sec_timeout_
= 0;
46 this->transfer_
.usec_timeout_
= 0;
48 else // Do a "timed wait."
50 this->block_forever (0);
51 // Keep track of how long client is willing to wait.
52 this->transfer_
.sec_timeout_
= timeout
->sec ();
53 this->transfer_
.usec_timeout_
= timeout
->usec ();
56 // Set up pointers and copy name value and type into request.
57 this->name_
= this->transfer_
.data_
;
58 this->value_
= &this->name_
[name_length
/ sizeof (ACE_WCHAR_T
) ];
59 this->type_
= (char *)(&this->value_
[value_length
/ sizeof (ACE_WCHAR_T
)]); //
61 (void) ACE_OS::memcpy (this->name_
,
64 (void) ACE_OS::memcpy (this->value_
,
67 (void) ACE_OS::memcpy (this->type_
,
71 // Compute size of the fixed portion of the message...
72 size_t len
= sizeof this->transfer_
- sizeof this->transfer_
.data_
;
74 // ... then add in the amount of the variable-sized portion.
75 len
+= name_length
+ value_length
+ type_length
;
77 this->length (static_cast<ACE_UINT32
> (len
));
80 // Initialize length_ in order to avoid problems with byte-ordering.
83 ACE_Name_Request::init (void)
85 ACE_TRACE ("ACE_Name_Request::init");
86 this->length (sizeof this->transfer_
);
89 // = Set/get the length of the encoded/decoded message.
92 ACE_Name_Request::length (void) const
94 ACE_TRACE ("ACE_Name_Request::length");
95 return this->transfer_
.length_
;
99 ACE_Name_Request::length (ACE_UINT32 l
)
101 ACE_TRACE ("ACE_Name_Request::length");
102 this->transfer_
.length_
= l
;
105 // = Set/get the type of the message.
108 ACE_Name_Request::msg_type (void) const
110 ACE_TRACE ("ACE_Name_Request::msg_type");
111 return this->transfer_
.msg_type_
;
115 ACE_Name_Request::msg_type (ACE_INT32 t
)
117 ACE_TRACE ("ACE_Name_Request::msg_type");
118 this->transfer_
.msg_type_
= t
;
121 // = Set/get the len of the name
124 ACE_Name_Request::name_len (void) const
126 ACE_TRACE ("ACE_Name_Request::name_len");
127 return this->transfer_
.name_len_
;
131 ACE_Name_Request::name_len (ACE_UINT32 t
)
133 ACE_TRACE ("ACE_Name_Request::name_len");
134 this->transfer_
.name_len_
= t
;
137 // = Set/get the len of the value
140 ACE_Name_Request::value_len (void) const
142 ACE_TRACE ("ACE_Name_Request::value_len");
143 return this->transfer_
.value_len_
;
147 ACE_Name_Request::value_len (ACE_UINT32 t
)
149 ACE_TRACE ("ACE_Name_Request::value_len");
150 this->transfer_
.value_len_
= t
;
153 // = Set/get the len of the type
156 ACE_Name_Request::type_len (void) const
158 ACE_TRACE ("ACE_Name_Request::type_len");
159 return this->transfer_
.type_len_
;
163 ACE_Name_Request::type_len (ACE_UINT32 t
)
165 ACE_TRACE ("ACE_Name_Request::type_len");
166 this->transfer_
.type_len_
= t
;
169 // = Set/get the blocking semantics.
172 ACE_Name_Request::block_forever (void) const
174 ACE_TRACE ("ACE_Name_Request::block_forever");
175 return this->transfer_
.block_forever_
;
179 ACE_Name_Request::block_forever (ACE_UINT32 bs
)
181 ACE_TRACE ("ACE_Name_Request::block_forever");
182 this->transfer_
.block_forever_
= bs
;
185 // = Set/get the timeout.
188 ACE_Name_Request::timeout (void) const
190 ACE_TRACE ("ACE_Name_Request::timeout");
191 time_t sec
= ACE_Utils::truncate_cast
<time_t> (this->transfer_
.sec_timeout_
);
192 return ACE_Time_Value (sec
, this->transfer_
.usec_timeout_
);
196 ACE_Name_Request::timeout (const ACE_Time_Value timeout
)
198 ACE_TRACE ("ACE_Name_Request::timeout");
199 this->transfer_
.sec_timeout_
= timeout
.sec ();
200 this->transfer_
.usec_timeout_
= timeout
.usec ();
203 // = Set/get the name
206 ACE_Name_Request::name (void) const
208 ACE_TRACE ("ACE_Name_Request::name");
213 ACE_Name_Request::name (const ACE_WCHAR_T
*t
)
215 ACE_TRACE ("ACE_Name_Request::name");
216 (void) ACE_OS::memcpy (this->name_
,
221 // = Set/get the value
224 ACE_Name_Request::value (void) const
226 ACE_TRACE ("ACE_Name_Request::value");
231 ACE_Name_Request::value (const ACE_WCHAR_T
*c
)
233 ACE_TRACE ("ACE_Name_Request::value");
235 (void) ACE_OS::memcpy (this->value_
,
240 // = Set/get the type
243 ACE_Name_Request::type (void) const
245 ACE_TRACE ("ACE_Name_Request::type");
250 ACE_Name_Request::type (const char *c
)
252 ACE_TRACE ("ACE_Name_Request::type");
253 ACE_OS::strsncpy (this->type_
,
258 // Encode the transfer buffer into network byte order so that it can
259 // be sent to the server.
262 ACE_Name_Request::encode (void *&buf
)
264 ACE_TRACE ("ACE_Name_Request::encode");
265 // Compute the length *before* doing the marshaling.
267 ACE_UINT32 len
= this->length ();
270 (this->transfer_
.name_len_
+ this->transfer_
.value_len_
)
271 / sizeof (ACE_WCHAR_T
);
273 for (size_t i
= 0; i
< nv_data_len
; i
++)
274 this->transfer_
.data_
[i
] =
275 ACE_HTONS (this->transfer_
.data_
[i
]);
277 buf
= (void *) &this->transfer_
;
278 this->transfer_
.block_forever_
= ACE_HTONL (this->transfer_
.block_forever_
);
279 this->transfer_
.usec_timeout_
= ACE_HTONL (this->transfer_
.usec_timeout_
);
280 #if defined (ACE_LITTLE_ENDIAN)
281 ACE_UINT64 secs
= this->transfer_
.sec_timeout_
;
282 ACE_CDR::swap_8 ((const char *)&secs
, (char *)&this->transfer_
.sec_timeout_
);
284 this->transfer_
.length_
= ACE_HTONL (this->transfer_
.length_
);
285 this->transfer_
.msg_type_
= ACE_HTONL (this->transfer_
.msg_type_
);
286 this->transfer_
.name_len_
= ACE_HTONL (this->transfer_
.name_len_
);
287 this->transfer_
.value_len_
= ACE_HTONL (this->transfer_
.value_len_
);
288 this->transfer_
.type_len_
= ACE_HTONL (this->transfer_
.type_len_
);
293 // Decode the transfer buffer into host byte byte order so that it can
294 // be used by the server.
297 ACE_Name_Request::decode (void)
299 ACE_TRACE ("ACE_Name_Request::decode");
300 // Decode the fixed-sized portion first.
301 this->transfer_
.block_forever_
= ACE_NTOHL (this->transfer_
.block_forever_
);
302 this->transfer_
.usec_timeout_
= ACE_NTOHL (this->transfer_
.usec_timeout_
);
303 #if defined (ACE_LITTLE_ENDIAN)
304 ACE_UINT64 secs
= this->transfer_
.sec_timeout_
;
305 ACE_CDR::swap_8 ((const char *)&secs
, (char *)&this->transfer_
.sec_timeout_
);
307 this->transfer_
.length_
= ACE_NTOHL (this->transfer_
.length_
);
308 this->transfer_
.msg_type_
= ACE_NTOHL (this->transfer_
.msg_type_
);
309 this->transfer_
.name_len_
= ACE_NTOHL (this->transfer_
.name_len_
);
310 this->transfer_
.value_len_
= ACE_NTOHL (this->transfer_
.value_len_
);
311 this->transfer_
.type_len_
= ACE_NTOHL (this->transfer_
.type_len_
);
314 (this->transfer_
.name_len_
+ this->transfer_
.value_len_
)
315 / sizeof (ACE_WCHAR_T
);
317 for (size_t i
= 0; i
< nv_data_len
; i
++)
318 this->transfer_
.data_
[i
] =
319 ACE_NTOHS (this->transfer_
.data_
[i
]);
321 this->name_
= this->transfer_
.data_
;
322 this->value_
= &this->name_
[this->transfer_
.name_len_
/ sizeof (ACE_WCHAR_T
)];
323 this->type_
= (char *)(&this->value_
[this->transfer_
.value_len_
/ sizeof (ACE_WCHAR_T
)]);
324 this->type_
[this->transfer_
.type_len_
] = '\0';
326 // Decode the variable-sized portion.
330 // Print out the current values of the ACE_Name_Request.
333 ACE_Name_Request::dump (void) const
335 #if defined (ACE_HAS_DUMP)
336 ACE_TRACE ("ACE_Name_Request::dump");
337 ACE_DEBUG ((LM_DEBUG
,
338 ACE_TEXT ("*******\nlength = %d\n"),
340 ACE_DEBUG ((LM_DEBUG
,
341 ACE_TEXT ("message-type = ")));
343 switch (this->msg_type ())
345 case ACE_Name_Request::BIND
:
346 ACE_DEBUG ((LM_DEBUG
,
347 ACE_TEXT ("BIND\n")));
349 case ACE_Name_Request::REBIND
:
350 ACE_DEBUG ((LM_DEBUG
,
351 ACE_TEXT ("REBIND\n")));
353 case ACE_Name_Request::RESOLVE
:
354 ACE_DEBUG ((LM_DEBUG
,
355 ACE_TEXT ("RESOLVE\n")));
357 case ACE_Name_Request::UNBIND
:
358 ACE_DEBUG ((LM_DEBUG
,
359 ACE_TEXT ("UNBIND\n")));
361 case ACE_Name_Request::LIST_NAMES
:
362 ACE_DEBUG ((LM_DEBUG
,
363 ACE_TEXT ("LIST_NAMES\n")));
365 case ACE_Name_Request::LIST_VALUES
:
366 ACE_DEBUG ((LM_DEBUG
,
367 ACE_TEXT ("LIST_VALUES\n")));
369 case ACE_Name_Request::LIST_TYPES
:
370 ACE_DEBUG ((LM_DEBUG
,
371 ACE_TEXT ("LIST_TYPES\n")));
373 case ACE_Name_Request::LIST_NAME_ENTRIES
:
374 ACE_DEBUG ((LM_DEBUG
,
375 ACE_TEXT ("LIST_NAME_ENTRIES\n")));
377 case ACE_Name_Request::LIST_VALUE_ENTRIES
:
378 ACE_DEBUG ((LM_DEBUG
,
379 ACE_TEXT ("LIST_VALUE_ENTRIES\n")));
381 case ACE_Name_Request::LIST_TYPE_ENTRIES
:
382 ACE_DEBUG ((LM_DEBUG
,
383 ACE_TEXT ("LIST_TYPE_ENTRIES\n")));
386 ACE_DEBUG ((LM_DEBUG
,
387 ACE_TEXT ("<unknown type> = %d\n"),
392 if (this->block_forever ())
393 ACE_DEBUG ((LM_DEBUG
,
394 ACE_TEXT ("blocking forever\n")));
397 #if !defined (ACE_NLOGGING)
398 ACE_Time_Value tv
= this->timeout ();
399 #endif /* ! ACE_NLOGGING */
400 ACE_DEBUG ((LM_DEBUG
,
401 ACE_TEXT ("waiting for %d secs and %d usecs\n"),
405 ACE_DEBUG ((LM_DEBUG
,
406 ACE_TEXT ("*******\nname_len = %d\n"),
408 ACE_DEBUG ((LM_DEBUG
,
409 ACE_TEXT ("*******\nvalue_len = %d\n"),
410 this->value_len ()));
412 ACE_DEBUG ((LM_DEBUG
,
413 ACE_TEXT ("+++++++\n")));
414 #endif /* ACE_HAS_DUMP */
417 // Default constructor.
419 ACE_Name_Reply::ACE_Name_Reply (void)
421 ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply");
423 // Initialize to a known quantity.
426 this->length (sizeof this->transfer_
);
429 // Create a ACE_Name_Reply message.
431 ACE_Name_Reply::ACE_Name_Reply (ACE_UINT32 t
, ACE_UINT32 err
) // Type of reply.
433 ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply");
436 this->length (sizeof this->transfer_
);
439 // Initialize length_ to avoid problems with byte-ordering.
442 ACE_Name_Reply::init (void)
444 ACE_TRACE ("ACE_Name_Reply::init");
445 this->length (sizeof this->transfer_
);
448 // = Set/get the length of the encoded/decoded message.
451 ACE_Name_Reply::length (void) const
453 ACE_TRACE ("ACE_Name_Reply::length");
454 return this->transfer_
.length_
;
458 ACE_Name_Reply::length (ACE_UINT32 l
)
460 ACE_TRACE ("ACE_Name_Reply::length");
461 this->transfer_
.length_
= l
;
464 // = Set/get the type of the message.
467 ACE_Name_Reply::msg_type (void) const
469 ACE_TRACE ("ACE_Name_Reply::msg_type");
470 return this->transfer_
.type_
;
474 ACE_Name_Reply::msg_type (ACE_INT32 t
)
476 ACE_TRACE ("ACE_Name_Reply::msg_type");
477 this->transfer_
.type_
= t
;
480 // Get the status of the reply (0 == success, -1 == failure).
483 ACE_Name_Reply::status (void) const
485 ACE_TRACE ("ACE_Name_Reply::status");
486 return this->transfer_
.type_
;
489 // Set the status of the reply (0 == success, -1 == failure).
492 ACE_Name_Reply::status (ACE_INT32 s
)
494 ACE_TRACE ("ACE_Name_Reply::status");
496 this->transfer_
.type_
= -1;
498 this->transfer_
.type_
= 0;
501 // = Set/get the errno of a failed reply.
503 ACE_Name_Reply::errnum (void) const
505 ACE_TRACE ("ACE_Name_Reply::errnum");
506 return this->transfer_
.errno_
;
510 ACE_Name_Reply::errnum (ACE_UINT32 e
)
512 ACE_TRACE ("ACE_Name_Reply::errnum");
513 this->transfer_
.errno_
= e
;
516 // Encode the transfer buffer into network byte order
517 // so that it can be sent to the client.
520 ACE_Name_Reply::encode (void *&buf
)
522 ACE_TRACE ("ACE_Name_Reply::encode");
523 int len
= this->length (); // Get length *before* marshaling.
525 this->transfer_
.length_
= ACE_HTONL (this->transfer_
.length_
);
526 this->transfer_
.type_
= ACE_HTONL (this->transfer_
.type_
);
527 this->transfer_
.errno_
= ACE_HTONL (this->transfer_
.errno_
);
528 buf
= (void *) &this->transfer_
;
532 // Decode the transfer buffer into host byte order so that it can be
533 // used by the client.
536 ACE_Name_Reply::decode (void)
538 ACE_TRACE ("ACE_Name_Reply::decode");
539 this->transfer_
.length_
= ACE_NTOHL (this->transfer_
.length_
);
540 this->transfer_
.type_
= ACE_NTOHL (this->transfer_
.type_
);
541 this->transfer_
.errno_
= ACE_NTOHL (this->transfer_
.errno_
);
545 // Print out current values of the ACE_Name_Reply object.
548 ACE_Name_Reply::dump (void) const
550 #if defined (ACE_HAS_DUMP)
551 ACE_TRACE ("ACE_Name_Reply::dump");
552 ACE_DEBUG ((LM_DEBUG
,
553 ACE_TEXT ("*******\nlength = %d\nerrnum = %d"),
556 ACE_DEBUG ((LM_DEBUG
,
557 ACE_TEXT ("type = ")));
558 switch (this->msg_type ())
561 ACE_DEBUG ((LM_DEBUG
,
562 ACE_TEXT ("SUCCESS\n")));
565 ACE_DEBUG ((LM_DEBUG
,
566 ACE_TEXT ("FAILURE\n")));
569 ACE_DEBUG ((LM_DEBUG
,
570 ACE_TEXT ("<unknown type> = %d\n"),
574 #endif /* ACE_HAS_DUMP */
577 ACE_END_VERSIONED_NAMESPACE_DECL