Tizen 2.0 Release
[framework/web/wrt-commons.git] / examples / metronome / metronome_client.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        metronome_client.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of metronome client example
21  */
22 #include <stddef.h>
23 #include <dpl/tcp_socket_rpc_client.h>
24 #include <dpl/tcp_socket_rpc_connection.h>
25 #include <dpl/application.h>
26 #include <dpl/log/log.h>
27
28 class MetronomeClientApplication
29     : public DPL::Application,
30       private DPL::EventListener<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>,
31       private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>,
32       private DPL::EventListener<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>,
33       private DPL::EventListener<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>
34 {
35 private:
36     DPL::TcpSocketRPCClient m_rpcClient;
37     std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
38
39     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
40     {
41         (void)event;
42
43         // Heart beat
44         LogInfo("* Got metronome signal *");
45     }
46
47     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
48     {
49         (void)event;
50
51         LogInfo("Connection closed");
52
53         // Must quit
54         Quit();
55     }
56
57     virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
58     {
59         (void)event;
60
61         LogInfo("Connection broken");
62
63         // Must quit
64         Quit();
65     }
66
67     virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
68     {
69         // Save connection pointer
70         LogInfo("Connected to metronome server");
71         m_rpcConnection.reset(event.GetArg1());
72
73         // Attach event listeners
74         m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::AddListener(this);
75         m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::AddListener(this);
76         m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::AddListener(this);
77     }
78
79 public:
80     MetronomeClientApplication(int argc, char **argv)
81         : Application(argc, argv, "rpc")
82     {
83         // Attach RPC server listeners
84         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
85
86         // Open RPC server
87         m_rpcClient.Open("127.0.0.1", 12345);
88
89         // Started
90         LogInfo("Metronome client started");
91      }
92
93     virtual ~MetronomeClientApplication()
94     {
95         // Delete all RPC connections
96         if (m_rpcConnection.get())
97         {
98             m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::AsyncCallEvent>::RemoveListener(this);
99             m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent>::RemoveListener(this);
100             m_rpcConnection->DPL::EventSupport<DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent>::RemoveListener(this);
101             m_rpcConnection.reset();
102         }
103
104         // Close RPC server
105         m_rpcClient.CloseAll();
106
107         // Detach RPC server listener
108         m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
109     }
110 };
111
112 int main(int argc, char *argv[])
113 {
114     return MetronomeClientApplication(argc, argv).Exec();
115 }