mRunCtrl.cpp

00001 /*
00002   モニタ対応の走行制御クラス
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "mRunCtrl.h"
00008 #include "parseArgs.h"
00009 #include "multiConnectionDevice.h"
00010 
00011 
00012 #ifndef PACKAGE_STR_VERSION
00013 #define PACKAGE_STR_VERSION "0.0.0"
00014 #endif
00015 
00016 bool mRunCtrl::isVersionPrinted = false;
00017 bool mRunCtrl::isHelpPrinted = false;
00018 
00019 
00020 mRunCtrl::mRunCtrl(void)
00021   : mon(vmonitor::getObject()), MonitorMode(Monitor::Unknown),
00022     cond(SDL_CreateCond()), mutex(SDL_CreateMutex()),
00023     simulator(new tRunCtrl_Simulator()), ticksPos(simulator->ticksPos) {
00024   mon->add(simulator);
00025   mon->task->setRunObject(this);
00026 }
00027 
00028 
00029 mRunCtrl::~mRunCtrl(void) {
00030   if (isConnected()) {
00031     mon->del(simulator);
00032     delete simulator;
00033   }
00034   SDL_DestroyMutex(mutex);
00035   SDL_DestroyCond(cond);
00036 }
00037 
00038 
00039 void mRunCtrl::printVersion(void) {
00040   printf("Run Ctrl Library with Monitor " PACKAGE_STR_VERSION "\n");
00041 }
00042 
00043 
00044 void mRunCtrl::printHelp(void) {
00045   printf("\n"
00046          "----- Run Ctrl Library with Monitor -----\n"
00047          "Options:\n"
00048          "None\n"
00049          "\n");
00050 }
00051 
00052 
00053 void mRunCtrl::parseArgs(int argc, char* argv[]) {
00054   bool help = false;
00055   bool version = false;
00056 
00057   for (int i = 0; i < argc; ++i) {
00058     if (!strcmp("--help", argv[i]) || !strcmp("-h", argv[i])) {
00059       help = true;
00060 
00061     } else if (!strcmp("--version", argv[i]) || !strcmp("-v", argv[i])) {
00062       version = true;
00063     }
00064   }
00065   if (version && !isVersionPrinted) {
00066     printVersion();
00067     isVersionPrinted = true;
00068   }
00069   if (help && !isHelpPrinted) {
00070     printHelp();
00071     isHelpPrinted = true;
00072   }
00073 }
00074 
00075 
00076 int mRunCtrl::connect(int argc, char *argv[]) {
00077 
00078   MonitorMode = mon->connect(argc, argv);
00079   parseArgs(argc-1, &argv[1]);
00080   return RunCtrl::connect(argc, argv);
00081 }
00082 
00083 
00084 int mRunCtrl::connect(void) {
00085   return RunCtrl::connect();
00086 }
00087 
00088 
00089 int mRunCtrl::connect(const char* device, long baudrate) {
00090 
00091   MonitorMode = mon->getMonitorMode();
00092   if (MonitorMode == Monitor::RealDevice) {
00093     MultiConnectionDevice* conObj = new MultiConnectionDevice();
00094     conObj->subConnect("localhost", SimulatorPort);
00095     return raw_connect(conObj, device, baudrate);
00096 
00097   } else {
00098     return connectSocket("localhost", SimulatorPort);
00099   }
00100 }
00101 
00102 
00103 unsigned long mRunCtrl::getHostTicks(void) {
00104   unsigned long host_ticks = 0;
00105 
00106   mon->log->lock();
00107   if (MonitorMode == Monitor::Playback) {
00108     // ログからの読み出し
00109     unsigned long ticks = mon->log->readTag("run", "getHostTicks");
00110     const char* line = mon->log->getLineBuffer();
00111     sscanf(line, "%*s %*s ret=%lu", &host_ticks);
00112     mon->log->unlock();
00113     mon->task->waitToTicks(ticks, cond, mutex);
00114 
00115   } else {
00116     // ログへの書き込み
00117     host_ticks = RunCtrl::getHostTicks();
00118     mon->log->writeTag("run", "getHostTicks", mon->getTicks());
00119     fprintf(mon->log->fd, " ret=%lu", host_ticks);
00120     mon->log->writeTagEnd();
00121     mon->log->unlock();
00122   }
00123   return host_ticks;
00124 }
00125 
00126 
00127 void mRunCtrl::adjustOwnTicks(unsigned long setTicks) {
00128   unsigned long ticks = mon->getTicks();
00129   RunCtrl::adjustOwnTicks(setTicks);
00130   simulator->ticksPos.adjustOwnTicks(setTicks, ticks);
00131 }
00132 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1