Fixed several rather large memory leaks in the monitor.
[aesalon.git] / monitor / src / Initializer.cpp
blobf02f7f58e526d7920a7713088a8696740a2bc3c2
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <unistd.h>
5 #include "misc/StreamAsString.h"
6 #include "misc/String.h"
7 #include "misc/Message.h"
9 #include "Initializer.h"
11 #ifndef DEFAULT_PORT
12 #define DEFAULT_PORT 6321
13 #endif
15 #ifndef LIBC_PATH
16 #define LIBC_PATH "/lib/libc.so.6"
17 #endif
19 template<> Initializer *Misc::Singleton<Initializer>::instance = 0;
21 Initializer::Initializer(char *argv[]) : Misc::Singleton<Initializer>(), argv(argv) {
22 initialize();
25 Initializer::~Initializer() {
26 deinitialize();
29 void Initializer::initialize() {
30 /* set these to NULL so that the usage() function doesn't cause errors . . . */
31 program_manager = NULL;
32 server_socket = NULL;
33 event_queue = NULL;
34 return_value = 0;
35 storage_manager = NULL;
37 config_parser = new Misc::ConfigParser();
39 argument_parser = new Misc::ArgumentParser(argv);
41 argument_parser->add_argument(new Misc::Argument("help", 'h', Misc::Argument::NO_ARGUMENT, ""));
42 std::string port = config_parser->get_config_item("tcp-port");
43 if(port == "") port = Misc::StreamAsString() << DEFAULT_PORT;
44 argument_parser->add_argument(new Misc::Argument("tcp-port", 'p', Misc::Argument::REQUIRED_ARGUMENT, port));
45 std::string wait = config_parser->get_config_item("wait");
46 if(wait == "") wait = "1";
47 argument_parser->add_argument(new Misc::Argument("wait", 'w', Misc::Argument::OPTIONAL_ARGUMENT, wait));
48 std::string libc_path = config_parser->get_config_item("libc-path");
49 if(libc_path == "") libc_path = LIBC_PATH;
50 argument_parser->add_argument(new Misc::Argument("libc-path", 0, Misc::Argument::REQUIRED_ARGUMENT, libc_path));
51 std::string overload_path = config_parser->get_config_item("overload-path");
52 argument_parser->add_argument(new Misc::Argument("overload-path", 'o', Misc::Argument::REQUIRED_ARGUMENT, overload_path));
54 argument_parser->parse();
56 if(argument_parser->get_argument("help")->is_found()) {
57 usage();
58 return;
61 if(argument_parser->get_argument("overload-path")->get_data() == "")
62 throw Exception::BasicException("No overload-path specified.");
64 storage_manager = new StorageManager();
66 if(argument_parser->get_postargs()) {
67 int port;
68 Misc::String::to<int>(argument_parser->get_argument("tcp-port")->get_data(), port);
69 server_socket = new TCP::ServerSocket(port);
71 Misc::ArgumentList *al = new Misc::ArgumentList();
72 for(std::size_t x = 0; x < argument_parser->get_postargs(); x ++) {
73 al->add_argument(argument_parser->get_postarg(x));
75 program_manager = new ProgramManager(al);
77 else {
78 usage();
79 return;
82 event_queue = new Event::Queue();
84 if(argument_parser->get_argument("wait")->is_found()) {
85 int number;
86 Misc::String::to<int>(
87 argument_parser->get_argument("wait")->get_data(), number);
89 Misc::Message(Misc::Message::DEBUG_MESSAGE, Misc::StreamAsString() << "Waiting for " << number << " TCP connection(s) . . .");
90 for(int x = 0; x < number; x ++) {
91 server_socket->wait_for_connection();
95 run();
97 server_socket->disconnect_all();
100 void Initializer::deinitialize() {
101 if(config_parser) delete config_parser;
102 if(argument_parser) delete argument_parser;
103 if(program_manager) delete program_manager;
104 if(server_socket) delete server_socket;
105 if(storage_manager) delete storage_manager;
106 if(event_queue) delete event_queue;
109 void Initializer::usage() {
110 std::cout << "aesalon program monitor, version " << AESALON_MAJOR_VERSION << "." << AESALON_MINOR_VERSION << "." << AESALON_PATCHLEVEL;
111 std::cout << ", copyright (C) 2009-2010 strange <kawk256@gmail.com>" << std::endl;
112 std::cout << "usage: " << argv[0] << " [arguments] executable [executable arguments]" << std::endl;
113 std::cout << "\t--help, -h\t\tPrint this usage message." << std::endl;
114 std::cout << "\t--tcp-port, -p\t\tSet the port to listen on for connections. Currently is " << argument_parser->get_argument("tcp-port")->get_data() << "." << std::endl;
115 std::cout << "\t--wait, -w\t\tNumber of TCP connections to accept before executing. Defaults to 0." << std::endl;
116 std::cout << "\t--libc-path\t\tThe path to the current version of libc being used. Currently is " << LIBC_PATH << "." << std::endl;
117 std::cout << "\t--overload-path\t\tThe path to the aesalon overload library. Currently is " << argument_parser->get_argument("overload-path")->get_data() << "." << std::endl;
118 std::cout << "\t--\t\t\tOptional, denotes the end of the argument list." << std::endl;
121 void Initializer::run() {
122 program_manager->execute();
123 while(program_manager->is_running()) {
124 program_manager->wait();
125 if(event_queue->peek_event()) {
126 get_socket()->send_data(event_queue);
128 /*server_socket->accept_connections();*/