KjPerlServer.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 "KjPerlServer.h"
00035 
00036 KjPerlServer::KjPerlServer()
00037 {
00038         cout << "KjPerlServer::KjPerlServer()" << endl;
00039         m_pServer = NULL;
00040         m_strReadMsg = "";
00041         m_strWriteMsg = "";
00042 }
00043 
00044 KjPerlServer::~KjPerlServer()
00045 {
00046         cout << "KjPerlServer::~KjPerlServer()" << endl;
00047 }
00048 
00051 bool KjPerlServer::CanRead(const int _iClientId)
00052 {
00053         if(m_pServer)
00054         {
00055                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00056                 if(pClient)
00057                 {
00058                         if(pClient->GetConn())
00059                         {
00060                                 if(pClient->GetConn()->GetIsValid() == true)
00061                                 {
00062                                         if(pClient->GetCanRead())
00063                                         {
00064                                                 return true;
00065                                         }
00066                                 }
00067                         }
00068                 }
00069         }
00070         return false;
00071 }
00072 
00074 const char *KjPerlServer::Read(const int _iClientId)
00075 {
00076         if(m_pServer)
00077         {
00078                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00079                 if(pClient)
00080                 {
00081                         if(pClient->GetConn())
00082                         {
00083                                 if(pClient->GetConn()->GetIsValid() == true)
00084                                 {
00085                                         m_strReadMsg = pClient->ReadMsg();
00086                                         return m_strReadMsg.c_str();
00087                                 }
00088                         }
00089                 }
00090         }
00091         m_strReadMsg = "Error: Client is disconnected !!!";
00092         return m_strReadMsg.c_str();
00093 }
00094 
00096 void KjPerlServer::Write(const int _iClientId, const char* _strMsg)
00097 {
00098         if(m_pServer)
00099         {
00100                 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00101                 if(pClient)
00102                 {
00103                         if(pClient->GetConn())
00104                         {
00105                                 if(pClient->GetConn()->GetIsValid() == true)
00106                                 {
00107                                         m_strWriteMsg = _strMsg;
00108                                         pClient->WriteMsg(m_strWriteMsg);
00109                                         return;
00110                                 }
00111                         }
00112                 }
00113         }
00114         cout << "Client id " << _iClientId << " is disconnected !!!" << endl;;
00115 }
00116 
00118 int KjPerlServer::GetNumClients()
00119 {
00120         return m_pServer->GetNumClients();
00121 }
00122 
00127 void KjPerlServer::Connect(char* _strConn, const char* _strCertFile,const char* _strKeyFile)
00128 {
00129         cout << "KjPerlServer::Connect()" << endl;
00130         m_pServer = new KjServerThread(_strConn, _strCertFile, _strKeyFile);
00131 }
00132 
00133 int KjPerlServer::Start()
00134 {
00135         cout << "int KjPerlServer::Start()" << endl;
00136         cout << "this = " <<  this << endl;
00137         cout << "m_pServer = " <<  m_pServer << endl;
00138         if(m_pServer)
00139         {
00140                 if(!m_pServer->Start())
00141                 {
00142                         if(m_pServer)
00143                         {
00144                                 delete m_pServer;
00145                                 m_pServer = NULL;
00146                         }
00147                 }
00148         }
00149         return 0;
00150 }
00151 

Generated on Tue Jul 24 10:03:42 2007 for KjPerlServer by  doxygen 1.4.6