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
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>
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>
36 DPL::TcpSocketRPCClient m_rpcClient;
37 std::unique_ptr<DPL::AbstractRPCConnection> m_rpcConnection;
39 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::AsyncCallEvent &event)
44 LogInfo("* Got metronome signal *");
47 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionClosedEvent &event)
51 LogInfo("Connection closed");
57 virtual void OnEventReceived(const DPL::AbstractRPCConnectionEvents::ConnectionBrokenEvent &event)
61 LogInfo("Connection broken");
67 virtual void OnEventReceived(const DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent &event)
69 // Save connection pointer
70 LogInfo("Connected to metronome server");
71 m_rpcConnection.reset(event.GetArg1());
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);
80 MetronomeClientApplication(int argc, char **argv)
81 : Application(argc, argv, "rpc")
83 // Attach RPC server listeners
84 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::AddListener(this);
87 m_rpcClient.Open("127.0.0.1", 12345);
90 LogInfo("Metronome client started");
93 virtual ~MetronomeClientApplication()
95 // Delete all RPC connections
96 if (m_rpcConnection.get())
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();
105 m_rpcClient.CloseAll();
107 // Detach RPC server listener
108 m_rpcClient.DPL::EventSupport<DPL::AbstractRPCConnectorEvents::ConnectionEstablishedEvent>::RemoveListener(this);
112 int main(int argc, char *argv[])
114 return MetronomeClientApplication(argc, argv).Exec();