summaryrefslogtreecommitdiff
path: root/apps/JAWS/clients/Caching/Locator_Request_Reply.cpp
blob: 6f7a7898e59621e0842f2b381a73505f8aa1b252 (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
// $Id$

#if !defined (ACE_LOCATOR_REQUEST_REPLY_C)
#define ACE_LOCATOR_REQUEST_REPLY_C

#include "Locator_Request_Reply.h"

#if !defined (__ACE_INLINE__)
#include "Locator_Request_Reply.i"
#endif

#include "ace/Auto_Ptr.h"
#include "URL_Properties.h"
#include "URL_Array_Helper.h"
#include "URL_Locator.h"

ACE_RCSID(Caching, Locator_Request_Reply, "$Id$")

int
ACE_URL_Locator_Request::url_query (const int how,
				    const ACE_URL_Property_Seq &pseq,
				    const int how_many)
{
  ACE_TRACE ("ACE_URL_Locator_Request::url_query");

  if (how >= ACE_URL_Locator::INVALID_SELECTION)
    return -1;
  ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (pseq), -1);
  this->how_ = how;
  this->how_many_ = how_many;
  this->code_ = ACE_URL_Locator::QUERY;
  return 0;    
}

int
ACE_URL_Locator_Request::export_offer (const ACE_URL_Offer &offer)
{
  ACE_TRACE ("ACE_URL_Locator_Request::export_offer");
  
  ACE_NEW_RETURN (this->offer_, ACE_URL_Offer (offer), -1);
  this->code_ = ACE_URL_Locator::EXPORT;
  return 0;
}
  
int
ACE_URL_Locator_Request::withdraw_offer (const ACE_WString &offer_id)
{
  ACE_TRACE ("ACE_URL_Locator_Request::withdraw_offer");

  this->id_ = offer_id;
  this->code_ = ACE_URL_Locator::WITHDRAW;
  return 0;
}

int
ACE_URL_Locator_Request::describe_offer (const ACE_WString &offer_id)
{
  ACE_TRACE ("ACE_URL_Locator_Request::describe_offer");
  
  this->id_ = offer_id;
  this->code_ = ACE_URL_Locator::DESCRIBE;
  return 0;
}

int
ACE_URL_Locator_Request::modify_offer (const ACE_WString &offer_id,
				       const ACE_WString *url,
				       const ACE_URL_Property_Seq &del,
				       const ACE_URL_Property_Seq &modify)
{
  ACE_TRACE ("ACE_URL_Locator_Request::modify_offer");

  ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (del), -1);
  ACE_NEW_RETURN (this->seq2_, ACE_URL_Property_Seq (modify), -1);

  if (url != 0)
    this->url_ = *url;

  this->id_ = offer_id;
  this->code_ = ACE_URL_Locator::MODIFY;
  return 0;
}

#define ENCODE_UINT32(ADDR,LEN,V) \
   * (ACE_UINT32 *) (ADDR+LEN) = htonl (V); \
   LEN += sizeof (ACE_UINT32);

#define DECODE_UINT32(ADDR,LEN,V) \
   V = ntohl (* (ACE_UINT32 *) (ADDR+LEN)); \
   LEN += sizeof (ACE_UINT32);

size_t
ACE_URL_Locator_Request::encode (void)
{
  ACE_TRACE ("ACE_URL_Locator_Request::encode");

  size_t buf_size = this->size ();
  size_t total_length = 0;

  ACE_NEW_RETURN (this->buffer_, char [buf_size], 0);

  ENCODE_UINT32 (this->buffer_, total_length, buf_size);
  // Encode buffer size.

  ENCODE_UINT32 (this->buffer_, total_length, this->code_);
  // Encode Op code.

  ENCODE_UINT32 (this->buffer_, total_length, this->how_);
  // Encode selection criteria.
  
  ENCODE_UINT32 (this->buffer_, total_length, this->how_many_);
  // Encode number of offers interested.

  ENCODE_UINT32 (this->buffer_, total_length, this->valid_ptr_);
  // Encode valide pointer flag.

  if (this->seq1_ != 0)
    {
      ENCODE_UINT32 (this->buffer_, total_length, this->seq1_->size ());
      total_length += ace_array_encode (this->buffer_ + total_length, *this->seq1_);
    }
  if (this->seq2_ != 0)
    {
      ENCODE_UINT32 (this->buffer_, total_length, this->seq2_->size ());
      total_length += ace_array_encode (this->buffer_ + total_length, *this->seq2_);
    }
  if (this->offer_ != 0)
    total_length += this->offer_->encode (this->buffer_ + total_length);

  total_length += ACE_WString_Helper::encode (this->buffer_ + total_length,
					      this->id_);
  total_length += ACE_WString_Helper::encode (this->buffer_ + total_length,
					      this->url_);

  ACE_ASSERT (total_length == buf_size);
  return total_length;
}

