Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / utils / logWalker / GIOP_Buffer.cpp
blobc4a4f483561438dbe84f390381953ce548c9eb3e
1 #include "GIOP_Buffer.h"
2 #include "ace/OS_NS_string.h"
3 #include "ace/Log_Msg.h"
5 static const char *size_leadin_1_5 = "GIOP v1."; //"x msg, ";
6 static size_t leadin_len_1_5 = 15;
7 static const char *size_leadin_1_6 = "GIOP message v1."; //2, ";
8 static size_t leadin_len_1_6 = 19;
11 const char *GIOP_Buffer::size_leadin = 0;
12 size_t GIOP_Buffer::leadin_len = 0;
13 const size_t GIOP_Buffer::giop_header_len = 12;
15 void
16 GIOP_Buffer::init_leadin (int version)
18 if (size_leadin == 0)
20 if (version == 150)
22 size_leadin = size_leadin_1_5;
23 leadin_len = leadin_len_1_5;
25 else
27 size_leadin = size_leadin_1_6;
28 leadin_len = leadin_len_1_6;
33 // GIOP 1.2 header: 12 bytes
34 // Magic: 4
35 // ver: 2
36 // flags: 1 // bo
37 // type: 1 // req/repl/lf/excp
38 // len: 4
40 // Request 1.2 header:
41 // req_id: 4
42 // flags: 1
43 // RESVD: 3
44 // Address disposition: 4
45 // target: <4 len, + len> [0-3 pad]
46 // opname: <4 len, + len>
48 // GIOP 1.0 header: 12 bytes
49 // Magic: 4
50 // ver: 2
51 // byte_order: 1
52 // type: 1 // req/repl/lf/excp
53 // len: 4
55 // Request 1.0 header:
57 struct RequestHeader_1_0 { // Renamed from RequestHeader
58 IOP::ServiceContextList service_context;
59 unsigned long request_id;
60 boolean response_expected;
61 sequence <octet> object_key;
62 string operation;
63 CORBA::OctetSeq requesting_principal;
66 // service_context: 4(count)+contexts.
67 // context id: 4
68 // content blob: 4(len) + len octets + (0-3)pad
69 // request_id: 4
70 // respexp: 1
71 // RESVD: 3
72 // Address disposition: 4
73 // target: <4 len, + len> [0-3 pad]
74 // opname: <4 len, + len>
77 struct ReplyHeader_1_0 { // Renamed from ReplyHeader
78 IOP::ServiceContextList service_context;
79 unsigned long request_id;
80 ReplyStatusType_1_0 reply_status;
82 // GIOP 1.1
83 typedef ReplyHeader_1_0 ReplyHeader_1_1;
84 // Same Header contents for 1.0 and 1.1
85 #else
86 // GIOP 1.2, 1.3
87 enum ReplyStatusType_1_2 {
88 NO_EXCEPTION,
89 USER_EXCEPTION,
90 SYSTEM_EXCEPTION,
91 LOCATION_FORWARD,
92 LOCATION_FORWARD_PERM,// new value for 1.2
93 NEEDS_ADDRESSING_MODE // new value for 1.2
95 struct ReplyHeader_1_2 {
96 unsigned long request_id;
97 ReplyStatusType_1_2 reply_status;
98 IOP::ServiceContextList service_context;
104 //static const size_t target_offset_12 = GIOP_Buffer::giop_header_len + 12;
106 // 12 = req_id + flags + RESVD + addr disp.
108 GIOP_Buffer::GIOP_Buffer()
109 : cdr_ (0),
110 preamble_(),
111 log_offset_(0),
112 thr_(0),
113 time_(),
114 expected_req_id_(0),
115 expected_size_(0),
116 expected_type_(0),
117 buffer_size_(0),
118 wr_pos_ (0),
119 octets_ (0),
120 owner_ (0),
121 buffer_lost_ (false),
122 sending_(false),
123 oid_ (0),
124 oid_len_ (0),
125 opname_ (0),
126 req_id_ (0),
127 resp_exp_ (0),
128 reply_status_ (0),
129 ver_minor_ (0),
130 msg_size_ (0),
131 num_contexts_ (0),
132 header_parsed_ (false),
133 payload_start_ (0),
134 payload_size_ (0)
138 GIOP_Buffer::GIOP_Buffer(const char *text,
139 size_t offset,
140 Thread *thread,
141 Invocation *owner)
142 : cdr_ (0),
143 preamble_(text),
144 log_offset_(offset),
145 thr_(thread),
146 time_(ACE_Time_Value::zero),
147 expected_req_id_(0),
148 expected_size_(0),
149 expected_type_(0),
150 buffer_size_(0),
151 wr_pos_ (0),
152 octets_ (0),
153 owner_ (owner),
154 buffer_lost_ (false),
155 sending_(false),
156 oid_ (0),
157 oid_len_ (0),
158 opname_ (0),
159 req_id_ (0),
160 resp_exp_ (0),
161 reply_status_ (0),
162 ver_minor_ (0),
163 msg_size_ (0),
164 num_contexts_ (0),
165 header_parsed_ (false),
166 payload_start_ (0),
167 payload_size_ (0)
169 const char *size_str = ACE_OS::strstr(text, size_leadin);
170 if (size_str != 0)
172 size_str += leadin_len; //ACE_OS::strlen (size_leadin);
173 // size_str += 3;
174 this->expected_size_ = ACE_OS::strtol(size_str, 0, 10);
175 const char *id = ACE_OS::strchr(size_str, '[') + 1;
176 this->expected_req_id_ = ACE_OS::strtol(id, 0, 10);
178 this->sending_ = ACE_OS::strstr(text,"send") ? 0 : 1;
179 this->expected_type_ = ACE_OS::strstr(text,"Request") ? 0 : 1;
182 void
183 GIOP_Buffer::owner (Invocation *owner)
185 this->owner_ = owner;
188 Invocation *
189 GIOP_Buffer::owner ()
191 return this->owner_;
194 void
195 GIOP_Buffer::init_buf (const char *text, size_t offset)
197 // GIOP message - HEXDUMP
198 this->log_offset_ = offset;
199 const char * size_str = ACE_OS::strstr (text,"HEXDUMP ") + 8;
200 this->buffer_size_ = ACE_OS::strtol (size_str, 0, 10);
201 this->payload_size_ = this->buffer_size_ - 12;
202 size_str = ACE_OS::strstr (text,"showing first ");
203 if (size_str != 0)
205 size_str += 14;
206 this->buffer_size_ = ACE_OS::strtol (size_str, 0, 10);
208 if (this->octets_ != 0)
210 delete [] this->octets_;
212 this->octets_ = new char [this->buffer_size_];
213 ACE_OS::memset (this->octets_, 0, this->buffer_size_);
214 this->wr_pos_ = this->octets_;
217 GIOP_Buffer::~GIOP_Buffer()
219 delete [] this->octets_;
223 GIOP_Buffer::add_octets (const char *text, size_t offset)
225 if (this->octets_ == 0)
227 this->init_buf(text, offset);
228 return 0;
231 const char *c = text;
232 char *err;
233 for (int count = 0;
234 count < 16 && this->cur_size() < this->buffer_size_;
235 count++)
237 if (count == 8)
238 ++c;
239 int o = ::strtol(c, &err, 16);
240 if (err == c || *err == 0)
241 return -1;
242 *this->wr_pos_++ = o;
243 c = err+1;
245 size_t cs = this->cur_size();
246 int rtn = 0;
247 if (!this->header_parsed_)
249 this->header_parsed_ = this->parse_header();
250 if (this->header_parsed_ )
251 rtn = 1;
253 if (cs == this->buffer_size_)
255 char vmaj = this->octets_[4];
256 char order = this->octets_[6];
258 delete this->cdr_;
259 this->cdr_ = new ACE_InputCDR (this->octets_,
260 this->cur_size(),
261 order, vmaj, this->ver_minor_);
262 this->cdr_->skip_bytes (this->payload_start_ - this->octets_);
263 rtn = -1;
265 return rtn;
268 bool
269 GIOP_Buffer::sending () const
271 return this->sending_;
274 bool
275 GIOP_Buffer::is_full () const
277 return this->buffer_size_ > 0 && this->cur_size() == this->buffer_size_;
280 char
281 GIOP_Buffer::type () const
283 if (this->octets_ == 0)
284 return 127;
285 return this->octets_[7];
288 char
289 GIOP_Buffer::expected_type () const
291 return this->expected_type_;
294 char
295 GIOP_Buffer::minor_version () const
297 return this->ver_minor_;
300 size_t
301 GIOP_Buffer::reply_status () const
303 return this->reply_status_;
306 size_t
307 GIOP_Buffer::num_contexts () const
309 return this->num_contexts_;
312 bool
313 GIOP_Buffer::is_oneway ()
315 if (this->octets_ == 0)
317 return false;
320 if (!this->header_parsed_)
321 this->header_parsed_ = this->parse_header();
323 return (resp_exp_ &1) == 0;
326 size_t
327 GIOP_Buffer::log_posn () const
329 return this->log_offset_;
332 Thread *
333 GIOP_Buffer::thread ()
335 return this->thr_;
338 const ACE_Time_Value &
339 GIOP_Buffer::time () const
341 return this->time_;
344 void
345 GIOP_Buffer::time (const ACE_Time_Value &t)
347 this->time_ = t;
350 const ACE_CString &
351 GIOP_Buffer::preamble () const
353 return this->preamble_;
356 size_t
357 GIOP_Buffer::expected_size () const
359 return this->expected_size_;
362 size_t
363 GIOP_Buffer::msg_size ()
365 if (this->cur_size() < 12)
366 return 0;
367 if (!this->header_parsed_)
368 this->header_parsed_ = this->parse_header();
369 return (size_leadin == size_leadin_1_5) ? this->payload_size_ : this->msg_size_;
372 size_t
373 GIOP_Buffer::expected_req_id () const
375 return this->expected_req_id_;
378 size_t
379 GIOP_Buffer::actual_req_id ()
381 if (this->octets_ == 0)
382 return 0;
384 if (!this->header_parsed_)
385 this->header_parsed_ = this->parse_header();
387 return this->req_id_;
390 size_t
391 GIOP_Buffer::cur_size () const
393 return this->wr_pos_ - this->octets_;
396 bool
397 GIOP_Buffer::parse_svc_contexts ()
399 ACE_CDR::ULong temp;
400 ACE_CDR::ULong num_svc_cont;
401 if ( !(*this->cdr_ >> num_svc_cont))
402 return false;
403 this->num_contexts_ = static_cast<size_t>(num_svc_cont);
404 while (num_svc_cont > 0)
406 if (!(*this->cdr_ >> temp)) // tag really
407 return false;
408 if (!(*this->cdr_ >> temp))
409 return false;
410 if ((this->cur_size() == this->buffer_size_) && (this->cdr_->length() < temp))
412 return this->cdr_->skip_bytes (this->cdr_->length());
414 if (!this->cdr_->skip_bytes(temp))
416 return this->cur_size() == this->buffer_size_;
418 --num_svc_cont;
420 return true;
423 bool
424 GIOP_Buffer::parse_header ()
426 if (this->octets_ == 0 || this->cur_size() < 12)
428 return false;
431 char vmaj = this->octets_[4];
432 this->ver_minor_ = this->octets_[5];
433 char order = this->octets_[6];
435 char mtype = this->octets_[7];
436 if (mtype > 1) // not a request or reply
438 return false;
440 delete this->cdr_;
441 this->cdr_ = new ACE_InputCDR (this->octets_,
442 this->cur_size(),
443 order, vmaj, this->ver_minor_);
444 this->cdr_->skip_bytes (8);
445 this->payload_start_ = this->cdr_->rd_ptr();
446 ACE_CDR::ULong len_ulong;
447 if (!(*this->cdr_ >> len_ulong))
448 return false;
449 this->msg_size_ = len_ulong;
451 if (this->ver_minor_ < 2)
453 if (!this->parse_svc_contexts())
455 return false;
459 if (!(*this->cdr_ >> len_ulong))
461 return false;
463 this->req_id_ = static_cast<size_t>(len_ulong);
465 switch (mtype) {
466 case 0: //Request
467 if (!(*this->cdr_ >> this->resp_exp_))
469 return false;
471 if (this->ver_minor_ > 1 &&
472 !(*this->cdr_ >> len_ulong)) // address disposition
474 return false;
476 if (!(*this->cdr_ >> len_ulong))
478 return false;
480 this->oid_len_ = static_cast<size_t>(len_ulong);
481 this->oid_ = this->cdr_->rd_ptr();
482 if (!this->cdr_->skip_bytes(len_ulong))
484 return false;
486 if (!(*this->cdr_ >> len_ulong))
488 return false;
490 this->opname_ = this->cdr_->rd_ptr();
491 if (!this->cdr_->skip_bytes(len_ulong))
493 return false;
495 break;
496 case 1: //Reply
497 if (!(*this->cdr_ >> len_ulong))
499 return false;
501 this->reply_status_ = static_cast<size_t>(len_ulong);
502 break;
503 default:
504 return true;
506 if (this->ver_minor_ > 1)
508 if (!this->parse_svc_contexts())
509 return false;
511 this->cdr_->align_read_ptr (8);
512 this->payload_start_ = this->cdr_->rd_ptr();
513 return true;
516 const char *
517 GIOP_Buffer::target_oid (size_t &len)
519 if (this->octets_ == 0)
521 return 0;
524 if (!this->header_parsed_)
525 this->header_parsed_ = this->parse_header();
527 if (this->oid_ == 0)
528 return 0;
530 len = this->oid_len_;
531 return this->oid_;
534 const char *
535 GIOP_Buffer::operation ()
537 if (octets_ == 0)
538 return 0;
540 if (!this->header_parsed_)
541 this->header_parsed_ = this->parse_header();
543 return this->opname_;
546 ACE_InputCDR &
547 GIOP_Buffer::payload ()
549 if (octets_ != 0 && !this->header_parsed_)
550 this->header_parsed_ = this->parse_header();
551 return *this->cdr_;
554 bool
555 GIOP_Buffer::has_octets () const
557 return (octets_ != 0);
560 bool
561 GIOP_Buffer::validate ()
563 return
564 this->expected_req_id_ == this->actual_req_id() &&
565 this->expected_type_ == this->type() &&
566 this->expected_size_ == this->msg_size();
569 bool
570 GIOP_Buffer::matches (GIOP_Buffer *other) const
572 if (other->header_parsed_)
574 return this->expected_req_id_ == other->actual_req_id() &&
575 this->expected_type_ == other->type() &&
576 (this->expected_size_ == other->msg_size() ||
577 this->expected_size_ == other->msg_size() + 4);
579 return this->expected_req_id_ == other->expected_req_id() &&
580 this->expected_type_ == other->expected_type() &&
581 this->sending_ == other->sending() &&
582 this->expected_size_ == other->expected_size();
585 void
586 GIOP_Buffer::reset ()
588 this->octets_ = 0;
589 this->wr_pos_ = 0;
590 this->buffer_size_ = 0;
591 this->payload_size_ = 0;
592 this->buffer_lost_ = true;
593 this->header_parsed_ = false;
594 this->opname_ = 0;
597 void
598 GIOP_Buffer::transfer_from (GIOP_Buffer *other)
600 delete this->octets_;
601 this->octets_ = other->octets_;
602 this->wr_pos_ = other->wr_pos_;
603 this->buffer_size_ = other->buffer_size_;
604 this->payload_size_ = other->payload_size_;
605 this->header_parsed_ = false;
606 other->reset();
609 void
610 GIOP_Buffer::swap (GIOP_Buffer *other)
612 char *tmp_octets = this->octets_;
613 char *tmp_wr_pos = this->wr_pos_;
614 size_t tmp_bsize = this->buffer_size_;
615 size_t tmp_psize = this->payload_size_;
617 this->octets_ = other->octets_;
618 this->wr_pos_ = other->wr_pos_;
619 this->buffer_size_ = other->buffer_size_;
620 this->payload_size_ = other->payload_size_;
622 other->octets_ = tmp_octets;
623 other->wr_pos_ = tmp_wr_pos;
624 other->buffer_size_ = tmp_bsize;
625 other->payload_size_= tmp_psize;