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