Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}