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

phPlayerClient.cpp

Go to the documentation of this file.
00001 
00002 #include <playerc.h>
00003 
00004 #include <phPlayerClient.h>
00005 #include <phPlayerDevice.h>
00006 #include <phPlayer.h>
00007 #include <phbase.h>
00008 
00009 /* ------------------------------------------------------------------------ */
00010 struct ph_player_client_t
00011 {
00012     playerc_client_t *m_player_client;
00013 };
00014 
00015 /* ------------------------------------------------------------------------ */
00016 phPlayerClient::phPlayerClient()
00017 {
00018     phFUNCTION("phPlayerClient::phPlayerClient")
00019     int locked = 0;
00020 
00021     phTHIS_LOOSE_LOCK(locked);
00022 
00023     this->setName("phPlayerClient");
00024 
00025     this->m_client = (struct ph_player_client_t *)phCalloc(1,
00026                         sizeof(struct ph_player_client_t));
00027     phPRINT_PTR(this->m_client,"phCalloc","phCalloc failed");
00028 
00029     this->m_is_connected = 0;
00030     
00031     phTHIS_LOOSE_UNLOCK(locked);
00032 }
00033 
00034 /* ------------------------------------------------------------------------ */
00035 phPlayerClient::phPlayerClient(char *host, uint16_t port)
00036 {
00037     phFUNCTION("phPlayerClient::phPlayerClient")
00038     int locked = 0;
00039 
00040     phTHIS_LOOSE_LOCK(locked);
00041 
00042     phPlayerClient();
00043     
00044     rc = this->set(host,port);
00045     phPRINT_RC(rc,NULL,"this->set(%s,%u)",host,port);
00046 
00047     phTHIS_LOOSE_UNLOCK(locked);
00048 }
00049 
00050 /* ------------------------------------------------------------------------ */
00051 phPlayerClient::~phPlayerClient()
00052 {
00053     phFUNCTION("phPlayerClient::~phPlayerClient")
00054     int locked = 0;
00055 
00056     phTHIS_LOOSE_LOCK(locked);
00057 
00058     if (this->isRunning())
00059     {
00060         this->stop();
00061     }
00062 
00063     if (this->m_client != NULL)
00064     {
00065         if (this->m_client->m_player_client != NULL)
00066         {
00067             playerc_client_destroy(this->m_client->m_player_client);
00068             this->m_client->m_player_client = NULL;
00069         }
00070 
00071         phFree(this->m_client);
00072     }
00073 
00074     this->m_client = NULL;
00075 
00076     phTHIS_LOOSE_UNLOCK(locked);
00077 }
00078 
00079 /* ------------------------------------------------------------------------ */
00080 int phPlayerClient::run()
00081 {
00082     phFUNCTION("phPlayerClient::run")
00083     void               *prc             = NULL;
00084     phObjectNode       *node            = NULL;
00085     phPlayerDevice     *device          = NULL;
00086     int                 devices_locked  = 0;
00087 
00088     rc = this->connect();
00089     phCHECK_RC(rc,NULL,"this->connect()");
00090     
00091     rc = this->signal_running();
00092     phPRINT_RC(rc,NULL,"this->signal_running");
00093 
00094     DEBUG_PRINT("Thread enter\n");
00095     
00096     while (this->isRunning())
00097     {
00098         rc = 1;
00099         
00100         rc = playerc_client_peek(this->m_client->m_player_client,10);
00101         phCHECK_RC(rc,NULL,"playerc_client_peek");
00102         
00103         //phPROGRESS("peek?%d\n",rc);
00104         
00105         DEBUG_PRINT("Thread loop\n");
00106         if (rc == 1)
00107         {
00108             prc = playerc_client_read(this->m_client->m_player_client);
00109             //phPRINT_PTR(prc,NULL,"playerc_client_read");
00110             
00111             /*phPROGRESS("prc = %p\n",prc );*/
00112 
00113             if (prc != NULL)
00114             {
00115                 phMUTEX_LOCK(this->m_devices,devices_locked);
00116                 
00117                 node = this->m_devices.getHeadObject();
00118                 
00119                 /*  Loop through all the devices in the list */
00120                 while (node != NULL)
00121                 {
00122                     device = (phPlayerDevice *)node->getObject();
00123                     
00124                     if ((device != NULL) && (device->getId() == prc))
00125                     {
00126                         /*
00127                         phPROGRESS("prc = %p : device = %s\n",
00128                                  prc, device->getName() );
00129                         */
00130                         rc = device->updateData();
00131                         phPRINT_RC(rc,NULL,"device->updateData()");
00132                     }
00133                     
00134                     node = node->getNextObject();
00135                 }
00136                 
00137                 phMUTEX_UNLOCK(this->m_devices,devices_locked);
00138             }
00139         }
00140 
00141         phYield();
00142     }
00143     
00144     DEBUG_PRINT("Thread exit\n");
00145     
00146     return phSUCCESS;
00147 error:
00148     rc = this->signal_error();
00149     phPRINT_RC(rc,NULL,"this->signal_error");
00150     
00151     phPROGRESS("Thread exit on error\n");
00152     
00153     return phFAIL;
00154 }
00155 
00156 /* ------------------------------------------------------------------------ */
00157 int phPlayerClient::set( char *host, uint16_t port )
00158 {
00159     phFUNCTION("phPlayerClient::set")
00160     int locked = 0;
00161     char        *thost  = host;
00162     uint16_t    tport   = port;
00163 
00164     if (this->m_client == NULL) return phFAIL;
00165 
00166     phTHIS_LOCK(locked);
00167 
00168     if (this->m_is_connected)
00169     {
00170         rc = this->disconnect();
00171         phPRINT_RC(rc,NULL,"this->disconnect");
00172     }
00173 
00174     if (host == NULL)
00175         host = "localhost";
00176     if ((port == 0) || (port > 65536))
00177         port = 6665;
00178     
00179     if (this->m_client->m_player_client != NULL)
00180     {
00181         playerc_client_destroy(this->m_client->m_player_client);
00182         this->m_client->m_player_client = NULL;
00183         this->m_is_connected = 0;
00184     }
00185 
00186     if (this->m_client->m_player_client == NULL)
00187     {
00188         this->m_client->m_player_client = 
00189                 playerc_client_create(NULL,host,port);
00190         phPRINT_PTR(this->m_client->m_player_client,
00191                     "playerc_client_create","playerc_client_create");
00192         if (this->m_client->m_player_client != NULL)
00193         {
00194             this->m_is_connected = 0;
00195         }
00196     }   
00197     
00198     phTHIS_UNLOCK(locked);
00199     
00200     return phSUCCESS;
00201 error:
00202     phTHIS_LOOSE_UNLOCK(locked);
00203     
00204     return phFAIL;
00205 }
00206 
00207 /* ------------------------------------------------------------------------ */
00208 int phPlayerClient::connect(char *host, uint16_t port)
00209 {
00210     phFUNCTION("phPlayerClient::connect")
00211     int locked = 0;
00212 
00213     if (this->m_client == NULL) return phFAIL;
00214 
00215     phTHIS_LOCK(locked);
00216 
00217     if ((host != NULL) || (port != 0))
00218     {
00219         rc = this->set(host,port);
00220         phPRINT_RC(rc,NULL,"this->set(host:%s,port:%hd)",host,port);
00221     }
00222    
00223     if ((this->m_client->m_player_client != NULL) && 
00224         (!this->m_is_connected))
00225     {
00226         rc = playerc_client_connect(this->m_client->m_player_client);
00227         PLAYER_PRINT_RC(rc,"playerc_client_connect","playerc_client_connect");
00228 
00229         if (rc == 0)
00230             this->m_is_connected = 1;
00231         else
00232             this->m_is_connected = 0;
00233     }
00234     phTHIS_UNLOCK(locked);
00235     
00236     return phSUCCESS;
00237 error:
00238     phTHIS_ERROR_UNLOCK(locked);
00239 
00240     return phFAIL;
00241 }
00242 /* ------------------------------------------------------------------------ */
00243 int phPlayerClient::disconnect()
00244 {
00245     phFUNCTION("phPlayerClient::disconnect")
00246     int locked = 0;
00247 
00248     if (this->m_client == NULL) return phFAIL;
00249     
00250     phTHIS_LOCK(locked);
00251 
00252     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00253     {
00254         rc = playerc_client_disconnect(this->m_client->m_player_client);
00255         PLAYER_PRINT_RC(rc,"playerc_client_disconnect","playerc_client_disconnect");
00256 
00257         this->m_is_connected = 0;
00258     }
00259 
00260     phTHIS_UNLOCK(locked);
00261     
00262     return phSUCCESS;
00263 error:
00264     phTHIS_ERROR_UNLOCK(locked);
00265 
00266     return phFAIL;
00267 }
00268 /* ------------------------------------------------------------------------ */
00269 int phPlayerClient::datamode(int phmode)
00270 {
00271     phFUNCTION("phPlayerClient::datamode")
00272     int locked = 0;
00273     int mode = 0;
00274 
00275     if (this->m_client == NULL) return phFAIL;
00276     
00277     phTHIS_LOCK(locked);
00278 
00279     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00280     {
00281         switch(phmode)
00282         {
00283             case phDATAMODE_PUSH_ALL:
00284                 mode = PLAYERC_DATAMODE_PUSH_ALL;
00285                 break;
00286             case phDATAMODE_PUSH_NEW:
00287                 mode = PLAYERC_DATAMODE_PUSH_NEW;
00288                 break;
00289             case phDATAMODE_PULL_ALL:
00290                 mode = PLAYERC_DATAMODE_PULL_ALL;
00291                 break;
00292             case phDATAMODE_PULL_NEW:
00293                 mode = PLAYERC_DATAMODE_PULL_NEW;
00294                 break;
00295             case phDATAMODE_PUSH_ASYNC:
00296                 mode = PLAYERC_DATAMODE_PUSH_ASYNC;
00297                 break;
00298             default:
00299                 mode = PLAYERC_DATAMODE_PUSH_ASYNC;
00300                 break;
00301         }
00302         rc = playerc_client_datamode(this->m_client->m_player_client,mode);
00303         PLAYER_PRINT_RC(rc,"playerc_client_datamode","playerc_client_datamode");
00304     }
00305 
00306     phTHIS_UNLOCK(locked);
00307     
00308     return phSUCCESS;
00309 error:
00310     phTHIS_ERROR_UNLOCK(locked);
00311 
00312     return phFAIL;
00313 }
00314 /* ------------------------------------------------------------------------ */
00315 int phPlayerClient::requestdata()
00316 {
00317     phFUNCTION("phPlayerClient::requestdata")
00318     int locked = 0;
00319 
00320     if (this->m_client == NULL) return phFAIL;
00321     
00322     phTHIS_LOCK(locked);
00323 
00324     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00325     {
00326         rc = playerc_client_requestdata(this->m_client->m_player_client);
00327         phPRINT_RC(rc,"playerc_client_requestdata","playerc_client_requestdata");
00328     }
00329 
00330     phTHIS_UNLOCK(locked);
00331     
00332     return phSUCCESS;
00333 error:
00334     phTHIS_ERROR_UNLOCK(locked);
00335 
00336     return phFAIL;
00337 }
00338 /* ------------------------------------------------------------------------ */
00339 int phPlayerClient::datafreq(int freq)
00340 {
00341     phFUNCTION("phPlayerClient::datafreq")
00342     int locked = 0;
00343 
00344     if (this->m_client == NULL) return phFAIL;
00345     
00346     phTHIS_LOCK(locked);
00347 
00348     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00349     {
00350         rc = playerc_client_datafreq(this->m_client->m_player_client,freq);
00351         PLAYER_PRINT_RC(rc,"playerc_client_datafreq","playerc_client_datafreq");
00352     }
00353 
00354     phTHIS_UNLOCK(locked);
00355     
00356     return phSUCCESS;
00357 error:
00358     phTHIS_ERROR_UNLOCK(locked);
00359 
00360     return phFAIL;
00361 }
00362 
00363 /* ------------------------------------------------------------------------ */
00364 int phPlayerClient::getDevList()
00365 {
00366     phFUNCTION("phPlayerClient::getDevList")
00367     int locked = 0;
00368 
00369     if (this->m_client == NULL) return phFAIL;
00370     
00371     phTHIS_LOCK(locked);
00372 
00373     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00374     {
00375         rc = playerc_client_get_devlist(this->m_client->m_player_client);
00376         PLAYER_PRINT_RC(rc,"playerc_client_get_devlist","playerc_client_get_devlist");
00377     }
00378 
00379     phTHIS_UNLOCK(locked);
00380     
00381     return phSUCCESS;
00382 error:
00383     phTHIS_ERROR_UNLOCK(locked);
00384 
00385     return phFAIL;
00386 }
00387 
00388 /* ------------------------------------------------------------------------ */
00389 int phPlayerClient::peek( int timeout )
00390 {
00391     phFUNCTION("phPlayerClient::peek")
00392     int retrc = 0;
00393     int locked = 0;
00394 
00395     if (this->m_client == NULL) return phFAIL;
00396     
00397     phTHIS_LOCK(locked);
00398 
00399     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00400     {
00401         rc = playerc_client_peek(this->m_client->m_player_client,timeout);
00402         if (rc > 0)
00403         {
00404             retrc = rc;
00405             rc = 0;
00406         }
00407         PLAYER_PRINT_RC(rc,"playerc_client_peek","playerc_client_peek");
00408     }
00409 
00410     phTHIS_UNLOCK(locked);
00411     
00412     return retrc;
00413 error:
00414     phTHIS_ERROR_UNLOCK(locked);
00415 
00416     return phFAIL;
00417 }
00418 
00419 /* ------------------------------------------------------------------------ */
00420 void *phPlayerClient::read( )
00421 {
00422     phFUNCTION("phPlayerClient::read")
00423     void *ptr = NULL;
00424     int locked = 0;
00425 
00426     if (this->m_client == NULL) return NULL;
00427     
00428     phTHIS_LOCK(locked);
00429 
00430     if ((this->m_client->m_player_client != NULL) && (this->m_is_connected))
00431     {
00432         ptr = playerc_client_read(this->m_client->m_player_client);
00433         PLAYER_PRINT_PTR(ptr,"playerc_client_read","playerc_client_read");
00434     }
00435 
00436     phTHIS_UNLOCK(locked);
00437     
00438     return ptr;
00439 error:
00440     phTHIS_ERROR_UNLOCK(locked);
00441     
00442     return NULL;
00443 }
00444  
00445 /* ------------------------------------------------------------------------ */
00446 void *phPlayerClient::getClientPtr()
00447 {
00448     if (this->m_client == NULL) return NULL;
00449     return (void *)this->m_client->m_player_client;
00450 }
00451 
00452 /* ------------------------------------------------------------------------ */
00453 int phPlayerClient::subscribeDevice  ( phObject *device )
00454 {
00455     phFUNCTION("phPlayerClient::subscribeDevice")
00456     phObjectNode *objnode = NULL;
00457     int locked = 0;
00458 
00459     if (device == NULL) return phFAIL;
00460     
00461     if (!(device->hasParent("phPlayerDevice"))) return phFAIL;
00462 
00463     phTHIS_LOCK(locked);
00464     
00465     objnode = this->m_devices.pushObject(device);
00466     phCHECK_PTR(objnode,NULL,"m_devices.pushObject");
00467 
00468     phTHIS_UNLOCK(locked);
00469 
00470     return phSUCCESS;
00471 error:
00472     phTHIS_ERROR_UNLOCK(locked);
00473     
00474     return phFAIL;
00475 }
00476 
00477 /* ------------------------------------------------------------------------ */
00478 int phPlayerClient::unsubscribeDevice( phObject *device )
00479 {
00480     phFUNCTION("phPlayerClient::unsubscribeDevice")
00481     phObject *obj = NULL;
00482     int locked = 0;
00483 
00484     if (device == NULL) return phFAIL;
00485 
00486     if (!(device->hasParent("phPlayerDevice"))) return phFAIL;
00487 
00488     phTHIS_LOCK(locked);
00489     
00490     obj = this->m_devices.removeObject(device);
00491     phCHECK_PTR(obj,NULL,"this->m_devices.removeObject");
00492     
00493     phTHIS_UNLOCK(locked);
00494 
00495     return phSUCCESS;
00496 error:
00497     phTHIS_ERROR_UNLOCK(locked);
00498     
00499     return phFAIL;
00500 
00501 }
00502 




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:07 2007 for phission by  doxygen 1.4.4