ticksCtrlInterface.cpp

00001 /*
00002   モジュールの時刻同期管理インターフェース
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "ticksCtrlInterface.h"
00008 #include "coordinateCtrl.h"
00009 
00010 
00011 unsigned long TicksCtrlInterface::getModuleTicks(unsigned long rawTicks) {
00012   ticksInfo_t& ticksInfo = getTicksInfo();
00013 
00014   return (rawTicks - ticksInfo.moduleDiff) + ticksInfo.setDiff;
00015 }
00016 
00017 
00018 unsigned long TicksCtrlInterface::getModuleTicks(void) {
00019   unsigned long begin_before = getHostTicks();
00020   beginTimeAdjust();
00021   unsigned long begin_after = getHostTicks();
00022 
00023   unsigned long ticks = getModuleTicks(getModuleRawTicks());
00024   endTimeAdjust();
00025 
00026   return ticks - (begin_after - begin_before);
00027 }
00028 
00029 
00030 void TicksCtrlInterface::adjustOwnTicks(unsigned long setTicks) {
00031   unsigned long beginHostTicks = getHostTicks();
00032   ticksInfo_t& ticksInfo = getTicksInfo();
00033 
00034   // モジュール時刻印の調整開始
00035   beginTimeAdjust();
00036 
00037   // 通信遅延の評価
00038   int tryTimes = 1 << ticksInfo.tryTimes_shift;
00039   unsigned long transTicks_total = 0;
00040   unsigned long module_ticks = 0;
00041   unsigned long sendTicks = getHostTicks();
00042   unsigned long recvTicks = 0;
00043   for (int i = 0; i < tryTimes; ++i) {
00044     module_ticks = getModuleRawTicks();
00045     recvTicks = getHostTicks();
00046     transTicks_total += recvTicks - sendTicks;
00047 
00048     sendTicks = recvTicks;
00049   }
00050   // モジュール時刻印の調整終了
00051   endTimeAdjust();
00052 
00053   unsigned long recvAVG = transTicks_total >> (ticksInfo.tryTimes_shift +1);
00054   ticksInfo.moduleDiff = (module_ticks + recvAVG) - recvTicks;
00055 
00056   // ホスト時刻印の調整
00057   ticksInfo.setDiff = setTicks - beginHostTicks;
00058 }
00059 
00060 
00061 void TicksCtrlInterface::adjustChildCrdsTicks(TicksCtrlInterface* root,
00062                                               long hostDiff,
00063                                               CoordinateCtrl* subRoot) {
00064 
00065   for (std::list<CoordinateCtrl*>::iterator it =
00066          subRoot->getChildCrd().begin();
00067        it != subRoot->getChildCrd().end(); ++it) {
00068     TicksCtrlInterface* obj = dynamic_cast<TicksCtrlInterface*>(*it);
00069     if (obj) {
00070       obj->adjustOwnTicks(root->getHostTicks() - hostDiff);
00071     }
00072   }
00073 
00074   // 子座標系について再帰呼び出し
00075   for (std::list<CoordinateCtrl*>::iterator it =
00076          subRoot->getChildCrd().begin();
00077        it != subRoot->getChildCrd().end(); ++it) {
00078     TicksCtrlInterface* obj = dynamic_cast<TicksCtrlInterface*>(*it);
00079     if (obj) {
00080       obj->adjustChildCrdsTicks(root, hostDiff, *it);
00081     }
00082   }
00083 }
00084 
00085 
00086 void TicksCtrlInterface::adjustSubTreeTicks(unsigned long setTicks) {
00087   CoordinateCtrl* root = dynamic_cast<CoordinateCtrl*>(this);
00088   if (!root) {
00089     return;
00090   }
00091   long hostDiff = getHostTicks() - setTicks;
00092 
00093   // 時刻印の同期処理
00094   adjustOwnTicks(setTicks);
00095   adjustChildCrdsTicks(this, hostDiff, root);
00096 }
00097 

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