Perl/ClientVisitor.pm untested

filter: Statementstmt 0.0% 0 / 399 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 501 SCAR 71.4
CC
35
Cov
0%
visitOperation
702.0
visitRegularInterface
30.0
visitAbstractInterface
20.0
CRAP 1260.0
1
2 #
3 # Interface Definition Language (OMG IDL CORBA v3.0)
4 #
5
6 package CORBA::Perl::ClientVisitor;
7
8 use strict;
9 use warnings;
10
11 our $VERSION = '0.40';
12
13 use CORBA::Perl::CdrVisitor;
14 use base qw(CORBA::Perl::CdrVisitor);
15
16 use File::Basename;
17 use POSIX qw(ctime);
18
19 # needs $node->{pl_name} $node->{pl_package} (PerlNameVisitor)
20
21 sub new {
22 my $proto = shift;
23 my $class = ref($proto) || $proto;
24 my $self = {};
25 bless $self, $class;
26 my($parser) = @_;
27 $self->{srcname} = $parser->YYData->{srcname};
28 $self->{srcname_size} = $parser->YYData->{srcname_size};
29 $self->{srcname_mtime} = $parser->YYData->{srcname_mtime};
30 $self->{symbtab} = $parser->YYData->{symbtab};
31 $self->{client} = 1;
32 $self->{miop} = 0;
33 $self->{use} = {};
34 if (exists $parser->YYData->{opt_J}) {
35 $self->{path_use} = $parser->YYData->{opt_J};
36 $self->{path_use} =~ s/\//::/g;
37 $self->{path_use} .= '::';
38 }
39 else {
40 $self->{path_use} = q{};
41 }
42 my $filename = basename($self->{srcname}, '.idl') . '.pm';
43 $self->open_stream($filename);
44 $self->{done_hash} = {};
45 $self->{num_key} = 'num_pl_cli';
46 $self->{pkg_modif} = 0;
47 $self->{stringify} = 1;
48 $self->{id} = 1;
49 return $self;
50 }
51
52 #
53 # 3.5 OMG IDL Specification
54 #
55
56 sub visitSpecification {
57 my $self = shift;
58 my($node) = @_;
59 my $FH = $self->{out};
60 $self->{pkg_modif} = 0;
61 print $FH "# This file was generated (by ",$0,"). DO NOT modify it.\n";
62 print $FH "# From file : ",$self->{srcname},", ",$self->{srcname_size}," octets, ",POSIX::ctime($self->{srcname_mtime});
63 print $FH "\n";
64 print $FH "use strict;\n";
65 print $FH "\n";
66 print $FH "package main;\n";
67 print $FH "\n";
68 print $FH "use CORBA::Perl::rpc_corba;\n";
69 print $FH "use Carp;\n";
70 print $FH "\n";
71 foreach (@{$node->{list_decl}}) {
72 $self->_get_defn($_)->visit($self);
73 if ($self->{pkg_modif}) {
74 $self->{pkg_modif} = 0;
75 print $FH "package main;\n";
76 print $FH "\n";
77 }
78 }
79 print $FH "1;\n";
80 print $FH "\n";
81 print $FH "# end of file : ",$self->{filename},"\n";
82 close $FH;
83 }
84
85 #
86 # 3.7 Module Declaration (inherited)
87 #
88
89 #
90 # 3.8 Interface Declaration
91 #
92
93 sub visitRegularInterface {
94 my $self = shift;
95 my($node) = @_;
96 if ($self->{srcname} eq $node->{filename}) {
97 my $FH = $self->{out};
98 $self->{pkg_modif} = 0;
99 print $FH "#\n";
100 print $FH "# begin of interface ",$node->{pl_package},"\n";
101 print $FH "#\n";
102 print $FH "\n";
103 print $FH "package ",$node->{pl_package},";\n";
104 print $FH "\n";
105 print $FH "use CORBA::Perl::CORBA;\n";
106 print $FH "use Carp;\n";
107 print $FH "\n";
108 foreach (@{$node->{list_decl}}) {
109 my $defn = $self->_get_defn($_);
110 if ( $defn->isa('Operation')
111 or $defn->isa('Attributes') ) {
112 next;
113 }
114 $defn->visit($self);
115 if ($self->{pkg_modif}) {
116 $self->{pkg_modif} = 0;
117 print $FH "package ",$node->{pl_package},";\n";
118 print $FH "\n";
119 }
120 }
121 print $FH "\n";
122 if (keys %{$node->{hash_attribute_operation}}) {
123 $self->{itf} = $node->{pl_name};
124 $self->{repos_id} = $node->{repos_id};
125 print $FH "###### methodes\n";
126 print $FH "\n";
127 print $FH "# constructor\n";
128 print $FH "sub new {\n";
129 print $FH "\tmy \$proto = shift;\n";
130 print $FH "\tmy \$class = ref(\$proto) || \$proto;\n";
131 print $FH "\tmy \$self = {};\n";
132 print $FH "\tbless \$self, \$class;\n";
133 print $FH "\tmy(\$sock) = \@_;\n";
134 print $FH "\tcroak \"undefined parameter 'sock' in '",$node->{idf},"'.\\n\"\n";
135 print $FH "\t\t\tunless (defined \$sock);\n";
136 print $FH "\tcroak \"invalid parameter 'sock' in '",$node->{idf},"'.\\n\"\n";
137 print $FH "\t\t\tunless (ref \$sock eq 'IO::Socket::INET');\n";
138 print $FH "\t\$self->{sock} = \$sock;\n";
139 print $FH "\t\$self->{trace} = sub { print \@_ };\n";
140 print $FH "\treturn \$self;\n";
141 print $FH "}\n";
142 print $FH "\n";
143 print $FH "sub ",$node->{pl_name},"__id () {\n";
144 print $FH "\t\treturn \"",$node->{repos_id},"\";\n";
145 print $FH "}\n";
146 print $FH "\n";
147 print $FH "use Error qw(:try);\n";
148 print $FH "\n";
149 foreach (values %{$node->{hash_attribute_operation}}) {
150 $self->_get_defn($_)->visit($self);
151 }
152 print $FH "\n";
153 }
154 print $FH "#\n";
155 print $FH "# end of interface ",$node->{pl_package},"\n";
156 print $FH "#\n";
157 print $FH "\n";
158 $self->{pkg_modif} = 1;
159 }
160 else {
161 $self->_insert_use($node->{filename});
162 }
163 }
164
165 sub visitAbstractInterface {
166 my $self = shift;
167 my($node) = @_;
168 if ($self->{srcname} eq $node->{filename}) {
169 my $FH = $self->{out};
170 $self->{pkg_modif} = 0;
171 print $FH "#\n";
172 print $FH "# begin of abstract interface ",$node->{pl_package},"\n";
173 print $FH "#\n";
174 print $FH "\n";
175 print $FH "package ",$node->{pl_package},";\n";
176 print $FH "\n";
177 print $FH "use CORBA::Perl::CORBA;\n";
178 print $FH "use Carp;\n";
179 print $FH "\n";
180 foreach (@{$node->{list_decl}}) {
181 my $defn = $self->_get_defn($_);
182 if ( $defn->isa('Operation')
183 or $defn->isa('Attributes') ) {
184 next;
185 }
186 $defn->visit($self);
187 if ($self->{pkg_modif}) {
188 $self->{pkg_modif} = 0;
189 print $FH "package ",$node->{pl_package},";\n";
190 print $FH "\n";
191 }
192 }
193 print $FH "\n";
194 print $FH "#\n";
195 print $FH "# end of abstract interface ",$node->{pl_package},"\n";
196 print $FH "#\n";
197 print $FH "\n";
198 $self->{pkg_modif} = 1;
199 }
200 else {
201 $self->_insert_use($node->{filename});
202 }
203 }
204
205 #
206 # 3.9 Value Declaration (inherited)
207 #
208
209 #
210 # 3.10 Constant Declaration (inherited)
211 #
212
213 #
214 # 3.11 Type Declaration (inherited)
215 #
216
217 #
218 # 3.12 Exception Declaration (inherited)
219 #
220
221 #
222 # 3.13 Operation Declaration
223 #
224
225 sub visitOperation {
226 my $self = shift;
227 my($node) = @_;
228 my $type = $self->_get_defn($node->{type});
229 my $FH = $self->{out};
230 my $r_inout = q{};
231 foreach (@{$node->{list_param}}) { # paramater
232 if ($_->{attr} eq 'inout') {
233 $r_inout .= ", \$r_" . $_->{pl_name};
234 }
235 }
236 print $FH "# ",$self->{itf},"::",$node->{pl_name},"\n";
237 print $FH "sub ",$node->{pl_name}," {\n";
238 print $FH "\tmy \$self = shift;\n";
239 print $FH "\n";
240 print $FH "\tmy (\$_request_header, \$_buffer",$r_inout,") = \$self->",$node->{pl_name},"__marshal_request(\@_);\n";
241 if (exists $node->{modifier}) { # oneway
242 print $FH "\tGIOP::RequestOneWay(\$self->{sock},\$_request_header,\$_buffer);\t\t# oneway\n";
243 }
244 else {
245 print $FH "\tmy(\$_status, \$_service_context, \$_reply, \$_endian) = GIOP::RequestReply(\$self->{sock},\$_request_header,\$_buffer);\n";
246 print $FH "\tmy \$_offset = 0;\n";
247 print $FH "\treturn \$self->",$node->{pl_name},"__demarshal_reply(\$_status, \$_service_context, \\\$_reply, \\\$_offset, \$_endian",$r_inout,");\n";
248 }
249 print $FH "}\n";
250 print $FH "\n";
251 if (exists $self->{miop} and exists $node->{modifier}) {
252 print $FH "sub ",$node->{pl_name},"__marshal {\n";
253 foreach (@{$node->{list_param}}) { # paramater
254 print $FH "\tmy \$",$_->{pl_name}," = shift;\n";
255 print $FH "\tcroak \"undefined parameter '",$_->{pl_name},"' in '",$node->{pl_name},"'.\\n\"\n";
256 print $FH "\t\t\tunless (defined \$",$_->{pl_name},");\n";
257 }
258 print $FH "\n";
259 print $FH "\tmy \$_request = q{};\n";
260 print $FH "\tmy \$_request_header = {\n";
261 print $FH "\t\t\trequest_id => GIOP::GetRequestId(),\n";
262 print $FH "\t\t\tresponse_flags => 0, # NONE\n";
263 print $FH "\t\t\treserved => [0, 0, 0],\n";
264 print $FH "\t\t\ttarget => [GIOP::KeyAddr(), \"",$self->{repos_id},"\"],\n";
265 print $FH "\t\t\toperation => \"",$node->{idf},"\",\n";
266 print $FH "\t\t\tservice_context => [] # empty sequence\n";
267 print $FH "\t};\n";
268 print $FH "\tGIOP::RequestHeader_1_2__marshal(\\\$_request, \$_request_header);\n";
269 print $FH "\t# body\n";
270 foreach (@{$node->{list_param}}) { # paramater
271 my $type = $self->_get_defn($_->{type});
272 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal";
273 print $FH "(\\\$_request,\$",$_->{pl_name},");\n";
274 }
275 print $FH "\n";
276 print $FH "\tmy \$_message = q{};\n";
277 print $FH "\tmy \$_message_header = {\n";
278 print $FH "\t\t\tmagic => [ 'G', 'I', 'O', 'P' ],\n";
279 print $FH "\t\t\tGIOP_version => {\n";
280 print $FH "\t\t\t\t\tmajor => 1,\n";
281 print $FH "\t\t\t\t\tminor => 2,\n";
282 print $FH "\t\t\t},\n";
283 print $FH "\t\t\tflags => 0x01, # flags : little endian\n";
284 print $FH "\t\t\tmessage_type => 0, # Request\n";
285 print $FH "\t\t\tmessage_size => length(\$_request)\n";
286 print $FH "\t};\n";
287 print $FH "\tGIOP::MessageHeader_1_2__marshal(\\\$_message, \$_message_header);\n";
288 print $FH "\t\$_message .= \$_request;\n";
289 print $FH "\n";
290 print $FH "\treturn \$_message;\n";
291 print $FH "}\n";
292 print $FH "\n";
293 print $FH "sub ",$node->{pl_name},"__demarshal_body {\n";
294 print $FH "\tmy \$self = shift;\n";
295 print $FH "\tmy (\$r_buffer,\$r_offset,\$endian) = \@_;\n";
296 print $FH "\tmy \@_parameters = ();\n";
297 print $FH "\n";
298 foreach (@{$node->{list_param}}) { # paramater
299 my $type = $self->_get_defn($_->{type});
300 print $FH "\tpush \@_parameters, ",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer,\$r_offset,\$endian);\n";
301 }
302 print $FH "\n";
303 print $FH "\treturn \@_parameters;\n";
304 print $FH "}\n";
305 print $FH "\n";
306 }
307 print $FH "sub ",$node->{pl_name},"__marshal_request {\n";
308 print $FH "\tmy \$self = shift;\n";
309 print $FH "\n";
310 foreach (@{$node->{list_param}}) { # paramater
311 if ($_->{attr} eq 'in') {
312 print $FH "\tmy \$",$_->{pl_name}," = shift;\n";
313 print $FH "\tcroak \"undefined parameter '",$_->{pl_name},"' in '",$node->{pl_name},"'.\\n\"\n";
314 print $FH "\t\t\tunless (defined \$",$_->{pl_name},");\n";
315 }
316 if ($_->{attr} eq 'inout') {
317 print $FH "\tmy \$r_",$_->{pl_name}," = shift;\n";
318 print $FH "\tcroak \"undefined parameter '",$_->{pl_name},"' in '",$node->{pl_name},"'.\\n\"\n";
319 print $FH "\t\t\tunless (defined \$r_",$_->{pl_name},");\n";
320 }
321 }
322 print $FH "\n";
323 print $FH "\tmy \$_request_header = {\n";
324 print $FH "\t\t\trequest_id => 0, # overloaded by GIOP::Request* \n";
325 if (exists $node->{modifier}) { # oneway
326 print $FH "\t\t\tresponse_flags => 0, # NONE\n";
327 }
328 else {
329 print $FH "\t\t\tresponse_flags => 3, # WITH_TARGET\n";
330 }
331 print $FH "\t\t\treserved => [0, 0, 0],\n";
332 print $FH "\t\t\ttarget => [GIOP::KeyAddr(), \"",$self->{repos_id},"\"],\n";
333 print $FH "\t\t\toperation => \"",$node->{idf},"\",\n";
334 print $FH "\t\t\tservice_context => [] # empty sequence\n";
335 print $FH "\t};\n";
336 print $FH "\tmy \$_request_body = q{};\n";
337 foreach (@{$node->{list_param}}) { # paramater
338 my $type = $self->_get_defn($_->{type});
339 if ($_->{attr} eq 'in') {
340 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal";
341 print $FH "(\\\$_request_body,\$",$_->{pl_name},");\n";
342 }
343 elsif ($_->{attr} eq 'inout') {
344 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal";
345 print $FH "(\\\$_request_body,\${\$r_",$_->{pl_name},"});\n";
346 }
347 }
348 print $FH "\treturn (\$_request_header,\$_request_body",$r_inout,");\n";
349 print $FH "};\n";
350 print $FH "\n";
351 unless (exists $node->{modifier}) { # !oneway
352 print $FH "sub ",$node->{pl_name},"__demarshal_reply {\n";
353 print $FH "\tmy \$self = shift;\n";
354 print $FH "\n";
355 print $FH "\tmy(\$_status, \$_service_context, \$_reply, \$_offset, \$_endian",$r_inout,") = \@_;\n";
356 print $FH "\tif (\$_status eq CORBA::NO_EXCEPTION) {\n";
357 my $nb = 0;
358 my $type = $self->_get_defn($node->{type});
359 unless ($type->isa('VoidType')) {
360 print $FH "\t\tmy \$_return = ";
361 print $FH $type->{pl_package},"::",$type->{pl_name};
362 print $FH "__demarshal(\$_reply,\$_offset,\$_endian);\n";
363 $nb ++;
364 }
365 foreach (@{$node->{list_param}}) { # paramater
366 $type = $self->_get_defn($_->{type});
367 if ( $_->{attr} eq 'inout'
368 or $_->{attr} eq 'out' ) {
369 print $FH "\t\tmy \$",$_->{pl_name}," = ";
370 print $FH $type->{pl_package},"::",$type->{pl_name};
371 print $FH "__demarshal(\$_reply,\$_offset,\$_endian);\n";
372 $nb ++ if ($_->{attr} eq 'out');
373 }
374 }
375 foreach (@{$node->{list_param}}) { # paramater
376 if ($_->{attr} eq 'inout') {
377 print $FH "\t\t\${\$r_",$_->{pl_name},"} = \$",$_->{pl_name},";\n";
378 }
379 }
380 print $FH "\t\treturn";
381 print $FH " " if ($nb > 0);
382 print $FH "(" if ($nb > 1);
383 my $first = 1;
384 $type = $self->_get_defn($node->{type});
385 unless ($type->isa('VoidType')) {
386 print $FH "\$_return";
387 $first = 0;
388 }
389 foreach (@{$node->{list_param}}) { # paramater
390 if ($_->{attr} eq 'out') {
391 print $FH ", " unless ($first);
392 print $FH "\$",$_->{pl_name};
393 $first = 0;
394 }
395 }
396 print $FH ")" if ($nb > 1);
397 print $FH ";\n";
398 print $FH "\t}\n";
399 print $FH "\telsif (\$_status eq CORBA::USER_EXCEPTION) {\n";
400 print $FH "\t\tmy \$_exception_id = CORBA::string__demarshal(\$_reply,\$_offset,\$_endian);\n";
401 print $FH "\t\tif (0) {\n";
402 foreach (@{$node->{list_raise}}) {
403 my $defn = $self->_get_defn($_);
404 print $FH "\t\t}\n";
405 print $FH "\t\telsif (\$_exception_id eq \"",$defn->{repos_id},"\") {\n";
406 print $FH "\t\t\tmy \$_value = ";
407 print $FH $defn->{pl_package},"::",$defn->{pl_name};
408 print $FH "__demarshal(\$_reply,\$_offset,\$_endian);\n";
409 print $FH "\t\t\t\$self->{trace}(CORBA::USER_EXCEPTION,\" \$_exception_id.\\n\");\n";
410 print $FH "\t\t\tthrow ",$defn->{pl_package},"::",$defn->{pl_name},"(\n";
411 print $FH "\t\t\t\t\t_repos_id => \$_exception_id,\n";
412 print $FH "\t\t\t\t\t\%{\$_value}\n";
413 print $FH "\t\t\t);\n";
414 }
415 print $FH "\t\t}\n";
416 print $FH "\t\telse {\n";
417 print $FH "\t\t\twarn \"unknown user exception \$_exception_id.\\n\";\n";
418 print $FH "\t\t}\n";
419 print $FH "\t}\n";
420 print $FH "\telsif (\$_status eq CORBA::SYSTEM_EXCEPTION) {\n";
421 print $FH "\t\tmy \$_exception_id = CORBA::string__demarshal(\$_reply,\$_offset,\$_endian);\n";
422 print $FH "\t\tmy \$_minor_code_value = CORBA::unsigned_long__demarshal(\$_reply,\$_offset,\$_endian);\n";
423 print $FH "\t\tmy \$_completion_status = CORBA::completion_status__demarshal(\$_reply,\$_offset,\$_endian);\n";
424 print $FH "\t\t\$self->{trace}(CORBA::SYSTEM_EXCEPTION,\" \$_exception_id.\\n\");\n";
425 print $FH "\t\tthrow CORBA::SystemException(\n";
426 print $FH "\t\t\t\t_repos_id => \$_exception_id,\n";
427 print $FH "\t\t\t\tminor => \$_minor_code_value,\n";
428 print $FH "\t\t\t\tcompleted => \$_completion_status\n";
429 print $FH "\t\t);\n";
430 print $FH "\t}\n";
431 print $FH "\telse {\n";
432 print $FH "\t\twarn \"reply status \$_status.\\n\";\n";
433 print $FH "\t}\n";
434 print $FH "}\n";
435 print $FH "\n";
436 }
437
438 print $FH "sub srv_",$node->{pl_name}," {\n";
439 print $FH "\tmy \$self = shift;\n";
440 print $FH "\tmy (\$r_buffer,\$r_offset,\$endian) = \@_;\n";
441 print $FH "\tmy \@_parameters = ();\n";
442 print $FH "\n";
443 foreach (@{$node->{list_param}}) { # paramater
444 my $type = $self->_get_defn($_->{type});
445 if ($_->{attr} eq 'in') {
446 print $FH "\tpush \@_parameters, ",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer,\$r_offset,\$endian);\n";
447 }
448 elsif ($_->{attr} eq 'inout') {
449 print $FH "\tpush \@_parameters, ",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer,\$r_offset,\$endian);\n";
450 }
451 }
452 print $FH "\n";
453 if (exists $node->{modifier}) { # oneway
454 print $FH "\t\$self->",$node->{pl_name},"(\@_parameters);\n";
455 print $FH "\treturn undef;\n";
456 }
457 else {
458 print $FH "\tmy \$reply_body = q{};\n";
459 print $FH "\ttry {\n";
460 print $FH "\t\tmy \@result = (\$self->",$node->{pl_name},"(\@_parameters));\n";
461 unless ($type->isa('VoidType')) {
462 print $FH "\t\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\\\$reply_body, shift \@result);\n";
463 }
464 foreach (@{$node->{list_param}}) { # parameter
465 if ( $_->{attr} eq 'inout'
466 or $_->{attr} eq 'out' ) {
467 $type = $self->_get_defn($_->{type});
468 print $FH "\t\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\\\$reply_body, shift \@result);\n";
469 }
470 }
471 print $FH "\t\treturn (CORBA::NO_EXCEPTION, \$reply_body);\n";
472 print $FH "\t}\n";
473 foreach (@{$node->{list_raise}}) {
474 my $defn = $self->_get_defn($_);
475 print $FH "\tcatch ",$defn->{pl_package},"::",$defn->{pl_name}," with {\n";
476 print $FH "\t\tCORBA::string__marshal(\\\$reply_body,",$defn->{pl_package},"::",$defn->{pl_name},"__id());\n";
477 if (exists $defn->{list_expr}) {
478 print $FH "\t\tmy \$E = shift;\n";
479 }
480 print $FH "\t\treturn (CORBA::USER_EXCEPTION, \$reply_body);\n";
481 print $FH "\t}\n";
482 }
483 print $FH "\tcatch CORBA::Exception with {\n";
484 print $FH "\t\tmy \$E = shift;\n";
485 print $FH "\t\tCORBA::string__marshal(\\\$reply_body,\$E->{exception_id});\n";
486 print $FH "\t\tCORBA::unsigned_long__marshal(\\\$reply_body,\$E->{minor_code_value});\n";
487 print $FH "\t\tCORBA::completion_status__marshal(\\\$reply_body,\$E->{completion_status});\n";
488 print $FH "\t\treturn (CORBA::SYSTEM_EXCEPTION, \$reply_body);\n";
489 print $FH "\t};\n";
490 }
491 print $FH "}\n";
492 print $FH "\n";
493 }
494
495 #
496 # 3.14 Attribute Declaration (inherited)
497 #
498
499 1;
500