2 * Name : test_ucast_spocket
3 * Author : Chris Koeritz
4 * Purpose: This is the "main" program for our sockets tester. It parses command
5 * line parameters and starts up the tester class.
7 * Copyright (c) 2001-$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 *
15 #include "bcast_spocketer.h"
17 #include <application/hoople_main.h>
18 #include <basis/byte_array.h>
19 #include <basis/astring.h>
20 #include <loggers/program_wide_logger.h>
21 #include <processes/launch_process.h>
22 #include <structures/static_memory_gremlin.h>
23 #include <sockets/internet_address.h>
24 #include <timely/time_control.h>
25 #include <unit_test/unit_base.h>
30 using namespace application;
31 using namespace basis;
32 using namespace loggers;
33 using namespace mathematics;
34 using namespace processes;
35 using namespace sockets;
36 using namespace structures;
37 using namespace textual;
38 using namespace timely;
39 using namespace unit_test;
41 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
43 const int INITIAL_DELAY = 1; // number of seconds before starting sends.
45 typedef abyte ip_address_holder[4];
47 class test_ucast_spocket : public virtual application_shell, public virtual unit_base
50 test_ucast_spocket() {}
51 DEFINE_CLASS_NAME("test_ucast_spocket");
52 virtual int execute();
54 bool parse_address(const astring &input, ip_address_holder &ip_address);
57 //hmmm: extract this to function in sockets.
58 bool test_ucast_spocket::parse_address(const astring &input, ip_address_holder &ip_address)
60 int index = 0; // current storage position in our array.
62 const char *last_period = 0; // helps check for non-empty numbers.
63 bool got_digit = false;
64 for (const char *address = input.s(); *address; address++) {
65 if ( (*address <= '9') && (*address >= '0') ) {
66 current_byte *= 10; // shift over.
67 current_byte += *address - '0'; // add in current character.
69 } else if (*address == '.') {
71 if (last_period + 1 == address) {
72 LOG("The IP address entry has an empty digit. Exiting.");
75 last_period = address; // set our last period location.
76 if (current_byte > 255) {
77 LOG("The IP address entry has an illegal abyte. Exiting.");
80 ip_address[index] = abyte(current_byte); // store current byte.
81 current_byte = 0; // reset.
82 index++; // next place in array.
84 // stop if there are too many periods, but keep accumulated address.
86 LOG("The IP address entry has illegal characters. Exiting.");
90 // catch the case where we ran out of chars before adding the last byte.
91 if ( (index == 3) && got_digit) {
92 if (current_byte > 255) {
93 LOG("The IP address entry has an illegal abyte. Exiting.");
96 ip_address[index] = current_byte;
97 } else if (index < 4) {
98 LOG("The IP address entry is too short. Exiting.");
104 int test_ucast_spocket::execute()
106 //this preamble with command line parsing is identical to bcast version,
107 //and will be very close to regular spocket version.
108 //get it extracted to a helper class for use by all three.
110 ip_address_holder ip_address; // accumulates the source address.
111 ip_address_holder dest_addr; // where to send stuff to.
117 const char *DEFAULT_HOST = "127.0.0.1";
118 const int DEFAULT_PORT = 12342;
119 const int DEFAULT_SEND_SIZE = 1008;
120 const int DEFAULT_SEND_COUNT = 10;
122 if (_global_argc < 7) {
123 if (_global_argc > 1) {
125 This program takes six command line arguments to begin operation.\n\
126 These arguments (in order) are:\n\
127 \tIP address for src\tIn the form w.x.y.z\n\
128 \tIP address for dest\tIn the form w.x.y.z\n\
129 \tReceive Port number\tAs a short integer\n\
130 \tSending Port number\tAs a short integer\n\
131 \tSend size\t\tThe size of the data to exchange.\n\
132 \tSend count\t\tThe number of \"packets\" to exchange.\n\
133 Note: it is expected that the testers have equal send sizes; this\n\
134 allows the receiver to know when it's gotten all the data that's\n\
135 expected during a cycle.");
136 return 1; // bail if they provided anything; otherwise we test.
138 parse_address(DEFAULT_HOST, ip_address);
139 parse_address(DEFAULT_HOST, dest_addr);
140 rcv_port = DEFAULT_PORT;
141 send_port = DEFAULT_PORT + 1;
142 send_size = DEFAULT_SEND_SIZE;
143 send_count = DEFAULT_SEND_COUNT;
147 // only parse the parameters if we got enough from the user; otherwise we accept our
148 // defaults to do a simple test run.
149 if (_global_argc >= 7) {
151 if (!parse_address(_global_argv[1], ip_address)) {
152 LOG("failed to parse source address.");
156 LOG(a_sprintf("\tParsed a source of: \"%d.%d.%d.%d\".",
157 (int)ip_address[0], (int)ip_address[1], (int)ip_address[2],
158 (int)ip_address[3]));
160 if (!parse_address(_global_argv[2], dest_addr)) {
161 LOG("failed to parse dest address.");
165 LOG(a_sprintf("\tParsed a destination of: \"%d.%d.%d.%d\".",
166 (int)dest_addr[0], (int)dest_addr[1], (int)dest_addr[2],
169 // parse the third parameter: the port.
170 if (sscanf(_global_argv[3], "%d", &rcv_port) < 1) {
171 LOG("The port entry is malformed. Exiting.");
174 LOG(a_sprintf("\tGot a receive port of %d.", rcv_port));
176 // parse the fourth parameter: the port.
177 if (sscanf(_global_argv[4], "%d", &send_port) < 1) {
178 LOG("The port entry is malformed. Exiting.");
181 LOG(a_sprintf("\tGot a send port of %d.", send_port));
183 // parse the fifth parameter: the size of the sends.
184 if (sscanf(_global_argv[5], "%d", &send_size) < 1) {
185 LOG("The send size entry is malformed. Exiting.");
188 LOG(a_sprintf("\tGot a send size of %d.", send_size));
190 // parse the sixth parameter: the number of sends.
191 if (sscanf(_global_argv[6], "%d", &send_count) < 1) {
192 LOG("The send count entry is malformed. Exiting.");
195 LOG(a_sprintf("\tGot a send count of %d.", send_count));
198 if (_global_argc == 1) {
199 // launch a paired duplicate of our test so we can chat.
200 launch_process zingit;
202 un_int result = zingit.run(_global_argv[0],
203 astring(DEFAULT_HOST) + " " + DEFAULT_HOST + " "
204 /* we have reversed the send and receive ports. */
205 + a_sprintf("%d", DEFAULT_PORT + 1) + " " + a_sprintf("%d", DEFAULT_PORT)
206 + " " + a_sprintf("%d", DEFAULT_SEND_SIZE) + " " + a_sprintf("%d", DEFAULT_SEND_COUNT),
207 launch_process::RETURN_IMMEDIATELY, kidnum);
208 ASSERT_EQUAL(result, 0, "launching paired process should start successfully");
211 // package our parameters in a form the tester likes.
212 internet_address to_pass(byte_array(4, ip_address), "", rcv_port);
213 internet_address dest(byte_array(4, dest_addr), "", send_port);
215 // now, construct our tester object.
216 broadcast_spocket_tester tester(to_pass, true);
218 // choose the appropriate action based on our role.
219 bool outcome = tester.connect();
221 LOG(astring("Failed to connect on the tester."));
225 LOG(a_sprintf("you now have %d seconds; get other side ready.",
227 time_control::sleep_ms(INITIAL_DELAY * SECOND_ms);
228 LOG("starting test");
230 // so, we're connected. try sending the test packages out.
231 testing_statistics stats; // to be filled by the tester.
232 outcome = tester.perform_test(dest, send_size, send_count * 2, stats);
233 // multiply send_count since we count each side as one.
235 LOG("Failed out of send_data; maybe other side terminated.");
238 stats.total_runs /= 2; // cut down to the real number of tests.
240 if (!stats.total_runs)
241 stats.total_runs = 1;
243 // now report on the stats that we get from the data sending.
244 LOG(a_sprintf("Report for %d completed test cycles.", stats.total_runs));
246 LOG("\t\tsend stats\t\treceive stats");
247 LOG("\t\t----------\t\t-------------");
248 LOG(a_sprintf("bytes\t\t%d\t\t\t%d", stats.bytes_sent,
249 stats.bytes_received));
250 LOG(a_sprintf("time\t\t%d\t\t\t%d", stats.send_time, stats.receive_time));
251 LOG(a_sprintf("avg. bytes\t%d\t\t\t%d", stats.bytes_sent
252 / stats.total_runs / 2, stats.bytes_received / stats.total_runs / 2));
254 LOG(a_sprintf("round trip time: %d ms", stats.round_trip_time));
255 //hmmm: use the bandwidth measurer object!!!
256 double bandwidth = double(stats.bytes_sent + stats.bytes_received)
257 / stats.round_trip_time / 1024.0 * 1000.0;
258 LOG(a_sprintf("bandwidth overall: %f K/s", bandwidth));
260 if (_global_argc == 1) return final_report();
261 else return 0; // no unit test report for non-top-level process
264 HOOPLE_MAIN(test_ucast_spocket, );