Update User Agent String
[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 <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>
30 #include <string>
31
32 static const char *RPC_NAME = "/tmp/unix_socket_rpc";
33
34 class MyThread
35     : public DPL::Thread,
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>
40 {
41 private:
42     DPL::UnixSocketRPCClient m_rpcClient;
43     DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcConnection;
44
45     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
46     {
47         (void)event;
48
49         LogInfo("CLIENT: AsyncCallEvent received");
50
51         int value;
52         event.GetArg0().ConsumeArg(value);
53         LogInfo("CLIENT: Result from server: " << value);
54     }
55
56     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
57     {
58         (void)event;
59         LogInfo("CLIENT: ConnectionClosedEvent received");
60     }
61
62     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
63     {
64         (void)event;
65         LogInfo("CLIENT: ConnectionBrokenEvent received");
66     }
67
68     virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
69     {
70         // Save connection pointer
71         LogInfo("CLIENT: Acquiring new connection");
72         m_rpcConnection.Reset(event.GetArg1());
73
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);
79
80         LogInfo("CLIENT: Connection established");
81
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);
87     }
88
89 public:
90     virtual ~MyThread()
91     {
92         // Always quit thread
93        Quit();
94     }
95
96     virtual int ThreadEntry()
97     {
98         // Attach RPC listeners
99         LogInfo("CLIENT: Attaching connection established event");
100         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
101
102         // Open connection to server
103         LogInfo("CLIENT: Opening connection to RPC");
104         m_rpcClient.Open(RPC_NAME);
105
106         // Start message loop
107         LogInfo("CLIENT: Starting thread event loop");
108         int ret = Exec();
109
110         // Detach RPC listeners
111         if (m_rpcConnection.Get())
112         {
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);
117
118             LogInfo("CLIENT: Resetting connection");
119             m_rpcConnection.Reset();
120         }
121
122         // Detach RPC client listener
123         LogInfo("CLIENT: Detaching connection established event");
124         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
125
126         // Close RPC
127         LogInfo("CLIENT: Closing RPC client");
128         m_rpcClient.CloseAll();
129
130         // Done
131         return ret;
132     }
133 };
134
135 DECLARE_GENERIC_EVENT_0(QuitEvent)
136 DECLARE_GENERIC_EVENT_0(CloseThreadEvent)
137
138 class MyApplication
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>
146 {
147 private:
148     DPL::UnixSocketRPCServer m_rpcServer;
149     DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcConnection;
150
151     MyThread m_thread;
152
153     // Quit application event occurred
154     virtual void OnEventReceived(const QuitEvent &event)
155     {
156         (void)event;
157         Quit();
158     }
159
160     virtual void OnEventReceived(const CloseThreadEvent &event)
161     {
162         (void)event;
163         m_thread.Quit();
164     }
165
166     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
167     {
168         (void)event;
169
170         LogInfo("SERVER: AsyncCallEvent received");
171
172         int value;
173         event.GetArg0().ConsumeArg(value);
174         LogInfo("SERVER: Result from client: " << value);
175     }
176
177     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
178     {
179         (void)event;
180
181         LogInfo("SERVER: ConnectionClosedEvent received");
182
183         // Close RPC now
184         LogInfo("SERVER: Closing RPC connection on event...");
185
186         // Detach RPC connection listeners
187         if (m_rpcConnection.Get())
188         {
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);
193         }
194         LogInfo("SERVER: RPC connection closed");
195
196         LogInfo("SERVER: Closing RPC on event...");
197         m_rpcServer.CloseAll();
198         LogInfo("SERVER: RPC closed");
199     }
200
201     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
202     {
203         (void)event;
204         LogInfo("SERVER: ConnectionBrokenEvent received");
205     }
206
207     virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
208     {
209         // Save connection pointer
210         LogInfo("SERVER: Acquiring RPC connection");
211         m_rpcConnection.Reset(event.GetArg1());
212
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);
218
219         LogInfo("SERVER: Connection established");
220
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);
226     }
227
228 public:
229     MyApplication(int argc, char **argv)
230         : Application(argc, argv, "rpc")
231     {
232         // Attach RPC server listeners
233         LogInfo("SERVER: Attaching connection established event");
234         m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
235
236         // Self touch
237         LogInfo("SERVER: Touching controller");
238         Touch();
239
240         // Open RPC server
241         LogInfo("SERVER: Opening server RPC");
242         m_rpcServer.Open(RPC_NAME);
243
244         // Run RPC client in thread
245         LogInfo("SERVER: Starting RPC client thread");
246         m_thread.Run();
247
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);
252     }
253
254     virtual ~MyApplication()
255     {
256         // Quit thread
257         LogInfo("SERVER: Quitting thread");
258         m_thread.Quit();
259
260         // Close RPC server
261         LogInfo("SERVER: Closing RPC server");
262         m_rpcServer.CloseAll();
263
264         // Detach RPC server listener
265         m_rpcServer.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
266     }
267 };
268
269 int main(int argc, char *argv[])
270 {
271     LogInfo("Starting");
272     MyApplication app(argc, argv);
273     return app.Exec();
274 }