KjClient.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 "KjClient.h"
00035 
00036 
00037 
00038 KjClient::KjClient()
00039 {
00040         m_pCon = new KjSslConnection(KjSslConnection::KJ_SSL_CLIENT,KJ_TEST_CLIENT_CONN, KJ_TEST_CRT, KJ_TEST_KEY);
00041         m_pSignal = new KjSignal();
00042 }
00043 
00044 KjClient::~KjClient()
00045 {
00046         if(m_pCon)
00047         {
00048                 delete m_pCon;
00049                 m_pCon = NULL;
00050         }
00051         if(m_pSignal)
00052         {
00053                 delete m_pSignal;
00054                 m_pSignal = NULL;
00055         }
00056 }
00057 
00060 int KjClient::Start()
00061 {
00062         m_pSignal->InitSignals();
00063     if(!m_pCon)
00064         {
00065                 cerr<< "Error:: int KjClient::Start() -- KjSslConnection can`t allocated" << endl;
00066                 return 0;
00067         }
00068         if(m_pCon->GetIsValid())
00069         {
00070                 string strClientMsg;
00071                 string strServerMsg;
00072                 char str[KJ_SSL_READ_CHUNK_SIZE];
00073                 bool bCanWrite = true;
00074                 // Check key CTRL-C or CTRL-\ */
00075                 while(!m_pSignal->IsSigInt() && !m_pSignal->IsSigQuit()) 
00076                 {
00077                         cout << "Please enter some message ... " << endl;
00078                         // If length of input string is correct 
00079                         if(cin.getline(str, KJ_SSL_READ_CHUNK_SIZE) != NULL)
00080                         {
00081                                 strClientMsg += str;
00082                                 // Delete end message if exists
00083                                 DeleteEndMsg(strClientMsg);
00084                                 //strClientMsg += KJ_END_MSG;
00085                                 if(bCanWrite)
00086                                 {
00087                                         bCanWrite = false;
00088                                         cout << "Client send message '" << strClientMsg << "'" << endl;
00089                                         m_pCon->Write(strClientMsg);
00090                                         // Wait timeout interval 
00091                                         Wait(500);
00092                                         // Read message from server, 
00093                                         bool bIsEndMsg = false;
00094                                         strServerMsg.clear();
00095                                         do
00096                                         {
00097                                                 // Read some packet
00098                                                 string strPacket;
00099                                                 m_pCon->Read(strPacket);
00100                                                 if(strPacket.empty())
00101                                                 {
00102                                                         cout << "Server is disconnected ... '" << endl;
00103                                                         return 0;
00104                                                 }
00105                                                 strServerMsg += strPacket;
00106                                                 size_t iFound = strPacket.find(KJ_END_MSG);
00107                                                 if (iFound!=string::npos)
00108                                                 {
00109                                                         bIsEndMsg = true;
00110                                                 }
00111                                         }while (bIsEndMsg == false);
00112                                         // Delete end message if exists
00113                                         DeleteEndMsg(strServerMsg);
00114                                         cout << "Received server message is '" << strServerMsg << "'" << endl;
00115                                         // Clear client message
00116                                         strClientMsg.clear();
00117                                         bCanWrite = true;
00118                                 }
00119                         }
00120                         else
00121                         {
00122                                 cerr << "Error :: KjClient::Start() -- Input string is incorrect"  << endl;
00123                                 cout << "Maximum length of input string must be "  << KJ_SSL_READ_CHUNK_SIZE << endl;
00124                                 return 0;
00125                         }
00126                 }
00127         }
00128         else
00129         {
00130                 cerr<< "Error:: int KjClient::Start() --Invalid SSL connection !!! " << endl;
00131         }
00132         return 0;
00133         
00134 }
00135 
00138 void KjClient::DeleteEndMsg(string &_strMsg)
00139 {
00140         size_t iFound = _strMsg.find(KJ_END_MSG);
00141         if (iFound!=string::npos)
00142         {
00143                 string strEndMsg = KJ_END_MSG;
00144                 _strMsg.erase(iFound , strEndMsg.size());
00145         }
00146 }

Generated on Tue Jul 24 10:02:50 2007 for Clienttest by  doxygen 1.4.6