main.cpp

00001 
00002 /***************************************************************************
00003  *  main.cpp - Fawkes network log view
00004  *
00005  *  Created: Sat Dec 15 01:57:20 2007 (after I5 xmas party)
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <netcomm/fawkes/client.h>
00024 #include <netcomm/fawkes/client_handler.h>
00025 #include <netcomm/fawkes/component_ids.h>
00026 #include <netcomm/utils/network_logger.h>
00027 #include <utils/logging/console.h>
00028 #include <utils/system/signal.h>
00029 #include <utils/system/argparser.h>
00030 
00031 #include <cstring>
00032 #include <cstdio>
00033 #include <cstdlib>
00034 
00035 using namespace fawkes;
00036 
00037 /// @cond INTERNALS
00038 
00039 class NetLogConsolePrinter
00040   : public FawkesNetworkClientHandler,
00041     public SignalHandler
00042 {
00043  public:
00044   NetLogConsolePrinter(const char *hostport)
00045   {
00046     logger = new ConsoleLogger();
00047     quit = false;
00048 
00049     char *hp = strdup(hostport);
00050     const char *hostname = strtok(hp, ":");
00051     const char *portstr = strtok(NULL, "");
00052     int port = 1910;
00053     if ( portstr ) {
00054       port = atoi(portstr);
00055       if ( (port < 0) || ( port > 0xFFFF ) ) {
00056         printf("Invalid port given, must be in range [1:65535]. Using default 1910 instead\n");
00057         port = 1910;
00058       }
00059     }
00060 
00061     client = new FawkesNetworkClient(hostname, port);
00062     client->connect();
00063     client->register_handler(this, FAWKES_CID_NETWORKLOGGER);
00064 
00065     client->enqueue(new FawkesNetworkMessage(FAWKES_CID_NETWORKLOGGER,
00066                                              NetworkLogger::MSGTYPE_SUBSCRIBE));
00067   }
00068 
00069   ~NetLogConsolePrinter()
00070   {
00071     delete logger;
00072     delete client;
00073   }
00074 
00075   void handle_signal(int signal)
00076   {
00077     quit = true;
00078     client->wake(FAWKES_CID_NETWORKLOGGER);
00079   }
00080 
00081   virtual void inbound_received(FawkesNetworkMessage *m,
00082                                 unsigned int id) throw()
00083   {
00084     if ( (m->cid() == FAWKES_CID_NETWORKLOGGER) &&
00085          (m->msgid() == NetworkLogger::MSGTYPE_LOGMESSAGE) ) {
00086       NetworkLoggerMessageContent *content = m->msgc<NetworkLoggerMessageContent>();
00087       struct timeval t = content->get_time();
00088       /* Yes, it is risky to just use get_message() as format, but for now we are happy
00089        * and do not expect bad guys. To be fixed. */
00090       logger->tlog(content->get_loglevel(), &t, content->get_component(), content->get_message());
00091     }
00092   }
00093 
00094   virtual void deregistered(unsigned int id) throw()
00095   {
00096     quit = true;
00097   }
00098 
00099 
00100   virtual void connection_died(unsigned int id) throw()
00101   {
00102     printf("Connection to host died. Aborting.\n");
00103     quit = true;
00104   }
00105 
00106 
00107   virtual void connection_established(unsigned int id) throw()
00108   {
00109   }
00110 
00111 
00112   void run()
00113   {
00114     while ( ! quit ) {
00115       client->wait(FAWKES_CID_NETWORKLOGGER);
00116     }
00117     client->disconnect();
00118   }
00119 
00120 
00121  private:
00122   FawkesNetworkClient *client;
00123   ConsoleLogger *logger;
00124   bool quit;
00125 };
00126 /// @endcond
00127 
00128 
00129 void
00130 print_usage(const char *program_name)
00131 {
00132   printf("Usage: %s [hostname[:port]]\n", program_name);
00133 }
00134 
00135 int
00136 main(int argc, char **argv)
00137 {
00138   ArgumentParser argp(argc, argv, "h");
00139 
00140   if ( argp.has_arg("h") ) {
00141     print_usage(argv[0]);
00142     exit(0);
00143   }
00144 
00145   const char *hostport = (argp.num_items() > 0) ? argp.items()[0] : "localhost:1910";
00146   NetLogConsolePrinter printer(hostport);
00147 
00148   SignalManager::register_handler(SIGINT, &printer);
00149   printer.run();
00150 
00151 }
00152