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

phNodeControllerClient.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 #include <phNodeControllerClient.h>
00013 
00014 /* -------------------------------------------------------------------------- */
00015 phNodeControllerClient::phNodeControllerClient( char *host, int port ) :
00016                             phSocket(host,port)
00017 {
00018     phFUNCTION("phNodeControllerClient::phNodeControllerClient")
00019 
00020     this->m_controller_id   = phID_INVALID;
00021     this->m_client_id       = phID_INVALID;
00022 
00023 error:
00024     return;
00025 }
00026 
00027 /* -------------------------------------------------------------------------- */
00028 phNodeControllerClient::~phNodeControllerClient()
00029 {
00030     phFUNCTION("phNodeControllerClient::~phNodeControllerClient")
00031 
00032     this->disconnect();
00033 
00034 error:
00035     return;
00036 }
00037 
00038 /* -------------------------------------------------------------------------- */
00039 int phNodeControllerClient::connect( char *host, int port )
00040 {
00041     phFUNCTION("phNodeControllerClient::connect")
00042 
00043     rc = this->phSocket::connect(host,port);
00044     phCHECK_RC(rc,NULL,"setPort failed.");
00045    
00046     rc = this->initialize();
00047     phCHECK_RC(rc,NULL,"this->initialize()");
00048 
00049     return phSUCCESS;
00050 error:
00051     return phFAIL;
00052 }
00053 
00054 /* -------------------------------------------------------------------------- */
00055 void phNodeControllerClient::disconnect()
00056 {
00057     phFUNCTION("phNodeControllerClient::disconnect")
00058 
00059     rc = this->deinitialize();
00060     phPRINT_RC(rc,NULL,"deinitialize");
00061 
00062     this->phSocket::disconnect();
00063 }
00064 
00065 /* -------------------------------------------------------------------------- */
00066 bool phNodeControllerClient::isConnected()
00067 {
00068     return this->phSocket::isConnected();
00069 }
00070 /* -------------------------------------------------------------------------- *
00071  * setControllerId:
00072  * -------------------------------------------------------------------------- */
00073 int phNodeControllerClient::setControllerId(uint32_t controller_id)
00074 {
00075     phFUNCTION("phNodeControllerClient::setControllerId")
00076     int locked = 0;
00077     
00078     phTHIS_LOCK(locked);
00079 
00080     this->m_controller_id = controller_id;
00081     
00082     phTHIS_UNLOCK(locked);
00083 
00084     return phSUCCESS;
00085 error:
00086     phTHIS_ERROR_UNLOCK(locked);
00087 
00088     return phFAIL;
00089 }
00090 
00091 /* -------------------------------------------------------------------------- *
00092  * getControllerId:
00093  * -------------------------------------------------------------------------- */
00094 uint32_t phNodeControllerClient::getControllerId()
00095 {
00096     phFUNCTION("phNodeControllerClient::getControllerId")
00097     int ret_id = 0;
00098     int locked = 0;
00099     
00100     phTHIS_LOCK(locked);
00101 
00102     ret_id = this->m_controller_id;
00103     
00104 error:
00105     phTHIS_ERROR_UNLOCK(locked);
00106 
00107     return ret_id;
00108 }
00109 
00110 /* -------------------------------------------------------------------------- *
00111  * setClientId:
00112  * -------------------------------------------------------------------------- */
00113 int phNodeControllerClient::setClientId(uint32_t client_id)
00114 {
00115     phFUNCTION("phNodeControllerClient::setClientId")
00116     int locked = 0;
00117     
00118     phTHIS_LOCK(locked);
00119 
00120     this->m_client_id = client_id;
00121     
00122     phTHIS_UNLOCK(locked);
00123 
00124     return phSUCCESS;
00125 error:
00126     phTHIS_ERROR_UNLOCK(locked);
00127 
00128     return phFAIL;
00129 }
00130 
00131 /* -------------------------------------------------------------------------- *
00132  * getClientId:
00133  * -------------------------------------------------------------------------- */
00134 uint32_t phNodeControllerClient::getClientId()
00135 {
00136     phFUNCTION("phNodeControllerClient::getClientId")
00137     uint32_t ret_id = 0;
00138     int locked = 0;
00139     
00140     phTHIS_LOCK(locked);
00141 
00142     ret_id = this->m_client_id;
00143     
00144 error:
00145     phTHIS_ERROR_UNLOCK(locked);
00146 
00147     return ret_id;
00148 }
00149 
00150 /* -------------------------------------------------------------------------- */
00151 /* initialize: */
00152 /* -------------------------------------------------------------------------- */
00153 int phNodeControllerClient::initialize( )
00154 {
00155     phFUNCTION("phNodeControllerClient::initialize")
00156     int locked = 0;
00157 
00158     phTHIS_LOCK(locked);
00159     
00160     /* we get here if we're successful */
00161     rc = this->send_message(phCLIENT_HELLO);
00162     phCHECK_RC(rc,NULL,"this->send_message(phCLIENT_HELLO:%d) failed.",
00163              phCLIENT_HELLO);
00164 
00165     rc = this->receive_message();
00166     phCHECK_RC(rc,NULL,"this->receive_message() failed.");
00167 
00168     rc = this->setClientId(this->m_message.getDstId());
00169     phCHECK_RC(rc,NULL,"setClientId failed.");
00170 
00171     rc = this->setControllerId(this->m_message.getSrcId());
00172     phCHECK_RC(rc,NULL,"setControllerId failed.");
00173 
00174     phPROGRESS("Client connected- id:%u controller:%u\n",
00175              this->getClientId(),
00176              this->getControllerId());
00177 
00178     phTHIS_UNLOCK(locked);
00179     return phSUCCESS;
00180 error:
00181     phTHIS_ERROR_UNLOCK(locked);
00182     return phFAIL;
00183 }
00184 
00185 /* -------------------------------------------------------------------------- */
00186 /* deinitialize: */
00187 /* -------------------------------------------------------------------------- */
00188 int phNodeControllerClient::deinitialize( )
00189 {
00190     phFUNCTION("phNodeControllerClient::deinitialize")
00191     int locked = 0;
00192 
00193     phTHIS_LOCK(locked);
00194     
00195     if (this->phSocket::isConnected())
00196     {
00197         /* we get here if we're successful */
00198         rc = this->send_message(phCLIENT_GOODBYE);
00199         phCHECK_RC(rc,NULL,"this->send_message(phCLIENT_GOODBYE:%d) failed.",
00200                  phCLIENT_GOODBYE);
00201 
00202         rc = this->receive_message();
00203         phCHECK_RC(rc,NULL,"this->receive_message() failed.");
00204     }
00205 
00206     rc = this->setClientId(phID_INVALID);
00207     phCHECK_RC(rc,NULL,"setClientId failed.");
00208 
00209     rc = this->setControllerId(phID_INVALID);
00210     phCHECK_RC(rc,NULL,"setControllerId failed.");
00211 
00212     phTHIS_UNLOCK(locked);
00213     return phSUCCESS;
00214 error:
00215     phTHIS_ERROR_UNLOCK(locked);
00216     return phFAIL;
00217 }
00218 
00219 /* ------------------------------------------------------------------------- *
00220  * receive_message: 
00221  * ------------------------------------------------------------------------- */
00222 int phNodeControllerClient::receive_message()
00223 {
00224     phFUNCTION("phNodeControllerClient::receive_message")
00225     int locked = 0;
00226     int received_length = 0;
00227     int offset = 0;
00228     int retrc = phSUCCESS;
00229     
00230     /* lock the object so we can alter things without worrying about another
00231      * thread getting to it at the same time. If we succeed, set 'locked' so
00232      * the method knows to unlock should something go wrong. */
00233     phTHIS_LOCK(locked);
00234 
00235     DEBUG_PRINT("Receiving message...\n");
00236     
00237     /* If there isn't a receive buffer allocated, allocate one and store the
00238      * size of it in a member variable for safe keeping */
00239     rc = this->m_message.receive(this);
00240     phCHECK_RC(rc,NULL,"message.receive(%p) failed.",
00241              this);
00242     
00243     DEBUG_PRINT("Received message.\n");
00244     
00245     /* propagate a non-blocking return code; just in case */
00246     if (rc == 1)
00247     {
00248         retrc = 1;
00249     }
00250     else if (rc >= 0)
00251     {
00252         rc = this->m_message.print(stderr);
00253     }
00254         
00255     
00256     /* Unlock the phNodeControllerClient in case someone wants to do 
00257      * something with it or has been waiting to */
00258     phTHIS_UNLOCK(locked);
00259     
00260     return retrc;
00261 error:
00262     /* We're using 'locked' to tell whether we want to unlock
00263      * within this method. This thread could own it several times and
00264      * we don't want to unlock one this method doesn't own */
00265     phTHIS_ERROR_UNLOCK(locked);
00266 
00267     return phFAIL;
00268 }
00269 
00270 /* ------------------------------------------------------------------------- *
00271  * : 
00272  * ------------------------------------------------------------------------- */
00273 int phNodeControllerClient::send_message(uint32_t message, uint32_t mutex_id )
00274 {
00275     phFUNCTION("phNodeControllerClient::send_message")
00276     int locked = 0;
00277     
00278     /* lock the object so we can alter things without worrying about another
00279      * thread getting to it at the same time. If we succeed, set 'locked' so
00280      * the method knows to unlock should something go wrong. */
00281     phTHIS_LOCK(locked);
00282 
00283     /* Set the source info and dest info ID and CLASSes */
00284     /* SOURCE INFO */
00285     rc = this->m_message.setSrcId(this->getClientId());
00286     phCHECK_RC(rc,NULL,"m_message.setSrcId() failed.");
00287     
00288     rc = this->m_message.setSrcClass(phCLASS_CLIENT);
00289     phCHECK_RC(rc,NULL,"m_message.setSrcClass() failed.");
00290    
00291     /* DESTINATION INFO */
00292     rc = this->m_message.setDstId(this->getControllerId());
00293     phCHECK_RC(rc,NULL,"m_message.setDstId() failed.");
00294 
00295     rc = this->m_message.setDstClass(phCLASS_NODE);
00296     phCHECK_RC(rc,NULL,"m_message.setDstClass() failed.");
00297 
00298     /* Set the id of the message */
00299     rc = this->m_message.setMessageId(message);
00300     phCHECK_RC(rc,NULL,"m_message.setMessage failed.");
00301    
00302     /* set the Mutex id: */
00303     rc = this->m_message.setMutexId(mutex_id);
00304     phCHECK_RC(rc,NULL,"m_message.setMutexId failed.");
00305    
00306     DEBUG_PRINT("Sending message\n");
00307     rc = this->m_message.print(stderr);
00308     
00309     /* send it to the client */
00310     rc = this->m_message.send(this);
00311     phCHECK_RC(rc,NULL,"this->m_message.send(%p) failed.",this);
00312     DEBUG_PRINT("Sent message\n");
00313 
00314     /* Unlock the phNodeControllerClient in case someone wants to do 
00315      * something with it or has been waiting to */
00316     phTHIS_UNLOCK(locked);
00317     
00318     return phSUCCESS;
00319 
00320 error:
00321     /* We're using 'locked' to tell whether we want to unlock
00322      * within this method. This thread could own it several times and
00323      * we don't want to unlock one this method doesn't own */
00324     phTHIS_ERROR_UNLOCK(locked);
00325   
00326     return phFAIL;
00327 }
00328 
00329 /* -------------------------------------------------------------------------- *
00330  * request:
00331  * -------------------------------------------------------------------------- */
00332 int phNodeControllerClient::request(uint32_t mutex_id)
00333 {
00334     phFUNCTION("phNodeControllerClient::request")
00335     int locked = 0;
00336     
00337     phTHIS_LOCK(locked);
00338 
00339     /* we get here if we're successful */
00340     rc = this->send_message(phCLIENT_REQUEST, mutex_id );
00341     phCHECK_RC(rc,NULL,
00342         "this->send_message(phCLIENT_REQUEST:%d,mutex_id:%d) failed.",
00343         phCLIENT_REQUEST,mutex_id );
00344 
00345     rc = this->receive_message();
00346     phCHECK_RC(rc,NULL,"this->receive_message() failed.");
00347 
00348     if (this->m_message.getMessageId() == phCLIENT_FAIL)
00349     {
00350         phCHECK_RC(-1,NULL,"m_message.getMessageId() == phCLIENT_FAIL");
00351     }
00352     
00353     phTHIS_UNLOCK(locked);
00354 
00355     return phSUCCESS;
00356 error:
00357     phTHIS_ERROR_UNLOCK(locked);
00358 
00359     return phFAIL;
00360 }
00361 
00362 /* -------------------------------------------------------------------------- *
00363  * release:
00364  * -------------------------------------------------------------------------- */
00365 int phNodeControllerClient::release(uint32_t mutex_id)
00366 {
00367     phFUNCTION("phNodeControllerClient::release")
00368     int locked = 0;
00369     
00370     phTHIS_LOCK(locked);
00371 
00372     /* we get here if we're successful */
00373     rc = this->send_message(phCLIENT_RELEASE, mutex_id );
00374     phCHECK_RC(rc,NULL,
00375         "this->send_message(phCLIENT_RELEASE:%d,mutex_id:%d) failed.",
00376         phCLIENT_RELEASE,mutex_id );
00377 
00378     rc = this->receive_message();
00379     phCHECK_RC(rc,NULL,"this->receive_message() failed.");
00380 
00381     if (this->m_message.getMessageId() == phCLIENT_FAIL)
00382     {
00383         phCHECK_RC(-1,NULL,"m_message.getMessageId() == phCLIENT_FAIL");
00384     }
00385     
00386     phTHIS_UNLOCK(locked);
00387 
00388     return phSUCCESS;
00389 error:
00390     phTHIS_ERROR_UNLOCK(locked);
00391 
00392     return phFAIL;
00393 }
00394 




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