⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulator.cpp

📁 模拟P2P各种网络环境的,适合新手们的学习,不错的源码.
💻 CPP
字号:
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <map>
#include "SimEventScheduler.h"
#include "BT.h"
#include "Sysparameter.h"
#include "Topology.h"
#include "Graph.h"
#include "Property.h"
#include "Debug.h"
#include "Statistic.h"
using namespace std;
double sttime;
double entime;
SimEventScheduler Scheduler;
BT bt;
Statistic statistic;
void print_help(void){
	printf("\n The following is the usage of this program:\n"\
		"\t\t Format: simulator [arguments]\n"\
		"\t\t\t -a agent_num:\t\tagent number \n"\
		"\t\t\t -d doccument_num:\tdocument number \n"\
		"\t\t\t -s doccument_size:\tdocument size \n"\
		"\t\t\t -f config_file_name \n"\
		"\t\t\t -e debug \n"\
		"\t\t\t -h print these usage\n\n"
		);

}
void ConfigSystem( Property&);
int main( int argc, char* argv[]){
	int c = 0;
	char *end = " ";
	char conFile[20] = { 0 };
	strcpy( conFile, NET_CONFIG_FILE);
	while(  ( c = getopt(argc, argv, "a:d:s:f:he")) != -1){
		switch( c){
			case 'a':
				AGENT_NUM = strtol( optarg, &end, 0);
				if( errno < 0){
					printf("simulator:invalid max agentNum\n");
					exit(-1);
				}
				break;
			case'd':
				FILE_NUM = strtol( optarg, &end, 0);
				if( errno < 0){
					printf("simulator :invalid max docnum\n");
					exit(-1);
				}
				break;
			case's':
				 FILE_SIZE = strtol( optarg, &end, 0);
				if( errno < 0){
					printf("simulator :invalid max docnum\n");
					exit(-1);
				}
				break;
				
			case'f':
				strcpy( conFile, optarg);
				if( errno < 0){
					printf("simulator :invalid max docnum\n");
					exit(-1);
				}
				break;
			case'h':
				print_help();
				exit( 0 );
			case'e':
				Debug = 0;
				break;
			default:
				printf(" unknown parameter \n");
				exit( -1 );
		}
	}
	
	srand( time(NULL) );

	//read the overall configure file
	Property property1;
	property1.parseConfigFile( conFile );
	ConfigSystem( property1);
	
	//initialize the network topology 
	Graph graph;
	Property property2;
	property2.parseConfigFile( NET_CONFIG_FILE );
	graph.fromBRITEFile( GRAPH_FILE );
	Topology *topology = Topology::getInstance();
	(*topology).configNetwork(graph, property2);
	
	bt.setTopology(topology);
	bt.confAgent();
	bt.confDocument();
	bt.confEvents();
	sttime = time(NULL);
	Scheduler.run();
	
	bt.printEspInfo();
	
	statistic.printFileDownloadInfo();
	
	entime = time(NULL);
	printf("the elapse time %lf\n", entime-sttime);
	printf("the total time %f\n", Scheduler.getCurrent());

}

