summaryrefslogtreecommitdiff
path: root/ace/Name_Request_Reply.cpp
blob: 84de11d560ac5448e185e01cdebb379d4795e0cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Name_Request_Reply.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Name_Request_Reply.h"

ACE_RCSID(ace, Name_Request_Reply, "$Id$")

// Default "do nothing" constructor.

ACE_Name_Request::ACE_Name_Request (void)
{
  ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
}

// Create a ACE_Name_Request message.  

ACE_Name_Request::ACE_Name_Request (ACE_UINT32 t, // Type of request.
				    const ACE_USHORT16 name[], // Name
				    const size_t name_length, // size in bytes
				    const ACE_USHORT16 value[], // 
				    const size_t value_length, // size in bytes
				    const char type[], // 
				    const size_t type_length, // size in bytes
				    ACE_Time_Value *timeout) // Max time waiting for request.
{
  ACE_TRACE ("ACE_Name_Request::ACE_Name_Request");
  this->msg_type (t);
  this->name_len (name_length);
  this->value_len (value_length);
  this->type_len (type_length);

  // If timeout is a NULL pointer, then block forever...
  if (timeout == 0)
    {
      this->transfer_.block_forever_ = 1;
      this->transfer_.sec_timeout_   = 0;
      this->transfer_.usec_timeout_  = 0;
    }
  else // Do a "timed wait."
    {
      this->block_forever (0);
      // Keep track of how long client is willing to wait.
      this->transfer_.sec_timeout_ = timeout->sec ();
      this->transfer_.usec_timeout_ = timeout->usec ();
    }

  // Set up pointers and copy name value and type into request.
  this->name_ = this->transfer_.data_;
  this->value_  = &this->name_[name_length / sizeof (ACE_USHORT16) ];
  this->type_  = (char *)(&this->value_[value_length / sizeof (ACE_USHORT16)]); //

  (void) ACE_OS::memcpy (this->name_, name, name_length);
  (void) ACE_OS::memcpy (this->value_, value, value_length);
  (void) ACE_OS::memcpy (this->type_, type, type_length);

  // Compute size of the fixed portion of the message...
  size_t len = sizeof this->transfer_ - sizeof this->transfer_.data_;
  
  // ... then add in the amount of the variable-sized portion. 
  len += name_length + value_length + type_length ;

  this->length (len);
}

// Initialize length_ in order to avoid problems with byte-ordering
void
ACE_Name_Request::init (void)
{
  ACE_TRACE ("ACE_Name_Request::init");
  this->length (sizeof this->transfer_);
}

// = Set/get the length of the encoded/decoded message.
ACE_UINT32 
ACE_Name_Request::length (void) const
{
  ACE_TRACE ("ACE_Name_Request::length");
  return this->transfer_.length_;
}

void 
ACE_Name_Request::length (ACE_UINT32 l)
{
  ACE_TRACE ("ACE_Name_Request::length");
  this->transfer_.length_ = l;
}

// = Set/get the type of the message.
ACE_UINT32 
ACE_Name_Request::msg_type (void) const
{
  ACE_TRACE ("ACE_Name_Request::msg_type");
  return this->transfer_.msg_type_;
}

void 
ACE_Name_Request::msg_type (ACE_UINT32 t)
{
  ACE_TRACE ("ACE_Name_Request::msg_type");
  this->transfer_.msg_type_ = t;
}

// = Set/get the len of the name
ACE_UINT32 
ACE_Name_Request::name_len (void) const
{
  ACE_TRACE ("ACE_Name_Request::name_len");
  return this->transfer_.name_len_;
}

void 
ACE_Name_Request::name_len (ACE_UINT32 t)
{
  ACE_TRACE ("ACE_Name_Request::name_len");
  this->transfer_.name_len_ = t;
}

// = Set/get the len of the value
ACE_UINT32 
ACE_Name_Request::value_len (void) const
{
  ACE_TRACE ("ACE_Name_Request::value_len");
  return this->transfer_.value_len_;
}

