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