source: gs2-extensions/parallel-building/trunk/src/bin/script/test-server.pl@ 30354

Last change on this file since 30354 was 24846, checked in by jmt12, 12 years ago

A small SocketSwimmingThreadPoolServer to allow testing of the client script and (dummy) testing of the Greenstone database connector

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6BEGIN
7{
8 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
9 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
10
11 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
12 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
13 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/XML/XPath");
14 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
15 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
16
17 if (defined $ENV{'GSDLEXTS'})
18 {
19 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
20 foreach my $e (@extensions)
21 {
22 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
23 unshift (@INC, "$ext_prefix/perllib");
24 unshift (@INC, "$ext_prefix/perllib/cpan");
25 unshift (@INC, "$ext_prefix/perllib/plugins");
26 unshift (@INC, "$ext_prefix/perllib/classify");
27 }
28 }
29 if (defined $ENV{'GSDL3EXTS'})
30 {
31 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
32 foreach my $e (@extensions)
33 {
34 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
35 unshift (@INC, "$ext_prefix/perllib");
36 unshift (@INC, "$ext_prefix/perllib/cpan");
37 unshift (@INC, "$ext_prefix/perllib/plugins");
38 unshift (@INC, "$ext_prefix/perllib/classify");
39 }
40 }
41
42 # Manually installed CPAN package in GEXT*INSTALL
43 unshift (@INC, $ENV{'GEXTPARALLELBUILDING_INSTALLED'} . "/share/perl5");
44}
45
46use SocketsSwimmingThreadPoolClient;
47use SocketsSwimmingThreadPoolServer;
48
49# Configuration
50my $host = 'localhost';
51my $port = 8190;
52
53# Fork ourselves
54my $child_pid = fork();
55# Ensure fork() worked
56if (!defined($child_pid))
57{
58 die("Fatal Error! Fork didn't.");
59}
60# Parent process creates the server socket and sits there listening
61if ($child_pid > 0)
62{
63 my $server = SocketsSwimmingThreadPoolServer->new(host=>$host,
64 port=>$port,
65 processor_cb => \&serverProcess
66 );
67 print STDERR "[Server] Listening\n";
68 $server->start;
69 print STDERR "[Server] Shutdown\n";
70}
71# Child socket spawns 100,000 client connections one after the other
72else
73{
74 for (my $i = 0; $i < 100000; $i++)
75 {
76 sendMessage('msg' . $i);
77 }
78 # Ask the server to shutdown
79 sendMessage('quit');
80}
81
82exit(0);
83
84sub sendMessage
85{
86 my ($message) = @_;
87 my $client = SocketsSwimmingThreadPoolClient->new(host=>$host,
88 port=>$port
89 );
90 my $result = $client->query($message);
91 print "[Client] Response: $result\n";
92}
93
94# All the server does is echo the reverse of the request
95sub serverProcess
96{
97 my ($data, $ip, $tid, $fnstop) = @_;
98 print "[Server] Request: $data\n";
99 my $value = reverse $data;
100 if ($data =~ /quit/)
101 {
102 $fnstop->();
103 $value = 'Quitting!';
104 }
105 return $value;
106}
107
1081;
Note: See TracBrowser for help on using the repository browser.