blob: 0e7625f73657da3fecd1d26183895ae156b95712 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, 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#include "talk/base/testclient.h"
29#include "talk/base/thread.h"
30#include "talk/base/timeutils.h"
31
32namespace talk_base {
33
34// DESIGN: Each packet received is put it into a list of packets.
35// Callers can retrieve received packets from any thread by calling
36// NextPacket.
37
38TestClient::TestClient(AsyncPacketSocket* socket)
39 : socket_(socket), ready_to_send_(false) {
40 packets_ = new std::vector<Packet*>();
41 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
42 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
43}
44
45TestClient::~TestClient() {
46 delete socket_;
47 for (unsigned i = 0; i < packets_->size(); i++)
48 delete (*packets_)[i];
49 delete packets_;
50}
51
52bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
53 // Wait for our timeout value until the socket reaches the desired state.
54 uint32 end = TimeAfter(kTimeout);
55 while (socket_->GetState() != state && TimeUntil(end) > 0)
56 Thread::Current()->ProcessMessages(1);
57 return (socket_->GetState() == state);
58}
59
60int TestClient::Send(const char* buf, size_t size) {
61 return socket_->Send(buf, size);
62}
63
64int TestClient::SendTo(const char* buf, size_t size,
65 const SocketAddress& dest) {
66 return socket_->SendTo(buf, size, dest);
67}
68
69TestClient::Packet* TestClient::NextPacket() {
70 // If no packets are currently available, we go into a get/dispatch loop for
71 // at most 1 second. If, during the loop, a packet arrives, then we can stop
72 // early and return it.
73
74 // Note that the case where no packet arrives is important. We often want to
75 // test that a packet does not arrive.
76
77 // Note also that we only try to pump our current thread's message queue.
78 // Pumping another thread's queue could lead to messages being dispatched from
79 // the wrong thread to non-thread-safe objects.
80
81 uint32 end = TimeAfter(kTimeout);
82 while (packets_->size() == 0 && TimeUntil(end) > 0)
83 Thread::Current()->ProcessMessages(1);
84
85 // Return the first packet placed in the queue.
86 Packet* packet = NULL;
87 if (packets_->size() > 0) {
88 CritScope cs(&crit_);
89 packet = packets_->front();
90 packets_->erase(packets_->begin());
91 }
92
93 return packet;
94}
95
96bool TestClient::CheckNextPacket(const char* buf, size_t size,
97 SocketAddress* addr) {
98 bool res = false;
99 Packet* packet = NextPacket();
100 if (packet) {
101 res = (packet->size == size && std::memcmp(packet->buf, buf, size) == 0);
102 if (addr)
103 *addr = packet->addr;
104 delete packet;
105 }
106 return res;
107}
108
109bool TestClient::CheckNoPacket() {
110 bool res;
111 Packet* packet = NextPacket();
112 res = (packet == NULL);
113 delete packet;
114 return res;
115}
116
117int TestClient::GetError() {
118 return socket_->GetError();
119}
120
121int TestClient::SetOption(Socket::Option opt, int value) {
122 return socket_->SetOption(opt, value);
123}
124
125bool TestClient::ready_to_send() const {
126 return ready_to_send_;
127}
128
129void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
130 size_t size, const SocketAddress& remote_addr) {
131 CritScope cs(&crit_);
132 packets_->push_back(new Packet(remote_addr, buf, size));
133}
134
135void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
136 ready_to_send_ = true;
137}
138
139TestClient::Packet::Packet(const SocketAddress& a, const char* b, size_t s)
140 : addr(a), buf(0), size(s) {
141 buf = new char[size];
142 memcpy(buf, b, size);
143}
144
145TestClient::Packet::Packet(const Packet& p)
146 : addr(p.addr), buf(0), size(p.size) {
147 buf = new char[size];
148 memcpy(buf, p.buf, size);
149}
150
151TestClient::Packet::~Packet() {
152 delete[] buf;
153}
154
155} // namespace talk_base