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>
28 #include <dpl/scoped_ptr.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 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcUnixConnection;
66 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcFakeConnection;
68 virtual void OnEventReceived(const AsyncCallEvent &event)
70 LogInfo("CLIENT: AsyncCallEvent received");
72 event.GetArg0().ConsumeArg(m_receivedData);
73 LogInfo("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 LogInfo("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 LogInfo("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 LogInfo("CLIENT: Calling RPC function");
100 m_rpcFakeConnection->AsyncCall(proc);
107 // Always quit thread
111 virtual int ThreadEntry()
114 // Attach RPC listeners
115 LogInfo("CLIENT: Attaching connection established event");
116 m_rpcUnixClient.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
117 m_rpcFakeClient.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
119 // Open connection to server
120 LogInfo("CLIENT: Opening connection to RPC");
121 m_rpcUnixClient.Open(UNIX_RPC_NAME);
122 m_rpcFakeClient.Open(FAKE_RPC_NAME);
124 // Start message loop
125 LogInfo("CLIENT: Starting thread event loop");
128 if (m_rpcUnixConnection.Get()){
129 LogInfo("CLIENT: Removing Unix connection");
130 m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
131 m_rpcUnixConnection.Reset();
134 LogInfo("CLIENT: Closing");
136 if (m_rpcFakeConnection.Get()){
137 LogInfo("CLIENT: Removing Fake connection");
138 //this is not used on this side
139 // m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
140 m_rpcFakeConnection.Reset();
143 // Detach RPC client listener
144 LogInfo("CLIENT: Detaching connection established event");
145 m_rpcUnixClient.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
146 m_rpcFakeClient.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
149 LogInfo("CLIENT: Closing RPC client");
150 m_rpcUnixClient.CloseAll();
151 m_rpcFakeClient.Close();//not needed
158 DECLARE_GENERIC_EVENT_0(QuitEvent)
159 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
162 : public DPL::Application,
163 private DPL::Controller<DPL::TypeListDecl<QuitEvent,
164 CloseThreadEvent>::Type>,
165 private DPL::EventListener<AsyncCallEvent>,
166 private DPL::EventListener<ConnectionEstablishedEvent>
169 DPL::UnixSocketRPCServer m_rpcUnixServer;
170 DPL::FakeRpcServer m_rpcFakeServer;
172 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcUnixConnection;
173 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcFakeConnection;
177 // Quit application event occurred
178 virtual void OnEventReceived(const QuitEvent &/*event*/){
180 LogInfo("SERVER: Closing RPC connection...");
182 // Detach RPC connection listeners
183 if (m_rpcUnixConnection.Get()) {
184 //this is not used on this side
185 // m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
186 m_rpcUnixConnection.Reset();
189 if (m_rpcFakeConnection.Get()) {
190 m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::RemoveListener(this);
191 m_rpcFakeConnection.Reset();
194 LogInfo("SERVER: Closing Server");
195 m_rpcUnixServer.CloseAll();
196 m_rpcFakeServer.CloseAll();//not needed
197 m_rpcUnixServer.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
198 m_rpcFakeServer.DPL::EventSupport<ConnectionEstablishedEvent>::RemoveListener(this);
200 LogInfo("SERVER: Server closed");
205 virtual void OnEventReceived(const CloseThreadEvent &/*event*/){
209 virtual void OnEventReceived(const AsyncCallEvent &event)
211 LogInfo("SERVER: AsyncCallEvent received");
214 event.GetArg0().ConsumeArg(value);
215 LogInfo("SERVER: Result from client: " << value);
217 // send back data to client (via fake)
218 // Emit RPC function call
219 DPL::RPCFunction proc;
220 proc.AppendArg(value);
221 LogInfo("SERVER: Calling RPC function");
222 m_rpcUnixConnection->AsyncCall(proc);
225 virtual void OnEventReceived(const ConnectionEstablishedEvent &event)
227 // Save connection pointer
228 if (dynamic_cast<DPL::FakeRpcConnection *>(event.GetArg1())){
229 LogInfo("SERVER: Acquiring Fake RPC connection");
230 m_rpcFakeConnection.Reset(event.GetArg1());
231 m_rpcFakeConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
234 LogInfo("SERVER: Acquiring Unix RPC connection");
235 m_rpcUnixConnection.Reset(event.GetArg1());
236 //this is not used on this side
237 // m_rpcUnixConnection->DPL::EventSupport<AsyncCallEvent>::AddListener(this);
242 MyApplication(int argc, char **argv)
243 : Application(argc, argv, "rpc")
245 // Attach RPC server listeners
246 LogInfo("SERVER: Attaching connection established event");
247 m_rpcUnixServer.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
248 m_rpcFakeServer.DPL::EventSupport<ConnectionEstablishedEvent>::AddListener(this);
251 LogInfo("SERVER: Touching controller");
255 LogInfo("SERVER: Opening server RPC");
256 m_rpcUnixServer.Open(UNIX_RPC_NAME);
257 m_rpcFakeServer.Open(FAKE_RPC_NAME);
259 // Run RPC client in thread
260 LogInfo("SERVER: Starting RPC client thread");
263 // Quit application automatically in few seconds
264 LogInfo("SERVER: Sending control timed events");
265 DPL::ControllerEventHandler<CloseThreadEvent>::PostTimedEvent(CloseThreadEvent(), 2);
266 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 3);
269 virtual ~MyApplication()
272 LogInfo("SERVER: Quitting thread");
276 int main(int argc, char *argv[])
279 MyApplication app(argc, argv);