Esempio n. 1
0
/* updateTable()
 *
 * Updates table if possible. If table is changed, output to file.
 */
bool updateTable(struct router *currTable, struct router rcvdTable) {
	bool isChanged = false;
	int i;
	for (i=0; i<NUMROUTERS; i++) {
		// ignore own entry in table
		if (i != currTable->index) {
			// find shortest paths to other routers
			if (rcvdTable.costs[i] == INT_MAX) {
				continue;
			} else if ( currTable->costs[i] > rcvdTable.costs[i] + currTable->costs[rcvdTable.index] ) {

				currTable->otherRouters[i] = rcvdTable.otherRouters[i];
				currTable->costs[i] = rcvdTable.costs[i] + currTable->costs[rcvdTable.index];
				currTable->outgoingPorts[i] = rcvdTable.index + 10000;
				currTable->destinationPorts[i] = rcvdTable.destinationPorts[i];

				isChanged = true;
			}
		}
	}
	if (isChanged) {
		outputTable(currTable, false);
	}
	return isChanged;
}
Esempio n. 2
0
void generateParserTables(FILE* opcodes, FILE* table)
{
	struct Z80OpcodeTable* mainTable = createTableTree(opcodes, table);
	scanOpcodes(opcodes, mainTable);
	fprintf(table, "\n\n");
	outputTable(mainTable, table);
}
Esempio n. 3
0
void MainWindow::on_calculateButton2_clicked()
{

    //cout<<"Orientation "<<getTableOrientation()<<endl;
    //cout<<"Hello"<<endl;
    if(howInput==0){
        ui->comboBox->setEnabled(false);
    }
    else{
        ui->comboBox->setEnabled(true);
    }
    if(howInput==0){
        if(stroka1!=NULL){
         vector<QStringList> students=getInfoFromFields2(stroka1);
         mann->loadDataFromFields(students);
         mann->sortStudents();
         mann->setGroups();
         mann->setRate();
         //mann->showStudents();
         mann->results();
         outputTable(mann->getStudents(),ui->table_output);
         mann->resetVariables();
         students.clear();
         stroka1="";
        }
    }
    else{

    }
    if(stroka1!=NULL && stroka2!=NULL){
        vector<QStringList> students=getInfoFromFields(stroka1,stroka2);
        mann->loadDataFromFields(students);
        mann->sortStudents();

        mann->setRate();
        //mann->showStudents();
        mann->results();
        outputTable(mann->getStudents(),ui->table_output);
        mann->resetVariables();
        students.clear();
        stroka1="";
        stroka2="";
    }
}
Esempio n. 4
0
void outputTable(Z80OpcodeTable *table, FILE *file)
{
	int i;
	Z80OpcodeEntry *opc;
	Z80OpcodeTable *tbl;
	char fmt[MAX_LINE];
	
	printf("Outputting table %s...", table->name);
	
    fprintf(file, "Z80CPU::Z80OpcodeTable Z80CPU::opcodes_%s = { %d, {\n", table->name, table->opcode_offset);
	
	for (i = 0, opc = table->entries; i < 256; i++, opc++)
	{
		tbl = opc->table;

		strcpy(fmt, "NULL");
		if (opc->format)
			sprintf(fmt, "\"%s\"", opc->format);

		fprintf(file, "\t{ %s%-20s, %-9s, %-4i, %-20s, %s%s }%s\n",
                                                (opc->func ? "&Z80CPU::" : ""),
												(opc->func ? opc->func : "NULL"),
												OpTypeName[opc->operand_type],
                                                opc->cycles,
												fmt,
												(tbl ? "&opcodes_" : ""),
												(tbl ? tbl->name : "NULL"),
												(i == 255 ? "" : ",")
												);

	}
	fprintf(file, "} };\n");	
	fprintf(file, "\n\n");
		
	printf("done\n");
	
	for (i = 0, opc = table->entries; i < 256; i++, opc++)
	{
		tbl = opc->table;			
		if (tbl)
			outputTable(tbl, file);
	}
}
Esempio n. 5
0
void MainWindow::on_button_calc_clicked()
{
    vector <QStringList> data;
    data=getDataFromTable(ui->table_input,howInput);
    cout<<"DATA SIZE"<<data.size()<<" DATA SIZZZZ "<<data.at(0).at(0).toUtf8().constData()<< endl;
    cout<<"STYDENTY "<<mann->getStudents()->size()<<endl;
    mann->clearStudents();
    mann->loadDataFromFields(data);
    cout<<"STYDENTY "<<mann->getStudents()->size()<<endl;
    mann->sortStudents();
    if(howInput==0) mann->setGroups();
    mann->showStudents();
    mann->setRate();
    mann->showStudents();
    cout<<"Summs "<<mann->getSumScores(0)<<" "<<mann->getSumScores(1)<<endl;
    mann->results();

    outputTable(mann->getStudents(),ui->table_output);
    mann->resetVariables();
    data.clear();
}
int main()
{
	cout << "MLS Season predictor     Written by Derek Gathright   [email protected]\n";

	srand ( time(NULL) );
	
	int teamSkill[12], teamPoints[12], teamHomeAdvantage[12], tieRatio = 6, wins[12], ties[12], losses[12];
	char game[12][12], realResults = 'y', gameDiv[12][12], repeat = 'y';

	teamSkill[0] = 5;// LA
	teamSkill[1] = 7;// KC
	teamSkill[2] = 4;// SJ
	teamSkill[3] = 4;// Colorado
	teamSkill[4] = 7;// Chicago	
	teamSkill[5] = 5;// Dallas					
	teamSkill[6] = 7;// Tampa Bay
	teamSkill[7] = 4;// Columbus
	teamSkill[8] = 6;// DC
	teamSkill[9] = 6;// NY
	teamSkill[10] = 4;// Miami
	teamSkill[11] = 3;// NE					

	teamHomeAdvantage[0] = 2;// LA
	teamHomeAdvantage[1] = 1;// KC
	teamHomeAdvantage[2] = 3;// SJ
	teamHomeAdvantage[3] = 2;// Colorado
	teamHomeAdvantage[4] = 2;// Chicago	
	teamHomeAdvantage[5] = 2;// Dallas					
	teamHomeAdvantage[6] = 1;// Tampa Bay
	teamHomeAdvantage[7] = 2;// Columbus
	teamHomeAdvantage[8] = 3;// DC
	teamHomeAdvantage[9] = 1;// NY
	teamHomeAdvantage[10] = 1;// Miami
	teamHomeAdvantage[11] = 1;// NE					
	
	do
	{
		for (int i = 0; i <= 11; i++) teamPoints[i] = 0;
	
		//cout << "Would you like to use the real results from the MLS games so far? (y/n): ";
		//cin  >> realResults;
	
		for (int count = 0; count < 1; count++)
			for (int i = 0; i <= 11; i++)
				for (int j = 0; j <= 11; j++)
				{
					wins[i] = 0;
					ties[i] = 0;
					losses[i] = 0;

					game[i][j] = ' ';
					if(i != j) game[i][j] = getResult(teamSkill[i], teamHomeAdvantage[j], teamSkill[j], tieRatio);

					gameDiv[i][j] = ' ';
					if(i != j) gameDiv[i][j] = getResult(teamSkill[i], teamHomeAdvantage[j], teamSkill[j], tieRatio);
				}
		
			if ((realResults == 'y') || (realResults == 'Y'))
			{
				// *** Input real results here ***
				// game[home][away] = 'home team result'
				// Ex. LA @ KC, KC wins
				// game[1][0] = 'W';
				// game[1][0] = 'F';
				
				/*      LA    KC    SJ    Col   Chi   Dal   TB    CB    DC    NY    Mia   NE
				LA       -    .-.   .-.   .-x   .     .     .     .     .     .     .     .
				KC      .-x    -    .-.   .-.   .     .     .     .     .     .     x     .
				SJ      .-x   .-.    -    .-.   .     x     .     .     .     .     .     .
				Col     .-.   .-.   .-.    -    .     .     .     .     .     x     x     .
				Chi     .     .     .     .      -    .-.   .-.   .-.   x     .     .     .
				Dal     .     .     .     .     .-x    -    .-.   .-.   .     .     .     .
				TB      .     .     .     .     .-.   .-x    -    .-x   .     .     .     .
				C-Bus   .     x     .     .     .-x   .-.   .-.    -    .     .     .     .
				DC      .     x     .     .     .     .     .     .      -    .-.   .-.   .-x
				NY      .     .     .     .     .     .     x     .     .-.    -    .-.   .-x
				Mia     .     x     x     .     .     .     .     .     .-.   .-.    -    .-.
				NE      .     .     .     .     .     .     .     .     .-.   .-.   .-x    - */
				
				// 4/7 games
				gameDiv[2][0]  	= 'W'; 	// LA  @ SJ
				gameDiv[9][11] 	= 'W'; 	// NE  @ NY
				game[8][1]    	= 'W';	// KC  @ DC
				game[10][3]    	= 'W';	// Col @ Mia
				gameDiv[7][4] 	= 'T'; 	// Chi @ Clb
				gameDiv[5][6]  	= 'L'; 	// TB  @ Dal
				
				// 4/14 games
				game[2][5] 		= 'L'; 	//  Dal @ SJ
				gameDiv[6][7] 	= 'L'; 	//  Clb @ TB
				gameDiv[11][10] = 'L'; 	//  Mia @ NE
				gameDiv[1][0] 	= 'W'; 	//  LA  @ KC
				game[3][9] 		= 'T'; 	//  NY  @ Col
				game[4][8] 		= 'W'; 	//  DC  @ Chi
				
				// 4/18 games
				game[10][1] 	= 'W'; 	// Mia @ KC
				
				// 4/21 games
				gameDiv[0][3]	= 'W'; 	//Colorado at Los Angeles
				game[9][6]  	= 'L'; 	//Tampa Bay at MetroStars 
				gameDiv[5][4]  	= 'W'; 	//Chicago at Dallas
				game[10][2] 	= 'W'; 	//San Jose at Miami
				game[7][1]  	= 'L'; 	//Kansas City at Columbus 
				gameDiv[8][11] 	= 'W'; 	//New England at D.C. United
			}
		
			for (int i = 0; i < 12; i++)
			{
				if (i < 4)
					for (int k = 4; k < 12; k++)
						gameDiv[i][k] = ' ';
				if ((i > 3) && (i < 8))
					for (int k = 0; k < 12; k++)
						if ((k < 4) || (k > 7))
							gameDiv[i][k] = ' ';
				if (i > 7)
					for (int k = 0; k < 8; k++)
						gameDiv[i][k] = ' ';
			}

			for (int home = 0; home <= 11; home++)
				for (int away = 0; away <= 11; away++)
				{
					if (game[home][away] == 'W'){ 		teamPoints[home]  	= teamPoints[home] + 3; 
											      		wins[home] 			= wins[home] + 1; 
												  		losses[away]		= losses[away] + 1;
												  		}
					if (game[home][away] == 'T'){ 		teamPoints[home]  	= teamPoints[home] + 1; 
												  		ties[home] 			= ties[home] + 1;
												 		}

					if (game[home][away] == 'L'){ 		teamPoints[away]  	= teamPoints[away] + 3; 
												  		wins[away] 			= wins[away] + 1; 
												  		losses[home] 		= losses[home] + 1;
												  		}
					if (game[home][away] == 'T'){ 		teamPoints[away]  	= teamPoints[away] + 1; 
												  		ties[away] 			= ties[away] + 1;
												  		}
											
					if (gameDiv[home][away] == 'W'){ 	teamPoints[home] 	= teamPoints[home] + 3; 
													 	wins[home] 	  		= wins[home] + 1; 
													 	losses[away] 	  	= losses[away] + 1;
													 	}
					if (gameDiv[home][away] == 'T'){ 	teamPoints[home] 	= teamPoints[home] + 1; 
													 	ties[home] 	  		= ties[home] + 1;
													 	}

					if (gameDiv[home][away] == 'L'){ 	teamPoints[away] 	= teamPoints[away] + 3; 
													 	wins[away] 	  		= wins[away] + 1; 
													 	losses[home] 	  	= losses[home] + 1;
													 	}
					if (gameDiv[home][away] == 'T'){ 	teamPoints[away] 	= teamPoints[away] + 1; 
													 	ties[away] 	  		= ties[away] + 1;
													 	}
				}

		outputPoints(teamPoints, wins, ties, losses);
		outputTable(game, gameDiv);
		//cout << "\n\tNote: All results are the home teams results. Read the HOME teams on the" << "left and the AWAY teams on top.";
	//-----------------------------------------------
		int place[11], team[11], temp;
		
		for (int i = 0; i < 12; i++)
		{
			place[i] = teamPoints[i];
			team[i] = i;
		}
		
		for (int i = 11; i>=0; i--)
		{
		    for (int j = 0; j<i; j++) 
	    	{
				if (teamPoints[j] > teamPoints[j+1]) 
				{
			    	temp = teamPoints[j];
		    		teamPoints[j] = teamPoints[j+1];
			    	teamPoints[j+1] = temp;

			    	temp = team[j];
		    		team[j] = team[j+1];
			    	team[j+1] = temp;
				}
	    	}
		
		cout << endl;
		
		if 		(i ==11)		cout << endl;
		if 		(team[i] == 1)	cout << "KC   ";
		else if (team[i] == 2)	cout << "SJ   ";
		else if (team[i] == 3)	cout << "Col  ";
		else if (team[i] == 4)	cout << "Chi  ";
		else if (team[i] == 5)	cout << "Dal  ";
		else if (team[i] == 6)	cout << "TB   ";
		else if (team[i] == 7)	cout << "C-Bus";
		else if (team[i] == 8)	cout << "DC   ";
		else if (team[i] == 9)	cout << "NY   ";
		else if (team[i] == 10)	cout << "Mia  ";
		else if (team[i] == 11) cout << "NE   ";
		else 					cout << "LA   ";

		cout << " - " << teamPoints[i];
	
		if (i == 4)				cout << "\n----------";
		}
	//-----------------------------------------------
		cout << "\n\nWould you like to run the program again? (y/n): ";
		cin >> repeat;
		
		if ((repeat == 'y') || (repeat == 'Y'))
			cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	}
	while ((repeat == 'y') || (repeat == 'Y'));
	
	cout << "\nType something to exit: ";
	char a;
	cin >> a;
}
Esempio n. 7
0
int main(int argc, char *argv[])
{
	int sockfd[NUMROUTERS]; /* socket */
	int clientlen; /* byte size of client's address */
	struct sockaddr_in serveraddr[NUMROUTERS]; /* server's address */
	struct sockaddr_in clientaddr; /* client's address */
	struct hostent *hostp; /* client host info */
	int buf[BUFSIZE]; /* message buffer */
	char *hostaddrp; /* dotted decimal host address string */
	int optval; /* flag value for setsockopt */
	int n; /* message byte size */
	int count;
	fd_set socks;
	bool stableState = false;
	int nKills = 0;
    char * filepath = "sample.txt";
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 50000;
	int killedRouters[NUMROUTERS] = {0};

	/* for testing */
	struct router tableA, tableB, tableC, tableD, tableE, tableF;
/*
	struct router tableA = {
		INDEXA,
		{   'A',     NULL,    NULL,    NULL,    NULL,    NULL   },
		{    0,     INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX },
		{ ROUTERA,   NULL,    NULL,    NULL,    NULL,    NULL   },
		{ ROUTERA,   NULL,    NULL,    NULL,    NULL,    NULL   }
	};
*/
/*
	struct router tableA = {
		INDEXA,
        	{   'A',     NULL,    NULL,    NULL,    NULL,    NULL   },
        	{    0,     INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX },
        	{ ROUTERA,   NULL,    NULL,    NULL,    NULL,    NULL   },
        	{ ROUTERA,   NULL,    NULL,    NULL,    NULL,    NULL   }
	};


	struct router tableB = {
		INDEXB,
		{  NULL,     'B',    NULL,    NULL,    NULL,    NULL    },
		{ INT_MAX,    0,    INT_MAX, INT_MAX, INT_MAX, INT_MAX  },
		{  NULL,   ROUTERB,   NULL,    NULL,    NULL,    NULL   },
		{  NULL,   ROUTERB,   NULL,    NULL,    NULL,    NULL   }
	};
	struct router tableC = {
		INDEXC,
		{  NULL,     NULL,     'C',      NULL,    NULL,    NULL   },
		{ INT_MAX, INT_MAX,     0,     INT_MAX, INT_MAX, INT_MAX  },
		{  NULL,     NULL,   ROUTERC,    NULL,    NULL,    NULL   },
		{  NULL,     NULL,   ROUTERC,    NULL,    NULL,    NULL   }
	};
	struct router tableD = {
		INDEXD,
		{  NULL,     NULL,    NULL,    'D',     NULL,    NULL    },
		{ INT_MAX, INT_MAX, INT_MAX,    0,    INT_MAX, INT_MAX   },
		{  NULL,     NULL,    NULL,  ROUTERD,    NULL,    NULL   },
		{  NULL,     NULL,    NULL,  ROUTERD,    NULL,    NULL   }
	};
	struct router tableE = {
		INDEXE,
		{  NULL,     NULL,    NULL,    NULL,    'E',    NULL     },
		{ INT_MAX, INT_MAX, INT_MAX, INT_MAX,    0,    INT_MAX   },
		{  NULL,     NULL,    NULL,    NULL,  ROUTERE,    NULL   },
		{  NULL,     NULL,    NULL,    NULL,  ROUTERE,    NULL   }
	};
	struct router tableF = {
		INDEXF,
		{  NULL,     NULL,    NULL,    NULL,    NULL,     'F'   },
		{ INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX,    0     },
		{  NULL,     NULL,    NULL,    NULL,    NULL,  ROUTERF  },
		{  NULL,     NULL,    NULL,    NULL,    NULL,  ROUTERF  }
	};
*/
	struct router **network = malloc(NUMROUTERS * sizeof(struct router*));
	int i;
	for (i=0; i<NUMROUTERS; i++) {
		network[i] = malloc(NUMROUTERS * sizeof(struct router));
	}
	network[0] = &tableA;
	network[1] = &tableB;
	network[2] = &tableC;
	network[3] = &tableD;
	network[4] = &tableE;
	network[5] = &tableF;
/*
	for(i=0;i < NUMROUTERS; i++)
	{
		printRouter(network[i]);
	}
*/
re_initialize:
/*
	if (nKills > 0)
	{
		printf("\nenter\n");
	}
*/
	reinitializeTables(&tableA, &tableB, &tableC, &tableD, &tableE, &tableF);
/*
	printf("***\n***AFTER REINIT:***\n***");	
	for(i=0;i < NUMROUTERS; i++)
	{
		printRouter(network[i]);
	}
*/
	struct matrix neighborMatrix = initializeFromFile(&tableA, &tableB, &tableC, &tableD, &tableE, &tableF, killedRouters);	
	/* end testing */
/*
	printf("***\n***SECOND PRINT:***\n***");	

	for(i=0;i < NUMROUTERS; i++)
	{
		printRouter(network[i]);
	}

	int g;
	for (i=0; i < NUMROUTERS; i++)
	{
		for (g=0; g < NUMROUTERS; g++)
			printf("%d\t", neighborMatrix.r[i][g]);	
		printf("\n");
	}
*/
	initializeOutputFiles(network);
	for (i=0; i<NUMROUTERS; i++) {
		/* create parent socket */
		if ( (sockfd[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
			error("Error opening socket");

		/* server can be rerun immediately after killed */
		optval = 1;
		setsockopt(sockfd[i], SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
		setsockopt(sockfd[i], SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));

		/* build server's Internet address */
		bzero((char *) &serveraddr[i], sizeof(serveraddr[i]));
		serveraddr[i].sin_family = AF_INET;
		serveraddr[i].sin_addr.s_addr = htonl(INADDR_ANY);

		switch(i+10000) {
			case ROUTERA:
				serveraddr[i].sin_port = htons(ROUTERA);
				break;
			case ROUTERB:
				serveraddr[i].sin_port = htons(ROUTERB);
				break;
			case ROUTERC:
				serveraddr[i].sin_port = htons(ROUTERC);
				break;
			case ROUTERD:
				serveraddr[i].sin_port = htons(ROUTERD);
				break;
			case ROUTERE:
				serveraddr[i].sin_port = htons(ROUTERE);
				break;
			case ROUTERF:
				serveraddr[i].sin_port = htons(ROUTERF);
				break;
		}
		/* bind: associate parent socket with port */
		if (bind(sockfd[i], (struct sockaddr *) &serveraddr[i], sizeof(serveraddr[i])) < 0)
			error("Error on binding");
	}

	clientlen = sizeof(clientaddr);
	int serverlen = sizeof(serveraddr[0]);
	int start;
	/* begin by having ROUTERA send DV to one neighbor */
	/* for this implementation, ROUTERA will send to ROUTERB */
	struct router* starter;
	switch (argv[1][0])
	{
		case 'A':
			starter = &tableA;
			start = INDEXA;
			break;
		case 'B':
			starter = &tableB;
			start = INDEXB;
			break;
		case 'C':
			starter = &tableC;
			start = INDEXC;
			break;
		case 'D':
			starter = &tableD;
			start = INDEXD;
			break;
		case 'E':
			starter = &tableE;
			start = INDEXE;
			break;
		case 'F':
			starter = &tableF;
			start = INDEXF;
			break;
	}
//exit(0);

/*

	for (i=0; i<NUMROUTERS; i++) {
		if (neighborMatrix.r[0][i] != -1) {
			start = neighborMatrix.r[0][i];
			break;
		}
	}
*/
// start is 0 thru 5
	//in this case, start=1
	//printRouter(starter);
	memset(buf, 0, BUFSIZE);
	tableToBuffer(starter, &buf);
//	tableToBuffer(&tableA, &buf);

//	printf("Starting router: %d %c\n", start - 'A', start);

//	n = sendto(sockfd[start - 'A'], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[start], clientlen);
	n = sendto(sockfd[start], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[start], clientlen);

	int nsocks = max(sockfd[0], sockfd[1]);
	for (i=2; i<NUMROUTERS; i++) {
		nsocks = max(nsocks, sockfd[i]);
	}
	int counterdd = 0;
	count = 0;
	printf("Stabilizing network...");
	/* loop: wait for datagram, then echo it */
	while (1) {
		FD_ZERO(&socks);
		for (i=0; i<NUMROUTERS; i++) {
			FD_SET(sockfd[i], &socks);
		}
		
		if (select(nsocks+1, &socks, NULL, NULL, NULL) < 0) {
			printf("Error selecting socket\n");
		} else {
			/* receives UDP datagram from client */
			memset(buf, 0, BUFSIZE);
			if (FD_ISSET(sockfd[0], &socks)) {
				if ( (n = recvfrom(sockfd[0], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
					error("Error receiving datagram from client\n");

				struct router compTable;
				bufferToTable(buf, &compTable);

				if (updateTable(&tableA, compTable) == false)
					count++;
				else
					count = 0;

				tableToBuffer(&tableA, &buf);

				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[0][i] != -1) {
						n = sendto(sockfd[0], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[0][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}

			if (FD_ISSET(sockfd[1], &socks)) {
				if ( (n = recvfrom(sockfd[1], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
				{
					error("Error receiving datagram from client\n");
					printf("error\n");
				}

				struct router compTable;
				
				bufferToTable(&buf, &compTable);
				if (updateTable(&tableB, compTable) == false)
					count++;
				else
					count = 0;

				tableToBuffer(&tableB, &buf);

				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[1][i] != -1) {
						n = sendto(sockfd[1], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[1][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}

			if (FD_ISSET(sockfd[2], &socks)) {
				if ( (n = recvfrom(sockfd[2], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
					error("Error receiving datagram from client\n");

				struct router compTable;

				bufferToTable(&buf, &compTable);

				if(updateTable(&tableC, compTable) == false)
					count++;
				else
					count = 0;

				tableToBuffer(&tableC, &buf);

				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[2][i] != -1) {
						n = sendto(sockfd[2], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[2][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}
			if (FD_ISSET(sockfd[3], &socks)) {
				if ( (n = recvfrom(sockfd[3], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
					error("Error receiving datagram from client\n");

				struct router compTable;

				bufferToTable(&buf, &compTable);

				if(updateTable(&tableD, compTable) == false)
					count++;
				else
					count = 0;


				tableToBuffer(&tableD, &buf);
				
				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[3][i] != -1) {
						n = sendto(sockfd[3], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[3][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}
			if (FD_ISSET(sockfd[4], &socks)) {
				if ( (n = recvfrom(sockfd[4], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
					error("Error receiving datagram from client\n");

				struct router compTable;

				bufferToTable(&buf, &compTable);


				if(updateTable(&tableE, compTable) == false)
					count++;
				else
					count = 0;


				tableToBuffer(&tableE, &buf);

				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[4][i] != -1) {
						n = sendto(sockfd[4], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[4][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}
			if (FD_ISSET(sockfd[5], &socks)) {
				if ( (n = recvfrom(sockfd[5], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 )
					error("Error receiving datagram from client\n");

				struct router compTable;

				bufferToTable(&buf, &compTable);


				if(updateTable(&tableF, compTable) == false)
					count++;
				else
					count = 0;


				tableToBuffer(&tableF, &buf);

				for (i=0; i<NUMROUTERS; i++) {
					if (neighborMatrix.r[5][i] != -1) {
						n = sendto(sockfd[5], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[5][i]], clientlen);
						if (n < 0)
							error("Error sending to client");
					}
				}
			}
			if (count >= NUMROUTERS * 2) {
				for (i=0; i<NUMROUTERS; i++) {
					outputTable(network[i], true);
				}

				stableState = true;
				// printf("\n\nFINAL ROUTER INFO:\n\n");
				// printf("ROUTER A:\n\n");
				// printRouter(&tableA);
				// printf("\n\nROUTER B:\n\n");
				// printRouter(&tableB);
				// printf("\n\nROUTER C:\n\n");
				// printRouter(&tableC);
				// printf("\n\nROUTER D:\n\n");
				// printRouter(&tableD);
				// printf("\n\nROUTER E:\n\n");
				// printRouter(&tableE);
				// printf("\n\nROUTER F:\n\n");
				// printRouter(&tableF);
				printf("[OK]\n\n");
choose_action:
				printf("Press 1<ENTER> to kill a router\nPress 2<ENTER> to send packet across the network\nPress 3<ENTER> to profile the network\nPress 4<ENTER> to exit\n-> ");
				// steady state
				// scan for input to send a packe
				int option, k; // kill router, or send packet from x to y
				char toKill, srcRouter, dstRouter;
				scanf("%d", &option);
				switch (option)
				{
					case 1:
						printf("Label of router to kill (A-F):\n-> ");
						scanf("\n%c", &toKill);
						printf("Killing router %c\n", toupper(toKill));
						int n;
/*
                                		struct timeval tv;
                                		tv.tv_usec = 500000;
*/
                                		for (k = 0; k < NUMROUTERS; k++)
                                		{
							printf("Clearing Router %c's input buffers...", (char) k + 'A');
                                        		FD_ZERO(&socks);
                                        		FD_SET(sockfd[k], &socks);
                                                        while ( (n = recvfrom(sockfd[k], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) > 0)
                                                        {
//                                                                printf("%d %d\n", k, n);
                                                        }
							printf("[OK]\n");
						}
						reinitializeTopologyFile(toupper(toKill), nKills);
						killedRouters[toupper(toKill) - 'A'] = 1;
						nKills++;
						goto re_initialize;

						// will never reach this point
						break;
					case 2:
						printf("Label of source router (A-F):\n-> ");
						scanf("%c", &srcRouter);
						srcRouter = getchar();
						srcRouter = toupper(srcRouter);
						printf("Label of destination router (A-F):\n-> ");
						scanf("%c", &dstRouter);
						dstRouter = getchar();
						dstRouter = toupper(dstRouter);
						printf("Routing a packet from Router %c to Router %c...", srcRouter, dstRouter);
						struct packet p = { 'd', "message", srcRouter, dstRouter, 0, 0 };
						forwardPacket(&p, network);
						printf("[OK]\n\n");
						goto choose_action;
						break;
					case 3:
						printf("Routing tables:\n\n");
						for (k = 0; k < NUMROUTERS; k++)
						{
							printf("\nRouter %c:\n\n", 'A' + k);
							printRouter(network[k]);
						}
						goto choose_action;
						break;
					case 4:
						printf("Killing all routers.\n");
						
/*
                                		struct timeval tv;
                                		tv.tv_usec = 500000;
*/
                                		for (k = 0; k < NUMROUTERS; k++)
                                		{
							printf("Clearing Router %c's input buffers...", (char) k + 'A');
                                        		FD_ZERO(&socks);
                                        		FD_SET(sockfd[k], &socks);
                                                        while ( (n = recvfrom(sockfd[k], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) > 0)
                                                        {
//                                                                printf("%d %d\n", k, n);
                                                        }
							printf("[OK]\n");
						}
						break;
						
				} 
				break;
			}
		}
	}
}