| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief 走行制御シミュレータ |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "tRunCtrlSimulator.h" |
| 11 |
#include "taskCtrl.h" |
| 12 |
#include "transferCtrl.h" |
| 13 |
#include "TcpipCtrl.h" |
| 14 |
#include "TcpipAccept.h" |
| 15 |
#include "ThreadCreator.h" |
| 16 |
#include <string> |
| 17 |
|
| 18 |
using namespace beego; |
| 19 |
|
| 20 |
|
| 21 |
/*! |
| 22 |
\brief tRunCtrlSimulator の内部クラス |
| 23 |
*/ |
| 24 |
struct tRunCtrlSimulator::pImpl { |
| 25 |
enum { |
| 26 |
tRunCtrl_Port = 5525, // TCP/IP 用の接続ポート |
| 27 |
}; |
| 28 |
std::string error_message; |
| 29 |
bool activated; |
| 30 |
tRunCtrl_t run; |
| 31 |
packet_t packet; |
| 32 |
TcpipCtrl* con; |
| 33 |
TcpipAccept accept; |
| 34 |
|
| 35 |
/*! |
| 36 |
\brief スレッドの引数管理 |
| 37 |
*/ |
| 38 |
class ThreadArgs { |
| 39 |
public: |
| 40 |
TcpipAccept* accept; |
| 41 |
TcpipCtrl*& con; |
| 42 |
|
| 43 |
ThreadArgs(TcpipAccept* accept_obj, TcpipCtrl*& con_obj) |
| 44 |
: accept(accept_obj), con(con_obj) { |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
pImpl(void) : error_message("no error."), activated(false), con(NULL) { |
| 49 |
transfer_setTable(&packet, reinterpret_cast<unsigned char*>(&run)); |
| 50 |
} |
| 51 |
|
| 52 |
enum { |
| 53 |
AcceptFail = -1, |
| 54 |
Timeout = 100, // [msec] |
| 55 |
}; |
| 56 |
static int accept_thread(void* args) { |
| 57 |
ThreadArgs* info = static_cast<ThreadArgs*>(args); |
| 58 |
|
| 59 |
info->con = info->accept->accept(Timeout); |
| 60 |
return (info->con == NULL) ? AcceptFail : 0; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
tRunCtrlSimulator::tRunCtrlSimulator(void) : pimpl(new pImpl) { |
| 66 |
powerOn(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
tRunCtrlSimulator::~tRunCtrlSimulator(void) { |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
const char* tRunCtrlSimulator::what(void) { |
| 75 |
return pimpl->error_message.c_str(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
void tRunCtrlSimulator::activate(void) { |
| 80 |
if (pimpl->activated) { |
| 81 |
return; |
| 82 |
} |
| 83 |
pimpl->accept.activate(pImpl::tRunCtrl_Port); |
| 84 |
pimpl->activated = true; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
bool tRunCtrlSimulator::connect(ConnectionInterface* con) { |
| 89 |
|
| 90 |
pImpl::ThreadArgs args(&pimpl->accept, pimpl->con); |
| 91 |
ThreadCreator thread(pImpl::accept_thread, &args); |
| 92 |
thread.run(1); |
| 93 |
|
| 94 |
con->disconnect(); |
| 95 |
bool ret = con->connect("localhost", pImpl::tRunCtrl_Port); |
| 96 |
int ret_value; |
| 97 |
thread.wait(&ret_value); |
| 98 |
if ((! ret) && (ret_value == pImpl::AcceptFail)) { |
| 99 |
pimpl->error_message = con->what(); |
| 100 |
return false; |
| 101 |
} |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void tRunCtrlSimulator::reset(void) { |
| 107 |
powerOff(); |
| 108 |
powerOn(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void tRunCtrlSimulator::powerOff(void) { |
| 113 |
// !!! |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
void tRunCtrlSimulator::powerOn(void) { |
| 118 |
initState(&pimpl->run); |
| 119 |
// !!! |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
// !!! これを、Monitor のオブジェクトに登録して、周期的に呼び出す |
| 124 |
void tRunCtrlSimulator::runMainTask(void) { |
| 125 |
|
| 126 |
// !!! 実際問題として、1msec 毎の起動でよい |
| 127 |
// !!! ただ、シミュレーションの早送りを実装する場合には調整が必要かと |
| 128 |
if (pimpl->con) { |
| 129 |
transfer_update(&pimpl->packet, pimpl->con); |
| 130 |
} |
| 131 |
|
| 132 |
updateState(&pimpl->run); |
| 133 |
|
| 134 |
// !!! ロックをかけて、packetRecvHandler() を呼び出すのか? |
| 135 |
// !!! ただ、static の Packet まわりは、どうにかすべき |
| 136 |
// !!! tRunCtrl で宣言すべきかも。というか static はなくすべき |
| 137 |
// !!! リエントラント性が悪くなる |
| 138 |
// !!! 通信タスクも、かな? |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
// !!! 上記までが、共通かな? |
| 143 |
|
| 144 |
// 持ち上げて移動させる場合 |
| 145 |
void tRunCtrlSimulator::setPosition(Position<int>& position) { |
| 146 |
// !!! |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// 引きずって移動させる場合 |
| 151 |
void tRunCtrlSimulator::trailToPosition(Position<int>& position) { |
| 152 |
// !!! |
| 153 |
} |