Remove circular dependency
[platform/core/appfw/rpc-port.git] / benchmark / server / BenchmarkStub.cc
1 /*
2  * Generated by tidlc 1.9.1.
3  */
4
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <libgen.h>
8 #include <dlog.h>
9
10 #include "BenchmarkStub.h"
11
12
13 #ifdef LOG_TAG
14 #undef LOG_TAG
15 #endif
16
17 #define LOG_TAG "RPC_PORT_STUB"
18
19 #ifdef _E
20 #undef _E
21 #endif
22
23 #ifdef _W
24 #undef _W
25 #endif
26
27 #ifdef _I
28 #undef _I
29 #endif
30
31 #ifdef _D
32 #undef _D
33 #endif
34
35 #define _E(fmt, ...) dlog_print(DLOG_ERROR, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
36 #define _W(fmt, ...) dlog_print(DLOG_WARN, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
37 #define _I(fmt, ...) dlog_print(DLOG_INFO, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
38 #define _D(fmt, ...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
39
40 #ifndef TIDL_VERSION
41 #define TIDL_VERSION "1.9.1"
42 #endif
43
44 namespace rpc_port {
45 namespace BenchmarkStub {
46
47 namespace stub {
48
49 Benchmark::ServiceBase::ServiceBase(std::string sender, std::string instance)
50     : sender_(std::move(sender)), instance_(std::move(instance)),
51       active_object_(new ActiveObject()) {}
52
53 void Benchmark::ServiceBase::SetPort(rpc_port_h port) {
54   port_ = port;
55 }
56
57 void Benchmark::ServiceBase::Disconnect() {
58   int ret = rpc_port_disconnect(port_);
59   if (ret == RPC_PORT_ERROR_NONE) {
60     _E("Failed to disconnect the port(%d)", ret);
61     return;
62   }
63
64   port_ = nullptr;
65 }
66
67
68 std::atomic<int> Benchmark::CallbackBase::seq_num_ { 0 };
69
70 Benchmark::CallbackBase::CallbackBase(int delegate_id, bool once)
71     : id_(delegate_id), once_(once) {
72   seq_id_ = seq_num_++;
73 }
74
75 int Benchmark::CallbackBase::GetId() const {
76   return id_;
77 }
78
79 int Benchmark::CallbackBase::GetSeqId() const {
80   return seq_id_;
81 }
82
83 bool Benchmark::CallbackBase::IsOnce() const {
84   return once_;
85 }
86
87 std::string Benchmark::CallbackBase::GetTag() const {
88   return std::to_string(id_) + "::" + std::to_string(seq_id_);
89 }
90
91 rpc_port_parcel_h operator << (rpc_port_parcel_h h, const Benchmark::CallbackBase& cb) {
92   rpc_port_parcel_write_int32(h, cb.id_);
93   rpc_port_parcel_write_int32(h, cb.seq_id_);
94   rpc_port_parcel_write_bool(h, cb.once_);
95
96   return h;
97 }
98
99 rpc_port_parcel_h operator >> (rpc_port_parcel_h h, Benchmark::CallbackBase& cb) {
100   rpc_port_parcel_read_int32(h, &cb.id_);
101   rpc_port_parcel_read_int32(h, &cb.seq_id_);
102   rpc_port_parcel_read_bool(h, &cb.once_);
103
104   return h;
105 }
106
107 Benchmark::Benchmark() {
108   int r = rpc_port_stub_create(&stub_, "Benchmark");
109   if (r != RPC_PORT_ERROR_NONE) {
110     _E("Failed to create stub handle");
111     throw InvalidIOException();
112   }
113   rpc_port_stub_add_connected_event_cb(stub_, OnConnectedCB, this);
114   rpc_port_stub_add_disconnected_event_cb(stub_, OnDisconnectedCB, this);
115   rpc_port_stub_add_received_event_cb(stub_, OnReceivedCB, this);
116 }
117
118 Benchmark::~Benchmark() {
119   for (auto& i : services_) {
120     i->OnTerminate();
121   }
122
123   if (stub_) {
124     rpc_port_stub_destroy(stub_);
125   }
126 }
127
128 void Benchmark::Listen(std::shared_ptr<Benchmark::ServiceBase::Factory> service_factory) {
129   service_factory_ = std::move(service_factory);
130   int r = rpc_port_stub_listen(stub_);
131   if (r != RPC_PORT_ERROR_NONE) {
132     _E("Failed to listen stub");
133     switch (r) {
134       case RPC_PORT_ERROR_INVALID_PARAMETER:
135       case RPC_PORT_ERROR_IO_ERROR:
136         throw InvalidIOException();
137     }
138   }
139 }
140
141 void Benchmark::OnConnectedCB(const char* sender, const char* instance, void* data) {
142   Benchmark* stub = static_cast<Benchmark*>(data);
143   auto s = stub->service_factory_->CreateService(sender, instance);
144
145   rpc_port_h port;
146   int ret = rpc_port_stub_get_port(stub->stub_, RPC_PORT_PORT_CALLBACK, instance, &port);
147   if (ret != RPC_PORT_ERROR_NONE) {
148     _E("Failed to get the port(%d)", ret);
149     return;
150   }
151
152   s->SetPort(port);
153   s->OnCreate();
154   stub->services_.emplace_back(std::move(s));
155 }
156
157 void Benchmark::OnDisconnectedCB(const char* sender, const char* instance, void *data) {
158   Benchmark* stub = static_cast<Benchmark*>(data);
159
160   for (auto& i : stub->services_) {
161     if (i->GetInstance() == instance) {
162       i->OnTerminate();
163       stub->services_.remove(i);
164       return;
165     }
166   }
167 }
168
169 int Benchmark::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port, void *data)
170 {
171   auto* cxt = static_cast<Benchmark*>(data);
172   rpc_port_parcel_h p;
173   rpc_port_parcel_h result;
174   rpc_port_parcel_h header;
175   int seq_num = -1;
176   int cmd;
177   int ret;
178   std::shared_ptr<ServiceBase> b;
179   rpc_port_h callback_port;
180
181   for (auto& i : cxt->services_) {
182     if (i->GetInstance() == instance) {
183       b = i;
184       break;
185     }
186   }
187
188   if (b.get() == nullptr) {
189     _E("Failed to find Benchmark context(%s)", instance);
190     return -1;
191   }
192
193   ret = rpc_port_stub_get_port(cxt->stub_, RPC_PORT_PORT_CALLBACK, instance,
194       &callback_port);
195   if (ret != 0) {
196     _E("Failed to get callback port");
197   }
198
199   ret = rpc_port_parcel_create_from_port(&p, port);
200   if (ret != 0) {
201     _E("Failed to create parcel from port");
202     return ret;
203   }
204
205   rpc_port_parcel_get_header(p, &header);
206   rpc_port_parcel_header_get_seq_num(header, &seq_num);
207   _W("[Sequence] %d", seq_num);
208
209   rpc_port_parcel_create(&result);
210   rpc_port_parcel_get_header(result, &header);
211   rpc_port_parcel_header_set_tag(header, TIDL_VERSION);
212   rpc_port_parcel_header_set_seq_num(header, seq_num);
213
214   rpc_port_parcel_read_int32(p, &cmd);
215
216   switch (cmd) {
217     case static_cast<int>(MethodId::Test): {
218       char* param1_raw  = nullptr;
219       rpc_port_parcel_read_string(p, &param1_raw);
220       std::string param1(param1_raw);
221       free(param1_raw);
222       auto retVal = b->Test(param1);
223       rpc_port_parcel_write_int32(result, static_cast<int>(MethodId::__Result));
224       rpc_port_parcel_write_int32(result, retVal);
225       ret = rpc_port_parcel_send(result, port);
226       _I("Parcel send result(%d)", ret);
227       break;
228     }
229
230     default:
231       _E("Unknown command(%d)", cmd);
232       rpc_port_parcel_destroy(p);
233       rpc_port_parcel_destroy(result);
234       return -1;
235   }
236
237   rpc_port_parcel_destroy(p);
238   rpc_port_parcel_destroy(result);
239
240   return ret;
241 }
242
243 }  // namespace stub
244 }  // namespace BenchmarkStub
245 }  // namespace rpc_port