packaging: add AMB configuration that enables the plugin
[profile/ivi/ico-vic-amb-plugin.git] / tests / controlwebsocketclient.cc
1 #include <cstdio>
2 #include <iostream>
3 #include <sys/time.h>
4
5 #include "datamessage.h"
6
7 #include "controlwebsocketclient.h"
8
9 ControlWebsocketClient::ControlWebsocketClient() 
10 : context_(NULL), wsid_(NULL), fd_(0) {
11 }
12
13 ControlWebsocketClient::~ControlWebsocketClient() {
14 }
15
16 bool ControlWebsocketClient::initialize(const char *uri, const int port, const char *protocol) {
17     /* Memo */
18     /* If strlen(uri) == 0, websocket server. If strlen(uri) > 0, websocket client. */
19     if ((uri == NULL || strlen(uri) == 0) || (port < 1 || port > 65535) || (protocol == NULL || strlen(protocol) == 0)) {
20         return false;
21     }
22     char uribuf[256];
23     memset(uribuf, 0, sizeof(uribuf));
24     sprintf(uribuf, "%s:%d", uri, port);
25     context_ = ico_uws_create_context(uribuf, protocol);
26     if (context_ == NULL) {
27         return false;
28     }
29
30     int ret = ico_uws_set_event_cb(context_, callbackfunc, this);
31     if (ret != ICO_UWS_ERR_NONE) {
32         terminate();
33         return false;
34     }
35     return true;
36 }
37
38 void ControlWebsocketClient::terminate() {
39     if (context_ != NULL) {
40         ico_uws_unset_event_cb(context_);
41         ico_uws_close(context_);
42         fd_ = 0;
43     }
44 }
45
46 bool ControlWebsocketClient::send(char *sendbuf, const size_t len) {
47     if (context_ == NULL || wsid_ == NULL) {
48         return false;
49     }
50     ico_uws_send(context_, wsid_, reinterpret_cast<unsigned char*>(sendbuf), len);
51     return true;
52 }
53
54 bool ControlWebsocketClient::recv(char *recvbuf, const size_t len) {
55     std::cout << &recvbuf[0] << " ";
56     DataMessage dmsg;
57     dmsg.decode(recvbuf, len);
58     timeval tv;
59     tv = dmsg.getRecordtime();
60     std::cout << tv.tv_sec << "." << tv.tv_usec << " ";
61     DataOpt dopt = dmsg.getDataOpt();
62     std::cout << dopt.common_status << " ";
63     int statuslen = getStatussize(len);
64     for (int i = 0; i < statuslen; i++) {
65         std::cout << static_cast<int>(dopt.status[i]);
66     }
67     
68     std::cout << std::endl;
69     return true;
70 }
71
72 int ControlWebsocketClient::getsocketid() const {
73     return fd_;
74 }
75
76 void ControlWebsocketClient::setsocketid(int fd) {
77     fd_ = fd;
78 }
79
80 void ControlWebsocketClient::setwsid(void *id) {
81     wsid_ = id;
82 }
83
84 void ControlWebsocketClient::service() {
85     ico_uws_service(context_);
86 }
87
88 void ControlWebsocketClient::callbackfunc(const struct ico_uws_context *context, const ico_uws_evt_e event, const void *id, const ico_uws_detail *detail, void *user_data) {
89     ControlWebsocketClient *client = reinterpret_cast<ControlWebsocketClient*>(user_data);
90     switch (event) {
91     case ICO_UWS_EVT_RECEIVE :
92         //std::cout << "ICO_UWS_EVT_RECEIV\n";
93         client->recv(reinterpret_cast<char*>(detail->_ico_uws_message.recv_data), detail->_ico_uws_message.recv_len);
94         break;
95     case ICO_UWS_EVT_ADD_FD :
96         //std::cout << "ICO_UWS_EVT_ADD_FD\n";
97         if (detail->_ico_uws_fd.fd > 0) {
98             client->setsocketid(detail->_ico_uws_fd.fd);
99         }
100         client->setwsid(const_cast<void*>(id));
101         break;
102     case ICO_UWS_EVT_DEL_FD :
103         //std::cout << "ICO_UWS_EVT_DEL_FD\n";
104         client->setsocketid(0);
105         client->setwsid(NULL);
106         break;
107     default :
108         break;
109     }
110     return;
111 }