00001
00002
00003
00004
00005
00006 #ifndef __ProcessFramework_h__
00007 #define __ProcessFramework_h__
00008
00009 #include <OPENR/OObject.h>
00010 #include <OPENR/OSubject.h>
00011 #include <OPENR/OObserver.h>
00012 #include <ObjectEntryTable.h>
00013 #include <string.h>
00014 #include "Platform/SystemCall.h"
00015 #include "Platform/GTAssert.h"
00016
00017 const unsigned int ENTRY_TABLE_MIN = 5,
00018 ENTRY_TABLE_MAX = 100,
00019 NAME_LENGTH_MAX = 100,
00020 PROCESS_MAX = 1,
00021 ROBOT_MAX = 1;
00022
00023 class PlatformProcess;
00024 class ProcessBase;
00025
00026 #include "Sender.h"
00027 #include "MotorCommandsSender.h"
00028 #include "SoundDataSender.h"
00029 #include "SharedSender.h"
00030 #include "Receiver.h"
00031 #include "SharedReceiver.h"
00032
00033 #include "IPEndpoint.h"
00034
00035
00036
00037
00038 class PlatformProcess
00039 {
00040 private:
00041 SenderList* firstSender;
00042 ReceiverList* firstReceiver;
00043
00044 public:
00045
00046
00047
00048 PlatformProcess()
00049 {
00050 firstSender = 0;
00051 firstReceiver = 0;
00052 }
00053
00054
00055
00056
00057
00058
00059 SenderList*& getFirstSender() {return firstSender;}
00060
00061
00062
00063
00064
00065
00066 ReceiverList*& getFirstReceiver() {return firstReceiver;}
00067 };
00068
00069
00070
00071
00072
00073
00074 class ProcessBase : public OObject
00075 {
00076 public:
00077 static ProcessBase* theInstance;
00078 void getAntInformation(int*& listenContSelector, int*& sendContSelector,
00079 int*& receiveContSelector, int*& closeContSelector,
00080 int*& connectContSelector);
00081 OID* getOID(){
00082 return (&myOID_);
00083 }
00084
00085 private:
00086 static int blockMask,
00087 eventMask;
00088 bool running;
00089
00090
00091
00092
00093
00094 static void aperiosInit(OReasonMessage* reasonMsg);
00095
00096
00097
00098
00099
00100 static void aperiosStart(OReasonMessage* reasonMsg);
00101
00102
00103
00104
00105
00106 static void aperiosStop(OReasonMessage* reasonMsg);
00107
00108
00109
00110
00111
00112 static void aperiosDestroy(OReasonMessage* reasonMsg);
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 static void awakeOnTimer(OReasonMessage* reasonMsg);
00123
00124
00125
00126
00127
00128 static void antConnectCont(antEnvMsg* msg);
00129 int antConnectContSelector;
00130
00131 static void antReceiveCont(antEnvMsg* msg);
00132 int antReceiveContSelector;
00133
00134 static void antSendCont(antEnvMsg* msg);
00135 int antSendContSelector;
00136
00137 static void antListenCont(antEnvMsg* msg);
00138 int antListenContSelector;
00139
00140 static void antCloseCont(antEnvMsg* msg);
00141 int antCloseContSelector;
00142
00143
00144
00145
00146 static void nextFrame();
00147
00148 protected:
00149
00150
00151
00152
00153 void init();
00154
00155
00156
00157
00158 virtual void processNextFrame() = 0;
00159
00160
00161
00162
00163
00164 virtual PlatformProcess* getProcess() = 0;
00165
00166 public:
00167
00168
00169
00170 ProcessBase():running(true)
00171 {
00172 theInstance=this;
00173 }
00174
00175
00176
00177
00178 void stop() {running = false;}
00179
00180
00181
00182
00183
00184 bool isRunning() {return running;}
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 static void setBlockingId(int id,bool block = true);
00195
00196
00197
00198
00199
00200
00201
00202 static void setEventId(int id);
00203
00204
00205
00206
00207
00208 static int getEventMask() {return eventMask;}
00209
00210
00211
00212
00213 static void resetEventMask() {eventMask = 0;}
00214 };
00215
00216
00217
00218
00219
00220
00221 template<class T> class ProcessCreator : public ProcessBase
00222 {
00223 private:
00224 T* process;
00225 const char* name;
00226 int lastTime;
00227
00228
00229
00230
00231
00232
00233
00234 virtual OStatus DoInit(const OSystemEvent& event)
00235 {
00236 setBlockingId(31);
00237 int id = ENTRY_TABLE_MIN;
00238 for(ReceiverList* p = process->getFirstReceiver(); p; p = p->getNext())
00239 {
00240 ASSERT(strlen(name) + strlen(p->getName()) + 2 <= NAME_LENGTH_MAX);
00241 char buf[NAME_LENGTH_MAX];
00242 strcpy(buf,name);
00243 strcat(buf,".");
00244 strcat(buf,p->getName());
00245 OServiceEntry entry;
00246 entry.Set(myOID_,id++);
00247 VERIFY(RegisterServiceEntry(entry,buf) == oSUCCESS);
00248 entry.Set(myOID_,id++);
00249 VERIFY(p->SetNotifyEntry(entry) == oSUCCESS);
00250 }
00251 for(SenderList* q = process->getFirstSender(); q; q = q->getNext())
00252 {
00253 ASSERT(strlen(name) + strlen(q->getName()) + 2 <= NAME_LENGTH_MAX);
00254 char buf[NAME_LENGTH_MAX];
00255 strcpy(buf,name);
00256 strcat(buf,".");
00257 strcat(buf,q->getName());
00258 OServiceEntry entry;
00259 entry.Set(myOID_,id++);
00260 VERIFY(RegisterServiceEntry(entry,buf) == oSUCCESS);
00261 entry.Set(myOID_,id++);
00262 VERIFY(q->SetReadyEntry(entry) == oSUCCESS);
00263 }
00264 return oSUCCESS;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273 virtual OStatus DoStart(const OSystemEvent& event)
00274 {
00275 for(ReceiverList* p = process->getFirstReceiver(); p; p = p->getNext())
00276 p->AssertReady();
00277
00278 setEventId(31);
00279 return oSUCCESS;
00280 };
00281
00282
00283
00284
00285
00286
00287
00288
00289 virtual OStatus DoStop(const OSystemEvent& event)
00290 {
00291 stop();
00292 for(ReceiverList* p = process->getFirstReceiver(); p; p = p->getNext())
00293 p->DeassertReady();
00294 if(process)
00295 delete process;
00296 process = 0;
00297 return oSUCCESS;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307 virtual OStatus DoDestroy(const OSystemEvent& event) {return oSUCCESS;}
00308
00309
00310
00311
00312
00313
00314 void processNextFrame()
00315 {
00316 int frameTime = process->processMain();
00317 if(getProcess()->getFirstSender())
00318 getProcess()->getFirstSender()->finishFrame();
00319 if(getProcess()->getFirstReceiver())
00320 process->getFirstReceiver()->finishFrame();
00321 resetEventMask();
00322 int currentTime = SystemCall::getCurrentSystemTime();
00323 if(frameTime < 0)
00324 frameTime = lastTime - frameTime - currentTime;
00325 if(frameTime)
00326 {
00327 if(frameTime < 4)
00328 frameTime = 4;
00329 TimeEventInfoWithRelativeTime timeEvent(RelativeTime(frameTime / 1000,frameTime % 1000));
00330 EventID eventId;
00331 VERIFY(SetTimeEvent(&timeEvent,myOID_,Selector(4),0,0,&eventId) == sSUCCESS);
00332 }
00333 setBlockingId(31,frameTime != 0);
00334 lastTime = currentTime;
00335 }
00336
00337
00338
00339
00340
00341 virtual PlatformProcess* getProcess() {return process;}
00342
00343 public:
00344
00345
00346
00347
00348
00349 ProcessCreator(const char* name)
00350 {
00351 this->name = name;
00352 VERIFY(process = new T);
00353 init();
00354 lastTime = 0;
00355 }
00356 };
00357
00358
00359
00360
00361
00362
00363
00364
00365 #define MAKE_PROCESS(theClass) \
00366 ProcessCreator<theClass> theProcess(#theClass)
00367
00368
00369
00370
00371
00372
00373 #define GT_GLOBAL
00374
00375 #endif //__ProcessFramework_h__