rcxmain.cpp

00001
00002 /***************************************************************************
00003  *  rcxmain.cpp - Main application for Fountain in RCSoftX
00004  *
00005  *  Created: Fri May 20 14:25:28 2005
00006  *  Copyright  2005-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 /// @cond RCSoftX
00024 
00025 #include <apps/fountain/config.h>
00026 #include <fvutils/net/fuse_server.h>
00027
00028 #include <utils/system/argparser.h>
00029 #include <utils/system/console_colors.h>
00030 #include <utils/system/signal.h>
00031 #include <utils/config_reader/config_reader.h>
00032 #include <utils/logging/console.h>
00033
00034 #ifdef HAVE_AVAHI
00035 #include <netcomm/dns-sd/avahi_thread.h>
00036 #include <netcomm/service_discovery/service.h>
00037 #include <netcomm/utils/resolver.h>
00038 #endif
00039 
00040 #include <iostream>
00041 #include <cstdlib>
00042
00043 using namespace std;
00044 using namespace fawkes;
00045
00046 class FountainRCSoftX : public SignalHandler
00047 {
00048  public:
00049   FountainRCSoftX(int argc, char **argv)
00050   {
00051     __argp   = new ArgumentParser(argc, argv, "hx:");
00052     __logger = new ConsoleLogger();
00053
00054 #ifdef HAVE_AVAHI
00055     __avahi_thread          = new AvahiThread();
00056     __avahi_thread->start();
00057     __nnresolver = new NetworkNameResolver(__avahi_thread);
00058 #endif
00059 
00060     __cr = NULL;
00061     __config = NULL;
00062     __fuse_server = NULL;
00063   }
00064
00065   ~FountainRCSoftX()
00066   {
00067 #ifdef HAVE_AVAHI
00068     __avahi_thread->cancel();
00069     __avahi_thread->join();
00070     delete __nnresolver;
00071     delete __avahi_thread;
00072 #endif
00073 
00074     delete __argp;
00075     delete __logger;
00076     delete __cr;
00077     delete __config;
00078     delete __fuse_server;
00079   }
00080
00081   virtual void handle_signal(int signal)
00082   {
00083     if ( __fuse_server ) {
00084       __fuse_server->cancel();
00085     }
00086   }
00087
00088   void
00089   print_usage()
00090   {
00091     printf("Usage: %s [-x config] [-h]\n"
00092            "  -h             Show this help message\n"
00093            "  -x config      Load given RCSoftX config file\n",
00094            __argp->program_name());
00095   }
00096
00097   void run()
00098   {
00099     if ( __argp->has_arg("h") ) {
00100       print_usage();
00101       return;
00102     }
00103
00104     __cr = new CConfigReader();
00105
00106     const char *config_filename;
00107     if ( (config_filename = __argp->arg("x")) == NULL ) {
00108       config_filename = "../cfg/config.xml";
00109       cout << cblue << "FireVision Fountain" << cnormal
00110            << ": No config file given, using default ("
00111            << config_filename << ")" << endl;
00112     }
00113     __cr->LoadConfFile( config_filename );
00114     __config = new RCSoftXFountainConfig( __cr );
00115
00116     __fuse_server = new FuseServer(__config->FountainPort);
00117     __fuse_server->start();
00118
00119 #ifdef HAVE_AVAHI
00120     char *fountain_service_name;
00121     asprintf(&fountain_service_name, "Fountain on %s", __nnresolver->short_hostname());
00122     NetworkService *fountain_service = new NetworkService(fountain_service_name,
00123                                                           "_fountain._tcp",
00124                                                           __config->FountainPort);
00125     free(fountain_service_name);
00126     __avahi_thread->publish_service(fountain_service);
00127 #endif
00128 
00129     __fuse_server->join();
00130   }
00131
00132
00133  private:
00134   ArgumentParser        *__argp;
00135   ConsoleLogger         *__logger;
00136   CConfigReader         *__cr;
00137   RCSoftXFountainConfig *__config;
00138   FuseServer            *__fuse_server;
00139 #ifdef HAVE_AVAHI
00140   AvahiThread           *__avahi_thread;
00141   NetworkNameResolver   *__nnresolver;
00142 #endif
00143 };
00144
00145
00146 int
00147 main( int argc, char **argv )
00148 {
00149   FountainRCSoftX f(argc, argv);
00150   SignalManager::register_handler(SIGINT, &f);
00151   f.run();
00152   SignalManager::finalize();
00153 }
00154 
00155 /// @endcond