int run_haggle() { srand(time(NULL)); #ifdef ENABLE_DEBUG_MANAGER DebugManager *db = NULL; #endif ApplicationManager *am = NULL; DataManager *dm = NULL; // SW: START: SendPriorityManager SendPriorityManager *spm = NULL; // SW: END: SendPriorityManager NodeManager *nm = NULL; ProtocolManager *pm = NULL; ForwardingManager *fm = NULL; SecurityManager *sm = NULL; ConnectivityManager *cm = NULL; LossEstimateManager *lm = NULL; NetworkCodingManager* networkCodingManager = NULL; FragmentationManager* fragmentationManager = NULL; //JM ReplicationManager *replicationManager = NULL; BenchmarkManager *bm = NULL; ResourceManager *rm = NULL; // SW: START: interest manager InterestManager *im = NULL; // SW: END: interest manager ProtocolSocket *p = NULL; #ifdef OS_WINDOWS_MOBILE // For testing we force the deletion of the data store //recreateDataStore = true; #endif int retval = EXIT_FAILURE; #if defined(OS_ANDROID) //mallopt(-1, -1); // MOS - avoid trimming #elif defined(OS_LINUX) mallopt(M_TRIM_THRESHOLD, -1); // MOS - avoid trimming #endif xmlInitParser(); // MOS - this need to be called here for thread-safe libxml use #ifdef DEBUG Trace::trace.enableFileTrace(); #endif HAGGLE_LOG("\n\n****************** HAGGLE STARTUP *********************\n\n"); if (!create_path(HAGGLE_DEFAULT_STORAGE_PATH)) { HAGGLE_ERR("Could not create Haggle storage path : %s\n", HAGGLE_DEFAULT_STORAGE_PATH); return -1; } retval = write_pid_file(getpid()); if (retval != HAGGLE_PROCESS_NO_ERROR) { switch (retval) { case HAGGLE_PROCESS_BAD_PID: HAGGLE_ERR("Cannot read PID file %s.\n", PID_FILE.c_str()); break; case HAGGLE_PROCESS_CANNOT_WRITE_PID: HAGGLE_ERR("Cannot write PID file %s\n", PID_FILE.c_str()); break; case HAGGLE_PROCESS_ALREADY_RUNNING: HAGGLE_ERR("PID file %s indicates that Haggle is already running.\n", PID_FILE.c_str()); break; default: HAGGLE_ERR("Unknown PID file error\n"); } shouldCleanupPidFile = false; return -1; } #if defined(OS_UNIX) && !defined(OS_ANDROID) setrawtty(); #endif /* Seed the random number generator */ prng_init(); // SW: START CONFIG PATH (instead of hardcoded ~/.Haggle/config.xml), if (useMemoryDB) { kernel = new HaggleKernel(configFile, new MemoryDataStore(recreateDataStore)); } else { kernel = new HaggleKernel(configFile, new SQLDataStore(recreateDataStore)); } // SW: END CONFIG PATH. if (!kernel || !kernel->init()) { fprintf(stderr, "Kernel initialization error!\n"); return -1; } // Build a Haggle configuration am = new ApplicationManager(kernel); if (!am || !am->init()) { HAGGLE_ERR("Could not initialize application manager\n"); goto finish; } dm = new DataManager(kernel, setCreateTimeOnBloomfilterUpdate); if (!dm || !dm->init()) { HAGGLE_ERR("Could not initialize data manager\n"); goto finish; } // SW: START: SendPriorityManager spm = new SendPriorityManager(kernel); if (!spm || !spm->init()) { HAGGLE_ERR("Could not initialize send priority manager\n"); goto finish; } // SW: END: SendPriorityManager nm = new NodeManager(kernel); if (!nm || !nm->init()) { HAGGLE_ERR("Could not initialize node manager\n"); goto finish; } pm = new ProtocolManager(kernel); if (!pm || !pm->init()) { HAGGLE_ERR("Could not initialize protocol manager\n"); goto finish; } fm = new ForwardingManager(kernel); if (!fm || !fm->init()) { HAGGLE_ERR("Could not initialize forwarding manager\n"); goto finish; } sm = new SecurityManager(kernel, securityLevel); if (!sm || !sm->init()) { HAGGLE_ERR("Could not initialize security manager\n"); goto finish; } fragmentationManager = new FragmentationManager(kernel); if(!fragmentationManager || !fragmentationManager->init()) { HAGGLE_ERR("Could not initialize fragmentationManager\n"); goto finish; } networkCodingManager = new NetworkCodingManager(kernel); if(!networkCodingManager || !networkCodingManager->init()) { HAGGLE_ERR("Could not initialize networkCodingManager \n"); goto finish; } //JM replicationManager = new ReplicationManager(kernel); if (!replicationManager || !replicationManager->init()) { HAGGLE_ERR("Could not initialize replication manager\n"); goto finish; } lm = new LossEstimateManager(kernel); if(!lm || !lm->init()){ HAGGLE_ERR("Could not initialize LossEstimateManager \n"); goto finish; } #ifdef USE_UNIX_APPLICATION_SOCKET p = new ProtocolLOCAL(kernel->getStoragePath() + "/" + HAGGLE_LOCAL_SOCKET, pm); if (!p || !p->init()) { HAGGLE_ERR("Could not initialize LOCAL protocol\n"); goto finish; } p->setFlag(PROT_FLAG_APPLICATION); p->registerWithManager(); #endif p = new ProtocolUDP("127.0.0.1", HAGGLE_SERVICE_DEFAULT_PORT, pm); /* Add ConnectivityManager last since it will start to * discover interfaces and generate events. At that * point the other managers should already be * running. */ if (!p || !p->init()) { HAGGLE_ERR("Could not initialize UDP Application protocol\n"); goto finish; } p->setFlag(PROT_FLAG_APPLICATION); p->registerWithManager(); // SW: start interest manager im = new InterestManager(kernel); if (!im || !im->init()) { HAGGLE_ERR("Could not initialize interest manager\n"); goto finish; } // SW: end interest manager /* MOS - disable resource mananager due high cpu utilization bug on Android rm = new ResourceManager(kernel); if (!rm || !rm->init()) { HAGGLE_ERR("Could not initialize resource manager\n"); goto finish; } */ if (!isBenchmarking) { cm = new ConnectivityManager(kernel); if (!cm || !cm->init()) { HAGGLE_ERR("Could not initialize connectivity manager\n"); goto finish; } } else { bm = new BenchmarkManager(kernel, Benchmark_DataObjects_Attr, Benchmark_Nodes_Attr, Benchmark_Attr_Num, Benchmark_DataObjects_Num, Benchmark_Test_Num); if (!bm || !bm->init()) { HAGGLE_ERR("Could not initialize benchmark manager\n"); goto finish; } } #if defined(ENABLE_DEBUG_MANAGER) // It seems as if there can be only one accept() per // thread... we need to make the DebugManager register // protocol or something with the ProtocolTCPServer // somehow db = new DebugManager(kernel, runAsInteractive); if (!db || !db->init()) { HAGGLE_ERR("Could not initialize debug manager\n"); /* Treat as non critical error. */ } #endif HAGGLE_DBG("Starting Haggle...\n"); #ifdef OS_WINDOWS_MOBILE if (platform_type(current_platform()) == platform_windows_mobile_professional) tray_notification_add(g_hInstance, kernel); #endif kernel->run(); if(kernel->hasFatalError()) retval = EXIT_FAILURE; else retval = EXIT_SUCCESS; HAGGLE_DBG("Haggle finished...\n"); finish: if (bm) delete bm; if (lm) delete lm; if (fragmentationManager) { delete fragmentationManager; fragmentationManager = NULL; } if (networkCodingManager) delete networkCodingManager; //JM if (replicationManager) delete replicationManager; if (cm) delete cm; if (sm) delete sm; if (fm) delete fm; if (pm) delete pm; if (nm) delete nm; if (dm) delete dm; // SW: START: SendPriorityManager if (spm) delete spm; // SW: END: SendPriorityManager if (am) delete am; // SW: start interest manager if (im) delete im; // SW: end interest manager #if defined(ENABLE_DEBUG_MANAGER) if (db) delete db; #endif if (rm) delete rm; #ifdef OS_WINDOWS_MOBILE tray_notification_remove(); #endif delete kernel; kernel = NULL; xmlCleanupParser(); // MOS return retval; }
int main(int argc, char **argv) { char *infname, *outfname; int ret = 0; enum { NONE, MACROS, MACROS_WHERE, STATS, DEPS } dump = NONE; int i; int platform_win32 = 0; int freestanding = 0; infname = outfname = NULL; current_line = 1; current_fname = FNAME_BUILTIN; macro_add_limits(); for(i = 0; initial_defs[i].nam; i++){ if(initial_defs[i].is_fn) macro_add_func(initial_defs[i].nam, initial_defs[i].val, NULL, 0, 1); else macro_add(initial_defs[i].nam, initial_defs[i].val, 0); } switch(platform_type()){ case PLATFORM_x86_64: macro_add("__LP64__", "1", 0); macro_add("__x86_64__", "1", 0); /* TODO: __i386__ for 32 bit */ break; case PLATFORM_mipsel_32: macro_add("__MIPS__", "1", 0); } if(platform_bigendian()) macro_add("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__", 0); else macro_add("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__", 0); switch(platform_sys()){ #define MAP(t, s) case t: macro_add(s, "1", 0); break MAP(PLATFORM_LINUX, "__linux__"); MAP(PLATFORM_FREEBSD, "__FreeBSD__"); #undef MAP case PLATFORM_DARWIN: macro_add("__DARWIN__", "1", 0); macro_add("__MACH__", "1", 0); /* TODO: proper detection for these */ macro_add("__APPLE__", "1", 0); break; case PLATFORM_CYGWIN: macro_add("__CYGWIN__", "1", 0); platform_win32 = 1; break; } macro_add("__WCHAR_TYPE__", platform_win32 ? "short" : "int", 0); macro_add_sprintf("__BIGGEST_ALIGNMENT__", "%u", platform_align_max()); current_fname = FNAME_CMDLINE; for(i = 1; i < argc && *argv[i] == '-'; i++){ if(!strcmp(argv[i]+1, "-")) break; switch(argv[i][1]){ case 'I': if(argv[i][2]) include_add_dir(argv[i]+2); else goto usage; break; case 'o': if(outfname) goto usage; if(argv[i][2]) outfname = argv[i] + 2; else if(++i < argc) outfname = argv[i]; else goto usage; break; case 'P': option_line_info = 0; break; case 'C': if(argv[i][2] == '\0') strip_comments = STRIP_EXCEPT_DIRECTIVE; else if(!strcmp(argv[i] + 2, "C")) strip_comments = STRIP_NONE; break; case 'M': if(!strcmp(argv[i] + 2, "M")){ dump = DEPS; no_output = 1; }else{ goto usage; } break; case 'D': { char *arg = argv[i] + 2; char *eq; char *directive; if(!*arg){ /* allow "-D" "arg" */ arg = argv[++i]; if(!arg) goto usage; } /* -D'yo yo' means #define yo yo 1, that is, * we literally generate the #define line */ eq = strchr(arg, '='); if(eq) *eq = '\0'; directive = ustrprintf( "define %s %s", arg, eq ? eq + 1 : "1"); parse_internal_directive(directive); free(directive); break; } case 'U': if(!argv[i][2]) goto usage; macro_remove(argv[i] + 2); break; case 'd': if(argv[i][2] && argv[i][3]) goto defaul; switch(argv[i][2]){ case 'M': case 'S': case 'W': /* list #defines */ dump = ( argv[i][2] == 'M' ? MACROS : argv[i][2] == 'S' ? STATS : MACROS_WHERE); no_output = 1; break; case '\0': option_trace = 1; break; default: goto usage; } break; case '\0': /* we've been passed "-" as a filename */ break; case 'f': if(!strcmp(argv[i]+2, "freestanding")){ freestanding = 1; }else if(!strncmp(argv[i]+2, "message-length=", 15)){ const char *p = argv[i] + 17; warning_length = atoi(p); }else{ goto usage; } break; case 'W': { int off; unsigned j; char *p = argv[i] + 2; off = !strncmp(p, "no-", 3); if(off) p += 3; ITER_WARNS(j){ if(!strcmp(p, warns[j].warn)){ if(off) wmode &= ~warns[j].or_mask; else wmode |= warns[j].or_mask; break; } } /* if not found, we ignore - it was intended for cc1 */ break; } case 'O': { switch(argv[i][2]){ case '0': break; case 's': macro_add("__OPTIMIZE_SIZE__", "1", 0); /* fallthru */ default: macro_add("__OPTIMIZE__", "1", 0); } break; } case 'w': if(!argv[i][2]){ wmode = 0; break; } /* fall */ default: defaul: if(std_from_str(argv[i], &cpp_std, NULL) == 0){ /* we have an std */ }else if(!strcmp(argv[i], "-trigraphs")){ option_trigraphs = 1; }else if(!strcmp(argv[i], "-digraphs")){ option_digraphs = 1; }else{ fprintf(stderr, "unrecognised option \"%s\"\n", argv[i]); goto usage; } } } current_fname = FNAME_BUILTIN; macro_add("__STDC_HOSTED__", freestanding ? "0" : "1", 0); switch(cpp_std){ case STD_C89: case STD_C90: /* no */ break; case STD_C99: macro_add("__STDC_VERSION__", "199901L", 0); break; case STD_C11: macro_add("__STDC_VERSION__", "201112L", 0); } if(i < argc){ infname = argv[i++]; if(i < argc){ if(outfname) goto usage; outfname = argv[i++]; if(i < argc) goto usage; } } calctime(infname); #define CHECK_FILE(var, mode, target) \ if(var && strcmp(var, "-")){ \ if(!freopen(var, mode, target)){ \ fprintf(stderr, "open: %s: ", var); \ perror(NULL); \ return 1; \ } \ } CHECK_FILE(outfname, "w", stdout) CHECK_FILE(infname, "r", stdin) if(infname){ dirname_push(udirname(infname)); }else{ infname = "<stdin>"; dirname_push(ustrdup(".")); } current_fname = infname; preprocess(); if(wmode & WUNUSED) macros_warn_unused(); switch(dump){ case NONE: break; case MACROS: case MACROS_WHERE: macros_dump(dump == MACROS_WHERE); break; case STATS: macros_stats(); break; case DEPS: deps_dump(infname); break; } free(dirname_pop()); errno = 0; fclose(stdout); if(errno) die("close():"); return ret; usage: fprintf(stderr, "Usage: %s [options] files...\n", *argv); fputs(" Options:\n" " -Idir: Add search directory\n" " -Dxyz[=abc]: Define xyz (to equal abc)\n" " -Uxyz: Undefine xyz\n" " -o output: output file\n" " -P: don't add #line directives\n" " -dM: debug output\n" " -dS: print macro usage stats\n" " -MM: generate Makefile dependencies\n" " -C: don't discard comments, except in macros\n" " -CC: don't discard comments, even in macros\n" " -trigraphs: enable trigraphs\n" " -digraphs: enable digraphs\n" , stderr); { unsigned i; ITER_WARNS(i) fprintf(stderr, " -W%s: %s\n", warns[i].warn, warns[i].desc); } return 1; }
int run_haggle() { #ifdef ENABLE_DEBUG_MANAGER DebugManager *db = NULL; #endif ApplicationManager *am = NULL; DataManager *dm = NULL; NodeManager *nm = NULL; ProtocolManager *pm = NULL; ForwardingManager *fm = NULL; SecurityManager *sm = NULL; ConnectivityManager *cm = NULL; #ifdef BENCHMARK BenchmarkManager *bm = NULL; //recreateDataStore = true; #endif ResourceManager *rm = NULL; ProtocolSocket *p = NULL; #ifdef OS_WINDOWS_MOBILE // For testing we force the deletion of the data store //recreateDataStore = true; #endif int retval = EXIT_FAILURE; if (!create_path(HAGGLE_DEFAULT_STORAGE_PATH)) { HAGGLE_ERR("Could not create Haggle storage path : %s\n", HAGGLE_DEFAULT_STORAGE_PATH); return -1; } retval = write_pid_file(getpid()); if (retval != HAGGLE_PROCESS_NO_ERROR) { switch (retval) { case HAGGLE_PROCESS_BAD_PID: HAGGLE_ERR("Cannot read PID file %s.\n", PID_FILE.c_str()); break; case HAGGLE_PROCESS_CANNOT_WRITE_PID: HAGGLE_ERR("Cannot write PID file %s\n", PID_FILE.c_str()); break; case HAGGLE_PROCESS_ALREADY_RUNNING: HAGGLE_ERR("PID file %s indicates that Haggle is already running.\n", PID_FILE.c_str()); break; default: HAGGLE_ERR("Unknown PID file error\n"); } shouldCleanupPidFile = false; return -1; } #if defined(OS_UNIX) && !defined(OS_ANDROID) setrawtty(); #endif /* Seed the random number generator */ prng_init(); kernel = new HaggleKernel(new SQLDataStore(recreateDataStore)); if (!kernel || !kernel->init()) { fprintf(stderr, "Kernel initialization error!\n"); return -1; } // Build a Haggle configuration am = new ApplicationManager(kernel); if (!am || !am->init()) { HAGGLE_ERR("Could not initialize application manager\n"); goto finish; } dm = new DataManager(kernel, setCreateTimeOnBloomfilterUpdate); if (!dm || !dm->init()) { HAGGLE_ERR("Could not initialize data manager\n"); goto finish; } nm = new NodeManager(kernel); if (!nm || !nm->init()) { HAGGLE_ERR("Could not initialize node manager\n"); goto finish; } pm = new ProtocolManager(kernel); if (!pm || !pm->init()) { HAGGLE_ERR("Could not initialize protocol manager\n"); goto finish; } fm = new ForwardingManager(kernel); if (!fm || !fm->init()) { HAGGLE_ERR("Could not initialize forwarding manager\n"); goto finish; } sm = new SecurityManager(kernel, securityLevel); if (!sm || !sm->init()) { HAGGLE_ERR("Could not initialize security manager\n"); goto finish; } #ifdef USE_UNIX_APPLICATION_SOCKET p = new ProtocolLOCAL(kernel->getStoragePath() + "/" + HAGGLE_LOCAL_SOCKET, pm); if (!p || !p->init()) { HAGGLE_ERR("Could not initialize LOCAL protocol\n"); goto finish; } p->setFlag(PROT_FLAG_APPLICATION); p->registerWithManager(); #endif p = new ProtocolUDP("127.0.0.1", HAGGLE_SERVICE_DEFAULT_PORT, pm); /* Add ConnectivityManager last since it will start to * discover interfaces and generate events. At that * point the other managers should already be * running. */ if (!p || !p->init()) { HAGGLE_ERR("Could not initialize UDP Application protocol\n"); goto finish; } p->setFlag(PROT_FLAG_APPLICATION); p->registerWithManager(); rm = new ResourceManager(kernel); if (!rm || !rm->init()) { HAGGLE_ERR("Could not initialize resource manager\n"); goto finish; } #ifdef BENCHMARK if (!isBenchmarking) { #endif cm = new ConnectivityManager(kernel); if (!cm || !cm->init()) { HAGGLE_ERR("Could not initialize connectivity manager\n"); goto finish; } #ifdef BENCHMARK } else { bm = new BenchmarkManager(kernel, Benchmark_DataObjects_Attr, Benchmark_Nodes_Attr, Benchmark_Attr_Num, Benchmark_DataObjects_Num, Benchmark_Test_Num); if (!bm || !bm->init()) { HAGGLE_ERR("Could not initialize benchmark manager\n"); goto finish; } } #endif #if defined(ENABLE_DEBUG_MANAGER) // It seems as if there can be only one accept() per // thread... we need to make the DebugManager register // protocol or something with the ProtocolTCPServer // somehow db = new DebugManager(kernel, runAsInteractive); if (!db || !db->init()) { HAGGLE_ERR("Could not initialize debug manager\n"); /* Treat as non critical error. */ } #endif HAGGLE_DBG("Starting Haggle...\n"); #ifdef OS_WINDOWS_MOBILE if (platform_type(current_platform()) == platform_windows_mobile_professional) tray_notification_add(g_hInstance, kernel); #endif kernel->run(); retval = EXIT_SUCCESS; HAGGLE_DBG("Haggle finished...\n"); finish: #ifdef BENCHMARK if (bm) delete bm; #endif if (cm) delete cm; if (sm) delete sm; if (fm) delete fm; if (pm) delete pm; if (nm) delete nm; if (dm) delete dm; if (am) delete am; #if defined(ENABLE_DEBUG_MANAGER) if (db) delete db; #endif if (rm) delete rm; #ifdef OS_WINDOWS_MOBILE tray_notification_remove(); #endif delete kernel; kernel = NULL; return retval; }