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
26 #include "controlwebsocketclient.h"
27 #include "datamessage.h"
28 #include "eventmessage.h"
29 #include "messageformat.h"
31 pthread_mutex_t ControlWebsocketClient::mutex_scenario =
32 PTHREAD_MUTEX_INITIALIZER;
33 pthread_cond_t ControlWebsocketClient::cond_scenario = PTHREAD_COND_INITIALIZER;
34 std::string ControlWebsocketClient::vehiclename_scenario = "";
36 ControlWebsocketClient::ControlWebsocketClient()
38 mutex = PTHREAD_MUTEX_INITIALIZER;
39 protocollist[1] = {NULL, NULL, 0};
42 ControlWebsocketClient::~ControlWebsocketClient()
47 ControlWebsocketClient::initialize(int port,
48 enum ControlWebsocket::ServerProtocol stype)
50 DebugOut(10) << "ControlWebsocketClient initialize.(" << port << ")\n";
53 case ControlWebsocket::DATA_STANDARD:
55 protocollist[0] = {"standarddatamessage-only", ControlWebsocketClient::callback_receive, 0};
58 case ControlWebsocket::CONTROL_STANDARD : {
59 protocollist[0] = {"standardcontrolmessage-only", ControlWebsocketClient::callback_receive, 0};
62 case ControlWebsocket::DATA_CUSTOM : {
63 protocollist[0] = {"customdatamessage-only", ControlWebsocketClient::callback_receive, 0};
66 case ControlWebsocket::CONTROL_CUSTOM : {
67 protocollist[0] = {"customcontrolmessage-only", ControlWebsocketClient::callback_receive, 0};
74 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, "lo",
76 libwebsocket_internal_extensions,
77 NULL, NULL, -1, -1, 0);
78 if (context == NULL) {
81 socket = libwebsocket_client_connect(context, "127.0.0.1", port, 0, "/",
82 "localhost", "websocket",
83 protocollist[0].name, -1);
87 if (pthread_create(&threadid, NULL, ControlWebsocketClient::run,
89 libwebsocket_context_destroy(context);
96 ControlWebsocketClient::send(char *keyeventtype, timeval time, void *data,
99 pthread_mutex_lock(&mutex);
100 memset(buf, 0, sizeof(buf));
102 case ControlWebsocket::DATA_STANDARD:
103 case ControlWebsocket::DATA_CUSTOM:
105 memcpy(buf + LWS_SEND_BUFFER_PRE_PADDING,
106 datamsg.encode(keyeventtype, time,
107 *(reinterpret_cast<DataOpt*>(data))),
109 DebugOut(10) << keyeventtype << " encode\n";
112 case ControlWebsocket::CONTROL_STANDARD:
113 case ControlWebsocket::CONTROL_CUSTOM:
115 memcpy(buf + LWS_SEND_BUFFER_PRE_PADDING,
116 eventmsg.encode(keyeventtype, time,
117 *(reinterpret_cast<EventOpt*>(data))),
127 int ret = libwebsocket_write(
129 reinterpret_cast<unsigned char*>(buf + LWS_SEND_BUFFER_PRE_PADDING),
130 len, LWS_WRITE_BINARY);
131 DebugOut(10) << "libwebsocket_write return " << ret << "\n";
132 pthread_mutex_unlock(&mutex);
137 ControlWebsocketClient::receive(char *keyeventtype, timeval recordtime,
138 void *data, size_t len)
140 datamsg.decodeOpt(keyeventtype, recordtime, data);
145 ControlWebsocketClient::observation()
149 ret = libwebsocket_service(context, 100);
157 ControlWebsocketClient::callback_receive(libwebsocket_context *context,
159 libwebsocket_callback_reasons reason,
160 void *user, void *in, size_t len)
163 case LWS_CALLBACK_CLIENT_RECEIVE:
166 msg.decode(reinterpret_cast<char*>(in), len);
167 DebugOut(10) << "[R]: " << msg.getKeyEventType() << " , "
168 << msg.getRecordtime().tv_sec << "."
169 << msg.getRecordtime().tv_usec << " , ";
170 if (string(msg.getKeyEventType())
171 == ControlWebsocketClient::vehiclename_scenario) {
172 pthread_cond_signal(&ControlWebsocketClient::cond_scenario);
185 ControlWebsocketClient::run(void *arg)
187 ControlWebsocketClient *control =
188 reinterpret_cast<ControlWebsocketClient*>(arg);
189 control->observation();