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
23 #include <dpl/unix_socket.h>
24 #include <dpl/abstract_socket.h>
25 #include <dpl/application.h>
26 #include <dpl/controller.h>
27 #include <dpl/generic_event.h>
28 #include <dpl/thread.h>
29 #include <dpl/log/log.h>
31 #include <dpl/assert.h>
33 namespace // anonymous
35 static const char *SOCKET_NAME = "/tmp/unix_sock";
36 } // namespace anonymous
40 private DPL::EventListener<DPL::AbstractSocketEvents::AcceptEvent>
43 DPL::UnixSocket m_socket;
45 // Socket accept event
46 virtual void OnEventReceived(const DPL::AbstractSocketEvents::AcceptEvent &event)
49 LogInfo("Accept event occurred");
51 DPL::UnixSocket *client = static_cast<DPL::UnixSocket *>(m_socket.Accept());
53 LogInfo("Accepted client remote address: " << client->GetRemoteAddress().ToString());
54 LogInfo("Accepted client local address: " << client->GetLocalAddress().ToString());
66 virtual int ThreadEntry()
69 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::AddListener(this);
72 LogInfo("Starting server...");
74 m_socket.Bind(DPL::Address(SOCKET_NAME));
77 LogInfo("Server started");
79 LogInfo("Server local address: " << m_socket.GetLocalAddress().ToString());
84 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::RemoveListener(this);
86 // Must close socket in same context
93 DECLARE_GENERIC_EVENT_0(QuitEvent)
96 : public DPL::Application,
97 public DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
98 private DPL::EventListener<DPL::AbstractSocketEvents::ConnectedEvent>
102 DPL::UnixSocket sock;
104 // Quit application event occurred
105 virtual void OnEventReceived(const QuitEvent &event)
111 // Socket connected event
112 virtual void OnEventReceived(const DPL::AbstractSocketEvents::ConnectedEvent &event)
115 LogInfo("Connected event occurred");
119 MyApplication(int argc, char **argv)
120 : Application(argc, argv, "example_socket_application")
123 sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::AddListener(this);
125 // Touch self controller
128 // Start threaded server
129 LogInfo("Running threaded server");
131 // Run server in thread
134 LogInfo("Waiting for server to start...");
138 sock.Connect(DPL::Address(SOCKET_NAME));
140 // Quit application automatically in few seconds
141 DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 2);
144 virtual ~MyApplication()
147 sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::RemoveListener(this);
151 int main(int argc, char *argv[])
153 MyApplication app(argc, argv);