Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2702_Regression / fakeserver.pm
blobac6a954178cdfb0ea267a8f36d3800252b5b3812
2 package fakeserver;
4 use Socket;
5 use IO::Socket;
7 sub new {
8 my $self = { };
9 $self->{'filebits'} = '';
10 $self->{'connections'} = { };
11 $self->{'server'} = '';
12 $self->{'server_fileno'} = '';
13 $self->{''} = '';
16 sub run {
17 my $self = shift;
18 my $timeoutafter = shift; # in seconds
19 my $rout;
20 while ( 1 ) {
21 print STDERR "fakeserver: Loop\n";
23 select( undef, undef, undef, 1 );
25 my ($n, $left) = select( $rout = $filebits, undef, undef, $timeoutafter); # Time out after 15 seconds
26 if ($n == 0) {
27 print STDERR "fakeserver: Terminating normally\n";
28 exit 0;
31 my $routs = unpack("b*", $rout);
32 print STDERR "fakeserver: Select $routs\n";
33 my $pos = index( $routs,'1');
34 while ( $pos >= 0 ) {
35 $self->HandleFile( $pos );
36 $pos = index( $routs,'1', $pos+1);
41 sub CloseServer {
42 my $self = shift;
43 vec($self->{'filebits'},$self->{'server_fileno'},1) = 0;
44 $server->close();
45 undef $server;
48 sub OpenServer {
49 my $self = shift;
50 my $server = IO::Socket::INET->new(Listen => 5,
51 LocalPort => 1192,
52 Reuse => 1,
53 ReuseAddr => 1,
54 Timeout => 0,
55 Proto => 'tcp');
57 die "Could not create socket $!" unless $server;
59 $server->blocking(0);
60 my $server_fileno = fileno($server);
61 vec($filebits,$server_fileno,1) = 1;
63 print STDERR "fakeserver: Starting $server_fileno\n";
65 open (DATA, 'closeconnection.dat') or die "couldn't open closeconnection.dat: $!\n";
66 my $n = read(DATA, $closeconnectionmessage, 100);
67 close(DATA);
69 print STDERR "fakeserver: read $n <$closeconnectionmessage> ", length($closeconnectionmessage), "\n";
70 $self->{'server'} = $server;
71 $self->{'server_fileno'} = $server_fileno;
74 sub SendMessage {
75 my $self = shift;
76 my( $message ) = @_;
78 print STDERR "fakeserver: SendMessage $message\n";
79 $message .= "\r\n";
81 foreach my $fileno (keys %{$self->{'connections'}}) {
82 if ( ${$self->{'connections'}}{$fileno} ) {
83 my $client = ${$self->{'connections'}}{$fileno}{client};
84 print $client $message;
89 sub HandleFile {
90 my $self = shift;
91 my ( $fileno ) = @_;
93 print STDERR "fakeserver: HandleFile $fileno\n";
94 if ( $fileno == $self->{'server_fileno'} ) {
95 HandleServer();
96 } elsif ( ${$self->{'connections'}}{$fileno} ) {
97 HandleClient( $fileno );
98 } else {
99 print STDERR "fakeserver: Weird fileno $fileno\n";
103 sub HandleServer {
104 my $self = shift;
105 my $client = $server->accept();
107 print STDERR "fakeserver: HandleServer\n";
109 if ( $client ) {
110 my $fileno = fileno($client);
111 $client->blocking(0);
112 ${$self->{'connections'}}{$fileno}{client} = $client;
113 ${$self->{'connections'}}{$fileno}{loggedin} = 0;
114 vec($self->{'filebits'},$fileno,1) = 1;
115 print STDERR "fakeserver: New client on $fileno\n";
116 } else {
117 print STDERR "fakeserver: No accept for server, reopen\n";
118 $self->CloseServer();
119 $self->OpenServer();
123 sub HandleClient {
124 my $self = shift;
125 my ( $fileno ) = @_;
127 print STDERR "fakeserver: HandleClient $fileno\n";
128 my $receive;
129 my $n = recv( ${$self->{'connections'}}{$fileno}{client}, $receive, 200, 0 );
130 my $rlen = length($receive);
131 if ( $receive ) {
132 if ($receive =~ /^GIOP/) {
133 print STDERR "fakeserver: Got $rlen byte GIOP message from client $fileno\n";
135 else {
136 print STDERR "fakeserver: Got $rlen bytes from client $fileno\n";
138 ${$self->{'connections'}}{$fileno}{receive} = $receive;
139 my $slen = length($closeconnectionmessage);
140 print STDERR "fakeserver: Sending $slen bytes GIOP closeconnection to $fileno\n";
141 sleep(3);
142 $n = send(${$self->{'connections'}}{$fileno}{client}, $closeconnectionmessage, 0);
143 print STDERR "fakeserver: Sent $n bytes GIOP closeconnection to $fileno\n";
144 } else {
145 print STDERR "fakeserver: Close client $fileno\n";
146 vec(${$self->{'filebits'}},$fileno,1) = 0;
147 ${$self->{'connections'}}{$fileno}{client}->close();
148 undef ${$self->{'connections'}}{$fileno};
149 $self->SendMessage( "Close Client" );