feisty meow concerns codebase  2.140
test_many_cromp.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_many_cromp *
4 * Author : Chris Koeritz *
5 * *
6 * Purpose: *
7 * *
8 * Sees how a cromp server does with a large number of connections. *
9 * *
10 *******************************************************************************
11 * Copyright (c) 2002-$now By Author. This program is free software; you can *
12 * redistribute it and/or modify it under the terms of the GNU General Public *
13 * License as published by the Free Software Foundation; either version 2 of *
14 * the License or (at your option) any later version. This is online at: *
15 * http://www.fsf.org/copyleft/gpl.html *
16 * Please send any updates to: fred@gruntose.com *
17 \*****************************************************************************/
18 
19 #include "crompish_pax.h"
20 
21 #include <mathematics/chaos.h>
22 #include <basis/astring.h>
23 
27 #include <structures/amorph.h>
28 #include <cromp/cromp_client.h>
29 #include <octopus/entity_defs.h>
30 #include <octopus/infoton.h>
31 #include <loggers/console_logger.h>
33 #include <loggers/file_logger.h>
37 #include <timely/time_control.h>
38 #include <unit_test/unit_base.h>
39 
40 #include <stdio.h>
41 
42 using namespace application;
43 using namespace basis;
44 using namespace configuration;
45 using namespace cromp;
46 using namespace mathematics;
47 using namespace filesystem;
48 using namespace loggers;
49 using namespace octopi;
50 using namespace processes;
51 using namespace sockets;
52 using namespace structures;
53 using namespace textual;
54 using namespace timely;
55 using namespace unit_test;
56 
57 #define DEBUG_TESTER
58  // uncomment for noisier version.
59 
60 const int REPORTING_INTERVAL = 20 * SECOND_ms;
61  // how frequently we tell about bad crompers.
62 
63 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s))
64 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), astring(s))
65 
66 class many_cromp_tester : virtual public unit_base, virtual public application_shell
67 {
68 public:
69  many_cromp_tester();
70  ~many_cromp_tester();
71 
72  virtual int execute();
73 
74  DEFINE_CLASS_NAME("many_cromp_tester");
75 
76 private:
77  amorph<cromp_client> _uplinks; // a list of cromp clients.
78  bool _encryption; // true if we're encrypting.
79  int _count; // number of cromps.
80 };
81 
83 
84 many_cromp_tester::many_cromp_tester()
87  _uplinks(),
88  _encryption(false),
89  _count(1)
90 {
91  FUNCDEF("constructor");
92  LOG("");
93  LOG("");
94 
95  internet_address server_loc;
96 
98 //LOG(a_sprintf("argc is %d and first is %s", application::_global_argc, application::_global_argv[0]));
99 
100  // check for a port on the command line.
101  astring port_text;
102  int port = 5678;
103  if (args.get_value("port", port_text, false)) {
104  LOG(astring("using port: ") + port_text);
105  port = port_text.convert(5678);
106  }
107  server_loc.port = port;
108 
109  astring count_text;
110  if (args.get_value("count", count_text, false)) {
111  LOG(astring("using count: ") + count_text);
112  _count = count_text.convert(_count);
113  }
114 
115 //hmmm: normalize host so this can take either name or IP.
116 
117  int indy = 0;
118  if (args.find("encrypt", indy, false)
119  || (args.find('e', indy, false)) ) {
120  // they're saying that we should encrypt the communication.
121  _encryption = true;
122  }
123 
124  // check for a hostname on the command line.
125  astring hostname("local");
126  astring host_temp;
127  if (args.get_value("host", host_temp, false)) {
128  LOG(astring("using host: ") + host_temp);
129  hostname = host_temp;
130  }
131 LOG(astring("using host: ") + hostname);
132  strcpy(server_loc.hostname, hostname.s());
133 
134 LOG(astring("opening at ") + server_loc.text_form());
135 
136 LOG(a_sprintf("count of %d cromps will be created.", _count));
137 
138  for (int i = 0; i < _count; i++) {
139 LOG(a_sprintf("%d. A", i));
140  cromp_client *uplink = new cromp_client(server_loc);
141 LOG(a_sprintf("%d. B", i));
142  uplink->add_tentacle(new bubbles_tentacle(false));
143 LOG(a_sprintf("%d. C", i));
144  _uplinks.append(uplink);
145  }
146 
147 }
148 
149 many_cromp_tester::~many_cromp_tester()
150 {
151 }
152 
153 int many_cromp_tester::execute()
154 {
155  FUNCDEF("execute");
156 
157  if (_encryption) {
158  for (int i = 0; i < _uplinks.elements(); i++) {
159  _uplinks.borrow(i)->enable_encryption();
160  }
161  }
162 
163  for (int i = 0; i < _uplinks.elements(); i++) {
164  outcome ret = _uplinks.borrow(i)->connect();
165  if (ret != cromp_client::OKAY) {
166  deadly_error(class_name(), func, astring("connection failed with error: ")
167  + cromp_client::outcome_name(ret));
168  }
169  }
170 
171 time_stamp when_to_leave(10 * HOUR_ms);
172 
173  time_stamp next_report(REPORTING_INTERVAL);
174 
175  while (time_stamp() < when_to_leave) {
176  int unconnected = 0;
177  for (int i = 0; i < _uplinks.elements(); i++) {
178  if (!_uplinks.borrow(i)->connected())
179  unconnected++;
180  }
181 
182  if (time_stamp() > next_report) {
183  int connected = _uplinks.elements() - unconnected;
184  LOG(a_sprintf("[ %d connected and %d did not ]", connected, unconnected));
185  next_report.reset(REPORTING_INTERVAL);
186  }
187 
188 //do something with uplinks.
189 
190  time_control::sleep_ms(100);
191  }
192 
193 
194  BASE_LOG("many cromp_client:: works for those functions tested.");
195 
196  return 0;
197 }
198 
200 
201 HOOPLE_MAIN(many_cromp_tester, )
202 
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:760
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
virtual basis::outcome add_tentacle(octopi::tentacle *to_add, bool filter=false)
this type of address describes a destination out on the internet.
basis::astring text_form() const
char hostname[MAXIMUM_HOSTNAME_LENGTH]
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38
#define deadly_error(c, f, i)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
const int SECOND_ms
Number of milliseconds in a second.
Definition: definitions.h:120
const int HOUR_ms
Number of milliseconds in an hour.
Definition: definitions.h:122
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
Provides access to the operating system's socket methods.
Definition: base_address.h:26
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
#include <time.h>
Definition: earth_time.cpp:37
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define LOG(s)
#define BASE_LOG(s)
const int REPORTING_INTERVAL