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