Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phServerThread.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 /* -------------------------------------------------------------------------- *
00013  * phServerThread.cpp:Thread class handles all the server's responsibilities  *
00014  * -------------------------------------------------------------------------- */
00015 
00016 #include <phServerThread.h>
00017 #include <phClientHandler.h>
00018 
00019 /* -------------------------------------------------------------------------- * 
00020  * phServerThread:
00021  * -------------------------------------------------------------------------- */
00022 phServerThread::phServerThread(int port)
00023 {
00024     phFUNCTION("phServerThread::phServerThread")
00025     int locked = 0;
00026     
00027     phTHIS_LOOSE_LOCK(locked);
00028     
00029     this->m_port = port;
00030 
00031 error:
00032     
00033     phTHIS_LOOSE_UNLOCK(locked);
00034     
00035     rc = 0;  
00036 }
00037 
00038 /* -------------------------------------------------------------------------- *
00039  * ~phServerThread:
00040  * -------------------------------------------------------------------------- */
00041 phServerThread::~phServerThread()
00042 {
00043     phFUNCTION("phServerThread::~phServerThread")
00044     int locked = 0;
00045 
00046     phTHIS_LOOSE_LOCK(locked);
00047 }
00048 
00049 /* -------------------------------------------------------------------------- *
00050  * setPort:
00051  * -------------------------------------------------------------------------- */
00052 int phServerThread::setPort(int port)
00053 {
00054     phFUNCTION("phServerThread::setPort")
00055     int locked = 0;
00056     
00057     phTHIS_LOCK(locked);
00058 
00059     this->m_port = port;
00060     
00061     phTHIS_UNLOCK(locked);
00062 
00063     return phSUCCESS;
00064 error:
00065     phTHIS_ERROR_UNLOCK(locked);
00066 
00067     return phFAIL;
00068 }
00069 
00070 /* -------------------------------------------------------------------------- *
00071  * getPort:
00072  * -------------------------------------------------------------------------- */
00073 int phServerThread::getPort()
00074 {
00075     phFUNCTION("phServerThread::getPort")
00076     int ret_port = 0;
00077     int locked = 0;
00078     
00079     phTHIS_LOCK(locked);
00080 
00081     ret_port = this->m_port;
00082     
00083 error:
00084     phTHIS_ERROR_UNLOCK(locked);
00085 
00086     return ret_port;
00087 }
00088 
00089 /* -------------------------------------------------------------------------- *
00090  * run:
00091  * -------------------------------------------------------------------------- */
00092 int phServerThread::run()
00093 {
00094     phFUNCTION("phServerThread::run")
00095 
00096     phSocket        *client_sock    = NULL;
00097     phClientHandler *client_thread  = NULL;
00098     
00099     /* Do any setup here */
00100     rc = this->m_server.setPort(this->getPort());
00101     phPRINT_RC(rc,NULL,"server.setPort");
00102     
00103     rc = this->m_server.setBackLog(10);
00104     phPRINT_RC(rc,NULL,"server.setBackLog()");
00105   
00106     /* Start the server socket listening on the port number
00107      * passed to it in the constructor*/
00108     rc =this->m_server.listen();
00109     phCHECK_RC(rc,NULL,"server.listen()");
00110    
00111     /* Set the server thread to be non-blocking */
00112     rc = this->m_server.setBlocking(phNONBLOCK);
00113     phCHECK_RC(rc,NULL,"server.setBlocking(phNONBLOCK)");
00114     
00115     /* run has the lock until here */
00116     rc = this->signal_running();
00117     phCHECK_RC(rc, NULL, "this->signal_running()");
00118     
00119     while (this->isRunning())
00120     {
00121         /* phPROGRESS("port:%d\n",this->m_port); */
00122 
00123         client_sock = this->m_server.accept();
00124         if (this->isRunning() == 0)
00125         {
00126             phDelete(client_sock);
00127             continue;
00128         }
00129         phCONT_NULLPTR(client_sock, "accept",
00130                      "failed to accept client connection\n\n");
00131 
00132         phPROGRESS("Accepted client on port %d\n",this->m_port);
00133         /* Start up a client handler */
00134         if (client_thread == NULL)
00135         {
00136             client_thread = new phClientHandler(client_sock);
00137 
00138             /* Set the client thread's socket to blocking */
00139             rc = client_sock->setBlocking(phBLOCK);
00140             phPRINT_RC(rc,NULL,"client_sock->setBlocking(phBLOCK)");
00141             
00142             /* Spawn the thread */
00143             /* ...Detach the thread and leave it up to 
00144              * the client thread for cleanup */
00145             /* We pass the detached flag because 
00146              * if the main run method doesn't loop,
00147              * it may finish before we can detach */
00148             rc = client_thread->start(phDETACHED);
00149             phPRINT_RC(rc,NULL,"client_thread->start()");
00150 
00151             client_sock = NULL;
00152             client_thread = NULL;
00153         }
00154     }    
00155 
00156     return this->m_port;
00157     return phSUCCESS;
00158 error:
00159     rc = this->signal_error();
00160     phCHECK_RC(rc, NULL, "this->signal_error()");
00161    
00162     return phFAIL;
00163 }
00164  
00165 /* ------------------------------------------------------------------------- *
00166  * wakeup: 
00167  * ------------------------------------------------------------------------- */
00168 int phServerThread::wakeup()
00169 {
00170     phFUNCTION("phServerThread::wakeup")
00171     int locked = 0;
00172 
00173     phTHIS_LOCK(locked);
00174     
00175     /* phPROGRESS("\n"); */
00176     this->m_server.wakeup();
00177     
00178     phTHIS_UNLOCK(locked);
00179     
00180     return phSUCCESS;
00181 error:
00182     phTHIS_ERROR_UNLOCK(locked);
00183     
00184     return phFAIL;
00185 }
00186     
00187 /* ------------------------------------------------------------------------- *
00188  * cleanup:
00189  * ------------------------------------------------------------------------- */
00190 int phServerThread::cleanup()
00191 {
00192     phFUNCTION("phServerThread::cleanup")
00193     int locked = 0;
00194 
00195     phTHIS_LOCK(locked);
00196     
00197     /* phPROGRESS("\n"); */
00198     
00199     phTHIS_UNLOCK(locked);
00200     
00201     return phSUCCESS;
00202 error:
00203     phTHIS_ERROR_UNLOCK(locked);
00204     
00205     return phFAIL;
00206 }
00207     
00208 
00209 /* ------------------------------------------------------------------------- * 
00210  * error:
00211  * ------------------------------------------------------------------------- */
00212 int phServerThread::error()
00213 {
00214     phFUNCTION("phServerThread::error")
00215     int locked = 0;
00216 
00217     phTHIS_LOCK(locked);
00218 
00219     /* phPROGRESS("\n"); */
00220    
00221     phTHIS_UNLOCK(locked);
00222     
00223     return phSUCCESS;
00224 error:
00225     phTHIS_ERROR_UNLOCK(locked);
00226     
00227     return phFAIL;
00228 }
00229 
00230 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:06 2007 for phission by  doxygen 1.4.4