00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00075 while(!m_pSignal->IsSigInt() && !m_pSignal->IsSigQuit())
00076 {
00077 cout << "Please enter some message ... " << endl;
00078
00079 if(cin.getline(str, KJ_SSL_READ_CHUNK_SIZE) != NULL)
00080 {
00081 strClientMsg += str;
00082
00083 DeleteEndMsg(strClientMsg);
00084
00085 if(bCanWrite)
00086 {
00087 bCanWrite = false;
00088 cout << "Client send message '" << strClientMsg << "'" << endl;
00089 m_pCon->Write(strClientMsg);
00090
00091 Wait(500);
00092
00093 bool bIsEndMsg = false;
00094 strServerMsg.clear();
00095 do
00096 {
00097
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
00113 DeleteEndMsg(strServerMsg);
00114 cout << "Received server message is '" << strServerMsg << "'" << endl;
00115
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 }