void 
ACE_Name_Request::value_len (ACE_UINT32 t)
{
  ACE_TRACE ("ACE_Name_Request::value_len");
  this->transfer_.value_len_ = t;
}

// = Set/get the len of the type
ACE_UINT32 
ACE_Name_Request::type_len (void) const
{
  ACE_TRACE ("ACE_Name_Request::type_len");
  return this->transfer_.type_len_;
}

void 
ACE_Name_Request::type_len (ACE_UINT32 t)
{
  ACE_TRACE ("ACE_Name_Request::type_len");
  this->transfer_.type_len_ = t;
}

// = Set/get the blocking semantics.
ACE_UINT32
ACE_Name_Request::block_forever (void) const
{
  ACE_TRACE ("ACE_Name_Request::block_forever");
  return this->transfer_.block_forever_;
}

void 
ACE_Name_Request::block_forever (ACE_UINT32 bs)
{
  ACE_TRACE ("ACE_Name_Request::block_forever");
  this->transfer_.block_forever_ = bs;
}

// = Set/get the timeout.
ACE_Time_Value
ACE_Name_Request::timeout (void) const
{
  ACE_TRACE ("ACE_Name_Request::timeout");
  return ACE_Time_Value (this->transfer_.sec_timeout_, this->transfer_.usec_timeout_);
}

void 
ACE_Name_Request::timeout (const ACE_Time_Value timeout)
{
  ACE_TRACE ("ACE_Name_Request::timeout");
  this->transfer_.sec_timeout_  = timeout.sec ();
  this->transfer_.usec_timeout_ = timeout.usec ();
}

// = Set/get the name
const ACE_USHORT16 *
ACE_Name_Request::name (void) const
{
  ACE_TRACE ("ACE_Name_Request::name");
  return this->name_;
}

void 
ACE_Name_Request::name (const ACE_USHORT16 *t)
{
  ACE_TRACE ("ACE_Name_Request::name");
  (void) ACE_OS::memcpy (this->name_, t, this->name_len());
}

// = Set/get the value
const ACE_USHORT16 *
ACE_Name_Request::value (void) const
{
  ACE_TRACE ("ACE_Name_Request::value");
  return this->value_;
}

void 
ACE_Name_Request::value (const ACE_USHORT16 *c)
{
  ACE_TRACE ("ACE_Name_Request::value");
  (void) ACE_OS::memcpy (this->value_, c, this->value_len());
}

// = Set/get the type
const char *
ACE_Name_Request::type (void) const
{
  ACE_TRACE ("ACE_Name_Request::type");
  return this->type_;
}

void 
ACE_Name_Request::type (const char *c)
{
  ACE_TRACE ("ACE_Name_Request::type");
  (void) ::strncpy (this->type_, c, MAXPATHLEN + 1);
}

// Encode the transfer buffer into network byte order 
// so that it can be sent to the server.

int
ACE_Name_Request::encode (void *&buf)
{
  ACE_TRACE ("ACE_Name_Request::encode");
  // Compute the length *before* doing the marshaling.
  ssize_t len = this->length ();
  size_t nv_data_len =
    (this->transfer_.name_len_ + this->transfer_.value_len_) / sizeof (ACE_USHORT16);

  for (size_t i = 0; i < nv_data_len; i++)
    this->transfer_.data_[i] = htons (this->transfer_.data_[i]);

  buf = (void *) &this->transfer_;
  this->transfer_.block_forever_ = htonl (this->transfer_.block_forever_);
  this->transfer_.usec_timeout_	 = htonl (this->transfer_.usec_timeout_);
  this->transfer_.sec_timeout_	 = htonl (this->transfer_.sec_timeout_);
  this->transfer_.length_	 = htonl (this->transfer_.length_);
  this->transfer_.msg_type_	 = htonl (this->transfer_.msg_type_);
  this->transfer_.name_len_	 = htonl (this->transfer_.name_len_);
  this->transfer_.value_len_	 = htonl (this->transfer_.value_len_);
  this->transfer_.type_len_	 = htonl (this->transfer_.type_len_);

  return len;
}

