KjServerThread.cpp

Go to the documentation of this file.
00001 
00014 #include "KjServerThread.h"
00015 
00016 using namespace K3d;
00017 
00018 KjServerThread::KjServerThread(char* _strConn, const char* _strCertFile,const char* _strKeyFile)
00019 {
00020         cout << "KjServerThread::KjServerThread()"  << endl;
00021         m_pServer = new KjSslServer(_strConn, _strCertFile, _strKeyFile);
00022         m_pSignal = new KjSignal();
00023         m_bIsNewClientConn = false;
00024         m_pThread = NULL;
00025 }
00026 
00027 KjServerThread::~KjServerThread()
00028 {
00029         if(m_pServer)
00030         {
00031                 delete m_pServer;
00032                 m_pServer = NULL;
00033         }
00034         if(m_pSignal)
00035         {
00036                 delete m_pSignal;
00037                 m_pSignal = NULL;
00038         }
00039 }
00040 
00041 void *KjServerThread::NewThread(void *_pArg)
00042 {
00043         cout << "KjServerThread::NewThread()" << pthread_self() << endl;
00044         printf("Process %s: start\n", (char *)_pArg);
00045         // Check new client
00046         class KjClientConn *pClientConn = (class KjClientConn *) _pArg;
00047         cout << "pClientConn " << pClientConn << endl;
00048         pClientConn->NewClient();
00049         cout << "Thread" << pthread_self() << "Exiting" << endl;
00050         printf("Process %s: end\n", (char *)_pArg);
00051         return NULL;
00052 }
00053 
00055 void KjServerThread::CreateNewThread()
00056 {
00057         int iRet;
00058         pthread_t *pThread = new pthread_t;
00059         // Create new client
00060         KjClientConn *pClientConn = new KjClientConn(m_pServer, pThread);
00061         m_tClientArray.push_back(pClientConn);
00062         
00063         // Create new thread and send client connection to the thread
00064         iRet = pthread_create(pThread, NULL, &NewThread, pClientConn);
00065         if (iRet != 0) fprintf(stderr, "create a failed %d\n", iRet);
00066 }
00067 
00069 int KjServerThread::GetNumClients()
00070 {
00071         return (int)m_tClientArray.size();
00072 }
00073 
00075 KjClientConn *KjServerThread::GetClient(const int _iClientId)
00076 {
00077         return m_tClientArray[_iClientId];
00078 }
00079 
00082 int KjServerThread::Start()
00083 {
00084         int iRet;
00085         if(!m_pServer) 
00086         {
00087                 cerr<< "Error:: int KjServer::Start() -- KjSslServer can`t allocated" << endl;
00088                 return 0;
00089     }
00090         if(m_pServer->GetIsValid())
00091         {
00092                 void * pRetval;
00093                 // Create new thread 
00094                 CreateNewThread();
00095                 for(;;)
00096                 {
00097                         // Check exit signals
00098                         if(m_pSignal->IsSigInt() || m_pSignal->IsSigQuit())
00099                         {
00100                                 break;
00101                         }
00102                         for(int i=0; i<(int)m_tClientArray.size(); i++)
00103                         {
00104                                 KjClientConn *pClient = m_tClientArray[i];
00105                                 // Get if is client connected
00106                                 if(pClient->GetIsConnected() == true)
00107                                 {
00108                                         if(pClient->GetConn())
00109                                         {
00110                                                 // Erase non valid client
00111                                                 if(pClient->GetConn()->GetIsValid() == false)
00112                                                 {
00113                                                         // Get client thread index
00114                                                         pthread_t iThreadId = pClient->GetThreadId();
00115                                                         cout << "Detach thread id " << iThreadId << endl;
00116                                                         printf("Wake up all waiters...\n");
00117                                                         pthread_t *pThread = pClient->GetThread();
00118                                                         iRet = pthread_join(*pThread, &pRetval);
00119                                                         if (pRetval != 0) fprintf(stderr, "join a failed %d\n", iRet);
00120                                                         cout << "Delete thread " << pThread << endl;
00121                                                         if(pThread)
00122                                                         {
00123                                                                 cout << "Delete thread " << pThread << endl;
00124                                                                 delete pThread;
00125                                                                 pThread = NULL;
00126                                                         }
00127                                                         cout << "Thread is canceled " << endl;
00128                                                         // Delete client
00129                                                         if(pClient)
00130                                                         {
00131                                                                 cout << "Delete client " << pClient << endl;
00132                                                                 delete pClient;
00133                                                                 pClient = NULL;
00134                                                         }
00135                                                         cout << "Erase client " << i << endl;
00136                                                         m_tClientArray.erase(m_tClientArray.begin()+i);
00137                                                 }
00138                                                 else
00139                                                 {
00140                                                         // Check if client unlocked new thread
00141                                                         if (pClient->GetIsLockNewThread() == false)
00142                                                         {
00143                                                                 pClient->GetIsLockNewThread() = true;
00144                                                                 // Create new thread 
00145                                                                 CreateNewThread();
00146                                                         }
00147                                                 }
00148                                         }
00149                                         else
00150                                         {
00151                                                 cout << "Client " << i << " is disconnected" << endl;
00152                                         }       
00153                                 }
00154                         }
00155                 }
00156         }
00157         else
00158         {
00159                 cerr<< "Error:: int KjServer::Start() --Invalid SSL connection !!! " << endl;
00160         }
00161         cout << "int KjServer::Start() -- Server exit" << endl;
00162         return 0;
00163 }

Generated on Tue Jul 24 10:03:07 2007 for KjSslServer by  doxygen 1.4.6