Began removal of platform/. The Monitor:: namespace is completely converted.
[aesalon.git] / src / monitor / Initializer.cpp
blobd6616495b571b8a4f56f272b5f7b3bbf7e66a9b1
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <unistd.h>
5 #include "misc/ArgumentParser.h"
6 #include "misc/ReferenceCounter.h"
7 #include "misc/StreamAsString.h"
8 #include "misc/String.h"
9 #include "Message.h"
11 #include "Initializer.h"
13 #ifndef DEFAULT_PORT
14 #define DEFAULT_PORT 6321
15 #endif
17 #ifndef LIBC_PATH
18 #define LIBC_PATH "/lib/libc.so.6"
19 #endif
21 namespace Aesalon {
22 namespace Monitor {
24 template<> Initializer *Misc::Singleton<Initializer>::instance = 0;
26 Initializer::Initializer(char *argv[]) : Misc::Singleton<Initializer>(), argv(argv) {
27 initialize();
30 Initializer::~Initializer() {
31 deinitialize();
34 void Initializer::initialize() {
35 new Misc::ReferenceCounter();
36 Misc::ArgumentParser *ap = new Misc::ArgumentParser();
38 ap->add_argument("usage", new Misc::BooleanArgument("--usage", 'h', "", 0, false));
39 ap->add_argument("logfile", new Misc::StringArgument("--log-file", 'l', ""));
40 ap->add_argument("tcp port", new Misc::StringArgument("--use-port", 0, Misc::StreamAsString() << DEFAULT_PORT));
41 ap->add_argument("wait", new Misc::BooleanArgument("--wait", 'w', "", 0, false));
42 ap->add_argument("wait for", new Misc::StringArgument("--wait-for", 0, "1"));
43 ap->add_argument("libc path", new Misc::StringArgument("--libc-path", 0, LIBC_PATH));
45 ap->parse_argv(argv);
47 if(ap->get_argument("usage").to<Misc::BooleanArgument>()->get_status()) {
48 usage();
49 return;
52 if(ap->get_files()) {
53 int port;
54 Misc::String::to<int>(ap->get_argument("tcp port").to<Misc::StringArgument>()->get_value(), port);
55 server_socket = new TCPServerSocket(port);
57 Misc::SmartPointer<Misc::ArgumentList> al = new Misc::ArgumentList();
58 /*al->add_argument(ap->get_file(0)->get_filename());*/
59 for(std::size_t x = 0; x < ap->get_files(); x ++) {
60 al->add_argument(ap->get_file(x)->get_filename());
62 program_manager = new ProgramManager(al);
64 else {
65 usage();
66 return;
69 event_queue = new Misc::EventQueue();
71 /*symbol_manager = new Platform::SymbolManager();*/
73 /*symbol_manager->parse_from_executable(ap->get_file(0)->get_filename());*/
75 if(ap->get_argument("wait").to<Misc::BooleanArgument>()->get_status()) {
76 int number;
77 Misc::String::to<int>(
78 ap->get_argument("wait for").to<Misc::StringArgument>()->get_value(), number);
80 Message(Message::DEBUG_MESSAGE, Misc::StreamAsString() << "Waiting for " << number << " TCP connection(s) . . .");
81 for(int x = 0; x < number; x ++) {
82 server_socket->wait_for_connection();
86 run();
88 server_socket->disconnect_all();
91 void Initializer::deinitialize() {
92 if(program_manager) delete program_manager;
93 /*if(symbol_manager) delete symbol_manager;*/
94 if(server_socket) delete server_socket;
95 if(event_queue) delete event_queue;
97 Misc::ArgumentParser::lock_mutex();
98 delete Misc::ArgumentParser::get_instance();
99 Misc::ReferenceCounter::lock_mutex();
100 delete Misc::ReferenceCounter::get_instance();
103 void Initializer::usage() {
104 std::cout << "aesalon program monitor, version " << AESALON_MAJOR_VERSION << "." << AESALON_MINOR_VERSION << "." << AESALON_PATCHLEVEL;
105 std::cout << ", copyright (C) 2009-2010 strange <kawk256@gmail.com>" << std::endl;
106 std::cout << "usage: " << argv[0] << " [arguments] executable [executable arguments]" << std::endl;
107 std::cout << "\t--usage, -h\t\tPrint this usage message." << std::endl;
108 std::cout << "\t--log-file, -l\t\tSet the file to log memory events to, for future reconstruction." << std::endl;
109 std::cout << "\t--use-port\t\tSet the port to listen on for connections. Defaults to " << DEFAULT_PORT << "." << std::endl;
110 std::cout << "\t--wait, -w\t\tWait for a TCP connection before executing. Defaults to false." << std::endl;
111 std::cout << "\t--libc-path\t\tThe path to the current version of libc being used. Defaults to " << LIBC_PATH << "." << std::endl;
114 void Initializer::run() {
115 program_manager->execute();
116 while(program_manager->is_running()) {
117 program_manager->wait();
118 if(event_queue->peek_event().is_valid()) {
119 std::cout << "Sending data from event queue . . ." << std::endl;
120 get_socket()->send_data(event_queue);
122 server_socket->accept_connections();
126 } // namespace Monitor
127 } // namespace Aesalon