// Decode the transfer buffer into host byte byte order 
// so that it can be used by the server.

int
ACE_Name_Request::decode (void)
{
  ACE_TRACE ("ACE_Name_Request::decode");
  // Decode the fixed-sized portion first.
  this->transfer_.block_forever_ = ntohl (this->transfer_.block_forever_);
  this->transfer_.usec_timeout_	 = ntohl (this->transfer_.usec_timeout_);
  this->transfer_.sec_timeout_	 = ntohl (this->transfer_.sec_timeout_);
  this->transfer_.length_	 = ntohl (this->transfer_.length_);
  this->transfer_.msg_type_	 = ntohl (this->transfer_.msg_type_);
  this->transfer_.name_len_	 = ntohl (this->transfer_.name_len_);
  this->transfer_.value_len_	 = ntohl (this->transfer_.value_len_);
  this->transfer_.type_len_	 = ntohl (this->transfer_.type_len_);

  size_t nv_data_len =
    (this->transfer_.name_len_ + this->transfer_.value_len_) / sizeof (ACE_USHORT16);

  for (size_t i = 0; i < nv_data_len; i++)
    this->transfer_.data_[i] = ntohs (this->transfer_.data_[i]);

  this->name_ = this->transfer_.data_;
  this->value_  = &this->name_[this->transfer_.name_len_ / sizeof (ACE_USHORT16)];
  this->type_  = (char *)(&this->value_[this->transfer_.value_len_ / sizeof (ACE_USHORT16)]);
  this->type_[this->transfer_.type_len_] = '\0';

  // Decode the variable-sized portion.
  return 0;
}

// Print out the current values of the ACE_Name_Request.

void
ACE_Name_Request::dump (void) const
{
  ACE_TRACE ("ACE_Name_Request::dump");
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("*******\nlength = %d\n"), 
	      this->length ()));
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("message-type = ")));

  switch (this->msg_type ())
    {
    case ACE_Name_Request::BIND:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("BIND\n")));
      break;
    case ACE_Name_Request::REBIND:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("REBIND\n")));
      break;
    case ACE_Name_Request::RESOLVE:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("RESOLVE\n")));
      break;
    case ACE_Name_Request::UNBIND:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("UNBIND\n")));
      break;
    case ACE_Name_Request::LIST_NAMES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_NAMES\n")));
      break;
    case ACE_Name_Request::LIST_VALUES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_VALUES\n")));
      break;
    case ACE_Name_Request::LIST_TYPES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_TYPES\n")));
      break;
    case ACE_Name_Request::LIST_NAME_ENTRIES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_NAME_ENTRIES\n")));
      break;
    case ACE_Name_Request::LIST_VALUE_ENTRIES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_VALUE_ENTRIES\n")));
      break;
    case ACE_Name_Request::LIST_TYPE_ENTRIES:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("LIST_TYPE_ENTRIES\n")));
      break;
    default:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("<unknown type> = %d\n"), this->msg_type ()));
      break;
    }

  if (this->block_forever ())
    ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("blocking forever\n")));
  else
    {
      ACE_Time_Value tv = this->timeout ();
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("waiting for %ld secs and %ld usecs\n"), 
		  tv.sec (), tv.usec ()));
    }
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("*******\nname_len = %d\n"), 
	      this->name_len ()));
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("*******\nvalue_len = %d\n"), 
	      this->value_len ()));
#if 0
  cout << "Name = " << ACE_WString (this->name(), this->name_len() / sizeof (ACE_USHORT16)) << endl;
  cout << "value = " << ACE_WString (this->value(), this->value_len() / sizeof (ACE_USHORT16)) << endl;
  cout << "type = "  << this->type () << endl;
#endif
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("+++++++\n")));
}

// Default constructor.

ACE_Name_Reply::ACE_Name_Reply (void)
{
  ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply");
  // Initialize to a known quantity.
  this->msg_type (0);
  this->errnum (0);
  this->length (sizeof this->transfer_);
}

