1 /*****************************************************************************\
3 * Name : broadcast_spocket_tester *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2000-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "bcast_spocketer.h"
17 #include <basis/astring.h>
18 #include <basis/byte_array.h>
19 #include <basis/functions.h>
20 #include <loggers/critical_events.h>
21 #include <loggers/program_wide_logger.h>
22 #include <mathematics/chaos.h>
23 #include <sockets/internet_address.h>
24 #include <sockets/raw_socket.h>
25 #include <sockets/spocket.h>
26 #include <sockets/tcpip_definitions.h>
27 #include <sockets/tcpip_stack.h>
28 #include <timely/time_control.h>
29 #include <timely/time_stamp.h>
30 #include <unit_test/unit_base.h>
34 using namespace basis;
35 using namespace loggers;
36 using namespace mathematics;
37 using namespace sockets;
38 using namespace structures;
39 using namespace timely;
40 using namespace unit_test;
42 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
44 const int MAXIMUM_WINSOCK_MTU = 1500;
45 // the largest chunk of bytes we can receive at once.
47 const int MAXIMUM_TRANSFER_WAIT = 40 * SECOND_ms;
48 // the longest amount of time we wait in trying to receive data.
50 static abyte receive_buffer[MAXIMUM_WINSOCK_MTU + 1];
51 // used for dumping received data into.
53 //#define DEBUG_SPOCKET_TESTER
54 // uncomment for noisy version.
56 broadcast_spocket_tester::broadcast_spocket_tester
57 (const internet_address &where, bool unicast)
58 : _where(new internet_address(where)),
59 _stack(new tcpip_stack),
60 _socket(NULL_POINTER),
66 broadcast_spocket_tester::~broadcast_spocket_tester()
74 bool broadcast_spocket_tester::connect()
76 spocket::sock_types type = spocket::BROADCAST;
77 if (_ucast) type = spocket::UNICAST;
79 _socket = new spocket(*_where, type);
81 outcome ret = _socket->connect();
82 return ret == spocket::OKAY;
85 bool broadcast_spocket_tester::do_a_send(const internet_address &where_to,
86 abyte *buffer, int size, testing_statistics &stats)
88 time_stamp start_time;
90 time_stamp when_to_leave(MAXIMUM_TRANSFER_WAIT);
93 #ifdef DEBUG_SPOCKET_TESTER
94 LOG(a_sprintf("into do a send with %d bytes", size));
97 while (time_stamp() < when_to_leave) {
98 worked = _socket->send_to(where_to, buffer, size, len_sent);
99 if (worked == spocket::NONE_READY) {
100 time_control::sleep_ms(20);
102 } else if (worked == spocket::PARTIAL) {
103 //danger danger if we get wrong info.
106 time_control::sleep_ms(20);
110 #ifdef DEBUG_SPOCKET_TESTER
111 LOG("got out of loop");
114 stats.send_time += int(time_stamp().value() - start_time.value());
116 if ( (worked != spocket::OKAY) || !len_sent) {
117 LOG("No data went out on the socket.");
120 if (len_sent != size) {
121 LOG(a_sprintf("The full chunk didn't get sent out: %d bytes instead of %d",
123 //more bad news. what do we do about getting the rest out?
126 stats.bytes_sent += len_sent;
129 // int time_taken = int(end_time.value() - start_time.value());
134 bool broadcast_spocket_tester::do_a_receive(int size_expected,
135 testing_statistics &stats)
137 time_stamp start_time;
139 #ifdef DEBUG_SPOCKET_TESTER
140 LOG("into do a rcv");
143 time_stamp when_to_leave(MAXIMUM_TRANSFER_WAIT);
145 while ( (full_length < size_expected) && (time_stamp() < when_to_leave) ) {
146 time_stamp start_of_receive;
147 int len = MAXIMUM_WINSOCK_MTU;
148 internet_address where_from;
149 outcome ret = _socket->receive_from(receive_buffer, len, where_from);
150 ///LOG(astring("recvfrom outcome is ") + spocket::outcome_name(ret));
151 if (ret != spocket::OKAY) {
152 if (ret == spocket::NONE_READY) {
153 time_control::sleep_ms(20);
157 // reset our time if we've gotten good data.
158 if (ret == spocket::OKAY)
159 when_to_leave.reset(MAXIMUM_TRANSFER_WAIT);
161 int receive_duration = int(time_stamp().value()
162 - start_of_receive.value());
163 stats.receive_time += receive_duration;
165 #ifdef DEBUG_SPOCKET_TESTER
166 LOG(a_sprintf("did recv, len=%d", len));
170 LOG("Our socket has been disconnected.");
172 } else if (len < 0) {
173 if (errno == SOCK_EWOULDBLOCK) continue; // no data.
174 LOG(astring("The receive failed with an error ")
175 + critical_events::system_error_text(errno));
179 stats.bytes_received += len;
182 if (full_length != size_expected)
183 LOG(a_sprintf("Did not get the full size expected (wanted %d and "
184 "got %d bytes).", size_expected, full_length));
189 bool broadcast_spocket_tester::perform_test(const internet_address &dest,
190 int size, int count, testing_statistics &stats)
192 #ifdef DEBUG_SPOCKET_TESTER
193 LOG("into perf test");
196 // the statics are used to generate our random buffer for sending.
197 static abyte garbage_buffer[MAXIMUM_WINSOCK_MTU + 1];
198 static bool garbage_initialized = false;
201 // if our static buffer full of random stuff was never initialized, we do
202 // so now. this supports efficiently re-using the tester if desired.
203 if (!garbage_initialized) {
204 // note the less than or equal; we know we have one more byte to fill.
205 for (int i = 0; i <= MAXIMUM_WINSOCK_MTU; i++)
206 garbage_buffer[i] = randomizer.inclusive(0, 255);
207 garbage_initialized = true;
210 // reset the statistical package.
211 stats.total_runs = 0;
213 stats.receive_time = 0;
214 stats.bytes_sent = 0;
215 stats.bytes_received = 0;
217 // check that they aren't trying to do too big of a send.
218 if (size > MAXIMUM_WINSOCK_MTU) {
219 LOG("The size is over our limit. To fix this, edit the "
220 "send_data function.");
224 // check that our socket is usable.
226 LOG("One cannot send data on an uninitialized tester!");
230 int runs_completed = 0;
231 // counts up how many times we've done our test cycle.
233 while (runs_completed < count) {
234 #ifdef DEBUG_SPOCKET_TESTER
235 LOG(a_sprintf("iter %d", runs_completed));
237 // we're doing the client side routine here.
238 time_stamp trip_start;
239 #ifdef DEBUG_SPOCKET_TESTER
240 LOG("client about to send");
242 if (!do_a_send(dest, garbage_buffer, size, stats)) {
243 LOG("We failed on a send. Now quitting.");
246 #ifdef DEBUG_SPOCKET_TESTER
247 LOG("client about to rcv");
249 if (!do_a_receive(size, stats)) {
250 LOG("We failed on a receive. Now quitting.");
253 stats.round_trip_time += int(time_stamp().value() - trip_start.value());
255 runs_completed++; // finished a run.
256 stats.total_runs++; // count it in the overall stats too.
257 if ( !(runs_completed % 10) )
258 LOG(a_sprintf("Completed test #%d.", runs_completed));