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 application;
35 using namespace basis;
36 //using namespace filesystem;
37 using namespace loggers;
38 using namespace mathematics;
39 using namespace sockets;
40 using namespace structures;
41 //using namespace textual;
42 using namespace timely;
43 using namespace unit_test;
45 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
47 const int MAXIMUM_WINSOCK_MTU = 1500;
48 // the largest chunk of bytes we can receive at once.
50 const int MAXIMUM_TRANSFER_WAIT = 40 * SECOND_ms;
51 // the longest amount of time we wait in trying to receive data.
53 static abyte receive_buffer[MAXIMUM_WINSOCK_MTU + 1];
54 // used for dumping received data into.
56 //#define DEBUG_SPOCKET_TESTER
57 // uncomment for noisy version.
59 broadcast_spocket_tester::broadcast_spocket_tester
60 (const internet_address &where, bool unicast)
61 : _where(new internet_address(where)),
62 _stack(new tcpip_stack),
69 broadcast_spocket_tester::~broadcast_spocket_tester()
77 bool broadcast_spocket_tester::connect()
79 spocket::sock_types type = spocket::BROADCAST;
80 if (_ucast) type = spocket::UNICAST;
82 _socket = new spocket(*_where, type);
84 outcome ret = _socket->connect();
85 return ret == spocket::OKAY;
88 bool broadcast_spocket_tester::do_a_send(const internet_address &where_to,
89 abyte *buffer, int size, testing_statistics &stats)
91 time_stamp start_time;
93 time_stamp when_to_leave(MAXIMUM_TRANSFER_WAIT);
96 #ifdef DEBUG_SPOCKET_TESTER
97 LOG(a_sprintf("into do a send with %d bytes", size));
100 while (time_stamp() < when_to_leave) {
101 worked = _socket->send_to(where_to, buffer, size, len_sent);
102 if (worked == spocket::NONE_READY) {
103 time_control::sleep_ms(20);
105 } else if (worked == spocket::PARTIAL) {
106 //danger danger if we get wrong info.
109 time_control::sleep_ms(20);
113 #ifdef DEBUG_SPOCKET_TESTER
114 LOG("got out of loop");
117 stats.send_time += int(time_stamp().value() - start_time.value());
119 if ( (worked != spocket::OKAY) || !len_sent) {
120 LOG("No data went out on the socket.");
123 if (len_sent != size) {
124 LOG(a_sprintf("The full chunk didn't get sent out: %d bytes instead of %d",
126 //more bad news. what do we do about getting the rest out?
129 stats.bytes_sent += len_sent;
132 // int time_taken = int(end_time.value() - start_time.value());
137 bool broadcast_spocket_tester::do_a_receive(int size_expected,
138 testing_statistics &stats)
140 time_stamp start_time;
142 #ifdef DEBUG_SPOCKET_TESTER
143 LOG("into do a rcv");
146 time_stamp when_to_leave(MAXIMUM_TRANSFER_WAIT);
148 while ( (full_length < size_expected) && (time_stamp() < when_to_leave) ) {
149 time_stamp start_of_receive;
150 int len = MAXIMUM_WINSOCK_MTU;
151 internet_address where_from;
152 outcome ret = _socket->receive_from(receive_buffer, len, where_from);
153 ///LOG(astring("recvfrom outcome is ") + spocket::outcome_name(ret));
154 if (ret != spocket::OKAY) {
155 if (ret == spocket::NONE_READY) {
156 time_control::sleep_ms(20);
160 // reset our time if we've gotten good data.
161 if (ret == spocket::OKAY)
162 when_to_leave.reset(MAXIMUM_TRANSFER_WAIT);
164 int receive_duration = int(time_stamp().value()
165 - start_of_receive.value());
166 stats.receive_time += receive_duration;
168 #ifdef DEBUG_SPOCKET_TESTER
169 LOG(a_sprintf("did recv, len=%d", len));
173 LOG("Our socket has been disconnected.");
175 } else if (len < 0) {
176 if (errno == SOCK_EWOULDBLOCK) continue; // no data.
177 LOG(astring("The receive failed with an error ")
178 + critical_events::system_error_text(errno));
182 stats.bytes_received += len;
185 if (full_length != size_expected)
186 LOG(a_sprintf("Did not get the full size expected (wanted %d and "
187 "got %d bytes).", size_expected, full_length));
192 bool broadcast_spocket_tester::perform_test(const internet_address &dest,
193 int size, int count, testing_statistics &stats)
195 #ifdef DEBUG_SPOCKET_TESTER
196 LOG("into perf test");
199 // the statics are used to generate our random buffer for sending.
200 static abyte garbage_buffer[MAXIMUM_WINSOCK_MTU + 1];
201 static bool garbage_initialized = false;
204 // if our static buffer full of random stuff was never initialized, we do
205 // so now. this supports efficiently re-using the tester if desired.
206 if (!garbage_initialized) {
207 // note the less than or equal; we know we have one more byte to fill.
208 for (int i = 0; i <= MAXIMUM_WINSOCK_MTU; i++)
209 garbage_buffer[i] = randomizer.inclusive(0, 255);
210 garbage_initialized = true;
213 // reset the statistical package.
214 stats.total_runs = 0;
216 stats.receive_time = 0;
217 stats.bytes_sent = 0;
218 stats.bytes_received = 0;
220 // check that they aren't trying to do too big of a send.
221 if (size > MAXIMUM_WINSOCK_MTU) {
222 LOG("The size is over our limit. To fix this, edit the "
223 "send_data function.");
227 // check that our socket is usable.
229 LOG("One cannot send data on an uninitialized tester!");
233 int runs_completed = 0;
234 // counts up how many times we've done our test cycle.
236 while (runs_completed < count) {
237 #ifdef DEBUG_SPOCKET_TESTER
238 LOG(a_sprintf("iter %d", runs_completed));
240 // we're doing the client side routine here.
241 time_stamp trip_start;
242 #ifdef DEBUG_SPOCKET_TESTER
243 LOG("client about to send");
245 if (!do_a_send(dest, garbage_buffer, size, stats)) {
246 LOG("We failed on a send. Now quitting.");
249 #ifdef DEBUG_SPOCKET_TESTER
250 LOG("client about to rcv");
252 if (!do_a_receive(size, stats)) {
253 LOG("We failed on a receive. Now quitting.");
256 stats.round_trip_time += int(time_stamp().value() - trip_start.value());
258 runs_completed++; // finished a run.
259 stats.total_runs++; // count it in the overall stats too.
260 if ( !(runs_completed % 10) )
261 LOG(a_sprintf("Completed test #%d.", runs_completed));