KjServerTest.cpp

Go to the documentation of this file.
00001 
00013 /***************************************************************************
00014  *   Copyright (C) 2007 by Jan Koci   *
00015  *   honza.koci@email.cz   *
00016  *   http://kengine.sourceforge.net/tutorial/
00017  *                                                                         *
00018  *   This program is free software; you can redistribute it and/or modify  *
00019  *   it under the terms of the GNU General Public License as published by  *
00020  *   the Free Software Foundation; either version 2 of the License, or     *
00021  *   (at your option) any later version.                                   *
00022  *                                                                         *
00023  *   This program is distributed in the hope that it will be useful,       *
00024  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00025  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00026  *   GNU General Public License for more details.                          *
00027  *                                                                         *
00028  *   You should have received a copy of the GNU General Public License     *
00029  *   along with this program; if not, write to the                         *
00030  *   Free Software Foundation, Inc.,                                       *
00031  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00032  ***************************************************************************/
00033 
00034 #include "KjServerTest.h"
00035 
00036 KjServerTest::KjServerTest()
00037 {
00038         cout << "KjServerTest::KjServerTest()" << endl;
00039         m_pServer = NULL;
00040         m_strReadMsg = "";
00041         m_strWriteMsg = "";
00042         m_bIsConnected = false;
00043 }
00044 
00045 KjServerTest::~KjServerTest()
00046 {
00047         cout << "KjServerTest::~KjServerTest()" << endl;
00048 }
00049 
00052 bool KjServerTest::CanRead(const int _iClientId)
00053 {
00054         if(m_pServer)
00055         {
00056                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00057                 if(pClient)
00058                 {
00059                         if(pClient->GetConn())
00060                         {
00061                                 if(pClient->GetConn()->GetIsValid() == true)
00062                                 {
00063                                         if(pClient->GetCanRead())
00064                                         {
00065                                                 return true;
00066                                         }
00067                                 }
00068                         }
00069                 }
00070         }
00071         return false;
00072 }
00073 
00075 const char *KjServerTest::Read(const int _iClientId)
00076 {
00077         if(m_pServer)
00078         {
00079                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00080                 if(pClient)
00081                 {
00082                         if(pClient->GetConn())
00083                         {
00084                                 if(pClient->GetConn()->GetIsValid() == true)
00085                                 {
00086                                         m_strReadMsg = pClient->ReadMsg();
00087                                         return m_strReadMsg.c_str();
00088                                 }
00089                         }
00090                 }
00091         }
00092         m_strReadMsg = "Error: Client is disconnected !!!";
00093         return m_strReadMsg.c_str();
00094 }
00095 
00097 void KjServerTest::Write(const int _iClientId, const char* _strMsg)
00098 {
00099         if(m_pServer)
00100         {
00101                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00102                 if(pClient)
00103                 {
00104                         if(pClient->GetConn())
00105                         {
00106                                 if(pClient->GetConn()->GetIsValid() == true)
00107                                 {
00108                                         m_strWriteMsg = _strMsg;
00109                                         pClient->WriteMsg(m_strWriteMsg);
00110                                         return;
00111                                 }
00112                         }
00113                 }
00114         }
00115         cout << "Client id " << _iClientId << " is disconnected !!!" << endl;;
00116 }
00117 
00119 int KjServerTest::GetNumClients()
00120 {
00121         return m_pServer->GetNumClients();
00122 }
00123 
00128 void KjServerTest::Connect(char* _strConn, const char* _strCertFile,const char* _strKeyFile)
00129 {
00130         cout << "KjPerlServer::Connect()" << endl;
00131         m_pServer = new KjServerThread(_strConn, _strCertFile, _strKeyFile);
00132 }
00133 
00135 void KjServerTest::ConnectServer()
00136 {
00137         cout << "Try connection ssl server" << endl;
00138         Connect(KJ_TEST_SERVER_CONN, KJ_TEST_CRT, KJ_TEST_KEY);
00139         m_bIsConnected = true;
00140 }
00141 
00142 int KjServerTest::Start()
00143 {
00144         cout << "int KjServerTest::Start()" << endl;
00145         if(m_pServer)
00146         {
00147                 if(!m_pServer->Start())
00148                 {
00149                         if(m_pServer)
00150                         {
00151                                 delete m_pServer;
00152                                 m_pServer = NULL;
00153                         }
00154                 }
00155         }
00156         return 0;
00157 }
00158 
00159 void *KjServerTest::NewThread(void *_pArg)
00160 {
00161         cout << "KjServerThread::NewThread()" << pthread_self() << endl;
00162         printf("Process %s: start\n", (char *)_pArg);
00163         // Start server in this thread
00164         class KjServerTest *pServerTest = (class KjServerTest *) _pArg;
00165         cout << "pServerTest " << pServerTest << endl;
00166         pServerTest->Start();
00167         cout << "Thread" << pthread_self() << "Exiting" << endl;
00168         printf("Process %s: end\n", (char *)_pArg);
00169         return NULL;
00170 }
00171 
00173 void KjServerTest::CreateNewThread()
00174 {
00175         int iRet;
00176         pthread_t *pThread = new pthread_t;
00177         // Create new thread and send client connection to the thread
00178         iRet = pthread_create(pThread, NULL, &NewThread, this);
00179         if (iRet != 0) fprintf(stderr, "create a failed %d\n", iRet);
00180 }
00181 
00182 int KjServerTest::Update()
00183 {
00184         cout << "int KjServerTest::Update()" << endl;
00185         ConnectServer();
00186         if(m_bIsConnected == true)
00187         {
00188                 // Start server in new thread
00189                 CreateNewThread();
00190                 // Check server in this thread
00191                 for(;;) 
00192                 {
00193                         int iNumClients = GetNumClients();
00194                         // Go through all clients
00195                         for(int i=0; i<iNumClients; i++)
00196                         {
00197                                 // If can read client
00198                                 if(CanRead(i))
00199                                 {
00200                                         cout << "Client id = " << i << "Can read ..." << endl;
00201                                         // Read client message
00202                                         const char* strReadMsg = Read(i);
00203                                         cout << "strReadMsg = " << strReadMsg << endl;
00204                                         cout << endl;
00205                                         // Write client message
00206                                         stringstream strWriteMsg;
00207                                         strWriteMsg << "Client id " << i << " reads next message..." << strReadMsg << endl;
00208                                         Write(i, strWriteMsg.str().c_str());
00209                                 }
00210                         }
00211                 }
00212         }
00213         else
00214         {
00215                 cerr << "Error -- int KjServerTest::Start() -- Server doesn`t connected !!!"  << endl;
00216         }
00217         return 0;
00218 }
00219 

Generated on Tue Jul 24 10:03:56 2007 for Servertest by  doxygen 1.4.6