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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of socket example
22 #include <dpl/unix_socket.h>
23 #include <dpl/abstract_socket.h>
24 #include <dpl/application.h>
25 #include <dpl/controller.h>
26 #include <dpl/generic_event.h>
27 #include <dpl/thread.h>
28 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
32 namespace // anonymous
34 static const char *SOCKET_NAME = "/tmp/unix_sock";
35 } // namespace anonymous
39 private DPL::EventListener<DPL::AbstractSocketEvents::AcceptEvent>
42 DPL::UnixSocket m_socket;
44 // Socket accept event
45 virtual void OnEventReceived(const DPL::AbstractSocketEvents::AcceptEvent &event)
48 LogInfo("Accept event occurred");
50 DPL::UnixSocket *client = static_cast<DPL::UnixSocket *>(m_socket.Accept());
52 LogInfo("Accepted client remote address: " << client->GetRemoteAddress().ToString());
53 LogInfo("Accepted client local address: " << client->GetLocalAddress().ToString());
65 virtual int ThreadEntry()
68 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::AddListener(this);
71 LogInfo("Starting server...");
73 m_socket.Bind(DPL::Address(SOCKET_NAME));
76 LogInfo("Server started");
78 LogInfo("Server local address: " << m_socket.GetLocalAddress().ToString());
83 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::RemoveListener(this);
85 // Must close socket in same context
92 DECLARE_GENERIC_EVENT_0(QuitEvent)
95 : public DPL::Application,
96 public DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
97 private DPL::EventListener<DPL::AbstractSocketEvents::ConnectedEvent>
101 DPL::UnixSocket sock;
103 // Quit application event occurred
104 virtual void OnEventReceived(const QuitEvent &event)
110 // Socket connected event
111 virtual void OnEventReceived(const DPL::AbstractSocketEvents::ConnectedEvent &event)
114 LogInfo("Connected event occurred");
118 MyApplication(int argc, char **argv)
119 : Application(argc, argv, "example_socket_application")
122 sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::AddListener(this);
124 // Touch self controller
127 // Start threaded server
128 LogInfo("Running threaded server");
130 // Run server in thread
133 LogInfo("Waiting for server to start...");
137 sock.Connect(DPL::Address(SOCKET_NAME));
139 // Quit application automatically in few seconds
140 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 2);
143 virtual ~MyApplication()
146 sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::RemoveListener(this);
150 int main(int argc, char *argv[])
152 MyApplication app(argc, argv);