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