1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// Copyright 2017 AW SOFTWARE CO.,LTD
// Copyright 2017 AISIN AW CO.,LTD
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <systemd/sd-event.h>
#include <json-c/json.h>
#include <traces.h>
#include "RequestManage.h"
/**
* @brief constructor
*/
RequestManage::RequestManage() : listener(nullptr)
{
// Callback setting
this->wsj1_itf.on_hangup = RequestManage::OnHangupStatic;
this->wsj1_itf.on_call = RequestManage::OnCallStatic;
this->wsj1_itf.on_event = RequestManage::OnEventStatic;
pthread_cond_init(&this->cond, nullptr);
pthread_mutex_init(&this->mutex, nullptr);
}
/**
* @brief Destructor
*/
RequestManage::~RequestManage()
{
}
void* RequestManage::BinderThread(void* param)
{
RequestManage* instance = (RequestManage*) param;
sd_event *loop;
int rc = sd_event_default(&loop);
if (rc < 0) {
TRACE_ERROR("connection to default event loop failed: %s\n", strerror(-rc));
return nullptr;
}
instance->wsj1 = afb_ws_client_connect_wsj1(loop, instance->requestURL->c_str(), &instance->wsj1_itf, nullptr);
if (instance->wsj1 == nullptr)
{
TRACE_ERROR("connection to %s failed: %m\n", api_url);
return nullptr;
}
// Signal
pthread_mutex_unlock(&instance->mutex);
pthread_cond_signal(&instance->cond);
while (1)
{
sd_event_run(loop, 1000 * 1000); // 1sec
}
return nullptr;
}
/**
* @brief Connect with a service
* @param URL
* @return Success or failure of connection
*/
bool RequestManage::Connect(const char* api_url, RequestManageListener* listener)
{
this->listener = listener;
this->requestURL = new std::string(api_url);
pthread_t thread_id;
pthread_create(&thread_id, nullptr, RequestManage::BinderThread, (void*)this);
// Wait until response comes
pthread_mutex_lock(&this->mutex);
pthread_cond_wait(&this->cond, &this->mutex);
pthread_mutex_unlock(&this->mutex);
if (this->wsj1 == nullptr)
{
return false;
}
return true;
}
/**
* @brief Connection status check with service
* @return Connection status
*/
bool RequestManage::IsConnect(){
return (this->wsj1 != NULL);
}
/**
* @brief Call Binder's API
* @param api api
* @param verb method
* @param req_json Json style request
* @return Success or failure of processing
*/
bool RequestManage::CallBinderAPI(const char* api, const char* verb, const char* req_json)
{
// Send request
int rc = afb_wsj1_call_s(this->wsj1, api, verb, req_json, RequestManage::OnReplyStatic, this);
if (rc < 0)
{
TRACE_ERROR("calling %s/%s(%s) failed: %m\n", api, verb, req_json);
return false;
}
return true;
}
/**
* @brief Set session handle
* @param session Session handle
*/
void RequestManage::SetSessionHandle( uint32_t session )
{
this->sessionHandle = session;
}
/**
* @brief Get session handle
* @return Session handle
*/
uint32_t RequestManage::GetSessionHandle()
{
return this->sessionHandle;
}
/**
* @brief Set route handle
* @param route Route handle
*/
void RequestManage::SetRouteHandle( uint32_t route )
{
this->routeHandle = route;
}
/**
* @brief Get route handle
* @return Route handle
*/
uint32_t RequestManage::GetRouteHandle()
{
return this->routeHandle;
}
void RequestManage::OnReply(struct afb_wsj1_msg *msg)
{
struct json_object * json = afb_wsj1_msg_object_j(msg);
this->listener->OnReply(json);
}
void RequestManage::OnHangup(struct afb_wsj1 *wsj1)
{
}
void RequestManage::OnCallStatic(const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
}
void RequestManage::OnEventStatic(const char *event, struct afb_wsj1_msg *msg)
{
}
/**
* @brief Answer callback from service
*/
void RequestManage::OnReplyStatic(void *closure, struct afb_wsj1_msg *msg)
{
RequestManage* instance = (RequestManage *)closure;
instance->OnReply(msg);
}
/**
* @brief Service hang notification
*/
void RequestManage::OnHangupStatic(void *closure, struct afb_wsj1 *wsj1)
{
printf("DEBUG:%s:%d (%p,%p)\n", __func__, __LINE__, closure, wsj1);
fflush(stdout);
}
void RequestManage::OnCallStatic(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
printf("DEBUG:%s:%d (%p,%s,%s,%p)\n", __func__, __LINE__, closure, api, verb, msg);
fflush(stdout);
}
void RequestManage::OnEventStatic(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
printf("DEBUG:%s:%d (%p,%s,%p)\n", __func__, __LINE__, closure, event, msg);
fflush(stdout);
}
|