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.
17 * @file metronome_client.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of metronome client example
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>
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>
35 DPL::TcpSocketRPCClient m_rpcClient;
36 DPL::ScopedPtr<DPL::AbstractRPCConnection> m_rpcConnection;
38 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
43 LogInfo("* Got metronome signal *");
46 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
50 LogInfo("Connection closed");
56 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
60 LogInfo("Connection broken");
66 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
68 // Save connection pointer
69 LogInfo("Connected to metronome server");
70 m_rpcConnection.Reset(event.GetArg1());
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);
79 MetronomeClientApplication(int argc, char **argv)
80 : Application(argc, argv, "rpc")
82 // Attach RPC server listeners
83 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
86 m_rpcClient.Open("127.0.0.1", 12345);
89 LogInfo("Metronome client started");
92 virtual ~MetronomeClientApplication()
94 // Delete all RPC connections
95 if (m_rpcConnection.Get())
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();
104 m_rpcClient.CloseAll();
106 // Detach RPC server listener
107 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
111 int main(int argc, char *argv[])
113 return MetronomeClientApplication(argc, argv).Exec();