size_t
ACE_URL_Locator_Request::decode (void *buffer)
{
  ACE_TRACE ("ACE_URL_Locator_Request::decode");

  if (buffer == 0)
    return 0;
  // Check if we have a valid buffer available.

  char *cbuffer = (char *) buffer;

  size_t buf_size;
  size_t total_length = 0;
  
  DECODE_UINT32 (cbuffer, total_length, buf_size);
  // Decode length of buffer size first.

  DECODE_UINT32 (cbuffer, total_length, this->code_);
  // Get the operation code.
  
  DECODE_UINT32 (cbuffer, total_length, this->how_);
  // Decode selection criteria.
  
  DECODE_UINT32 (cbuffer, total_length, this->how_many_);
  // Decode number of offers interested.

  DECODE_UINT32 (cbuffer, total_length, this->valid_ptr_);
  // Decode valide pointer flag.

  if ((this->valid_ptr_ & VALID_SEQ1) != 0)
    {
      size_t n;
      DECODE_UINT32 (cbuffer, total_length, n);
      ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (n), 0);
      total_length += ace_array_decode (cbuffer + total_length, *this->seq1_);
    }
  if ((this->valid_ptr_ & VALID_SEQ2) != 0)
    {
      size_t n;
      DECODE_UINT32 (cbuffer, total_length, n);
      ACE_NEW_RETURN (this->seq2_, ACE_URL_Property_Seq (n), 0);
      total_length += ace_array_decode (cbuffer + total_length, *this->seq2_);
    }
  if ((this->valid_ptr_ & VALID_OFFER) != 0)
    {
      ACE_NEW_RETURN (this->offer_, ACE_URL_Offer, 0);
      total_length += this->offer_->decode (cbuffer + total_length);
    }
  
  this->id_ = ACE_WString ((ACE_USHORT16 *) (cbuffer + total_length));
  total_length += ACE_WString_Helper::decode (cbuffer + total_length);
  this->url_ = ACE_WString ((ACE_USHORT16 *) (cbuffer + total_length));
  total_length += ACE_WString_Helper::decode (cbuffer + total_length);

  ACE_ASSERT (total_length == buf_size);
  return total_length;
}


size_t
ACE_URL_Locator_Request::size (void)
{
  ACE_TRACE ("ACE_URL_Locator_Request::size");

  size_t total_length = 5 * sizeof (ACE_UINT32);
  // There are 5 UINT32 variables at the beginning
  // of the buffer.  <buffer size>, <code>, <how>,
  // <how_many>, <valid_ptr>.

  this->valid_ptr_ = 0;
  // Check valid pointers and mark corresponding flag in <valid_prt>.

  if (this->seq1_ != 0)
    {
      this->valid_ptr_ |= VALID_SEQ1;
      total_length += ace_array_size (*this->seq1_);
    }
  if (this->seq2_ != 0)
    {
      this->valid_ptr_ |= VALID_SEQ2;
      total_length += ace_array_size (*this->seq2_);
    }
  if (this->offer_ != 0)
    {
      this->valid_ptr_ |= VALID_OFFER;
      total_length += this->offer_->size ();
    }
  
  total_length += ACE_WString_Helper::size (this->id_);
  total_length += ACE_WString_Helper::size (this->url_);

  return total_length;
}

