2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Pawel Sikorski (p.sikorski@samsung.com)
21 * @brief This file is the implementation file of RPC example
24 #include <dpl/unix_socket_rpc_client.h>
25 #include <dpl/unix_socket_rpc_server.h>
26 #include <dpl/unix_socket_rpc_connection.h>
27 #include <dpl/fake_rpc_connection.h>
29 #include <dpl/application.h>
30 #include <dpl/controller.h>
31 #include <dpl/thread.h>
32 #include <dpl/log/log.h>
36 // 1st connection - UnixSockets
37 // 2nd connection - Fake
38 // client sends data via fake IPC
39 // server sends back this data via unix IPC.
40 // client compare sent and received data
42 static const char *UNIX_RPC_NAME = "/tmp/unix_socket_rpc";
43 static const char *FAKE_RPC_NAME = "/tmp/fake_rpc";
45 typedef DPL::AbstractRPCConnectionEvents::AsyncCallEvent AsyncCallEvent;
46 typedef DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent
47 ConnectionClosedEvent;
48 typedef DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent
49 ConnectionBrokenEvent;
50 typedef DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent
51 ConnectionEstablishedEvent;
55 private DPL::EventListener<AsyncCallEvent>,
56 private DPL::EventListener<ConnectionEstablishedEvent>
59 DPL::UnixSocketRPCClient m_rpcUnixClient;
60 DPL::FakeRpcClient m_rpcFakeClient;
65 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcUnixConnection;
66 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcFakeConnection;
68 virtual void OnEventReceived(const AsyncCallEvent &event)
70 LogDebug("CLIENT: AsyncCallEvent received");
72 event.GetArg0().ConsumeArg(m_receivedData);
73 LogDebug("CLIENT: Result from server: " << m_receivedData);
75 if (m_receivedData != m_sentData)
76 LogError("Wrong data Received!");
79 virtual void OnEventReceived(const ConnectionEstablishedEvent &event)
81 if (dynamic_cast<DPL::FakeRpcConnection *>(event.GetArg1())){
83 LogDebug("CLIENT: Acquiring new fake connection");
84 m_rpcFakeConnection.reset(event.GetArg1());
85 //this is not used on this side
86 // m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
90 LogDebug("CLIENT: Acquiring new unix connection");
91 m_rpcUnixConnection.reset(event.GetArg1());
92 m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
94 if(m_connections == 2){
96 // Emit RPC function call
97 DPL::RPCFunction proc;
98 proc.AppendArg(m_sentData);
99 LogDebug("CLIENT: Calling RPC function");
100 m_rpcFakeConnection->AsyncCall(proc);
106 m_rpcUnixClient(NULL),
107 m_rpcFakeClient(NULL),
115 // Always quit thread
119 virtual int ThreadEntry()
122 // Attach RPC listeners
123 LogDebug("CLIENT: Attaching connection established event");
124 m_rpcUnixClient.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
125 m_rpcFakeClient.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
127 // Open connection to server
128 LogDebug("CLIENT: Opening connection to RPC");
129 m_rpcUnixClient.Open(UNIX_RPC_NAME);
130 m_rpcFakeClient.Open(FAKE_RPC_NAME);
132 // Start message loop
133 LogDebug("CLIENT: Starting thread event loop");
136 if (m_rpcUnixConnection.get()){
137 LogDebug("CLIENT: Removing Unix connection");
138 m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
139 m_rpcUnixConnection.reset();
142 LogDebug("CLIENT: Closing");
144 if (m_rpcFakeConnection.get()){
145 LogDebug("CLIENT: Removing Fake connection");
146 //this is not used on this side
147 // m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
148 m_rpcFakeConnection.reset();
151 // Detach RPC client listener
152 LogDebug("CLIENT: Detaching connection established event");
153 m_rpcUnixClient.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
154 m_rpcFakeClient.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
157 LogDebug("CLIENT: Closing RPC client");
158 m_rpcUnixClient.CloseAll();
159 m_rpcFakeClient.Close();//not needed
166 DECLARE_GENERIC_EVENT_0(QuitEvent)
167 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
170 : public DPL::Application,
171 private DPL::Controller<DPL::TypeListDecl<QuitEvent,
172 CloseThreadEvent>::Type>,
173 private DPL::EventListener<AsyncCallEvent>,
174 private DPL::EventListener<ConnectionEstablishedEvent>
177 DPL::UnixSocketRPCServer m_rpcUnixServer;
178 DPL::FakeRpcServer m_rpcFakeServer;
180 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcUnixConnection;
181 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcFakeConnection;
185 // Quit application event occurred
186 virtual void OnEventReceived(const QuitEvent &/*event*/){
188 LogDebug("SERVER: Closing RPC connection...");
190 // Detach RPC connection listeners
191 if (m_rpcUnixConnection.get()) {
192 //this is not used on this side
193 // m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
194 m_rpcUnixConnection.reset();
197 if (m_rpcFakeConnection.get()) {
198 m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
199 m_rpcFakeConnection.reset();
202 LogDebug("SERVER: Closing Server");
203 m_rpcUnixServer.CloseAll();
204 m_rpcFakeServer.CloseAll();//not needed
205 m_rpcUnixServer.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
206 m_rpcFakeServer.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
208 LogDebug("SERVER: Server closed");
213 virtual void OnEventReceived(const CloseThreadEvent &/*event*/){
217 virtual void OnEventReceived(const AsyncCallEvent &event)
219 LogDebug("SERVER: AsyncCallEvent received");
222 event.GetArg0().ConsumeArg(value);
223 LogDebug("SERVER: Result from client: " << value);
225 // send back data to client (via fake)
226 // Emit RPC function call
227 DPL::RPCFunction proc;
228 proc.AppendArg(value);
229 LogDebug("SERVER: Calling RPC function");
230 m_rpcUnixConnection->AsyncCall(proc);
233 virtual void OnEventReceived(const ConnectionEstablishedEvent &event)
235 // Save connection pointer
236 if (dynamic_cast<DPL::FakeRpcConnection *>(event.GetArg1())){
237 LogDebug("SERVER: Acquiring Fake RPC connection");
238 m_rpcFakeConnection.reset(event.GetArg1());
239 m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
242 LogDebug("SERVER: Acquiring Unix RPC connection");
243 m_rpcUnixConnection.reset(event.GetArg1());
244 //this is not used on this side
245 // m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
250 MyApplication(int argc, char **argv)
251 : Application(argc, argv, "rpc")
253 // Attach RPC server listeners
254 LogDebug("SERVER: Attaching connection established event");
255 m_rpcUnixServer.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
256 m_rpcFakeServer.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
259 LogDebug("SERVER: Touching controller");
263 LogDebug("SERVER: Opening server RPC");
264 m_rpcUnixServer.Open(UNIX_RPC_NAME);
265 m_rpcFakeServer.Open(FAKE_RPC_NAME);
267 // Run RPC client in thread
268 LogDebug("SERVER: Starting RPC client thread");
271 // Quit application automatically in few seconds
272 LogDebug("SERVER: Sending control timed events");
273 DPL::ControllerEventHandler<CloseThreadEvent>::PostTimedEvent(CloseThreadEvent(), 2);
274 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 3);
277 virtual ~MyApplication()
280 LogDebug("SERVER: Quitting thread");
284 int main(int argc, char *argv[])
286 LogDebug("Starting");
287 MyApplication app(argc, argv);