| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use CORBA::Perl::CORBA; | ||
| 5 | use CORBA::Perl::IOP; | ||
| 6 | use CORBA::Perl::GIOP; | ||
| 7 | use CORBA::Perl::MIOP; | ||
| 8 | |||
| 9 | package CORBA::Perl::GIOP; | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use IO::Socket; | ||
| 13 | |||
| 14 | our $TRACE; | ||
| 15 | |||
| 16 | sub BEGIN { | ||
| 17 | $TRACE = 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | my $request_id = 0; | ||
| 21 | |||
| 22 | sub GetRequestId { | ||
| 23 | $request_id ++; | ||
| 24 | return $request_id; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub RequestOneWay { | ||
| 28 | my ($sock, $request_header, $request_body) = @_; | ||
| 29 | if ($sock->getsockopt(SOL_SOCKET, SO_TYPE) == SOCK_DGRAM) { | ||
| 30 | return CORBA::Perl::DIOP::RequestOneWay($sock, $request_header, $request_body); | ||
| 31 | } | ||
| 32 | else { | ||
| 33 | return _RequestOneWay($sock, $request_header, $request_body); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _RequestOneWay { | ||
| 38 | my ($sock, $request_header, $request_body) = @_; | ||
| 39 | $request_header->{request_id} = GetRequestId(); | ||
| 40 | my $request = q{}; | ||
| 41 | CORBA::Perl::GIOP::RequestHeader_1_2__marshal(\$request, $request_header); | ||
| 42 | $request .= $request_body; | ||
| 43 | my $buffer = q{}; | ||
| 44 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 45 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 46 | GIOP_version => { | ||
| 47 | major => 1, | ||
| 48 | minor => 2, | ||
| 49 | }, | ||
| 50 | flags => 0x01, # flags : little endian | ||
| 51 | message_type => 0, # Request | ||
| 52 | message_size => length $request | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | $buffer .= $request; | ||
| 56 | my $rc = $sock->send($buffer); | ||
| 57 | croak "send error with $sock ($!).\n" | ||
| 58 | unless (defined $rc); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub RequestReply { | ||
| 62 | my ($sock, $request_header, $request_body) = @_; | ||
| 63 | if ($sock->getsockopt(SOL_SOCKET, SO_TYPE) == SOCK_DGRAM) { | ||
| 64 | return CORBA::Perl::DIOP::RequestReply($sock, $request_header, $request_body); | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | return _RequestReply($sock, $request_header, $request_body); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _RequestReply { | ||
| 72 | my ($sock, $request_header, $request_body) = @_; | ||
| 73 | $request_header->{request_id} = GetRequestId(); | ||
| 74 | # print "request id $request_id\n"; | ||
| 75 | my $request = q{}; | ||
| 76 | CORBA::Perl::GIOP::RequestHeader_1_2__marshal(\$request, $request_header); | ||
| 77 | $request .= $request_body; | ||
| 78 | my $buffer = q{}; | ||
| 79 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 80 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 81 | GIOP_version => { | ||
| 82 | major => 1, | ||
| 83 | minor => 2, | ||
| 84 | }, | ||
| 85 | flags => 0x01, # flags : little endian | ||
| 86 | message_type => 0, # Request | ||
| 87 | message_size => length $request | ||
| 88 | } | ||
| 89 | ); | ||
| 90 | $buffer .= $request; | ||
| 91 | my $rc = $sock->send($buffer); | ||
| 92 | croak "send error with $sock ($!).\n" | ||
| 93 | unless (defined $rc); | ||
| 94 | my $header; | ||
| 95 | RETRY: | ||
| 96 | $rc = $sock->recv($header, 12); | ||
| 97 | croak "header: recv error with $sock ($!).\n" | ||
| 98 | unless (defined $rc); | ||
| 99 | my $endian = 0; | ||
| 100 | my $offset = 0; | ||
| 101 | my $magic = q{}; | ||
| 102 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$header, \$offset, $endian); | ||
| 103 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$header, \$offset, $endian); | ||
| 104 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$header, \$offset, $endian); | ||
| 105 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$header, \$offset, $endian); | ||
| 106 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$header, \$offset, $endian); | ||
| 107 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$header, \$offset, $endian); | ||
| 108 | $endian = $flags & 0x01; # now, endian is known | ||
| 109 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$header, \$offset, $endian); | ||
| 110 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$header, \$offset, $endian); | ||
| 111 | |||
| 112 | if ( $magic eq 'GIOP' | ||
| 113 | and $GIOP_version->{major} == 1 | ||
| 114 | and $GIOP_version->{minor} == 2 | ||
| 115 | and $message_type == 1 ) { | ||
| 116 | my $reply = q{}; | ||
| 117 | while ($message_size > 0) { | ||
| 118 | my $reply_i; | ||
| 119 | $rc = $sock->recv($reply_i, $message_size); | ||
| 120 | croak "reply: recv error with $sock ($!).\n" | ||
| 121 | unless (defined $rc); | ||
| 122 | $reply .= $reply_i; | ||
| 123 | $message_size -= length $reply_i; | ||
| 124 | } | ||
| 125 | $offset = 0; | ||
| 126 | my $reply_header = CORBA::Perl::GIOP::ReplyHeader_1_2__demarshal(\$reply, \$offset, $endian); | ||
| 127 | my $reply_body = substr $reply, $offset; | ||
| 128 | if ($request_header->{request_id} == $reply_header->{request_id}) { | ||
| 129 | # print "reply id $reply_header->{request_id}\n"; | ||
| 130 | return ($reply_header->{reply_status}, $reply_header->{service_context}, $reply_body, $endian); | ||
| 131 | } | ||
| 132 | elsif ($request_id > $reply_header->{request_id}) { | ||
| 133 | warn "bad request id $reply_header->{request_id} (waiting $request_header->{request_id}).\n"; | ||
| 134 | goto RETRY; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | croak "bad request id $reply_header->{request_id} (waiting $request_header->{request_id}).\n"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | croak "bad header.\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | package CORBA::Perl::GIOP::NB; | ||
| 146 | |||
| 147 | use Carp; | ||
| 148 | |||
| 149 | sub new { | ||
| 150 | my $proto = shift; | ||
| 151 | my $class = ref($proto) || $proto; | ||
| 152 | my $self = {}; | ||
| 153 | bless $self, $class; | ||
| 154 | $self->{callback} = {}; | ||
| 155 | return $self; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub Collect { | ||
| 159 | my $self = shift; | ||
| 160 | my $collection = q{}; | ||
| 161 | my $idx = 1; | ||
| 162 | my $nb = scalar(@_) / 3; | ||
| 163 | while (@_) { | ||
| 164 | my $message = q{}; | ||
| 165 | my $request_header = shift; | ||
| 166 | my $request_body = shift; | ||
| 167 | my $callback = shift; | ||
| 168 | $request_header->{request_id} = CORBA::Perl::GIOP::GetRequestId(); | ||
| 169 | $self->{callback}->{$request_header->{request_id}} = $callback; | ||
| 170 | my $request = q{}; | ||
| 171 | CORBA::Perl::GIOP::RequestHeader_1_2__marshal(\$request, $request_header); | ||
| 172 | $request .= $request_body; | ||
| 173 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$message, { | ||
| 174 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 175 | GIOP_version => { | ||
| 176 | major => 1, | ||
| 177 | minor => 2, | ||
| 178 | }, | ||
| 179 | flags => 0x01 # little endian | ||
| 180 | | (($idx != $nb) ? 0x80 : 0x00), | ||
| 181 | message_type => 0, # Request | ||
| 182 | message_size => length $request | ||
| 183 | } | ||
| 184 | ); | ||
| 185 | $message .= $request; | ||
| 186 | $collection .= $message; | ||
| 187 | $idx ++; | ||
| 188 | } | ||
| 189 | return $collection; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub Dispatch { | ||
| 193 | my $self = shift; | ||
| 194 | my ($message) = @_; | ||
| 195 | |||
| 196 | my $endian = 0; | ||
| 197 | my $more; | ||
| 198 | while (1) { | ||
| 199 | my $offset = 0; | ||
| 200 | my $magic = q{}; | ||
| 201 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 202 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 203 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 204 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 205 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$message, \$offset, $endian); | ||
| 206 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 207 | $endian = $flags & 0x01; # now, endian is known | ||
| 208 | $more = $flags & 0x80; | ||
| 209 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 210 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$message, \$offset, $endian); | ||
| 211 | |||
| 212 | if ( $magic eq 'GIOP' | ||
| 213 | and $GIOP_version->{major} == 1 | ||
| 214 | and $GIOP_version->{minor} == 2 | ||
| 215 | and $message_type == 1 ) { | ||
| 216 | |||
| 217 | my $reply_header = CORBA::Perl::GIOP::ReplyHeader_1_2__demarshal(\$message, \$offset, $endian); | ||
| 218 | my $request_id = $reply_header->{request_id}; | ||
| 219 | if (exists $self->{callback}->{$request_id}) { | ||
| 220 | my $callback = $self->{callback}->{$request_id}; | ||
| 221 | ${$callback}[0](${$callback}[1], $reply_header->{reply_status}, $reply_header->{service_context}, \$message, \$offset, $endian); | ||
| 222 | delete $self->{callback}->{$request_id}; | ||
| 223 | } | ||
| 224 | $message = substr $message, 12+$message_size; | ||
| 225 | } | ||
| 226 | last unless ($more); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | package CORBA::Perl::GIOP::Servant; | ||
| 231 | |||
| 232 | use Carp; | ||
| 233 | |||
| 234 | sub new { | ||
| 235 | my $proto = shift; | ||
| 236 | my $class = ref($proto) || $proto; | ||
| 237 | my $self = {}; | ||
| 238 | bless $self, $class; | ||
| 239 | $self->{itf} = {}; | ||
| 240 | $self->{all_reply} = q{}; | ||
| 241 | return $self; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub Register { | ||
| 245 | my $self = shift; | ||
| 246 | my ($interface, $classname) = @_; | ||
| 247 | |||
| 248 | $self->{itf}->{$interface} = $classname; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub Servant { | ||
| 252 | my $self = shift; | ||
| 253 | my ($message) = @_; | ||
| 254 | |||
| 255 | my $endian = 0; | ||
| 256 | my $offset = 0; | ||
| 257 | my $magic = q{}; | ||
| 258 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 259 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 260 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 261 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 262 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$message, \$offset, $endian); | ||
| 263 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 264 | $endian = $flags & 0x01; # now, endian is known | ||
| 265 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 266 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$message, \$offset, $endian); | ||
| 267 | |||
| 268 | if ( $magic eq 'GIOP' | ||
| 269 | and $GIOP_version->{major} == 1 | ||
| 270 | and $GIOP_version->{minor} == 2 | ||
| 271 | and $message_type == 0 ) { | ||
| 272 | |||
| 273 | my ($reply_status, $reply_body); | ||
| 274 | my $request_header = CORBA::Perl::GIOP::RequestHeader_1_2__demarshal(\$message, \$offset, $endian); | ||
| 275 | my $interface = ${$request_header->{target}}[1]; | ||
| 276 | if (!exists $self->{itf}->{$interface}) { | ||
| 277 | warn "unknown interface '$interface'.\n"; | ||
| 278 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 279 | $reply_body = q{}; | ||
| 280 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/NO_IMPLEMENT:1.0"); | ||
| 281 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 11); | ||
| 282 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | my $classname = $self->{itf}->{$interface}; | ||
| 286 | my $op = $request_header->{operation}; | ||
| 287 | if (! $classname->can($op)) { | ||
| 288 | warn "unknown operation '$request_header->{operation}'.\n"; | ||
| 289 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 290 | $reply_body = q{}; | ||
| 291 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/BAD_OPERATION:1.0"); | ||
| 292 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 13); | ||
| 293 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | my $srv_op = 'srv_' . $op; | ||
| 297 | ($reply_status, $reply_body) = $classname->$srv_op(\$message, \$offset, $endian); | ||
| 298 | return undef unless ($reply_status); # oneway | ||
| 299 | } | ||
| 300 | } | ||
| 301 | my $reply = q{}; | ||
| 302 | CORBA::Perl::GIOP::ReplyHeader_1_2__marshal(\$reply, { | ||
| 303 | request_id => $request_header->{request_id}, | ||
| 304 | reply_status => $reply_status, | ||
| 305 | service_context => [], | ||
| 306 | } | ||
| 307 | ); | ||
| 308 | $reply .= $reply_body; | ||
| 309 | my $buffer = q{}; | ||
| 310 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 311 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 312 | GIOP_version => { | ||
| 313 | major => 1, | ||
| 314 | minor => 2, | ||
| 315 | }, | ||
| 316 | flags => 0x01, # flags : little endian | ||
| 317 | message_type => 1, # Reply | ||
| 318 | message_size => length $reply | ||
| 319 | } | ||
| 320 | ); | ||
| 321 | $buffer .= $reply; | ||
| 322 | return $buffer | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | sub ServantNB { | ||
| 327 | my $self = shift; | ||
| 328 | my ($message) = @_; | ||
| 329 | |||
| 330 | my $endian = 0; | ||
| 331 | my $more; | ||
| 332 | while (1) { | ||
| 333 | return undef unless (length($message)); | ||
| 334 | my $offset = 0; | ||
| 335 | my $magic = q{}; | ||
| 336 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 337 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 338 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 339 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 340 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$message, \$offset, $endian); | ||
| 341 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 342 | $endian = $flags & 0x01; # now, endian is known | ||
| 343 | $more = $flags & 0x80; | ||
| 344 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 345 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$message, \$offset, $endian); | ||
| 346 | |||
| 347 | if ( $magic eq 'GIOP' | ||
| 348 | and $GIOP_version->{major} == 1 | ||
| 349 | and $GIOP_version->{minor} == 2 | ||
| 350 | and $message_type == 0 ) { | ||
| 351 | |||
| 352 | my ($reply_status, $reply_body); | ||
| 353 | my $request_header = CORBA::Perl::GIOP::RequestHeader_1_2__demarshal(\$message, \$offset, $endian); | ||
| 354 | my $interface = ${$request_header->{target}}[1]; | ||
| 355 | if (!exists $self->{itf}->{$interface}) { | ||
| 356 | warn "unknown interface '$interface'.\n"; | ||
| 357 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 358 | $reply_body = q{}; | ||
| 359 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/NO_IMPLEMENT:1.0"); | ||
| 360 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 11); | ||
| 361 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 362 | } | ||
| 363 | else { | ||
| 364 | my $classname = $self->{itf}->{$interface}; | ||
| 365 | my $op = $request_header->{operation}; | ||
| 366 | if (! $classname->can($op)) { | ||
| 367 | warn "unknown operation '$request_header->{operation}'.\n"; | ||
| 368 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 369 | $reply_body = q{}; | ||
| 370 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/BAD_OPERATION:1.0"); | ||
| 371 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 13); | ||
| 372 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 373 | } | ||
| 374 | else { | ||
| 375 | my $srv_op = 'srv_' . $op; | ||
| 376 | ($reply_status, $reply_body) = $classname->$srv_op(\$message, \$offset, $endian); | ||
| 377 | } | ||
| 378 | } | ||
| 379 | if (defined $reply_status) { # !oneway | ||
| 380 | my $msg = q{}; | ||
| 381 | my $reply = q{}; | ||
| 382 | CORBA::Perl::GIOP::ReplyHeader_1_2__marshal(\$reply, { | ||
| 383 | request_id => $request_header->{request_id}, | ||
| 384 | reply_status => $reply_status, | ||
| 385 | service_context => [], | ||
| 386 | } | ||
| 387 | ); | ||
| 388 | $reply .= $reply_body; | ||
| 389 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$msg, { | ||
| 390 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 391 | GIOP_version => { | ||
| 392 | major => 1, | ||
| 393 | minor => 2, | ||
| 394 | }, | ||
| 395 | flags => $flags, | ||
| 396 | message_type => 1, # Reply | ||
| 397 | message_size => length $reply | ||
| 398 | } | ||
| 399 | ); | ||
| 400 | $msg .= $reply; | ||
| 401 | $self->{all_reply} .= $msg; | ||
| 402 | } | ||
| 403 | $message = substr $message, 12+$message_size; | ||
| 404 | } | ||
| 405 | last unless ($more); | ||
| 406 | } | ||
| 407 | my $all_reply = $self->{all_reply}; | ||
| 408 | $self->{all_reply} = q{}; | ||
| 409 | return $all_reply; | ||
| 410 | } | ||
| 411 | |||
| 412 | package CORBA::Perl::MIOP; | ||
| 413 | |||
| 414 | use Carp; | ||
| 415 | |||
| 416 | sub Collect { | ||
| 417 | my (@msg_giop) = @_; | ||
| 418 | |||
| 419 | my $collection = q{}; | ||
| 420 | my $idx = 0; | ||
| 421 | my $nb = scalar(@msg_giop); | ||
| 422 | foreach my $msg (@msg_giop) { | ||
| 423 | CORBA::Perl::MIOP::PacketHeader_1_0__marshal(\$collection, { | ||
| 424 | magic => [ 'M', 'I', 'O', 'P' ], | ||
| 425 | hdr_version => 0x10, # version 1.0 | ||
| 426 | flags => 0x01 # little endian | ||
| 427 | | (($idx == $nb -1) ? 0x02 : 0x00), | ||
| 428 | packet_length => 2048, # MTU | ||
| 429 | packet_number => $idx, | ||
| 430 | number_of_packets => 0, # optional | ||
| 431 | id => q{} # empty sequence of octet | ||
| 432 | } | ||
| 433 | ); | ||
| 434 | $collection .= $msg; | ||
| 435 | $idx ++; | ||
| 436 | } | ||
| 437 | return $collection; | ||
| 438 | } | ||
| 439 | |||
| 440 | package CORBA::Perl::MIOP::Servant; | ||
| 441 | |||
| 442 | use Carp; | ||
| 443 | |||
| 444 | sub new { | ||
| 445 | my $proto = shift; | ||
| 446 | my $class = ref($proto) || $proto; | ||
| 447 | my $self = {}; | ||
| 448 | bless $self, $class; | ||
| 449 | $self->{itf} = {}; | ||
| 450 | return $self; | ||
| 451 | } | ||
| 452 | |||
| 453 | sub Register { | ||
| 454 | my $self = shift; | ||
| 455 | my ($interface, $classname) = @_; | ||
| 456 | |||
| 457 | $self->{itf}->{$interface} = $classname; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub Servant { | ||
| 461 | my $self = shift; | ||
| 462 | my ($collection) = @_; | ||
| 463 | |||
| 464 | my $endian = 0; | ||
| 465 | my $offset = 0; | ||
| 466 | my $last_packet; | ||
| 467 | my @list_reply; | ||
| 468 | while (1) { | ||
| 469 | my $magic = q{}; | ||
| 470 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$collection, \$offset, $endian); | ||
| 471 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$collection, \$offset, $endian); | ||
| 472 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$collection, \$offset, $endian); | ||
| 473 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$collection, \$offset, $endian); | ||
| 474 | my $version_hdr = CORBA::Perl::CORBA::octet__demarshal(\$collection, \$offset, $endian); | ||
| 475 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$collection, \$offset, $endian); | ||
| 476 | $endian = $flags & 0x01; # now, endian is known | ||
| 477 | $last_packet = $flags & 0x02; | ||
| 478 | my $packet_length = CORBA::Perl::CORBA::unsigned_short__demarshal(\$collection, \$offset, $endian); | ||
| 479 | my $packet_number = CORBA::Perl::CORBA::unsigned_long__demarshal(\$collection, \$offset, $endian); | ||
| 480 | my $number_of_packets = CORBA::Perl::CORBA::unsigned_short__demarshal(\$collection, \$offset, $endian); | ||
| 481 | my $id = CORBA::Perl::MIOP::UniqueId__demarshal(\$collection, \$offset, $endian); | ||
| 482 | |||
| 483 | if ( $magic eq 'MIOP' | ||
| 484 | and $version_hdr == 0x10 ) { | ||
| 485 | my $message_header = CORBA::Perl::GIOP::MessageHeader_1_2__demarshal(\$collection, \$offset, $endian); | ||
| 486 | my $GIOP_magic = join q{}, @{$message_header->{magic}}; | ||
| 487 | |||
| 488 | if ( $GIOP_magic eq 'GIOP' | ||
| 489 | and $message_header->{GIOP_version}->{major} == 1 | ||
| 490 | and $message_header->{GIOP_version}->{minor} == 2 ) { | ||
| 491 | if ( $message_header->{message_type} != 0 ) { | ||
| 492 | warn "no request messsage ($message_header->{type})"; | ||
| 493 | $offset += $message_header->{message_size}; | ||
| 494 | } | ||
| 495 | else { | ||
| 496 | my $request_header = CORBA::Perl::GIOP::RequestHeader_1_2__demarshal(\$collection, \$offset, $endian); | ||
| 497 | my $interface = ${$request_header->{target}}[1]; | ||
| 498 | if (!exists $self->{itf}->{$interface}) { | ||
| 499 | warn "unknown interface '$interface'.\n"; | ||
| 500 | $offset += $message_header->{message_size}; | ||
| 501 | } | ||
| 502 | else { | ||
| 503 | my $classname = $self->{itf}->{$interface}; | ||
| 504 | my $op = $request_header->{operation}; | ||
| 505 | if (! $classname->can($op)) { | ||
| 506 | warn "unknown operation '$request_header->{operation}'.\n"; | ||
| 507 | $offset += $message_header->{message_size}; | ||
| 508 | } | ||
| 509 | else { | ||
| 510 | my $demarshal_body = $op . '__demarshal_body'; | ||
| 511 | my @args = $classname->$demarshal_body(\$collection, \$offset, $endian); | ||
| 512 | my $reply = $classname->$op(@args); | ||
| 513 | push @list_reply, $reply if ($reply); | ||
| 514 | } | ||
| 515 | } | ||
| 516 | } | ||
| 517 | } | ||
| 518 | } | ||
| 519 | last if ($last_packet); | ||
| 520 | } | ||
| 521 | return CORBA::Perl::MIOP::Collect(@list_reply); | ||
| 522 | } | ||
| 523 | |||
| 524 | package CORBA::Perl::DIOP; | ||
| 525 | |||
| 526 | use Carp; | ||
| 527 | |||
| 528 | our $TRACE; | ||
| 529 | |||
| 530 | sub BEGIN { | ||
| 531 | $TRACE = 1; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub GetRequestId { | ||
| 535 | $CORBA::Perl::GIOP::request_id ++; | ||
| 536 | return $CORBA::Perl::GIOP::request_id; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub SendAckReply { | ||
| 540 | my ($sock, $rep_id) = @_; | ||
| 541 | my $buffer = q{}; | ||
| 542 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 543 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 544 | GIOP_version => { | ||
| 545 | major => 1, | ||
| 546 | minor => 2, | ||
| 547 | }, | ||
| 548 | flags => 0x01, # flags : little endian | ||
| 549 | message_type => 1 + 128, # Reply | Ack | ||
| 550 | message_size => 4 | ||
| 551 | } | ||
| 552 | ); | ||
| 553 | CORBA::Perl::CORBA::unsigned_long__marshal(\$buffer, $rep_id); | ||
| 554 | my $rc = $sock->send($buffer); | ||
| 555 | croak "send error with $sock ($!).\n" | ||
| 556 | unless (defined $rc); | ||
| 557 | # print "send ack $rep_id\n"; | ||
| 558 | } | ||
| 559 | |||
| 560 | sub SendRequest { | ||
| 561 | my ($sock, $request, $req_id) = @_; | ||
| 562 | REWRITE: | ||
| 563 | my $rc = $sock->send($request); | ||
| 564 | croak "send error with $sock ($!).\n" | ||
| 565 | unless (defined $rc); | ||
| 566 | # print "send request $req_id\n"; | ||
| 567 | my $ack; | ||
| 568 | REREAD: | ||
| 569 | $rc = $sock->recv($ack, 1024); | ||
| 570 | croak "header: recv error with $sock ($!).\n" | ||
| 571 | unless (defined $rc); | ||
| 572 | my $endian = 0; | ||
| 573 | my $offset = 0; | ||
| 574 | my $magic = q{}; | ||
| 575 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 576 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 577 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 578 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 579 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$ack, \$offset, $endian); | ||
| 580 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$ack, \$offset, $endian); | ||
| 581 | $endian = $flags & 0x01; # now, endian is known | ||
| 582 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$ack, \$offset, $endian); | ||
| 583 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$ack, \$offset, $endian); | ||
| 584 | |||
| 585 | if ( $magic eq 'GIOP' | ||
| 586 | and $GIOP_version->{major} == 1 | ||
| 587 | and $GIOP_version->{minor} == 2 ) { | ||
| 588 | if ($message_type != 0 + 128) { # Request | Ack | ||
| 589 | goto REREAD; | ||
| 590 | } | ||
| 591 | my $ack_id = CORBA::Perl::CORBA::unsigned_long__demarshal(\$ack, \$offset, $endian); | ||
| 592 | # print "recv ack $ack_id\n"; | ||
| 593 | if ($ack_id != $req_id) { | ||
| 594 | goto REWRITE; | ||
| 595 | } | ||
| 596 | } | ||
| 597 | else { | ||
| 598 | croak "bad header.\n"; | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | sub RequestOneWay { | ||
| 603 | my($sock, $request_header, $request_body) = @_; | ||
| 604 | $request_header->{request_id} = GetRequestId(); | ||
| 605 | my $request = q{}; | ||
| 606 | CORBA::Perl::GIOP::RequestHeader_1_2__marshal(\$request, $request_header); | ||
| 607 | $request .= $request_body; | ||
| 608 | my $buffer = q{}; | ||
| 609 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 610 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 611 | GIOP_version => { | ||
| 612 | major => 1, | ||
| 613 | minor => 2, | ||
| 614 | }, | ||
| 615 | flags => 0x01, # flags : little endian | ||
| 616 | message_type => 0, # Request | ||
| 617 | message_size => length $request | ||
| 618 | } | ||
| 619 | ); | ||
| 620 | $buffer .= $request; | ||
| 621 | SendRequest($sock, $buffer, $request_header->{request_id}); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub RequestReply { | ||
| 625 | my($sock, $request_header, $request_body) = @_; | ||
| 626 | $request_header->{request_id} = GetRequestId(); | ||
| 627 | # print "request id $request_id\n"; | ||
| 628 | my $request = q{}; | ||
| 629 | CORBA::Perl::GIOP::RequestHeader_1_2__marshal(\$request, $request_header); | ||
| 630 | $request .= $request_body; | ||
| 631 | my $buffer = q{}; | ||
| 632 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 633 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 634 | GIOP_version => { | ||
| 635 | major => 1, | ||
| 636 | minor => 2, | ||
| 637 | }, | ||
| 638 | flags => 0x01, # flags : little endian | ||
| 639 | message_type => 0, # Request | ||
| 640 | message_size => length $request | ||
| 641 | } | ||
| 642 | ); | ||
| 643 | $buffer .= $request; | ||
| 644 | SendRequest($sock, $buffer, $request_header->{request_id}); | ||
| 645 | my $reply; | ||
| 646 | RETRY: | ||
| 647 | my $rc = $sock->recv($reply, 1024); | ||
| 648 | croak "header: recv error with $sock ($!).\n" | ||
| 649 | unless (defined $rc); | ||
| 650 | my $endian = 0; | ||
| 651 | my $offset = 0; | ||
| 652 | my $magic = q{}; | ||
| 653 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$reply, \$offset, $endian); | ||
| 654 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$reply, \$offset, $endian); | ||
| 655 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$reply, \$offset, $endian); | ||
| 656 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$reply, \$offset, $endian); | ||
| 657 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$reply, \$offset, $endian); | ||
| 658 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$reply, \$offset, $endian); | ||
| 659 | $endian = $flags & 0x01; # now, endian is known | ||
| 660 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$reply, \$offset, $endian); | ||
| 661 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$reply, \$offset, $endian); | ||
| 662 | |||
| 663 | if ( $magic eq 'GIOP' | ||
| 664 | and $GIOP_version->{major} == 1 | ||
| 665 | and $GIOP_version->{minor} == 2 ) { | ||
| 666 | if ($message_type == 1) { | ||
| 667 | my $reply_header = CORBA::Perl::GIOP::ReplyHeader_1_2__demarshal(\$reply, \$offset, $endian); | ||
| 668 | # print "recv reply $reply_header->{request_id}\n"; | ||
| 669 | SendAckReply($sock, $reply_header->{request_id}); | ||
| 670 | my $reply_body = substr $reply, $offset; | ||
| 671 | if ($request_header->{request_id} == $reply_header->{request_id}) { | ||
| 672 | return ($reply_header->{reply_status}, $reply_header->{service_context}, $reply_body, $endian); | ||
| 673 | } | ||
| 674 | elsif ($request_id > $reply_header->{request_id}) { | ||
| 675 | warn "bad request id $reply_header->{request_id} (waiting $request_header->{request_id}).\n"; | ||
| 676 | goto RETRY; | ||
| 677 | } | ||
| 678 | else { | ||
| 679 | croak "bad request id $reply_header->{request_id} (waiting $request_header->{request_id}).\n"; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | else { | ||
| 683 | goto RETRY; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | else { | ||
| 687 | croak "bad header.\n"; | ||
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | package CORBA::Perl::DIOP::Servant; | ||
| 692 | |||
| 693 | use Carp; | ||
| 694 | |||
| 695 | sub new { | ||
| 696 | my $proto = shift; | ||
| 697 | my $class = ref($proto) || $proto; | ||
| 698 | my $self = {}; | ||
| 699 | bless $self, $class; | ||
| 700 | $self->{itf} = {}; | ||
| 701 | return $self; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub Register { | ||
| 705 | my $self = shift; | ||
| 706 | my ($interface, $classname) = @_; | ||
| 707 | |||
| 708 | $self->{itf}->{$interface} = $classname; | ||
| 709 | } | ||
| 710 | |||
| 711 | sub SendAckRequest { | ||
| 712 | my ($sock, $req_id) = @_; | ||
| 713 | my $buffer = q{}; | ||
| 714 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 715 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 716 | GIOP_version => { | ||
| 717 | major => 1, | ||
| 718 | minor => 2, | ||
| 719 | }, | ||
| 720 | flags => 0x01, # flags : little endian | ||
| 721 | message_type => 0 + 128, # Request | Ack | ||
| 722 | message_size => 4 | ||
| 723 | } | ||
| 724 | ); | ||
| 725 | CORBA::Perl::CORBA::unsigned_long__marshal(\$buffer, $req_id); | ||
| 726 | my $rc = $sock->send($buffer); | ||
| 727 | croak "send error with $sock ($!).\n" | ||
| 728 | unless (defined $rc); | ||
| 729 | # print "send ack $req_id\n"; | ||
| 730 | } | ||
| 731 | |||
| 732 | sub SendReply { | ||
| 733 | my ($sock, $reply, $rep_id) = @_; | ||
| 734 | REWRITE: | ||
| 735 | my $rc = $sock->send($reply); | ||
| 736 | croak "send error with $sock ($!).\n" | ||
| 737 | unless (defined $rc); | ||
| 738 | my $ack; | ||
| 739 | REREAD: | ||
| 740 | $rc = $sock->recv($ack, 1024); | ||
| 741 | croak "header: recv error with $sock ($!).\n" | ||
| 742 | unless (defined $rc); | ||
| 743 | # print "send reply $rep_id\n"; | ||
| 744 | my $endian = 0; | ||
| 745 | my $offset = 0; | ||
| 746 | my $magic = q{}; | ||
| 747 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 748 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 749 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 750 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$ack, \$offset, $endian); | ||
| 751 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$ack, \$offset, $endian); | ||
| 752 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$ack, \$offset, $endian); | ||
| 753 | $endian = $flags & 0x01; # now, endian is known | ||
| 754 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$ack, \$offset, $endian); | ||
| 755 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$ack, \$offset, $endian); | ||
| 756 | |||
| 757 | if ( $magic eq 'GIOP' | ||
| 758 | and $GIOP_version->{major} == 1 | ||
| 759 | and $GIOP_version->{minor} == 2 ) { | ||
| 760 | if ($message_type != 1 + 128) { # Reply | Ack | ||
| 761 | goto REREAD; | ||
| 762 | } | ||
| 763 | my $ack_id = CORBA::Perl::CORBA::unsigned_long__demarshal(\$ack, \$offset, $endian); | ||
| 764 | # print "recv ack $ack_id\n"; | ||
| 765 | if ($ack_id != $rep_id) { | ||
| 766 | goto REWRITE; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | else { | ||
| 770 | croak "bad header.\n"; | ||
| 771 | } | ||
| 772 | } | ||
| 773 | |||
| 774 | sub Run { | ||
| 775 | my $self = shift; | ||
| 776 | my ($sock) = @_; | ||
| 777 | |||
| 778 | my $message; | ||
| 779 | print "waiting first data ...\n"; | ||
| 780 | while (1) { | ||
| 781 | $sock->recv($message, 1024); | ||
| 782 | my $endian = 0; | ||
| 783 | my $offset = 0; | ||
| 784 | my $magic = q{}; | ||
| 785 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 786 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 787 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 788 | $magic .= CORBA::Perl::CORBA::char__demarshal(\$message, \$offset, $endian); | ||
| 789 | my $GIOP_version = CORBA::Perl::GIOP::Version__demarshal(\$message, \$offset, $endian); | ||
| 790 | my $flags = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 791 | $endian = $flags & 0x01; # now, endian is known | ||
| 792 | my $message_type = CORBA::Perl::CORBA::octet__demarshal(\$message, \$offset, $endian); | ||
| 793 | my $message_size = CORBA::Perl::CORBA::unsigned_long__demarshal(\$message, \$offset, $endian); | ||
| 794 | |||
| 795 | if ( $magic eq 'GIOP' | ||
| 796 | and $GIOP_version->{major} == 1 | ||
| 797 | and $GIOP_version->{minor} == 2 | ||
| 798 | and $message_type == 0 ) { | ||
| 799 | |||
| 800 | my ($reply_status, $reply_body); | ||
| 801 | my $request_header = CORBA::Perl::GIOP::RequestHeader_1_2__demarshal(\$message, \$offset, $endian); | ||
| 802 | # print "recv request $request_header->{request_id}\n"; | ||
| 803 | SendAckRequest($sock, $request_header->{request_id}); | ||
| 804 | my $interface = ${$request_header->{target}}[1]; | ||
| 805 | if (!exists $self->{itf}->{$interface}) { | ||
| 806 | warn "unknown interface '$interface'.\n"; | ||
| 807 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 808 | $reply_body = q{}; | ||
| 809 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/NO_IMPLEMENT:1.0"); | ||
| 810 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 11); | ||
| 811 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 812 | } | ||
| 813 | else { | ||
| 814 | my $classname = $self->{itf}->{$interface}; | ||
| 815 | my $op = $request_header->{operation}; | ||
| 816 | if (! $classname->can($op)) { | ||
| 817 | warn "unknown operation '$request_header->{operation}'.\n"; | ||
| 818 | $reply_status = CORBA::Perl::CORBA::SYSTEM_EXCEPTION; | ||
| 819 | $reply_body = q{}; | ||
| 820 | CORBA::Perl::CORBA::string__marshal(\$reply_body, "IDL:CORBA/BAD_OPERATION:1.0"); | ||
| 821 | CORBA::Perl::CORBA::unsigned_long__marshal(\$reply_body, 13); | ||
| 822 | CORBA::Perl::CORBA::completion_status__marshal(\$reply_body, CORBA::COMPLETED_NO()); | ||
| 823 | } | ||
| 824 | else { | ||
| 825 | my $srv_op = 'srv_' . $op; | ||
| 826 | ($reply_status, $reply_body) = $classname->$srv_op(\$message, \$offset, $endian); | ||
| 827 | return undef unless ($reply_status); # oneway | ||
| 828 | } | ||
| 829 | } | ||
| 830 | my $reply = q{}; | ||
| 831 | CORBA::Perl::GIOP::ReplyHeader_1_2__marshal(\$reply, { | ||
| 832 | request_id => $request_header->{request_id}, | ||
| 833 | reply_status => $reply_status, | ||
| 834 | service_context => [], | ||
| 835 | } | ||
| 836 | ); | ||
| 837 | $reply .= $reply_body; | ||
| 838 | my $buffer = q{}; | ||
| 839 | CORBA::Perl::GIOP::MessageHeader_1_2__marshal(\$buffer, { | ||
| 840 | magic => [ 'G', 'I', 'O', 'P' ], | ||
| 841 | GIOP_version => { | ||
| 842 | major => 1, | ||
| 843 | minor => 2, | ||
| 844 | }, | ||
| 845 | flags => 0x01, # flags : little endian | ||
| 846 | message_type => 1, # Reply | ||
| 847 | message_size => length $reply | ||
| 848 | } | ||
| 849 | ); | ||
| 850 | $buffer .= $reply; | ||
| 851 | SendReply($sock, $buffer, $request_header->{request_id}); | ||
| 852 | } | ||
| 853 | } | ||
| 854 | } | ||
| 855 | |||
| 856 | 1; | ||
| 857 |