void ConfigSystem( Property& property){
	map<string, string> tmap = property.getProperty();
	assert(tmap.find("DEBUGLEVEL") != tmap.end());
		Debug = atoi( tmap["DEBUGLEVEL"].c_str());

	assert( tmap.find("AGENT_NUMBER") != tmap.end());
	AGENT_NUM = atoi (tmap["AGENT_NUMBER"].c_str() );

	assert( tmap.find("DOCUMENT_NUMBER") != tmap.end());
	FILE_NUM = atoi(tmap["DOCUMENT_NUMBER"].c_str() );

	assert( tmap.find( "FILE_SIZE" ) != tmap.end());
	FILE_SIZE = atoi( tmap[ "FILE_SIZE"].c_str() )*1024*1024 ;

	assert( tmap.find( "BLOCK_LENTH" ) != tmap.end());
	BLOCK_LENTH = atoi(tmap[ "BLOCK_LENTH"].c_str())*1024;

	assert( tmap.find( "PIECE_LENTH" ) != tmap.end());
	PIECE_LENTH = atoi( tmap[ "PIECE_LENTH"].c_str())*1024;

	assert( tmap.find( "MAX_CONNECTION_NUM" ) != tmap.end());
	MAX_CONNECTION_NUM = atoi(tmap[ "MAX_CONNECTION_NUM"].c_str());

	assert( tmap.find( "MAX_UNCHOKE_NUM" ) != tmap.end());
	MAX_UNCHOKE_NUM =atoi( tmap[ "MAX_UNCHOKE_NUM"].c_str() );

	assert( tmap.find( "MAX_CANDIDATES" ) != tmap.end());
	MAX_CANDIDATES = atoi(tmap[ "MAX_CANDIDATES"].c_str());

	assert( tmap.find( "PEER_PEER_NUM_LIMIT" ) != tmap.end());
	PEER_PEERNUMLIMIT= atoi(tmap[ "PEER_PEER_NUM_LIMIT"].c_str());

	assert( tmap.find( "TRACKER_PEER_NUM_LIMIT" ) != tmap.end());
	TRACKER_PEERNUMLIMIT = atoi(tmap[ "TRACKER_PEER_NUM_LIMIT"].c_str());

	assert( tmap.find( "RECHOKE_INTERVAL" ) != tmap.end());
	RECHOKE_INTERVAL = atoi( tmap[ "RECHOKE_INTERVAL"].c_str() );

	assert( tmap.find( "SNUB_CHECK_INTERVAL" ) != tmap.end());
	SNUB_CHECK_INTERVAL = atoi( tmap[ "SNUB_CHECK_INTERVAL"].c_str() );

	assert( tmap.find( "SNUB_UNCHOKE_NUM" ) != tmap.end());
	SNUB_UNCHOKE_NUM =atoi( tmap[ "SNUB_UNCHOKE_NUM"].c_str());

	assert( tmap.find( "OPTIMISTIC_INTERVAL" ) != tmap.end());
	OPTIMISTIC_INTERVAL = atoi( tmap[ "OPTIMISTIC_INTERVAL"].c_str() );

	assert( tmap.find( "ARRIVE_INTERVAL" ) != tmap.end());
	ARRIVE_INTERVAL = atoi( tmap[ "ARRIVE_INTERVAL"].c_str() );

	assert( tmap.find( "NETWORK_CONFIG_FILE" ) != tmap.end());
	strcpy(NET_CONFIG_FILE , tmap[ "NETWORK_CONFIG_FILE"].c_str());

	assert( tmap.find( "GRAPH_FILE" ) != tmap.end());
	strcpy(GRAPH_FILE, tmap[ "GRAPH_FILE"].c_str());

	assert( tmap.find( "CONNECTION_TIMEOUT" ) != tmap.end());
	BT_CONNECTION_TIMEOUT= atoi(tmap[ "CONNECTION_TIMEOUT"].c_str());

	assert( tmap.find( "ANNOUNCE_INTERVAL" ) != tmap.end());
	ANNOUNCE_INTERVAL= atoi(tmap[ "ANNOUNCE_INTERVAL"].c_str());

	assert( tmap.find( "CACHE_NUM" ) != tmap.end());
	CACHE_NUM = atoi(tmap[ "CACHE_NUM"].c_str());

	assert( tmap.find( "SIM_DEBUG_LEVEL" ) != tmap.end());
	SIMDEBUGLEVEL = atoi(tmap[ "SIM_DEBUG_LEVEL"].c_str());

	assert( tmap.find( "BT_DEBUG_LEVEL" ) != tmap.end());
	BTDEBUGLEVEL = atoi(tmap[ "BT_DEBUG_LEVEL"].c_str());

	assert( tmap.find( "PEER_DEBUG_LEVEL" ) != tmap.end());
	PEERDEBUGLEVEL = atoi(tmap[ "PEER_DEBUG_LEVEL"].c_str());

	assert( tmap.find( "SESSION_DEBUG_LEVEL" ) != tmap.end());
	SESSIONDEBUGLEVEL= atoi(tmap[ "SESSION_DEBUG_LEVEL"].c_str());

	assert( tmap.find( "SOCKET_DEBUG_LEVEL" ) != tmap.end());
	SOCKETDEBUGLEVEL = atoi(tmap[ "SOCKET_DEBUG_LEVEL"].c_str());

	assert( tmap.find( "DOC_DEBUG_LEVEL" ) != tmap.end());
	DOCDUBLEVEL= atoi(tmap[ "DOC_DEBUG_LEVEL"].c_str());

	assert( tmap.find("RECHOKE_DEBUG_LEVEL") != tmap.end());
	RECHOKEDEBUGLEVEL= atoi(tmap[ "RECHOKE_DEBUG_LEVEL"].c_str());

	assert( tmap.find("PEER_RETIRE_TIME") != tmap.end());
	PEER_RETIRE_TIME = atoi(tmap[ "PEER_RETIRE_TIME"].c_str());

	assert( tmap.find("ESP_OUT_BANDWIDTH") != tmap.end());
	ESP_OUT_BANDWIDTH = atoi(tmap[ "ESP_OUT_BANDWIDTH"].c_str())*1024;

	assert( tmap.find("PEER_IN_BANDWIDTH") != tmap.end());
	MAX_IN_BANDWIDTH_PER_NODE = atoi(tmap[ "PEER_IN_BANDWIDTH"].c_str())*1024;

	assert( tmap.find("PEER_OUT_BANDWIDTH") != tmap.end());
	MAX_OUT_BANDWIDTH_PER_NODE = atoi(tmap[ "PEER_OUT_BANDWIDTH"].c_str())*1024;

	assert( tmap.find("REQUEST_TIME_OUT") != tmap.end());
	BTREQUESTTIMEOUT  = atoi(tmap[ "REQUEST_TIME_OUT"].c_str());

	assert( tmap.find("ESP_RETIRE_PEER_NUM") != tmap.end());
	ESP_RETIRE_PEER_NUM = atoi(tmap[ "ESP_RETIRE_PEER_NUM"].c_str());

	assert( tmap.find("DANGER_FILE_NUM") != tmap.end());
	DANGER_INFO_NUM = atoi(tmap[ "DANGER_FILE_NUM"].c_str());
	

	assert( tmap.find("MIN_COM_NUM") != tmap.end());
	MIN_COM_NUM = atoi(tmap[ "MIN_COM_NUM"].c_str());


	assert( tmap.find("ARRIVE_NUM") != tmap.end());
	ARRIVE_NUM = atoi(tmap[ "ARRIVE_NUM"].c_str());

	assert( tmap.find("MIN_OUT_BANDWIDTH") != tmap.end());
	MIN_OUT_BANDWIDTH = atoi(tmap[ "MIN_OUT_BANDWIDTH"].c_str())*1024;

	assert( tmap.find("PRINT_INTERVAL") != tmap.end());
	PRINTINTERVAL = atoi(tmap[ "PRINT_INTERVAL"].c_str());

	assert( tmap.find("PRINT_STATISTIC_ON") != tmap.end());
	PRINT_STATISTIC_ON = atoi(tmap[ "PRINT_STATISTIC_ON"].c_str());
	
	DEBUG(SIMDEBUGLEVEL){
		printf(" debug %d \n", Debug);
		printf(" agentnum %d \n", AGENT_NUM);
		printf(" filenum %d \n", FILE_NUM);
		printf(" filesize %d\n", FILE_SIZE);
		printf(" blocklen %d\n", BLOCK_LENTH);
		printf(" piecelen %d\n", PIECE_LENTH);
		printf(" max_connection_num %d\n", MAX_CONNECTION_NUM);
		printf(" max_unchoke_num %d\n", MAX_UNCHOKE_NUM);
		printf(" max_candidates %d\n", MAX_CANDIDATES);
		printf(" peer_peernumlimit %d\n", PEER_PEERNUMLIMIT);
		printf(" tracker_peernumlimit %d\n", TRACKER_PEERNUMLIMIT);
		printf(" Rechoke_interval %d\n", RECHOKE_INTERVAL);
		printf(" snub_check_interval %d\n", SNUB_CHECK_INTERVAL);
		printf(" snub_unchoke_num %d\n", SNUB_UNCHOKE_NUM);
		printf(" optimistic_unchoke_interval %d\n", OPTIMISTIC_INTERVAL);
		printf(" arrive_interavl %d\n", ARRIVE_INTERVAL);
		printf(" net_config_file %s\n", NET_CONFIG_FILE);
		printf(" graph_file %s\n", GRAPH_FILE);
		printf(" bt_coonnection_timeout %d\n", BT_CONNECTION_TIMEOUT);
		printf(" announce_interval %d\n", ANNOUNCE_INTERVAL);
		printf(" sim_Debug_level %d\n", SIMDEBUGLEVEL);
		printf(" Peer_Debug_level %d\n", PEERDEBUGLEVEL);
		printf(" Session_debug_level %d\n", SESSIONDEBUGLEVEL);
		printf(" socket_debug_level %d\n", SOCKETDEBUGLEVEL);
		printf(" doc_debug_level %d\n", DOCDUBLEVEL);
		printf(" rechoke_debug_level %d\n", RECHOKEDEBUGLEVEL);
		printf(" peer_retire_time %u\n", PEER_RETIRE_TIME);
		printf(" esp_out_bandwidth %d\n", ESP_OUT_BANDWIDTH);
		printf(" peer_in_bandwidth_limit %d\n", MAX_IN_BANDWIDTH_PER_NODE);
		printf(" peer_out_bandwidth_limit %d\n", MAX_OUT_BANDWIDTH_PER_NODE);
		printf(" request_timeout %d\n", BTREQUESTTIMEOUT);
		printf(" esp_retire_peer_num %d\n", ESP_RETIRE_PEER_NUM);
		printf(" danger_file_num %d\n", DANGER_INFO_NUM);
		printf(" min_com_num %d\n", MIN_COM_NUM );
		printf(" arrive num in the same time %d\n", ARRIVE_NUM);
		printf(" min_out_bandwidth %d\n", MIN_OUT_BANDWIDTH );
		printf(" printf_interval %d\n", PRINTINTERVAL );
		printf(" print statistic on %d\n", PRINT_STATISTIC_ON);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -