Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / testechoserver.h
1 /*
2  * libjingle
3  * Copyright 2004--2011, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef TALK_BASE_TESTECHOSERVER_H_
29 #define TALK_BASE_TESTECHOSERVER_H_
30
31 #include <list>
32 #include "talk/base/asynctcpsocket.h"
33 #include "talk/base/socketaddress.h"
34 #include "talk/base/sigslot.h"
35 #include "talk/base/thread.h"
36
37 namespace talk_base {
38
39 // A test echo server, echoes back any packets sent to it.
40 // Useful for unit tests.
41 class TestEchoServer : public sigslot::has_slots<> {
42  public:
43   TestEchoServer(Thread* thread, const SocketAddress& addr)
44       : server_socket_(thread->socketserver()->CreateAsyncSocket(addr.family(),
45                                                                  SOCK_STREAM)) {
46     server_socket_->Bind(addr);
47     server_socket_->Listen(5);
48     server_socket_->SignalReadEvent.connect(this, &TestEchoServer::OnAccept);
49   }
50   ~TestEchoServer() {
51     for (ClientList::iterator it = client_sockets_.begin();
52          it != client_sockets_.end(); ++it) {
53       delete *it;
54     }
55   }
56
57   SocketAddress address() const { return server_socket_->GetLocalAddress(); }
58
59  private:
60   void OnAccept(AsyncSocket* socket) {
61     AsyncSocket* raw_socket = socket->Accept(NULL);
62     if (raw_socket) {
63       AsyncTCPSocket* packet_socket = new AsyncTCPSocket(raw_socket, false);
64       packet_socket->SignalReadPacket.connect(this, &TestEchoServer::OnPacket);
65       packet_socket->SignalClose.connect(this, &TestEchoServer::OnClose);
66       client_sockets_.push_back(packet_socket);
67     }
68   }
69   void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size,
70                 const SocketAddress& remote_addr,
71                 const PacketTime& packet_time) {
72     talk_base::PacketOptions options;
73     socket->Send(buf, size, options);
74   }
75   void OnClose(AsyncPacketSocket* socket, int err) {
76     ClientList::iterator it =
77         std::find(client_sockets_.begin(), client_sockets_.end(), socket);
78     client_sockets_.erase(it);
79     Thread::Current()->Dispose(socket);
80   }
81
82   typedef std::list<AsyncTCPSocket*> ClientList;
83   scoped_ptr<AsyncSocket> server_socket_;
84   ClientList client_sockets_;
85   DISALLOW_EVIL_CONSTRUCTORS(TestEchoServer);
86 };
87
88 }  // namespace talk_base
89
90 #endif  // TALK_BASE_TESTECHOSERVER_H_