main.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 :
00077 setsid();
00078 launchThreads(listConfFiles(confDir),&threads,&bots);
00079 break;
00080 default :
00081 exit(0);
00082 }
00083 }
00084 else {
00085 launchThreads(listConfFiles(confDir),&threads,&bots);
00086 }
00087
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 }