CommModule.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033
00038
00039
00040 #ifndef __COMM_MODULE_H_
00041 #define __COMM_MODULE_H_
00042
00043 #ifdef MIP_HOST_APPLE
00044
00045 #include <applePatch.h>
00046 #endif
00047
00048 #include <stdlib.h>
00049 #include <assert.h>
00050 #include <string>
00051 #include <list>
00052 #include <sstream>
00053 #include <vector>
00054 #include <utility>
00055 #include <map>
00056
00057 #include <baselib.h>
00058 #include <Resource.h>
00059 #include <Serialization.h>
00060
00061 using namespace MipBaselib;
00062
00063 namespace MipResources{
00065
00066
00067
00071 class CommNode{
00072 private:
00073 friend class boost::serialization::access;
00074 template<class Archive>
00075 void serialize(Archive & ar, const unsigned int version){
00076 ar & _ids;
00077 ar & _tasks;
00078 ar & _taskNames;
00079 }
00080
00081 vector<int> _ids;
00082 vector<int> _tasks;
00083 vector<string> _taskNames;
00084
00085
00086 static const int IDS_RESERV_SIZE = 10;
00087 static const int TASKS_RESERV_SIZE = 10;
00088
00089
00090
00091 public:
00094 CommNode();
00095
00099 CommNode(int id);
00100
00105 CommNode(int id, int task);
00106
00111 CommNode(int id, string taskname);
00112
00116 CommNode(const CommNode& n);
00117
00119 CommNode& operator=(const CommNode& n);
00120
00125 bool addId(const int id);
00126
00131 bool addTask(const int task);
00132
00137 bool addTaskName(const string taskname);
00138
00142 bool deleteId(const int id);
00143
00147 bool deleteTask(const int task);
00148
00152 bool deleteTaskName(const string taskname);
00153
00156 vector<int> ids();
00157
00160 vector<int> tasks();
00161
00164 vector<string> taskNames();
00165
00169 int getId(int i);
00170
00173 int getIdsSize();
00174
00178 int getTask(int i);
00179
00182 int getTasksSize();
00183
00187 string getTaskNames(int i);
00188
00191 int getTaskNamesSize();
00192
00196 string print();
00197 };
00198
00199
00200
00205 class CommPacket{
00206 private:
00207 friend class boost::serialization::access;
00208 template<class Archive>
00209 void serialize(Archive & ar, const unsigned int version){
00210 ar & _message;
00211 ar & _messageType;
00212 ar & _sender;
00213 ar & _recipient;
00214 ar & _time;
00215 }
00216
00217 string _message;
00218 string _messageType;
00219 CommNode _sender;
00220 CommNode _recipient;
00221 Time _time;
00222
00223 public:
00226 CommPacket();
00227
00234 CommPacket(string message, string messageType, CommNode sender, CommNode recipient);
00235
00238 CommPacket(const CommPacket& p);
00239
00241 CommPacket& operator=(const CommPacket& p);
00242
00246 bool deleteRecipientId(const int id);
00247
00251 bool deleteRecipientTask(const int task);
00252
00256 string message();
00257
00261 string messageType();
00262
00266 CommNode sender();
00267
00271 CommNode recipient();
00272
00276 Time time();
00277
00281 void setSender(const CommNode& sender);
00282
00286 void setRecipient(const CommNode& recipient);
00287
00291 string print();
00292 };
00293
00294
00298 class CommModulePar{
00299 private:
00300 protected:
00301 public:
00304 CommModulePar ();
00305
00308 CommModulePar(const CommModulePar &p);
00309
00311 CommModulePar& operator=(const CommModulePar& p);
00312 };
00313
00314
00318 typedef vector<CommPacket*> CommBox;
00319
00320
00324 typedef pair<string,CommNode> CommStoredPacketString;
00325
00326
00327
00332 class CommModuleVar{
00333 private:
00334
00335
00336 static const int PACKETS_RESERV_SIZE = 100;
00337
00338 protected:
00339
00340 public:
00341
00343
00345
00346 list<CommPacket> storedPackets;
00347
00348 pthread_mutex_t rcvMutex;
00350 map<int,CommBox> boxes;
00351 map<int,CommBox>::iterator boxesIt;
00352
00354 vector<string> assignedBoxes;
00355
00358 CommModuleVar ();
00359
00362 CommModuleVar(const CommModuleVar &v);
00363
00365 CommModuleVar& operator=(const CommModuleVar& v);
00366
00370 string print();
00371 };
00372
00373
00377 class CommModule : public Resource{
00378
00379 private:
00380
00383 bool isBox(int task);
00384
00388 bool isBox(string taskName, int& boxNumber);
00389
00391 void updateBoxes();
00392
00394 void flush();
00395
00398 bool storePacket(CommPacket packet);
00399
00400 protected:
00401
00402 CommModulePar *_par;
00403
00404 CommModuleVar *_var;
00406 bool on;
00407
00408 public:
00409
00411 CommModule(){
00412 on = false;
00413 _var = new CommModuleVar();
00414 _par = new CommModulePar();
00415 };
00416
00420 bool storePacketString(string& packetString);
00421
00424 void requestBox(int task);
00425
00428 int requestBox(string taskName);
00429
00433 bool receive(int task, CommBox & packets);
00434
00438 void receiveAck(int task, CommBox & packets);
00439
00442 void send(CommPacket packet);
00443
00447 virtual bool sendPacketString(string& packetString, CommNode recipient)=0;
00448
00452 string print();
00453
00454 };
00455
00459
00460
00461
00462 };
00463
00464
00465 #endif
00466
00467
00468