Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / JAWS / clients / Caching / Locator_Request_Reply.cpp
blobbca4ff1dd549cf43ff608116e8eb785ea1b9340e
1 #if !defined (ACE_LOCATOR_REQUEST_REPLY_C)
2 #define ACE_LOCATOR_REQUEST_REPLY_C
4 #include "Locator_Request_Reply.h"
6 #if !defined (__ACE_INLINE__)
7 #include "Locator_Request_Reply.inl"
8 #endif
10 #include "URL_Properties.h"
11 #include "URL_Array_Helper.h"
12 #include "URL_Locator.h"
13 #include <memory>
15 int
16 ACE_URL_Locator_Request::url_query (const int how,
17 const ACE_URL_Property_Seq &pseq,
18 const int how_many)
20 ACE_TRACE ("ACE_URL_Locator_Request::url_query");
22 if (how >= ACE_URL_Locator::INVALID_SELECTION)
23 return -1;
24 ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (pseq), -1);
25 this->how_ = how;
26 this->how_many_ = how_many;
27 this->code_ = ACE_URL_Locator::QUERY;
28 return 0;
31 int
32 ACE_URL_Locator_Request::export_offer (const ACE_URL_Offer &offer)
34 ACE_TRACE ("ACE_URL_Locator_Request::export_offer");
36 ACE_NEW_RETURN (this->offer_, ACE_URL_Offer (offer), -1);
37 this->code_ = ACE_URL_Locator::EXPORT;
38 return 0;
41 int
42 ACE_URL_Locator_Request::withdraw_offer (const ACE_WString &offer_id)
44 ACE_TRACE ("ACE_URL_Locator_Request::withdraw_offer");
46 this->id_ = offer_id;
47 this->code_ = ACE_URL_Locator::WITHDRAW;
48 return 0;
51 int
52 ACE_URL_Locator_Request::describe_offer (const ACE_WString &offer_id)
54 ACE_TRACE ("ACE_URL_Locator_Request::describe_offer");
56 this->id_ = offer_id;
57 this->code_ = ACE_URL_Locator::DESCRIBE;
58 return 0;
61 int
62 ACE_URL_Locator_Request::modify_offer (const ACE_WString &offer_id,
63 const ACE_WString *url,
64 const ACE_URL_Property_Seq &del,
65 const ACE_URL_Property_Seq &modify)
67 ACE_TRACE ("ACE_URL_Locator_Request::modify_offer");
69 ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (del), -1);
70 ACE_NEW_RETURN (this->seq2_, ACE_URL_Property_Seq (modify), -1);
72 if (url != 0)
73 this->url_ = *url;
75 this->id_ = offer_id;
76 this->code_ = ACE_URL_Locator::MODIFY;
77 return 0;
80 #define ENCODE_UINT32(ADDR,LEN,V) \
81 * (ACE_UINT32 *) (ADDR+LEN) = htonl (V); \
82 LEN += sizeof (ACE_UINT32);
84 #define DECODE_UINT32(ADDR,LEN,V) \
85 V = ntohl (* (ACE_UINT32 *) (ADDR+LEN)); \
86 LEN += sizeof (ACE_UINT32);
88 size_t
89 ACE_URL_Locator_Request::encode ()
91 ACE_TRACE ("ACE_URL_Locator_Request::encode");
93 size_t buf_size = this->size ();
94 size_t total_length = 0;
96 ACE_NEW_RETURN (this->buffer_, char [buf_size], 0);
98 ENCODE_UINT32 (this->buffer_, total_length, buf_size);
99 // Encode buffer size.
101 ENCODE_UINT32 (this->buffer_, total_length, this->code_);
102 // Encode Op code.
104 ENCODE_UINT32 (this->buffer_, total_length, this->how_);
105 // Encode selection criteria.
107 ENCODE_UINT32 (this->buffer_, total_length, this->how_many_);
108 // Encode number of offers interested.
110 ENCODE_UINT32 (this->buffer_, total_length, this->valid_ptr_);
111 // Encode valide pointer flag.
113 if (this->seq1_ != 0)
115 ENCODE_UINT32 (this->buffer_, total_length, this->seq1_->size ());
116 total_length += ace_array_encode (this->buffer_ + total_length, *this->seq1_);
118 if (this->seq2_ != 0)
120 ENCODE_UINT32 (this->buffer_, total_length, this->seq2_->size ());
121 total_length += ace_array_encode (this->buffer_ + total_length, *this->seq2_);
123 if (this->offer_ != 0)
124 total_length += this->offer_->encode (this->buffer_ + total_length);
126 total_length += ACE_WString_Helper::encode (this->buffer_ + total_length,
127 this->id_);
128 total_length += ACE_WString_Helper::encode (this->buffer_ + total_length,
129 this->url_);
131 ACE_ASSERT (total_length == buf_size);
132 return total_length;
135 size_t
136 ACE_URL_Locator_Request::decode (void *buffer)
138 ACE_TRACE ("ACE_URL_Locator_Request::decode");
140 if (buffer == 0)
141 return 0;
142 // Check if we have a valid buffer available.
144 char *cbuffer = (char *) buffer;
146 size_t buf_size;
147 size_t total_length = 0;
149 DECODE_UINT32 (cbuffer, total_length, buf_size);
150 // Decode length of buffer size first.
152 DECODE_UINT32 (cbuffer, total_length, this->code_);
153 // Get the operation code.
155 DECODE_UINT32 (cbuffer, total_length, this->how_);
156 // Decode selection criteria.
158 DECODE_UINT32 (cbuffer, total_length, this->how_many_);
159 // Decode number of offers interested.
161 DECODE_UINT32 (cbuffer, total_length, this->valid_ptr_);
162 // Decode valide pointer flag.
164 if ((this->valid_ptr_ & VALID_SEQ1) != 0)
166 size_t n;
167 DECODE_UINT32 (cbuffer, total_length, n);
168 ACE_NEW_RETURN (this->seq1_, ACE_URL_Property_Seq (n), 0);
169 total_length += ace_array_decode (cbuffer + total_length, *this->seq1_);
171 if ((this->valid_ptr_ & VALID_SEQ2) != 0)
173 size_t n;
174 DECODE_UINT32 (cbuffer, total_length, n);
175 ACE_NEW_RETURN (this->seq2_, ACE_URL_Property_Seq (n), 0);
176 total_length += ace_array_decode (cbuffer + total_length, *this->seq2_);
178 if ((this->valid_ptr_ & VALID_OFFER) != 0)
180 ACE_NEW_RETURN (this->offer_, ACE_URL_Offer, 0);
181 total_length += this->offer_->decode (cbuffer + total_length);
184 this->id_ = ACE_WString ((ACE_UINT16 *) (cbuffer + total_length));
185 total_length += ACE_WString_Helper::decode (cbuffer + total_length);
186 this->url_ = ACE_WString ((ACE_UINT16 *) (cbuffer + total_length));
187 total_length += ACE_WString_Helper::decode (cbuffer + total_length);
189 ACE_ASSERT (total_length == buf_size);
190 return total_length;
194 size_t
195 ACE_URL_Locator_Request::size ()
197 ACE_TRACE ("ACE_URL_Locator_Request::size");
199 size_t total_length = 5 * sizeof (ACE_UINT32);
200 // There are 5 UINT32 variables at the beginning
201 // of the buffer. <buffer size>, <code>, <how>,
202 // <how_many>, <valid_ptr>.
204 this->valid_ptr_ = 0;
205 // Check valid pointers and mark corresponding flag in <valid_prt>.
207 if (this->seq1_ != 0)
209 this->valid_ptr_ |= VALID_SEQ1;
210 total_length += ace_array_size (*this->seq1_);
212 if (this->seq2_ != 0)
214 this->valid_ptr_ |= VALID_SEQ2;
215 total_length += ace_array_size (*this->seq2_);
217 if (this->offer_ != 0)
219 this->valid_ptr_ |= VALID_OFFER;
220 total_length += this->offer_->size ();
223 total_length += ACE_WString_Helper::size (this->id_);
224 total_length += ACE_WString_Helper::size (this->url_);
226 return total_length;
229 void
230 ACE_URL_Locator_Request::dump () const
232 //ACE_TRACE ("ACE_URL_Locator_Request::dump");
234 size_t i;
236 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
238 if (this->code_ < ACE_URL_Locator::INVALID_OPERATION)
239 ACE_DEBUG ((LM_DEBUG, "%s Request:\n", ACE_URL_Locator::opname[this->code_]));
240 else
241 ACE_DEBUG ((LM_DEBUG, "Invalid Operation: %d\n", this->code_));
243 if (this->how_ < ACE_URL_Locator::INVALID_SELECTION)
244 ACE_DEBUG ((LM_DEBUG, "Select: %s\n", ACE_URL_Locator::selection_name[this->how_]));
245 else
246 ACE_DEBUG ((LM_DEBUG, "Invalid selection method: %d\n", this->how_));
248 ACE_DEBUG ((LM_DEBUG, "At most %d reply.\n", this->how_many_));
250 ACE_DEBUG ((LM_DEBUG, "Valid pointer pattern: %x\n", this->valid_ptr_));
252 ACE_DEBUG ((LM_DEBUG, "Property sequence 1: %x\n", this->seq1_));
253 if (this->seq1_ != 0)
254 for (i = 0; i < this->seq1_->size (); i++)
255 (*this->seq1_)[i].dump ();
257 ACE_DEBUG ((LM_DEBUG, "Property sequence 2: %x\n", this->seq2_));
258 if (this->seq2_ != 0)
259 for (i = 0; i < this->seq2_->size (); i++)
260 (*this->seq2_)[i].dump();
262 ACE_DEBUG ((LM_DEBUG, "Offer: %x\n", this->offer_));
263 if (this->offer_ != 0)
264 this->offer_->dump ();
266 if (this->id_.length () > 0)
267 ACE_DEBUG ((LM_DEBUG, "Offer ID: %s\n",
268 std::unique_ptr<char[]> (this->id_.char_rep ()).get ()));
269 else
270 ACE_DEBUG ((LM_DEBUG, "Offer ID: \"\"\n"));
272 if (this->url_.length () > 0)
273 ACE_DEBUG ((LM_DEBUG, "URL: %s\n",
274 std::unique_ptr<char[]> (this->url_.char_rep ()).get ()));
275 else
276 ACE_DEBUG ((LM_DEBUG, "URL: \"\"\n"));
278 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
282 ACE_URL_Locator_Reply::status_reply (u_int op, int result)
284 ACE_TRACE ("ACE_URL_Locator_Reply::status_reply");
286 this->code_ = op;
287 this->status_ = result;
288 return 0;
292 ACE_URL_Locator_Reply::query_reply (int result, size_t num,
293 const ACE_URL_Offer_Seq &offers)
295 ACE_TRACE ("ACE_URL_Locator_Reply::query_reply");
297 this->code_ = ACE_URL_Locator::QUERY;
298 this->status_ = result;
299 ACE_NEW_RETURN (this->offers_, ACE_URL_Offer_Seq (offers), -1);
300 return 0;
304 ACE_URL_Locator_Reply::describe_reply (int result,
305 const ACE_URL_Offer &offer)
307 ACE_TRACE ("ACE_URL_Locator_Reply::describe_reply");
309 this->code_ = ACE_URL_Locator::DESCRIBE;
310 this->status_ = result;
311 ACE_NEW_RETURN (this->offer_, ACE_URL_Offer (offer), -1);
312 return 0;
315 size_t
316 ACE_URL_Locator_Reply::encode ()
318 ACE_TRACE ("ACE_URL_Locator_Reply::encode");
320 size_t buf_size = this->size ();
321 size_t total_length = 0;
323 ACE_NEW_RETURN (this->buffer_, char [buf_size], 0);
325 ENCODE_UINT32 (this->buffer_, total_length, buf_size);
326 // Encode buffer size.
328 ENCODE_UINT32 (this->buffer_, total_length, this->code_);
329 // Encode Op code.
331 ENCODE_UINT32 (this->buffer_, total_length, this->status_);
332 // Encode Op result status.
334 ENCODE_UINT32 (this->buffer_, total_length, this->num_offers_);
335 // Encode number of offers in this->offers_.
337 ENCODE_UINT32 (this->buffer_, total_length, this->valid_ptr_);
338 // Encode valid pointers mask.
340 if (this->offer_ != 0)
341 total_length += this->offer_->encode (this->buffer_ + total_length);
343 if (this->offers_ != 0)
345 ENCODE_UINT32 (this->buffer_, total_length, this->offers_->size ());
346 total_length += ace_array_encode (this->buffer_ + total_length, *this->offers_);
349 ACE_ASSERT (total_length == buf_size);
350 return 0;
353 size_t
354 ACE_URL_Locator_Reply::decode (void *buffer)
356 ACE_TRACE ("ACE_URL_Locator_Reply::decode");
358 if (buffer == 0)
359 return 0;
360 // Check if we have a buffer available.
362 char *cbuffer = (char *) buffer;
364 size_t buf_size;
365 size_t total_length = 0;
367 DECODE_UINT32 (cbuffer, total_length, buf_size);
368 // Get the length of the buffer first.
370 DECODE_UINT32 (cbuffer, total_length, this->code_);
371 // Decode Op code.
373 DECODE_UINT32 (cbuffer, total_length, this->status_);
374 // Decode Op result status.
376 DECODE_UINT32 (cbuffer, total_length, this->num_offers_);
377 // Decode number of offers in this->offers_.
379 DECODE_UINT32 (cbuffer, total_length, this->valid_ptr_);
380 // Decode valid pointers mask.
382 if ((this->valid_ptr_ & VALID_OFFER) != 0)
384 ACE_NEW_RETURN (this->offer_, ACE_URL_Offer, 0);
385 total_length += this->offer_->decode (cbuffer + total_length);
388 if ((this->valid_ptr_ & VALID_OFFERS) != 0)
390 size_t n;
391 DECODE_UINT32 (cbuffer, total_length, n);
392 ACE_NEW_RETURN (this->offers_, ACE_URL_Offer_Seq (n), 0);
393 total_length += ace_array_decode (cbuffer + total_length, *this->offers_);
396 ACE_ASSERT (total_length ==buf_size);
397 return 0;
400 size_t
401 ACE_URL_Locator_Reply::size ()
403 ACE_TRACE ("ACE_URL_Locator_Reply:size");
405 size_t total_length = 5 * sizeof (ACE_UINT32);
406 // size for 5 ACE_UINT32 objects: <buffer size>, <code_>,
407 // <status_>, <num_offers_>, and <valid_ptr_>.
409 this->valid_ptr_ = 0;
410 if (this->offer_ != 0)
412 this->valid_ptr_ |= VALID_OFFER;
413 total_length += this->offer_->size ();
415 if (this->offers_ != 0)
417 this->valid_ptr_ |= VALID_OFFERS;
418 total_length += ace_array_size (*this->offers_);
420 return total_length;
423 void
424 ACE_URL_Locator_Reply::dump () const
426 //ACE_TRACE ("ACE_URL_Locator_Reply::dump");
428 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
430 if (this->code_ < ACE_URL_Locator::INVALID_OPERATION)
431 ACE_DEBUG ((LM_DEBUG, "Original request: %s\n", ACE_URL_Locator::opname[this->code_]));
432 else
433 ACE_DEBUG ((LM_DEBUG, "Invalid Original Request: %d\n", this->code_));
435 if (this->status_ < ACE_URL_Locator::MAX_URL_ERROR)
436 ACE_DEBUG ((LM_DEBUG, "Reply status: %s\n", ACE_URL_Locator::err_name[this->status_]));
437 else
438 ACE_DEBUG ((LM_DEBUG, "Invalid reply status: %d\n", this->status_));
440 ACE_DEBUG ((LM_DEBUG, "Number of offers: %d\n", this->num_offers_));
442 ACE_DEBUG ((LM_DEBUG, "Valid pointer pattern: %x\n", this->valid_ptr_));
444 ACE_DEBUG ((LM_DEBUG, "Offer: %x\n", this->offer_));
445 if (this->offer_ != 0)
446 this->offer_->dump ();
448 ACE_DEBUG ((LM_DEBUG, "Offer sequence: %x\n", this->offers_));
449 if (this->offers_ != 0)
450 for (size_t i = 0; i < this->offers_->size (); i++)
451 (*this->offers_)[i].dump();
453 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
455 #endif /* ACE_LOCATOR_REQUEST_REPLY_C */