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
23 #include <dpl/unix_socket_rpc_client.h>
24 #include <dpl/unix_socket_rpc_server.h>
25 #include <dpl/unix_socket_rpc_connection.h>
27 #include <dpl/application.h>
28 #include <dpl/controller.h>
29 #include <dpl/thread.h>
30 #include <dpl/log/log.h>
33 static const char *RPC_NAME = "/tmp/unix_socket_rpc";
37 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>,
38 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>,
39 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>,
40 private DPL::EventListener<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>
43 DPL::UnixSocketRPCClient m_rpcClient;
44 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
46 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
50 LogDebug("CLIENT: AsyncCallEvent received");
53 event.GetArg0().ConsumeArg(value);
54 LogDebug("CLIENT: Result from server: " << value);
57 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
60 LogDebug("CLIENT: ConnectionClosedEvent received");
63 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
66 LogDebug("CLIENT: ConnectionBrokenEvent received");
69 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
71 // Save connection pointer
72 LogDebug("CLIENT: Acquiring new connection");
73 m_rpcConnection.reset(event.GetArg1());
75 // Attach listener to new connection
76 LogDebug("CLIENT: Attaching connection event listeners");
77 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::AddListener(this);
78 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::AddListener(this);
79 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::AddListener(this);
81 LogDebug("CLIENT: Connection established");
83 // Emit RPC function call
84 DPL::RPCFunction proc;
85 proc.AppendArg((int)1111);
86 LogDebug("CLIENT: Calling RPC function");
87 m_rpcConnection->AsyncCall(proc);
97 virtual int ThreadEntry()
99 // Attach RPC listeners
100 LogDebug("CLIENT: Attaching connection established event");
101 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
103 // Open connection to server
104 LogDebug("CLIENT: Opening connection to RPC");
105 m_rpcClient.Open(RPC_NAME);
107 // Start message loop
108 LogDebug("CLIENT: Starting thread event loop");
111 // Detach RPC listeners
112 if (m_rpcConnection.get())
114 LogDebug("CLIENT: Detaching RPC connection events");
115 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::RemoveListener(this);
116 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::RemoveListener(this);
117 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::RemoveListener(this);
119 LogDebug("CLIENT: Resetting connection");
120 m_rpcConnection.reset();
123 // Detach RPC client listener
124 LogDebug("CLIENT: Detaching connection established event");
125 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
128 LogDebug("CLIENT: Closing RPC client");
129 m_rpcClient.CloseAll();
136 DECLARE_GENERIC_EVENT_0(QuitEvent)
137 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
140 : public DPL::Application,
141 private DPL::Controller<DPL::TypeListDecl<QuitEvent,
142 CloseThreadEvent>::Type>,
143 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>,
144 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>,
145 private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>,
146 private DPL::EventListener<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>
149 DPL::UnixSocketRPCServer m_rpcServer;
150 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
154 // Quit application event occurred
155 virtual void OnEventReceived(const QuitEvent &event)
161 virtual void OnEventReceived(const CloseThreadEvent &event)
167 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
171 LogDebug("SERVER: AsyncCallEvent received");
174 event.GetArg0().ConsumeArg(value);
175 LogDebug("SERVER: Result from client: " << value);
178 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
182 LogDebug("SERVER: ConnectionClosedEvent received");
185 LogDebug("SERVER: Closing RPC connection on event...");
187 // Detach RPC connection listeners
188 if (m_rpcConnection.get())
190 LogDebug("SERVER: Detaching connection events");
191 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::RemoveListener(this);
192 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::RemoveListener(this);
193 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::RemoveListener(this);
195 LogDebug("SERVER: RPC connection closed");
197 LogDebug("SERVER: Closing RPC on event...");
198 m_rpcServer.CloseAll();
199 LogDebug("SERVER: RPC closed");
202 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
205 LogDebug("SERVER: ConnectionBrokenEvent received");
208 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
210 // Save connection pointer
211 LogDebug("SERVER: Acquiring RPC connection");
212 m_rpcConnection.reset(event.GetArg1());
214 // Attach event listeners
215 LogDebug("SERVER: Attaching connection listeners");
216 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::AddListener(this);
217 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::AddListener(this);
218 m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::AddListener(this);
220 LogDebug("SERVER: Connection established");
222 // Emit RPC function call
223 DPL::RPCFunction proc;
224 proc.AppendArg((int)2222);
225 LogDebug("SERVER: Calling RPC function");
226 m_rpcConnection->AsyncCall(proc);
230 MyApplication(int argc, char **argv)
231 : Application(argc, argv, "rpc")
233 // Attach RPC server listeners
234 LogDebug("SERVER: Attaching connection established event");
235 m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
238 LogDebug("SERVER: Touching controller");
242 LogDebug("SERVER: Opening server RPC");
243 m_rpcServer.Open(RPC_NAME);
245 // Run RPC client in thread
246 LogDebug("SERVER: Starting RPC client thread");
249 // Quit application automatically in few seconds
250 LogDebug("SERVER: Sending control timed events");
251 DPL::ControllerEventHandler<CloseThreadEvent>::PostTimedEvent(CloseThreadEvent(), 2);
252 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 3);
255 virtual ~MyApplication()
258 LogDebug("SERVER: Quitting thread");
262 LogDebug("SERVER: Closing RPC server");
263 m_rpcServer.CloseAll();
265 // Detach RPC server listener
266 m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
270 int main(int argc, char *argv[])
272 LogDebug("Starting");
273 MyApplication app(argc, argv);