main.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00031 #include "botkernel.h"
00032 #include "cppthread.h"
00033 
00034 vector<string> listConfFiles(string);
00035 void launchThreads(vector<string>,vector<CPPThread*>*,vector<BotKernel*>*);
00036 void * launchBot(void *);
00037 void displayHelp(string,bool);
00038 
00039 int main(int nbArgs, char *arrayArgs[])  { 
00040    srand((unsigned)time(NULL));
00041    char * pHOME = NULL;
00042    pid_t pid;
00043    string confDir = "" ;
00044         unsigned char optchar;
00045    int opt;
00046         bool background=false;
00047    vector<CPPThread*> threads;
00048    vector<BotKernel*> bots;
00049         opterr = 0 ;    
00050    pHOME = getenv("HOME");
00051    if ( pHOME != NULL ) {
00052       confDir = (string)pHOME+"/.trustyrc/";
00053    }
00054         while ( (opt = getopt(nbArgs,arrayArgs,"-bc:")) != EOF ) {
00055       optchar = opt;
00056                 switch (optchar) {
00057                         case 'c' : confDir = optarg;
00058                         break;
00059                         case 'b' : background = true;
00060                         break;
00061                         case 1 : cout << "Invalid argument : "<<  optarg  << endl;
00062                   displayHelp(arrayArgs[0],true);
00063                         break;
00064                         default: 
00065                         case '?' : cout << "Invalid option : "<< (char) optopt << endl;
00066                     displayHelp(arrayArgs[0],true);
00067                         break;
00068                 }       
00069         }
00070         if (background) {
00071       pid =  fork();
00072       switch (pid) {
00073          case -1 : cout << "Fork ERROR !, exiting" << endl;
00074          exit(-1);
00075          break;
00076          case 0 : // son
00077             setsid();
00078             launchThreads(listConfFiles(confDir),&threads,&bots);
00079          break;
00080          default :  // father 
00081             exit(0);
00082       }
00083    }
00084    else {
00085       launchThreads(listConfFiles(confDir),&threads,&bots);
00086    }
00087    // Wait for threads and delete them
00088    for(unsigned int i = 0 ; i < threads.size() ; i ++) {
00089       threads[i]->join();
00090       delete(threads[i]);
00091       delete(bots[i]);
00092    }
00093    return 0;
00094 }
00095 
00096 vector<string> listConfFiles(string confDir) {
00097    DIR *dp;
00098    struct dirent *ep;
00099    dp = opendir (confDir.c_str());
00100    vector<string> files;
00101    string::size_type pos ;
00102    if (dp != NULL) {
00103       while ((ep = readdir(dp))) {
00104          pos = ((string)ep->d_name).find(".") ;
00105          if ( (pos != string::npos) && (((string)ep->d_name).substr(pos)==".conf") ) {
00106             files.push_back(confDir+"/"+ep->d_name);
00107          }
00108       }
00109       closedir (dp);
00110    }
00111    else {
00112       cerr << "Couldn't open configuration directory. Exiting ..." << endl;
00113       exit(0);
00114    }
00115    return files;
00116 }
00117 
00118 void launchThreads(vector<string> confFiles,vector<CPPThread*>* threads,vector<BotKernel*>* bots) {
00119    for(unsigned int i = 0 ; i < confFiles.size() ; i ++ ) {
00120       threads->push_back(new CPPThread());
00121       bots->push_back(new BotKernel(confFiles[i]));
00122       threads->at(i)->exec(launchBot,bots->at(i));
00123    }
00124 }
00125 
00126 void * launchBot(void * arg) {
00127    ((BotKernel*)arg)->run();
00128    return NULL;
00129 }
00130 
00131 void displayHelp(string firstArg,bool quit) {
00132    cout << "Usage : "<< firstArg << " [-c configuration_dir] [-b]" << endl;
00133    if ( quit ) {
00134       exit(0);
00135    }
00136 }

Generated on Sun Aug 16 15:28:27 2009 for trustyRC by  doxygen 1.5.8