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 <unistd.h>
00032 #include "botkernel.h"
00033 #include "pthread.h"
00034 
00035 vector<string> listConfFiles(string);
00036 void launchThreads(vector<string>,vector<PThread*>*,vector<BotKernel*>*);
00037 void * launchBot(void *);
00038 void displayHelp(string,bool);
00039 
00040 int main(int nbArgs, char *arrayArgs[])  { 
00041    srand((unsigned)time(NULL));
00042    char * pHOME = NULL;
00043    pid_t pid;
00044    string confDir = "" ;
00045         unsigned char optchar;
00046    int opt;
00047         bool background=false;
00048    vector<PThread*> threads;
00049    vector<BotKernel*> bots;
00050         opterr = 0 ;    
00051    pHOME = getenv("HOME");
00052    if ( pHOME != NULL ) {
00053       confDir = (string)pHOME+"/.trustyrc/";
00054    }
00055         while ( (opt = getopt(nbArgs,arrayArgs,"-bc:")) != EOF ) {
00056       optchar = opt;
00057                 switch (optchar) {
00058                         case 'c' : confDir = optarg;
00059                         break;
00060                         case 'b' : background = true;
00061                         break;
00062                         case 1 : cout << "Invalid argument : "<<  optarg  << endl;
00063                   displayHelp(arrayArgs[0],true);
00064                         break;
00065                         default: 
00066                         case '?' : cout << "Invalid option : "<< (char) optopt << endl;
00067                     displayHelp(arrayArgs[0],true);
00068                         break;
00069                 }       
00070         }
00071         if (background) {
00072       pid =  fork();
00073       switch (pid) {
00074          case -1 : cout << "Fork ERROR !, exiting" << endl;
00075          exit(-1);
00076          break;
00077          case 0 : // son
00078             setsid();
00079             launchThreads(listConfFiles(confDir),&threads,&bots);
00080          break;
00081          default :  // father 
00082             exit(0);
00083       }
00084    }
00085    else {
00086       launchThreads(listConfFiles(confDir),&threads,&bots);
00087    }
00088 
00089    // Wait for threads and delete them
00090    for(unsigned int i = 0 ; i < threads.size() ; i ++) {
00091       threads[i]->join();
00092       delete(threads[i]);
00093       delete(bots[i]);
00094    }
00095    return 0;
00096 }
00097 
00098 vector<string> listConfFiles(string confDir) {
00099    DIR *dp;
00100    struct dirent *ep;
00101    dp = opendir (confDir.c_str());
00102    vector<string> files;
00103    string::size_type pos ;
00104    if (dp != NULL) {
00105       while ((ep = readdir(dp))) {
00106          pos = ((string)ep->d_name).find(".") ;
00107          if ( (pos != string::npos) && (((string)ep->d_name).substr(pos)==".conf") ) {
00108             files.push_back(confDir+"/"+ep->d_name);
00109          }
00110       }
00111       closedir (dp);
00112    }
00113    else {
00114       cerr << "Couldn't open configuration directory. Exiting ..." << endl;
00115       exit(0);
00116    }
00117    return files;
00118 }
00119 
00120 void launchThreads(vector<string> confFiles,vector<PThread*>* threads,vector<BotKernel*>* bots) {
00121    for(unsigned int i = 0 ; i < confFiles.size() ; i ++ ) {
00122       threads->push_back(new PThread());
00123       bots->push_back(new BotKernel(confFiles[i]));
00124       threads->at(i)->exec(launchBot,bots->at(i));
00125    }
00126 }
00127 
00128 void * launchBot(void * arg) {
00129    ((BotKernel*)arg)->run();
00130    return NULL;
00131 }
00132 
00133 void displayHelp(string firstArg,bool quit) {
00134    cout << "Usage : "<< firstArg << " [-c configuration_dir] [-b]" << endl;
00135    if ( quit ) {
00136       exit(0);
00137    }
00138 }

Generated on Sun Apr 19 02:47:15 2009 for trustyRC by  doxygen 1.5.7.1