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 tcpsock example
23 #include <dpl/tcp_socket.h>
24 #include <dpl/abstract_socket.h>
25 #include <dpl/application.h>
26 #include <dpl/generic_event.h>
27 #include <dpl/binary_queue.h>
28 #include <dpl/scoped_array.h>
29 #include <dpl/log/log.h>
31 #include <dpl/assert.h>
34 : public DPL::Application,
35 private DPL::EventListener<DPL::AbstractSocketEvents::ConnectedEvent>,
36 private DPL::EventListener<DPL::AbstractSocketEvents::ReadEvent>
39 DPL::TcpSocket m_socket;
41 virtual void OnEventReceived(const DPL::AbstractSocketEvents::ConnectedEvent &event)
44 LogInfo("Connected!");
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());
53 virtual void OnEventReceived(const DPL::AbstractSocketEvents::ReadEvent &event)
58 DPL::BinaryQueueAutoPtr data = m_socket.Read(100); // Bad: DLOG cannot log more than about 450 bytes...
60 Assert(data.get() != NULL);
64 LogInfo("Connection closed!");
73 DPL::ScopedArray<char> text(new char[data->Size()]);
74 data->Flatten(text.Get(), data->Size());
76 LogPedantic("READ: \n--------------------------------------------------------\n"
77 << std::string(text.Get(), text.Get() + data->Size()) <<
78 "\n--------------------------------------------------------");
82 MyApplication(int argc, char **argv)
83 : Application(argc, argv, "tcpsock")
88 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::AddListener(this);
89 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ReadEvent>::AddListener(this);
93 LogInfo("Connecting...");
94 m_socket.Connect(DPL::Address("en.wikipedia.org", 80));
97 virtual ~MyApplication()
102 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ConnectedEvent>::RemoveListener(this);
103 m_socket.DPL::EventSupport<DPL::AbstractSocketEvents::ReadEvent>::RemoveListener(this);
107 int main(int argc, char *argv[])
109 MyApplication app(argc, argv);