Esempio n. 1
0
SIDDSensorModel::SIDDSensorModel(const csm::Isd& isd,
                                 const std::string& dataDir)
{
    setSchemaDir(dataDir);

    // Support multi-segment SIDDs
    // In this case, the ISD should tell us which image it wants to use if it's
    // not the first one
    const std::string imageIndexStr(isd.param(IMAGE_INDEX_PARAM));
    size_t imageIndex;
    if (imageIndexStr.empty())
    {
        imageIndex = 0;
    }
    else if (!str::isNumeric(imageIndexStr))
    {
        throw csm::Error(csm::Error::SENSOR_MODEL_NOT_CONSTRUCTIBLE,
                "Unexpected " + std::string(IMAGE_INDEX_PARAM) +
                        " parameter: " + imageIndexStr,
                "SIDDSensorModel::SIDDSensorModel");
    }
    else
    {
        imageIndex = str::toType<size_t>(imageIndexStr);
    }

    // Based on the ISD format, initialize as appropriate
    const std::string& format(isd.format());
    if (format == "NITF2.1")
    {
        // NOTE: Used dynamic_cast here previously but using it with IAI's
        //       version of vts, it threw an exception.  Did older versions of
        //       vts not use the /GR flag (they've since reported it worked
        //       fine when our plugin used dynamic_cast)?
        initializeFromISD((const csm::Nitf21Isd&)isd,
                          imageIndex);
    }
    else if (format == "FILENAME")
    {
        // Note: this case has not been tested
        initializeFromFile(isd.filename(), imageIndex);
    }
    else
    {
        throw csm::Error(csm::Error::SENSOR_MODEL_NOT_CONSTRUCTIBLE,
                           "Unsupported ISD format " + format,
                           "SIDDSensorModel::constructModelFromISD");
    }

}
Esempio n. 2
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;
			}
		}
	}
}