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)
20 * @brief This file is the implementation file of RPC example
22 #include <dpl/unix_socket_rpc_client.h>
23 #include <dpl/unix_socket_rpc_server.h>
24 #include <dpl/unix_socket_rpc_connection.h>
25 #include <dpl/scoped_ptr.h>
26 #include <dpl/application.h>
27 #include <dpl/controller.h>
28 #include <dpl/thread.h>
29 #include <dpl/log/log.h>
32 static const char *RPC_NAME = "/tmp/unix_socket_rpc";
36 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>,
37 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>,
38 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>,
39 private DPL::EventListener<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>
42 DPL::UnixSocketRPCClient m_rpcClient;
43 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcConnection;
45 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
49 LogInfo("CLIENT: AsyncCallEvent received");
52 event.GetArg0().ConsumeArg(value);
53 LogInfo("CLIENT: Result from server: " << value);
56 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
59 LogInfo("CLIENT: ConnectionClosedEvent received");
62 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
65 LogInfo("CLIENT: ConnectionBrokenEvent received");
68 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
70 // Save connection pointer
71 LogInfo("CLIENT: Acquiring new connection");
72 m_rpcConnection.Reset(event.GetArg1());
74 // Attach listener to new connection
75 LogInfo("CLIENT: Attaching connection event listeners");
76 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::AddListener(this);
77 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::AddListener(this);
78 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::AddListener(this);
80 LogInfo("CLIENT: Connection established");
82 // Emit RPC function call
83 DPL::RPCFunction proc;
84 proc.AppendArg((int)1111);
85 LogInfo("CLIENT: Calling RPC function");
86 m_rpcConnection->AsyncCall(proc);
96 virtual int ThreadEntry()
98 // Attach RPC listeners
99 LogInfo("CLIENT: Attaching connection established event");
100 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
102 // Open connection to server
103 LogInfo("CLIENT: Opening connection to RPC");
104 m_rpcClient.Open(RPC_NAME);
106 // Start message loop
107 LogInfo("CLIENT: Starting thread event loop");
110 // Detach RPC listeners
111 if (m_rpcConnection.Get())
113 LogInfo("CLIENT: Detaching RPC connection events");
114 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::RemoveListener(this);
115 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::RemoveListener(this);
116 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::RemoveListener(this);
118 LogInfo("CLIENT: Resetting connection");
119 m_rpcConnection.Reset();
122 // Detach RPC client listener
123 LogInfo("CLIENT: Detaching connection established event");
124 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
127 LogInfo("CLIENT: Closing RPC client");
128 m_rpcClient.CloseAll();
135 DECLARE_GENERIC_EVENT_0(QuitEvent)
136 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
139 : public DPL::Application,
140 private DPL::Controller<DPL::TypeListDecl<QuitEvent,
141 CloseThreadEvent>::Type>,
142 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>,
143 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>,
144 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>,
145 private DPL::EventListener<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>
148 DPL::UnixSocketRPCServer m_rpcServer;
149 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcConnection;
153 // Quit application event occurred
154 virtual void OnEventReceived(const QuitEvent &event)
160 virtual void OnEventReceived(const CloseThreadEvent &event)
166 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
170 LogInfo("SERVER: AsyncCallEvent received");
173 event.GetArg0().ConsumeArg(value);
174 LogInfo("SERVER: Result from client: " << value);
177 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
181 LogInfo("SERVER: ConnectionClosedEvent received");
184 LogInfo("SERVER: Closing RPC connection on event...");
186 // Detach RPC connection listeners
187 if (m_rpcConnection.Get())
189 LogInfo("SERVER: Detaching connection events");
190 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::RemoveListener(this);
191 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::RemoveListener(this);
192 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::RemoveListener(this);
194 LogInfo("SERVER: RPC connection closed");
196 LogInfo("SERVER: Closing RPC on event...");
197 m_rpcServer.CloseAll();
198 LogInfo("SERVER: RPC closed");
201 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
204 LogInfo("SERVER: ConnectionBrokenEvent received");
207 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
209 // Save connection pointer
210 LogInfo("SERVER: Acquiring RPC connection");
211 m_rpcConnection.Reset(event.GetArg1());
213 // Attach event listeners
214 LogInfo("SERVER: Attaching connection listeners");
215 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::AddListener(this);
216 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::AddListener(this);
217 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::AddListener(this);
219 LogInfo("SERVER: Connection established");
221 // Emit RPC function call
222 DPL::RPCFunction proc;
223 proc.AppendArg((int)2222);
224 LogInfo("SERVER: Calling RPC function");
225 m_rpcConnection->AsyncCall(proc);
229 MyApplication(int argc, char **argv)
230 : Application(argc, argv, "rpc")
232 // Attach RPC server listeners
233 LogInfo("SERVER: Attaching connection established event");
234 m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
237 LogInfo("SERVER: Touching controller");
241 LogInfo("SERVER: Opening server RPC");
242 m_rpcServer.Open(RPC_NAME);
244 // Run RPC client in thread
245 LogInfo("SERVER: Starting RPC client thread");
248 // Quit application automatically in few seconds
249 LogInfo("SERVER: Sending control timed events");
250 DPL::ControllerEventHandler<CloseThreadEvent>::PostTimedEvent(CloseThreadEvent(), 2);
251 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 3);
254 virtual ~MyApplication()
257 LogInfo("SERVER: Quitting thread");
261 LogInfo("SERVER: Closing RPC server");
262 m_rpcServer.CloseAll();
264 // Detach RPC server listener
265 m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
269 int main(int argc, char *argv[])
272 MyApplication app(argc, argv);