tizen 2.4 release
[framework/web/wrt-commons.git] / examples / socket / socket.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        socket.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of socket example
21  */
22 #include <stddef.h>
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/wrt_log.h>
30 #include <string>
31 #include <dpl/assert.h>
32
33 namespace // anonymous
34 {
35 static const char *SOCKET_NAME = "/tmp/unix_sock";
36 } // namespace anonymous
37
38 class MyThread
39     : public DPL::Thread,
40       private DPL::EventListener<DPL::AbstractSocketEvents::AcceptEvent>
41 {
42 private:
43     DPL::UnixSocket m_socket;
44
45     // Socket accept event
46     virtual void OnEventReceived(const DPL::AbstractSocketEvents::AcceptEvent &event)
47     {
48         (void)event;
49         WrtLogD("Accept event occurred");
50
51         DPL::UnixSocket *client = static_cast<DPL::UnixSocket *>(m_socket.Accept());
52
53         WrtLogD("Accepted client remote address: %s", client->GetRemoteAddress().ToString().c_str());
54         WrtLogD("Accepted client local address: %s", client->GetLocalAddress().ToString().c_str());
55      
56         delete client;
57     }
58
59 public:
60     virtual ~MyThread()
61     {
62         // Quit thread
63         Quit();
64     }
65
66     virtual int ThreadEntry()
67     {
68         // Add listeners
69         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::AddListener(this);
70
71         // Create server
72         WrtLogD("Starting server...");
73
74         m_socket.Bind(DPL::Address(SOCKET_NAME));
75         m_socket.Listen(5);
76
77         WrtLogD("Server started");
78
79         WrtLogD("Server local address: %s", m_socket.GetLocalAddress().ToString().c_str());
80
81         int result = Exec();
82
83         // Remove listeners
84         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::AcceptEvent>::RemoveListener(this);
85
86         // Must close socket in same context
87         m_socket.Close();
88
89         return result;
90     }
91 };
92
93 DECLARE_GENERIC_EVENT_0(QuitEvent)
94
95 class MyApplication
96     : public DPL::Application,
97       public DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
98       private DPL::EventListener<DPL::AbstractSocketEvents::ConnectedEvent>
99 {
100 private:
101     MyThread thread;
102     DPL::UnixSocket sock;
103
104     // Quit application event occurred
105     virtual void OnEventReceived(const QuitEvent &event)
106     {
107         (void)event;
108         Quit();
109     }
110
111     // Socket connected event
112     virtual void OnEventReceived(const DPL::AbstractSocketEvents::ConnectedEvent &event)
113     {
114         (void)event;
115         WrtLogD("Connected event occurred");
116     }
117
118 public:
119     MyApplication(int argc, char **argv)
120         : Application(argc, argv, "example_socket_application")
121     {
122         // Add listeners
123         sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::AddListener(this);
124
125         // Touch self controller
126         Touch();
127
128         // Start threaded server
129         WrtLogD("Running threaded server");
130
131         // Run server in thread
132         thread.Run();
133
134         WrtLogD("Waiting for server to start...");
135         sleep(1);
136
137         // Connect to server
138         sock.Connect(DPL::Address(SOCKET_NAME));
139
140         // Quit application automatically in few seconds
141         DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 2);
142     }
143
144     virtual ~MyApplication()
145     {
146         // Remove listeners
147         sock.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::RemoveListener(this);
148     }
149 };
150
151 int main(int argc, char *argv[])
152 {
153     MyApplication app(argc, argv);
154     return app.Exec();
155 }