Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

src/Dispatcher.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Traplas visualisation.
00004 
00005 Copyright (C) 2006 Herbert de Vos & Willem Drost
00006 
00007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00010 
00011 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00012 
00013 (For full Licence see ../GPL-Licence.txt)
00014 
00015 */
00016 
00017 #ifndef _GLIBCXX_IOSTREAM
00018 #include <iostream>
00019 #endif
00020 
00021 #ifndef _GLIBCXX_SSTREAM
00022 #include <sstream>
00023 #endif
00024 
00025 #ifndef MESSAGECONTROLLER_H
00026 #include "MessageController.h"
00027 #endif
00028 
00029 #ifndef DISPATCHER_H
00030 #include "../include/Dispatcher.h"
00031 #endif
00032 
00033 #ifndef STATECONTROL_H
00034 #include "../include/StateControl.h"
00035 #endif
00036 
00037 #ifndef _PTHREAD_H
00038 #include <pthread.h>
00039 #endif
00040 
00041 #ifndef GLOBALS_H
00042 #include "Globals.h"
00043 #endif
00044 
00045 #ifndef _GLIBCXX_STDEXCEPT
00046 #include <stdexcept>
00047 #endif
00048 
00049 Dispatcher::Dispatcher(){
00050         world = new StateControl(this);
00051         mc = new MessageController(this);
00052         parseQ = shared_ptr< queue< shared_ptr< vector< shared_ptr<string> > > > >( new queue< shared_ptr< vector< shared_ptr<string> > > >() );
00053 
00054         mutex_ptr = new pthread_mutex_t();
00055         cond_ptr = new pthread_cond_t();
00056         pthread_mutex_init(mutex_ptr, NULL);
00057         pthread_cond_init(cond_ptr, NULL);
00058 }
00059 
00060 Dispatcher::~Dispatcher(){
00061         delete world;
00062         delete mc;
00063 }
00064 
00065 /*Public functions:*/
00066 
00067 bool Dispatcher::proccessMessageFromMC(shared_ptr< vector< shared_ptr< string > > > msg){
00068         //place received message in queue. This thing is called from the outside.
00069 
00070         if ( pthread_mutex_trylock(mutex_ptr) != 0 )
00071             pthread_cond_wait(cond_ptr, mutex_ptr);
00072 
00073         parseQ->push(msg);
00074         pthread_mutex_unlock(mutex_ptr);
00075         pthread_cond_signal(cond_ptr);
00076         return true;
00077 }
00078 
00079 bool Dispatcher::dispatchMessage(){
00080         //      Parse anything in the queue. Call the correct methods in StateControl.
00081 
00082         //      Only try to lock the mutex, if not succesfull, better luck next time! This to prevent the OSG-mainloop from blocking.
00083         if ( pthread_mutex_trylock(mutex_ptr) != 0 ) {
00084                 while(!(parseQ->empty())) {
00085                         shared_ptr< vector< shared_ptr< string > > > msg;
00086                         msg = (parseQ->front());
00087                         parseQ->pop();
00088 
00089                         shared_ptr< string > msgIdent = msg->at(0);
00090 
00091                         if(*msgIdent == "DRV")
00092                                 drvparse(msg);
00093                         else if(*msgIdent == "LOAD")
00094                                 loadparse(msg);
00095                         else if(*msgIdent == "UNLOAD")
00096                                 unloadparse(msg);
00097                         else if(*msgIdent == "LOCATION")
00098                                 locationparse(msg);
00099                         else if(*msgIdent == "SETSPDINVIZ")
00100                                 setspdparse(msg);
00101                         else if(*msgIdent == "TRNEW")
00102                                 trnewparse(msg);
00103                         else if(*msgIdent == "TRDEL")
00104                                 trdelparse(msg);
00105                         else if(*msgIdent == "NEWCARG")
00106                                 newcargparse(msg);
00107                         else if(*msgIdent == "RMCARG")
00108                                 rmcargparse(msg);
00109                         else if(*msgIdent == "ARC")
00110                                 arcparse(msg);
00111                         else {
00112                                 cout << "In dispatchMessage(): Message ( " << *msgIdent << " ) not recognised." << endl;
00113                         }
00114                 }
00115                 pthread_mutex_unlock(mutex_ptr);
00116                 pthread_cond_signal(cond_ptr);
00117         }
00118 
00119         return true;
00120 }
00121 
00122 
00123 bool Dispatcher::setSpdInSim(int R, double fraction, double ts, double te){
00124         //compose a message, send it to mc
00125         shared_ptr< vector< shared_ptr< string > > > spd_msg;
00126         ostringstream s;
00127 
00128         //      Create the message contents.
00129         shared_ptr< string > temp0( new string("SETSPDINSIM") );
00130         spd_msg->push_back( temp0 );
00131 
00132         shared_ptr< string > temp1( new string() );
00133         spd_msg->push_back( temp1 );
00134 
00135         s << R;
00136         shared_ptr< string > temp2( new string( s.str() ) );
00137         spd_msg->push_back( temp2 );
00138         s.str("");
00139 
00140         s << fraction;
00141         shared_ptr< string > temp3( new string( s.str() ) );
00142         spd_msg->push_back( temp3 );
00143         s.str("");
00144 
00145         s << ts;
00146         shared_ptr< string > temp4( new string( s.str() ) );
00147         spd_msg->push_back( temp4 );
00148         s.str("");
00149 
00150         s << te;
00151         shared_ptr< string > temp5( new string( s.str() ) );
00152         spd_msg->push_back( temp5 );
00153         s.str("");
00154 
00155         //      Send the message.
00156         mc->sendMessageToComm(spd_msg);
00157         return true;
00158 }
00159 
00160 bool Dispatcher::order(int Ris, int Rid, double ts, double tf){
00161         //compose a message, send it to mc
00162         shared_ptr< vector< shared_ptr< string > > > ord_msg;
00163         ostringstream s;
00164 
00165         //      Create the message contents
00166         shared_ptr< string > temp0( new string("ORDER") );
00167         ord_msg->push_back( temp0 );
00168 
00169         shared_ptr< string > temp1( new string() );
00170         ord_msg->push_back( temp1 );
00171 
00172         s << Ris;
00173         shared_ptr< string > temp2( new string( s.str() ) );
00174         ord_msg->push_back( temp2 );
00175         s.str("");
00176 
00177         s << Rid;
00178         shared_ptr< string > temp3( new string( s.str() ) );
00179         ord_msg->push_back( temp3 );
00180         s.str("");
00181 
00182         s << ts;
00183         shared_ptr< string > temp4( new string( s.str() ) );
00184         ord_msg->push_back( temp4 );
00185         s.str("");
00186 
00187         s << tf;
00188         shared_ptr< string > temp5( new string( s.str() ) );
00189         ord_msg->push_back( temp5 );
00190         s.str("");
00191 
00192         //      Send the message.
00193         mc->sendMessageToComm(ord_msg);
00194         return true;
00195 }
00196 
00197 bool Dispatcher::communicationMode(int cMode, string fileName){
00198         //set communication mode of MessageController.
00199         return(mc->communicationMode(cMode, fileName));
00200 }
00201 
00202 bool Dispatcher::communicationMode(int cMode, string thisName, string thatName){
00203         //set communication mode of MessageController.
00204         return(mc->communicationMode(cMode, thisName, thatName));
00205 }
00206 
00207 bool Dispatcher::setTimeFlow(double flowFactor){
00208         return(mc->setTimeFlow(flowFactor));
00209 }
00210 
00211 double Dispatcher::getTimeFlow(){
00212         return(mc->getTimeFlow());
00213 }
00214 
00215 double Dispatcher::getTime(){
00216         return(mc->getVisualisationTime());
00217 }
00218 
00219 
00220 /*Private functions: parsing*/
00221 bool Dispatcher::drvparse(shared_ptr< vector< shared_ptr< string > > > msg){
00222         try {
00223                 int Rt = atoi(msg->at(2)->c_str());
00224                 int RiFrom = atoi(msg->at(3)->c_str());
00225                 int RiTo = atoi(msg->at(4)->c_str());
00226                 int RiD = atoi(msg->at(5)->c_str());
00227                 double ts = atof(msg->at(6)->c_str());
00228                 double te = atof(msg->at(7)->c_str());
00229         
00230                 //      Check if all conversions were succesfull.
00231                 if (    (Rt == 0 && msg->at(2)->at(0) != '0') ||
00232                                 (RiFrom == 0 && msg->at(3)->at(0) != '0') ||
00233                                 (RiTo == 0 && msg->at(4)->at(0) != '0') ||
00234                                 (RiD == 0 && msg->at(5)->at(0) != '0') ||
00235                                 (ts == 0 && msg->at(6)->at(0) != '0') ||
00236                                 (te == 0 && msg->at(7)->at(0) != '0') ){
00237         
00238                         cout << "Warning: (Dispatcher::drvparse) message malformed: Conversion error." << endl;
00239                         #if STRICT
00240                                 assert( false );
00241                         #endif
00242                 }
00243                 
00244                 return world->drv(Rt, RiFrom, RiTo, RiD, ts, te);
00245         } catch ( out_of_range ) {
00246                 cout << "Warning: (Dispatcher::drvparse) message malformed: Message too short." << endl;                
00247                 assert( false );        
00248         }  
00249 }
00250 
00251 bool Dispatcher::loadparse(shared_ptr< vector< shared_ptr< string > > > msg){
00252         try{
00253                 int Rt = atoi(msg->at(2)->c_str());
00254                 int Ri = atoi(msg->at(3)->c_str());
00255                 int C = atoi(msg->at(4)->c_str());
00256                 double ts = atof(msg->at(5)->c_str());
00257                 double tf = atof(msg->at(6)->c_str());
00258         
00259                 //      Check if all conversions were succesfull.
00260                 if (    (Rt == 0 && msg->at(2)->at(0) != '0') ||
00261                                 (Ri == 0 && msg->at(3)->at(0) != '0') ||
00262                                 (C == 0 && msg->at(4)->at(0) != '0') ||
00263                                 (ts == 0 && msg->at(5)->at(0) != '0') ||
00264                                 (tf == 0 && msg->at(6)->at(0) != '0') ){
00265         
00266                         cout << "Warning: (Dispatcher::loadparse) message malformed: Conversion error." << endl;
00267                         #if STRICT
00268                                 assert( false );
00269                         #endif
00270                 }
00271         
00272                 return world->load(Rt, Ri, C, ts, tf);
00273         } catch ( out_of_range ) {
00274                 cout << "Warning: (Dispatcher::loadparse) message malformed: Message too short." << endl;               
00275                 assert( false );                
00276         }  
00277 }
00278 
00279 bool Dispatcher::unloadparse(shared_ptr< vector< shared_ptr< string > > > msg){
00280         try{    
00281                 int Rt = atoi(msg->at(2)->c_str());
00282                 int Ri = atoi(msg->at(3)->c_str());
00283                 int C = atoi(msg->at(4)->c_str());
00284                 double ts = atof(msg->at(5)->c_str());
00285                 double tf = atof(msg->at(6)->c_str());
00286         
00287                 //      Check if all conversions were succesfull.
00288                 if (    (Rt == 0 && msg->at(2)->at(0) != '0') ||
00289                                 (Ri == 0 && msg->at(3)->at(0) != '0') ||
00290                                 (C == 0 && msg->at(4)->at(0) != '0') ||
00291                                 (ts == 0 && msg->at(5)->at(0) != '0') ||
00292                                 (tf == 0 && msg->at(6)->at(0) != '0') ){
00293         
00294                         cout << "Warning: (Dispatcher::unloadparse) message malformed: Conversion error." << endl;
00295                         #if STRICT
00296                                 assert( false );
00297                         #endif
00298                 }
00299         
00300                 return world->unload(Rt,Ri,C, ts, tf);
00301         } catch ( out_of_range ) {
00302                 cout << "Warning: (Dispatcher::unloadparse) message malformed: Message too short." << endl;             
00303                 assert( false );                
00304         }  
00305 }
00306 
00307 bool Dispatcher::setspdparse(shared_ptr< vector< shared_ptr< string > > > msg){
00308         try{    
00309                 int R  = atoi(msg->at(2)->c_str());
00310                 double f = atof(msg->at(3)->c_str());
00311         
00312                 //      Check if all conversions were succesfull.
00313                 if (    (R == 0 && msg->at(2)->at(0) != '0') ||
00314                                 (f == 0 && msg->at(3)->at(0) != '0') ){
00315         
00316                         cout << "Warning: (Dispatcher::setspdparse) message malformed: Conversion error." << endl;
00317                         #if STRICT
00318                                 assert( false );
00319                         #endif
00320                 }
00321         
00322                 return world->setspdinviz(R, f);
00323         } catch ( out_of_range ) {
00324                 cout << "Warning: (Dispatcher::setspdparse) message malformed: Message too short." << endl;             
00325                 assert( false );                
00326         }  
00327 }
00328 
00329 bool Dispatcher::locationparse(shared_ptr< vector< shared_ptr< string > > > msg){
00330         try{    
00331                 int id = atoi(msg->at(2)->c_str());
00332                 shared_ptr<string> desc = msg->at(3);
00333                 unsigned int cap = atoi(msg->at(4)->c_str());
00334                 double dist = atof(msg->at(5)->c_str());
00335                 double spd = atof(msg->at(6)->c_str());
00336                 int tile = atoi(msg->at(7)->c_str());
00337                 double w = atof(msg->at(8)->c_str());
00338                 double h = atof(msg->at(9)->c_str());
00339                 double x = atof(msg->at(10)->c_str());
00340                 double y = atof(msg->at(11)->c_str());
00341                 double rot = atof(msg->at(12)->c_str());
00342         
00343                 //      Check if all conversions were succesfull.
00344                 if (    (id == 0 && msg->at(2)->at(0) != '0') ||
00345                                 (cap == 0 && msg->at(4)->at(0) != '0') ||
00346                                 (dist == 0 && msg->at(5)->at(0) != '0') ||
00347                                 (spd == 0 && msg->at(6)->at(0) != '0') ||
00348                                 (tile == 0 && msg->at(7)->at(0) != '0') ||
00349                                 (w == 0 && msg->at(8)->at(0) != '0') ||
00350                                 (h == 0 && msg->at(9)->at(0) != '0') ||
00351                                 (x == 0 && msg->at(10)->at(0) != '0') ||
00352                                 (y == 0 && msg->at(11)->at(0) != '0') ||
00353                                 (rot == 0 && msg->at(12)->at(0) != '0') ){
00354         
00355                         cout << "Warning: (Dispatcher::locationparse) message malformed: Conversion error." << endl;
00356                         #if STRICT
00357                                 assert( false );
00358                         #endif
00359                 }
00360         
00361                 return world->location(id, desc, cap, dist, spd, tile, w, h, x, y, rot);
00362         } catch ( out_of_range ) {
00363                 cout << "Warning: (Dispatcher::locationparse) message malformed: Message too short." << endl;           
00364                 assert( false );                
00365         }
00366 }
00367 
00368 bool Dispatcher::trnewparse(shared_ptr< vector< shared_ptr< string > > > msg){
00369         try{
00370                 int Rt = atoi(msg->at(2)->c_str());
00371                 shared_ptr<string> desc = msg->at(3);
00372                 int Ri = atoi(msg->at(4)->c_str());
00373                 double s = atof(msg->at(5)->c_str());
00374         
00375                 //      Check if all conversions were succesfull.
00376                 if (    (Rt == 0 && msg->at(2)->at(0) != '0') ||
00377                                 (Ri == 0 && msg->at(4)->at(0) != '0') ||
00378                                 (s == 0 && msg->at(5)->at(0) != '0') ){
00379         
00380                         cout << "Warning: (Dispatcher::trnewparse) message malformed: Conversion error." << endl;
00381                         #if STRICT
00382                                 assert( false );
00383                         #endif
00384                 }
00385         
00386                 return world->trnew(Rt, desc, Ri, s);
00387         } catch ( out_of_range ) {
00388                 cout << "Warning: (Dispatcher::trnewparse) message malformed: Message too short." << endl;              
00389                 assert( false );                
00390         }
00391 }
00392 
00393 bool Dispatcher::trdelparse(shared_ptr< vector< shared_ptr< string > > > msg){
00394         try{    
00395                 int Rt = atoi(msg->at(2)->c_str());
00396         
00397                 //      Check if all conversions were succesfull.
00398                 if ( (Rt == 0 && msg->at(2)->at(0) != '0') ){
00399         
00400                         cout << "Warning: (Dispatcher::trdelparse) message malformed: Conversion error." << endl;
00401                         #if STRICT
00402                                 assert( false );
00403                         #endif
00404                 }
00405         
00406                 return world->trdel(Rt);
00407         } catch ( out_of_range ) {
00408                 cout << "Warning: (Dispatcher::trdelparse) message malformed: Message too short." << endl;              
00409                 assert( false );                
00410         }
00411 }
00412 
00413 bool Dispatcher::newcargparse(shared_ptr< vector< shared_ptr< string > > > msg){
00414         try{    
00415                 int C = atoi(msg->at(2)->c_str());
00416                 shared_ptr<string> desc = msg->at(3);
00417                 int Ri = atoi(msg->at(4)->c_str());
00418         
00419                 //      Check if all conversions were succesfull.
00420                 if (    (C == 0 && msg->at(2)->at(0) != '0') ||
00421                                 (Ri == 0 && msg->at(4)->at(0) != '0') ){
00422         
00423                         cout << "Warning: (Dispatcher::newcargparse) message malformed: Conversion error." << endl;
00424                         #if STRICT
00425                                 assert( false );
00426                         #endif
00427                 }
00428         
00429                 return world->newcarg(C, desc, Ri);
00430         } catch ( out_of_range ) {
00431                 cout << "Warning: (Dispatcher::newcargparse) message malformed: Message too short." << endl;            
00432                 assert( false );                
00433         }
00434 }
00435 
00436 bool Dispatcher::rmcargparse(shared_ptr< vector< shared_ptr< string > > > msg){
00437         try{    
00438                 int C = atoi(msg->at(2)->c_str());
00439         
00440                 //      Check if all conversions were succesfull.
00441                 if ( (C == 0 && msg->at(2)->at(0) != '0') ){
00442         
00443                         cout << "Warning: (Dispatcher::rmcargparse) message malformed: Conversion error." << endl;
00444                         #if STRICT
00445                                 assert( false );
00446                         #endif
00447                 }
00448         
00449                 return world->rmcarg(C);
00450         } catch ( out_of_range ) {
00451                 cout << "Warning: (Dispatcher::rmcargparse) message malformed: Message too short." << endl;             
00452                 assert( false );                
00453         }
00454 }
00455 
00456 bool Dispatcher::arcparse(shared_ptr< vector< shared_ptr< string > > > msg){
00457         try{
00458                 int arcFrom = atoi(msg->at(2)->c_str());
00459                 int arcTo = atoi(msg->at(3)->c_str());
00460         
00461                 //      Check if all conversions were succesfull.
00462                 if (    (arcFrom == 0 && msg->at(2)->at(0) != '0') ||
00463                                 (arcTo == 0 && msg->at(3)->at(0) != '0') ){
00464         
00465                         cout << "Warning: (Dispatcher::arcparse) message malformed: Conversion error." << endl;
00466                         #if STRICT
00467                                 assert( false );
00468                         #endif
00469                 }
00470         
00471                 return world->arc(arcFrom, arcTo);
00472         } catch ( out_of_range ) {
00473                 cout << "Warning: (Dispatcher::arcparse) message malformed: Message too short." << endl;                
00474                 assert( false );                
00475         }
00476 }
00477 

Generated on Mon Jun 19 10:22:04 2006 for TraplasVisualisation by  doxygen 1.4.4