Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2702_Regression / fakeserver2.pl
blob13d0a9a04ef134d0fdad6cb342b57b648adf76da
1 #!/usr/bin/env perl
3 use Socket;
4 use IO::Socket;
6 $filebits = '';
8 OpenServer();
10 my $rout;
11 while( 1 ) {
12 print STDERR "fakeserver: Loop\n";
14 select( undef, undef, undef, 1 );
16 ($n, $left) = select( $rout = $filebits, undef, undef, 15 ); # Time out after 15 seconds
17 if ($n == 0) {
18 print STDERR "fakeserver: Terminating normally\n";
19 exit 0;
22 my $routs = unpack("b*", $rout);
23 print STDERR "fakeserver: Select $routs\n";
24 my $pos = index( $routs,'1');
25 while ( $pos >= 0 ) {
26 HandleFile( $pos );
27 $pos = index( $routs,'1', $pos+1);
31 sub SendMessage {
32 local( $message ) = @_;
34 print STDERR "fakeserver: SendMessage $message\n";
35 $message .= "\r\n";
37 foreach $fileno (keys %connections) {
38 if ( $connections{$fileno} ) {
39 my $client = $connections{$fileno}{client};
40 print $client $message;
46 sub HandleFile {
47 local( $fileno ) = @_;
49 print STDERR "fakeserver: HandleFile $fileno\n";
50 if ( $fileno == $server_fileno ) {
51 HandleServer();
52 } elsif ( $connections{$fileno} ) {
53 HandleClient( $fileno );
54 } else {
55 print STDERR "fakeserver: Weird fileno $fileno\n";
59 sub HandleServer {
60 my $client = $server->accept();
62 print STDERR "fakeserver: HandleServer\n";
64 if ( $client ) {
65 my $fileno = fileno($client);
66 $client->blocking(0);
67 $connections{$fileno}{client} = $client;
68 $connections{$fileno}{loggedin} = 0;
69 vec($filebits,$fileno,1) = 1;
70 # print $client "Welcome $fileno\r\n";
71 # my $n = send($client, $closeconnectionmessage, 0);
72 print STDERR "fakeserver: New client on $fileno\n";
73 # SendMessage( "New Client" );
74 } else {
75 print STDERR "fakeserver: No accept for server, reopen\n";
76 CloseServer();
77 OpenServer();
81 sub HandleClient {
82 local( $fileno ) = @_;
84 print STDERR "fakeserver: HandleClient $fileno\n";
85 $n = recv( $connections{$fileno}{client}, $receive, 200, 0 );
86 $rlen = length($receive);
87 if ( $receive ) {
88 if ($receive =~ /^GIOP/) {
89 print STDERR "fakeserver: Got $rlen byte GIOP message from client $fileno\n";
91 else {
92 print STDERR "fakeserver: Got $rlen bytes from client $fileno\n";
94 $connections{$fileno}{receive} = $receive;
95 $slen = length($closeconnectionmessage);
96 print STDERR "fakeserver: Sending $slen bytes GIOP closeconnection to $fileno\n";
97 sleep(3);
98 $n = send($connections{$fileno}{client}, $closeconnectionmessage, 0);
99 print STDERR "fakeserver: Sent $n bytes GIOP closeconnection to $fileno\n";
100 } else {
101 print STDERR "fakeserver: Close client $fileno\n";
102 vec($filebits,$fileno,1) = 0;
103 $connections{$fileno}{client}->close();
104 undef $connections{$fileno};
105 SendMessage( "Close Client" );
110 sub CloseServer {
111 vec($filebits,$server_fileno,1) = 0;
112 $server->close();
113 undef $server;
116 sub OpenServer {
118 $server = IO::Socket::INET->new(Listen => 5,
119 LocalPort => 1192,
120 Reuse => 1,
121 ReuseAddr => 1,
122 Timeout => 0,
123 Proto => 'tcp');
125 die "Could not create socket $!" unless $server;
127 $server->blocking(0);
128 $server_fileno = fileno($server);
129 vec($filebits,$server_fileno,1) = 1;
131 print STDERR "fakeserver: Starting $server_fileno\n";
133 open (DATA, 'closeconnection.dat') or die "couldn't open closeconnection.dat: $!\n";
134 my $n = read(DATA, $closeconnectionmessage, 100);
135 close(DATA);
137 print STDERR "fakeserver: read $n <$closeconnectionmessage> ", length($closeconnectionmessage), "\n";