// Create a ACE_Name_Reply message.  

ACE_Name_Reply::ACE_Name_Reply (ACE_UINT32 t, ACE_UINT32 err) // Type of reply.
{
  ACE_TRACE ("ACE_Name_Reply::ACE_Name_Reply"); 
  this->msg_type (t);
  this->errnum (err);
  this->length (sizeof this->transfer_);
}

// Initialize length_ in order to avoid problems with byte-ordering
void
ACE_Name_Reply::init (void)
{
  ACE_TRACE ("ACE_Name_Reply::init");
  this->length (sizeof this->transfer_);
}

// = Set/get the length of the encoded/decoded message.
ACE_UINT32 
ACE_Name_Reply::length (void) const
{
  ACE_TRACE ("ACE_Name_Reply::length");
  return this->transfer_.length_;
}

void 
ACE_Name_Reply::length (ACE_UINT32 l)
{
  ACE_TRACE ("ACE_Name_Reply::length");
  this->transfer_.length_ = l;
}

// = Set/get the type of the message.
ACE_UINT32 
ACE_Name_Reply::msg_type (void) const
{
  ACE_TRACE ("ACE_Name_Reply::msg_type");
  return this->transfer_.type_;
}

void 
ACE_Name_Reply::msg_type (ACE_UINT32 t)
{
  ACE_TRACE ("ACE_Name_Reply::msg_type");
  this->transfer_.type_ = t;
}

// = Set/get the status of the reply (0 == success, -1 == failure).
ACE_UINT32 
ACE_Name_Reply::status (void) const
{
  ACE_TRACE ("ACE_Name_Reply::status");
  return this->transfer_.type_ == ACE_Name_Reply::SUCCESS ? 0 : -1;
}

void 
ACE_Name_Reply::status (ACE_UINT32 s)
{
  ACE_TRACE ("ACE_Name_Reply::status");
  this->transfer_.type_ = s == (ACE_UINT32) -1 ? ACE_Name_Reply::FAILURE : ACE_Name_Reply::SUCCESS;
}

// = Set/get the errno of a failed reply.
ACE_UINT32 
ACE_Name_Reply::errnum (void) const
{
  ACE_TRACE ("ACE_Name_Reply::errnum");
  return this->transfer_.errno_;
}

void 
ACE_Name_Reply::errnum (ACE_UINT32 e)
{
  ACE_TRACE ("ACE_Name_Reply::errnum");
  this->transfer_.errno_ = e;
}

// Encode the transfer buffer into network byte order 
// so that it can be sent to the client.

int
ACE_Name_Reply::encode (void *&buf)
{
  ACE_TRACE ("ACE_Name_Reply::encode");
  int len = this->length (); // Get length *before* marshaling.

  this->transfer_.length_ = htonl (this->transfer_.length_);
  this->transfer_.type_	  = htonl (this->transfer_.type_);
  this->transfer_.errno_  = htonl (this->transfer_.errno_);
  buf = (void *) &this->transfer_;
  return len;
}

// Decode the transfer buffer into host byte order 
// so that it can be used by the client.

int
ACE_Name_Reply::decode (void)
{
  ACE_TRACE ("ACE_Name_Reply::decode");
  this->transfer_.length_ = ntohl (this->transfer_.length_);
  this->transfer_.type_	  = ntohl (this->transfer_.type_);
  this->transfer_.errno_  = ntohl (this->transfer_.errno_);
  return 0;
}

// Print out current values of the ACE_Name_Reply object.

void
ACE_Name_Reply::dump (void) const
{
  ACE_TRACE ("ACE_Name_Reply::dump");
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("*******\nlength = %d\nerrnum = %d"), 
	      this->length (), this->errnum ()));
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("type = ")));

  switch (this->msg_type ())
    {
    case ACE_Name_Reply::SUCCESS:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("SUCCESS\n")));
      break;
    case ACE_Name_Reply::FAILURE:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("FAILURE\n")));
      break;
    default:
      ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("<unknown type> = %d\n"), this->msg_type ()));
      break;
    }
}