2 * Copyright (C) 2012 TOYOTA MOTOR CORPORATION.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "controlwebsocketclient.h"
29 #include "datamessage.h"
30 #include "eventmessage.h"
31 #include "messageformat.h"
33 pthread_mutex_t ControlWebsocketClient::mutex_scenario =
34 PTHREAD_MUTEX_INITIALIZER;
35 pthread_cond_t ControlWebsocketClient::cond_scenario = PTHREAD_COND_INITIALIZER;
36 std::string ControlWebsocketClient::vehiclename_scenario = "";
37 void *ControlWebsocketClient::wsi[4] = {NULL, NULL, NULL, NULL};
39 ControlWebsocketClient::ControlWebsocketClient()
41 mutex = PTHREAD_MUTEX_INITIALIZER;
44 ControlWebsocketClient::~ControlWebsocketClient()
49 ControlWebsocketClient::initialize(int port,
50 enum ControlWebsocket::ServerProtocol stype)
52 DebugOut(10) << "ControlWebsocketClient initialize.(" << port << ")\n";
54 stringstream address, protocol;
56 address << "ws://127.0.0.1:" << port;
59 case ControlWebsocket::DATA_STANDARD:
61 protocol << "standarddatamessage-only";
64 case ControlWebsocket::CONTROL_STANDARD:
66 protocol << "standardcontrolmessage-only";
69 case ControlWebsocket::DATA_CUSTOM:
71 protocol << "customdatamessage-only";
74 case ControlWebsocket::CONTROL_CUSTOM:
76 protocol << "customcontrolmessage-only";
84 context = ico_uws_create_context(address.str().c_str(),
85 protocol.str().c_str());
86 if (context == NULL) {
89 if (ico_uws_set_event_cb(context, ControlWebsocketClient::callback_receive,
90 ((void *)type)) != 0) {
91 DebugOut() << "ControlWebsocket[" << type << "]"
92 << " couldn't set callback function." << std::endl;
96 socket = libwebsocket_client_connect(context, "127.0.0.1", port, 0, "/",
97 "localhost", "websocket",
98 protocollist[0].name, -1);
103 if (pthread_create(&threadid, NULL, ControlWebsocketClient::run,
104 (void*)this) == -1) {
105 ico_uws_close(context);
112 ControlWebsocketClient::send(char *keyeventtype, timeval time, void *data,
115 pthread_mutex_lock(&mutex);
116 memset(buf, 0, sizeof(buf));
118 case ControlWebsocket::DATA_STANDARD:
119 case ControlWebsocket::DATA_CUSTOM:
122 datamsg.encode(keyeventtype, time,
123 *(reinterpret_cast<DataOpt*>(data))),
125 DebugOut(10) << keyeventtype << " encode\n";
128 case ControlWebsocket::CONTROL_STANDARD:
129 case ControlWebsocket::CONTROL_CUSTOM:
132 eventmsg.encode(keyeventtype, time,
133 *(reinterpret_cast<EventOpt*>(data))),
143 ico_uws_send(context, wsi[type], reinterpret_cast<unsigned char*>(buf),
145 pthread_mutex_unlock(&mutex);
150 ControlWebsocketClient::receive(char *keyeventtype, timeval recordtime,
151 void *data, size_t len)
153 datamsg.decodeOpt(keyeventtype, recordtime, data);
158 ControlWebsocketClient::observation()
161 ico_uws_service(context);
167 ControlWebsocketClient::callback_receive(const struct ico_uws_context *context,
168 const ico_uws_evt_e event,
170 const ico_uws_detail *detail,
174 case ICO_UWS_EVT_OPEN:
176 int idx = reinterpret_cast<int>(user_data);
177 DebugOut() << "Open. wsi index = " << idx << "\n";
178 ControlWebsocketClient::wsi[idx] = const_cast<void*>(id);
181 case ICO_UWS_EVT_ERROR:
183 case ICO_UWS_EVT_CLOSE:
185 int idx = reinterpret_cast<int>(user_data);
186 ControlWebsocketClient::wsi[idx] = NULL;
189 case ICO_UWS_EVT_RECEIVE:
193 reinterpret_cast<char*>(detail->_ico_uws_message.recv_data);
194 msg.decode(recvbuf, detail->_ico_uws_message.recv_len);
195 DebugOut(10) << "[R]: " << msg.getKeyEventType() << " , "
196 << msg.getRecordtime().tv_sec << "."
197 << msg.getRecordtime().tv_usec << " , ";
198 if (string(msg.getKeyEventType())
199 == ControlWebsocketClient::vehiclename_scenario) {
200 pthread_cond_signal(&ControlWebsocketClient::cond_scenario);
204 case ICO_UWS_EVT_ADD_FD:
206 case ICO_UWS_EVT_DEL_FD:
214 ControlWebsocketClient::run(void *arg)
216 ControlWebsocketClient *control =
217 reinterpret_cast<ControlWebsocketClient*>(arg);
218 control->observation();