00001 #!/usr/bin/perl -w
00002 # file KjPerlServer.pl
00003 # Perl server test using KjSslSocket library
00004 #
00005 # Author: Jan Koci <honza.koci@email.cz> (C) 2007
00006 # http://kjsslsocket.sourceforge.net/
00007 #
00008 # Copyright: See COPYING file that comes with this distribution
00009 #
00010 # 07.05.09 - First version
00011 #
00012 #
00013
00014 #***************************************************************************
00015 # Copyright (C) 2007 by Jan Koci *
00016 # honza.koci@email.cz *
00017 # http://kengine.sourceforge.net/tutorial/
00018 # *
00019 # This program is free software; you can redistribute it and/or modify *
00020 # it under the terms of the GNU General Public License as published by *
00021 # the Free Software Foundation; either version 2 of the License, or *
00022 # (at your option) any later version. *
00023 # *
00024 # This program is distributed in the hope that it will be useful, *
00025 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
00026 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
00027 # GNU General Public License for more details. *
00028 # *
00029 # You should have received a copy of the GNU General Public License *
00030 # along with this program; if not, write to the *
00031 # Free Software Foundation, Inc., *
00032 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
00033 #***************************************************************************/
00034
00035
00036 use KjPerlServer;
00037 use threads;
00038 use threads::shared;
00039
00040 # ----- Object creation -----
00041 my $g_pServer =new KjPerlServer::KjPerlServer();
00042 print " Created g_pKjServer $g_pServer\n";
00043 my $g_bIsConnected = 0;
00044
00045 sub NewThread
00046 {
00047 print "In the thread\n";
00048 #In new thread start server
00049 $g_pServer->Start();
00050 }
00051
00052 sub ConnectServer
00053 {
00054 my $strConn = 'localhost:4444';
00055 my $strCertFile = '/etc/kjssl/test.crt';
00056 my $strKeyFile = '/etc/kjssl/test.key';
00057 print("Try connection ssl server \n");
00058 print("Connection: $strConn\n");
00059 print("Cert file: $strCertFile\n");
00060 print("Key file: $strKeyFile\n");
00061 $g_pServer->Connect($strConn, $strCertFile, $strKeyFile);
00062 $g_bIsConnected = 1;
00063 }
00064
00065 sub Start
00066 {
00067 if(!$g_pServer)
00068 {
00069 print "Error:: Start -- pServer can`t allocated !!! \n";
00070 return 0;
00071 }
00072 ConnectServer();
00073 if($g_bIsConnected == 1)
00074 {
00075 $thr = threads->new(\&NewThread);
00076 for (;;)
00077 {
00078 my $iNumClients = $g_pServer->GetNumClients();
00079 # Go through all clients
00080 for($i=0; $i<$iNumClients; $i++)
00081 {
00082 # If can read client
00083 if($g_pServer->CanRead($i))
00084 {
00085 print "Client id = $i can read ... \n";
00086 # Read client message
00087 my $strReadMsg = $g_pServer->Read($i);
00088 print "strReadMsg = $strReadMsg \n";
00089 print " \n";
00090 # Write client message
00091 my $strWriteMsg = "Client id $i reads next message... $strReadMsg";
00092 $g_pServer->Write($i, $strWriteMsg);
00093 }
00094 }
00095 }
00096 }
00097 return 0;
00098 }
00099
00100 if(&Start() == 0)
00101 {
00102 if($g_pServer)
00103 {
00104 print "g_pServer now Destroyed\n";
00105 $g_pServer->DESTROY();
00106 }
00107 print "Goodbye\n";
00108 }
00109
00110