1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
9 #include <systemd/sd-event.h>
10 #include <json-c/json.h>
12 #include "RequestManage.h"
17 RequestManage::RequestManage() : listener(nullptr)
20 this->wsj1_itf.on_hangup = RequestManage::OnHangupStatic;
21 this->wsj1_itf.on_call = RequestManage::OnCallStatic;
22 this->wsj1_itf.on_event = RequestManage::OnEventStatic;
24 pthread_cond_init(&this->cond, nullptr);
25 pthread_mutex_init(&this->mutex, nullptr);
31 RequestManage::~RequestManage()
35 void* RequestManage::BinderThread(void* param)
37 RequestManage* instance = (RequestManage*) param;
40 int rc = sd_event_default(&loop);
42 TRACE_ERROR("connection to default event loop failed: %s\n", strerror(-rc));
46 instance->wsj1 = afb_ws_client_connect_wsj1(loop, instance->requestURL->c_str(), &instance->wsj1_itf, nullptr);
47 if (instance->wsj1 == nullptr)
49 TRACE_ERROR("connection to %s failed: %m\n", api_url);
54 pthread_mutex_unlock(&instance->mutex);
55 pthread_cond_signal(&instance->cond);
59 sd_event_run(loop, 1000 * 1000); // 1sec
66 * @brief Connect with a service
68 * @return Success or failure of connection
70 bool RequestManage::Connect(const char* api_url, RequestManageListener* listener)
72 this->listener = listener;
73 this->requestURL = new std::string(api_url);
76 pthread_create(&thread_id, nullptr, RequestManage::BinderThread, (void*)this);
78 // Wait until response comes
79 pthread_mutex_lock(&this->mutex);
80 pthread_cond_wait(&this->cond, &this->mutex);
81 pthread_mutex_unlock(&this->mutex);
83 if (this->wsj1 == nullptr)
92 * @brief Connection status check with service
93 * @return Connection status
95 bool RequestManage::IsConnect(){
96 return (this->wsj1 != NULL);
100 * @brief Call Binder's API
103 * @param req_json Json style request
104 * @return Success or failure of processing
106 bool RequestManage::CallBinderAPI(const char* api, const char* verb, const char* req_json)
109 int rc = afb_wsj1_call_s(this->wsj1, api, verb, req_json, RequestManage::OnReplyStatic, this);
112 TRACE_ERROR("calling %s/%s(%s) failed: %m\n", api, verb, req_json);
120 * @brief Set session handle
121 * @param session Session handle
123 void RequestManage::SetSessionHandle( uint32_t session )
125 this->sessionHandle = session;
129 * @brief Get session handle
130 * @return Session handle
132 uint32_t RequestManage::GetSessionHandle()
134 return this->sessionHandle;
138 * @brief Set route handle
139 * @param route Route handle
141 void RequestManage::SetRouteHandle( uint32_t route )
143 this->routeHandle = route;
147 * @brief Get route handle
148 * @return Route handle
150 uint32_t RequestManage::GetRouteHandle()
152 return this->routeHandle;
155 void RequestManage::OnReply(struct afb_wsj1_msg *msg)
157 struct json_object * json = afb_wsj1_msg_object_j(msg);
159 this->listener->OnReply(json);
162 void RequestManage::OnHangup(struct afb_wsj1 *wsj1)
166 void RequestManage::OnCallStatic(const char *api, const char *verb, struct afb_wsj1_msg *msg)
170 void RequestManage::OnEventStatic(const char *event, struct afb_wsj1_msg *msg)
176 * @brief Answer callback from service
178 void RequestManage::OnReplyStatic(void *closure, struct afb_wsj1_msg *msg)
180 RequestManage* instance = (RequestManage *)closure;
181 instance->OnReply(msg);
185 * @brief Service hang notification
187 void RequestManage::OnHangupStatic(void *closure, struct afb_wsj1 *wsj1)
189 printf("DEBUG:%s:%d (%p,%p)\n", __func__, __LINE__, closure, wsj1);
193 void RequestManage::OnCallStatic(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
195 printf("DEBUG:%s:%d (%p,%s,%s,%p)\n", __func__, __LINE__, closure, api, verb, msg);
199 void RequestManage::OnEventStatic(void *closure, const char *event, struct afb_wsj1_msg *msg)
201 printf("DEBUG:%s:%d (%p,%s,%p)\n", __func__, __LINE__, closure, event, msg);