tizen 2.4 release
[framework/web/wrt-commons.git] / examples / tcpsock / tcpsock.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        tcpsock.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of tcpsock example
21  */
22 #include <stddef.h>
23 #include <memory>
24 #include <dpl/tcp_socket.h>
25 #include <dpl/abstract_socket.h>
26 #include <dpl/application.h>
27 #include <dpl/generic_event.h>
28 #include <dpl/binary_queue.h>
29 #include <dpl/log/wrt_log.h>
30 #include <string>
31 #include <dpl/assert.h>
32
33 class MyApplication
34     : public DPL::Application,
35       private DPL::EventListener<DPL::AbstractSocketEvents::ConnectedEvent>,
36       private DPL::EventListener<DPL::AbstractSocketEvents::ReadEvent>
37 {
38 private:
39     DPL::TcpSocket m_socket;
40
41     virtual void OnEventReceived(const DPL::AbstractSocketEvents::ConnectedEvent &event)
42     {
43         (void)event;
44         WrtLogD("Connected!");
45
46         // Send request
47         DPL::BinaryQueue data;
48         const char *query = "GET /wiki/Main_Page HTTP/1.1\nHost: en.wikipedia.org\n\n";
49         data.AppendCopy(query, strlen(query) + 1);
50         m_socket.Write(data, data.Size());
51     }
52
53     virtual void OnEventReceived(const DPL::AbstractSocketEvents::ReadEvent &event)
54     {
55         (void)event;
56         WrtLogD("Read!");
57
58         DPL::BinaryQueueAutoPtr data = m_socket.Read(100); // Bad: DLOG cannot log more than about 450 bytes...
59
60         Assert(data.get() != NULL);
61
62         if (data->Empty())
63         {
64             WrtLogD("Connection closed!");
65             m_socket.Close();
66
67             // Done
68             Quit();
69             return;
70         }
71
72         // Show data
73         std::unique_ptr<char[]> text(new char[data->Size()]);
74         data->Flatten(text.get(), data->Size());
75
76         WrtLogD("READ: \n--------------------------------------------------------\n"
77                             << std::string(text.Get(), text.get() + data->Size()) <<
78                           "\n--------------------------------------------------------");
79     }
80
81 public:
82     MyApplication(int argc, char **argv)
83         : Application(argc, argv, "tcpsock")
84     {
85         WrtLogD("CTOR!");
86
87         // Add listeners
88         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::AddListener(this);
89         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ReadEvent>::AddListener(this);
90
91         // Connect
92         m_socket.Open();
93         WrtLogD("Connecting...");
94         m_socket.Connect(DPL::Address("en.wikipedia.org", 80));
95     }
96
97     virtual ~MyApplication()
98     {
99         WrtLogD("DTOR!");
100
101         // Remove listeners
102         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::RemoveListener(this);
103         m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ReadEvent>::RemoveListener(this);
104     }
105 };
106
107 int main(int argc, char *argv[])
108 {
109     MyApplication app(argc, argv);
110     return app.Exec();
111 }