tizen 2.4 release
[framework/web/wrt-commons.git] / examples / rpc / rpc.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        rpc.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of RPC example
21  */
22 #include <stddef.h>
23 #include <dpl/unix_socket_rpc_client.h>
24 #include <dpl/unix_socket_rpc_server.h>
25 #include <dpl/unix_socket_rpc_connection.h>
26 #include <memory>
27 #include <dpl/application.h>
28 #include <dpl/controller.h>
29 #include <dpl/thread.h>
30 #include <dpl/log/wrt_log.h>
31 #include <string>
32
33 static const char *RPC_NAME = "/tmp/unix_socket_rpc";
34
35 class MyThread
36     : public DPL::Thread,
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>
41 {
42 private:
43     DPL::UnixSocketRPCClient m_rpcClient;
44     std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
45
46     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
47     {
48         (void)event;
49
50         WrtLogD("CLIENT: AsyncCallEvent received");
51
52         int value;
53         event.GetArg0().ConsumeArg(value);
54         WrtLogD("CLIENT: Result from server: %i", value);
55     }
56
57     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
58     {
59         (void)event;
60         WrtLogD("CLIENT: ConnectionClosedEvent received");
61     }
62
63     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
64     {
65         (void)event;
66         WrtLogD("CLIENT: ConnectionBrokenEvent received");
67     }
68
69     virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
70     {
71         // Save connection pointer
72         WrtLogD("CLIENT: Acquiring new connection");
73         m_rpcConnection.reset(event.GetArg1());
74
75         // Attach listener to new connection
76         WrtLogD("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);
80
81         WrtLogD("CLIENT: Connection established");
82
83         // Emit RPC function call
84         DPL::RPCFunction proc;
85         proc.AppendArg((int)1111);
86         WrtLogD("CLIENT: Calling RPC function");
87         m_rpcConnection->AsyncCall(proc);
88     }
89
90 public:
91     virtual ~MyThread()
92     {
93         // Always quit thread
94        Quit();
95     }
96
97     virtual int ThreadEntry()
98     {
99         // Attach RPC listeners
100         WrtLogD("CLIENT: Attaching connection established event");
101         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
102
103         // Open connection to server
104         WrtLogD("CLIENT: Opening connection to RPC");
105         m_rpcClient.Open(RPC_NAME);
106
107         // Start message loop
108         WrtLogD("CLIENT: Starting thread event loop");
109         int ret = Exec();
110
111         // Detach RPC listeners
112         if (m_rpcConnection.get())
113         {
114             WrtLogD("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);
118
119             WrtLogD("CLIENT: Resetting connection");
120             m_rpcConnection.reset();
121         }
122
123         // Detach RPC client listener
124         WrtLogD("CLIENT: Detaching connection established event");
125         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
126
127         // Close RPC
128         WrtLogD("CLIENT: Closing RPC client");
129         m_rpcClient.CloseAll();
130
131         // Done
132         return ret;
133     }
134 };
135
136 DECLARE_GENERIC_EVENT_0(QuitEvent)
137 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
138
139 class MyApplication
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>
147 {
148 private:
149     DPL::UnixSocketRPCServer m_rpcServer;
150     std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
151
152     MyThread m_thread;
153
154     // Quit application event occurred
155     virtual void OnEventReceived(const QuitEvent &event)
156     {
157         (void)event;
158         Quit();
159     }
160
161     virtual void OnEventReceived(const CloseThreadEvent &event)
162     {
163         (void)event;
164         m_thread.Quit();
165     }
166
167     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
168     {
169         (void)event;
170
171         WrtLogD("SERVER: AsyncCallEvent received");
172
173         int value;
174         event.GetArg0().ConsumeArg(value);
175         WrtLogD("SERVER: Result from client: %i", value);
176     }
177
178     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
179     {
180         (void)event;
181
182         WrtLogD("SERVER: ConnectionClosedEvent received");
183
184         // Close RPC now
185         WrtLogD("SERVER: Closing RPC connection on event...");
186
187         // Detach RPC connection listeners
188         if (m_rpcConnection.get())
189         {
190             WrtLogD("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);
194         }
195         WrtLogD("SERVER: RPC connection closed");
196
197         WrtLogD("SERVER: Closing RPC on event...");
198         m_rpcServer.CloseAll();
199         WrtLogD("SERVER: RPC closed");
200     }
201
202     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
203     {
204         (void)event;
205         WrtLogD("SERVER: ConnectionBrokenEvent received");
206     }
207
208     virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
209     {
210         // Save connection pointer
211         WrtLogD("SERVER: Acquiring RPC connection");
212         m_rpcConnection.reset(event.GetArg1());
213
214         // Attach event listeners
215         WrtLogD("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);
219
220         WrtLogD("SERVER: Connection established");
221
222         // Emit RPC function call
223         DPL::RPCFunction proc;
224         proc.AppendArg((int)2222);
225         WrtLogD("SERVER: Calling RPC function");
226         m_rpcConnection->AsyncCall(proc);
227     }
228
229 public:
230     MyApplication(int argc, char **argv)
231         : Application(argc, argv, "rpc")
232     {
233         // Attach RPC server listeners
234         WrtLogD("SERVER: Attaching connection established event");
235         m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
236
237         // Self touch
238         WrtLogD("SERVER: Touching controller");
239         Touch();
240
241         // Open RPC server
242         WrtLogD("SERVER: Opening server RPC");
243         m_rpcServer.Open(RPC_NAME);
244
245         // Run RPC client in thread
246         WrtLogD("SERVER: Starting RPC client thread");
247         m_thread.Run();
248
249         // Quit application automatically in few seconds
250         WrtLogD("SERVER: Sending control timed events");
251         DPL::ControllerEventHandler<CloseThreadEvent>::PostTimedEvent(CloseThreadEvent(), 2);
252         DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 3);
253     }
254
255     virtual ~MyApplication()
256     {
257         // Quit thread
258         WrtLogD("SERVER: Quitting thread");
259         m_thread.Quit();
260
261         // Close RPC server
262         WrtLogD("SERVER: Closing RPC server");
263         m_rpcServer.CloseAll();
264
265         // Detach RPC server listener
266         m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
267     }
268 };
269
270 int main(int argc, char *argv[])
271 {
272     WrtLogD("Starting");
273     MyApplication app(argc, argv);
274     return app.Exec();
275 }