void
ACE_URL_Locator_Request::dump (void) const
{
  //ACE_TRACE ("ACE_URL_Locator_Request::dump");

  size_t i;

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  if (this->code_ < ACE_URL_Locator::INVALID_OPERATION)
    ACE_DEBUG ((LM_DEBUG, "%s Request:\n", ACE_URL_Locator::opname[this->code_]));
  else
    ACE_DEBUG ((LM_DEBUG, "Invalid Operation: %d\n", this->code_));

  if (this->how_ < ACE_URL_Locator::INVALID_SELECTION)
    ACE_DEBUG ((LM_DEBUG, "Select: %s\n", ACE_URL_Locator::selection_name[this->how_]));
  else
    ACE_DEBUG ((LM_DEBUG, "Invalid selection method: %d\n", this->how_));

  ACE_DEBUG ((LM_DEBUG, "At most %d reply.\n", this->how_many_));

  ACE_DEBUG ((LM_DEBUG, "Valid pointer pattern: %x\n", this->valid_ptr_));

  ACE_DEBUG ((LM_DEBUG, "Property sequence 1: %x\n", this->seq1_));
  if (this->seq1_ != 0)
    for (i = 0; i < this->seq1_->size (); i++)
      (*this->seq1_)[i].dump ();

  ACE_DEBUG ((LM_DEBUG, "Property sequence 2: %x\n", this->seq2_));
  if (this->seq2_ != 0)
    for (i = 0; i < this->seq2_->size (); i++)
      (*this->seq2_)[i].dump();

  ACE_DEBUG ((LM_DEBUG, "Offer: %x\n", this->offer_));
  if (this->offer_ != 0)
    this->offer_->dump ();

  if (this->id_.length () > 0)
    ACE_DEBUG ((LM_DEBUG, "Offer ID: %s\n",
		ACE_Auto_Basic_Array_Ptr<char> (this->id_.char_rep ()).get ()));
  else
    ACE_DEBUG ((LM_DEBUG, "Offer ID: \"\"\n"));

  if (this->url_.length () > 0)
    ACE_DEBUG ((LM_DEBUG, "URL: %s\n",
		ACE_Auto_Basic_Array_Ptr<char> (this->url_.char_rep ()).get ()));
  else
    ACE_DEBUG ((LM_DEBUG, "URL: \"\"\n"));
  
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

int
ACE_URL_Locator_Reply::status_reply (u_int op, int result)
{
  ACE_TRACE ("ACE_URL_Locator_Reply::status_reply");

  this->code_ = op;
  this->status_ = result;
  return 0;
}

int
ACE_URL_Locator_Reply::query_reply (int result, size_t num,
	     const ACE_URL_Offer_Seq &offers)
{
  ACE_TRACE ("ACE_URL_Locator_Reply::query_reply");
  
  this->code_ = ACE_URL_Locator::QUERY;
  this->status_ = result;
  ACE_NEW_RETURN (this->offers_, ACE_URL_Offer_Seq (offers), -1);
  return 0;
}

int
ACE_URL_Locator_Reply::describe_reply (int result,
		const ACE_URL_Offer &offer)
{
  ACE_TRACE ("ACE_URL_Locator_Reply::describe_reply");

  this->code_ = ACE_URL_Locator::DESCRIBE;
  this->status_ = result;
  ACE_NEW_RETURN (this->offer_, ACE_URL_Offer (offer), -1);
  return 0;
}

size_t
ACE_URL_Locator_Reply::encode (void)
{
  ACE_TRACE ("ACE_URL_Locator_Reply::encode");

  size_t buf_size = this->size ();
  size_t total_length = 0;

  ACE_NEW_RETURN (this->buffer_, char [buf_size], 0);

  ENCODE_UINT32 (this->buffer_, total_length, buf_size);
  // Encode buffer size.

  ENCODE_UINT32 (this->buffer_, total_length, this->code_);
  // Encode Op code.

  ENCODE_UINT32 (this->buffer_, total_length, this->status_);
  // Encode Op result status.

  ENCODE_UINT32 (this->buffer_, total_length, this->num_offers_);
  // Encode number of offers in this->offers_.

  ENCODE_UINT32 (this->buffer_, total_length, this->valid_ptr_);
  // Encode valid pointers mask.

  if (this->offer_ != 0)
    total_length += this->offer_->encode (this->buffer_ + total_length);

  if (this->offers_ != 0)
    {
      ENCODE_UINT32 (this->buffer_, total_length, this->offers_->size ());
      total_length += ace_array_encode (this->buffer_ + total_length, *this->offers_);
    }

  ACE_ASSERT (total_length == buf_size);
  return 0;
}
 
size_t
ACE_URL_Locator_Reply::decode (void *buffer)
{
  ACE_TRACE ("ACE_URL_Locator_Reply::decode");

  if (buffer == 0)
    return 0;
  // Check if we have a buffer available.

  char *cbuffer = (char *) buffer;

  size_t buf_size;
  size_t total_length = 0;

  DECODE_UINT32 (cbuffer, total_length, buf_size);
  // Get the length of the buffer first.

  DECODE_UINT32 (cbuffer, total_length, this->code_);
  // Decode Op code.

  DECODE_UINT32 (cbuffer, total_length, this->status_);
  // Decode Op result status.

  DECODE_UINT32 (cbuffer, total_length, this->num_offers_);
  // Decode number of offers in this->offers_.

  DECODE_UINT32 (cbuffer, total_length, this->valid_ptr_);
  // Decode valid pointers mask.

  if ((this->valid_ptr_ & VALID_OFFER) != 0)
    {
      ACE_NEW_RETURN (this->offer_, ACE_URL_Offer, 0);
      total_length += this->offer_->decode (cbuffer + total_length);
    }

  if ((this->valid_ptr_ & VALID_OFFERS) != 0)
    {
      size_t n;
      DECODE_UINT32 (cbuffer, total_length, n);
      ACE_NEW_RETURN (this->offers_, ACE_URL_Offer_Seq (n), 0);
      total_length += ace_array_decode (cbuffer + total_length, *this->offers_);
    }
  
  ACE_ASSERT (total_length ==buf_size);
  return 0;
}
 
size_t
ACE_URL_Locator_Reply::size (void)
{
  ACE_TRACE ("ACE_URL_Locator_Reply:size");

  size_t total_length = 5 * sizeof (ACE_UINT32);
  // size for 5 ACE_UINT32 objects: <buffer size>, <code_>,
  // <status_>, <num_offers_>, and <valid_ptr_>.

  this->valid_ptr_ = 0;
  if (this->offer_ != 0)
    {
      this->valid_ptr_ |= VALID_OFFER;
      total_length += this->offer_->size ();
    }
  if (this->offers_ != 0)
    {
      this->valid_ptr_ |= VALID_OFFERS;
      total_length += ace_array_size (*this->offers_);
    }
  return total_length;
}

void
ACE_URL_Locator_Reply::dump (void) const
{
  //ACE_TRACE ("ACE_URL_Locator_Reply::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  if (this->code_ < ACE_URL_Locator::INVALID_OPERATION)
    ACE_DEBUG ((LM_DEBUG, "Original request: %s\n", ACE_URL_Locator::opname[this->code_]));
  else
    ACE_DEBUG ((LM_DEBUG, "Invalid Original Request: %d\n", this->code_));

  if (this->status_ < ACE_URL_Locator::MAX_URL_ERROR)
    ACE_DEBUG ((LM_DEBUG, "Reply status: %s\n", ACE_URL_Locator::err_name[this->status_]));
  else
    ACE_DEBUG ((LM_DEBUG, "Invalid reply status: %d\n", this->status_));

  ACE_DEBUG ((LM_DEBUG, "Number of offers: %d\n", this->num_offers_));

  ACE_DEBUG ((LM_DEBUG, "Valid pointer pattern: %x\n", this->valid_ptr_));

  ACE_DEBUG ((LM_DEBUG, "Offer: %x\n", this->offer_));
  if (this->offer_ != 0)
    this->offer_->dump ();

  ACE_DEBUG ((LM_DEBUG, "Offer sequence: %x\n", this->offers_));
  if (this->offers_ != 0)
    for (size_t i = 0; i < this->offers_->size (); i++)
      (*this->offers_)[i].dump();

  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}
#endif /* ACE_LOCATOR_REQUEST_REPLY_C */