示例#1
0
文件: Bank.cpp 项目: drewart/school
void Bank::processQueue()
{
   Transaction* tran;
   while(transQueue->dequeue(tran))
   {
      if (!handleTransaction(tran))
         cout << "Unable to handle Transaction" << endl;
      //else TODO:  if not handled do we want to delete the tran?
         //delete tran; ??
   }
}
int main(int argc, char **argv)
{
	int					sock;
	int					length;
	struct sockaddr_in	server;
	int 				msgsock;
	int					c;

	/* The following are for getopt(3C) */
	extern char			*optarg;
	extern int			optind;
	extern int			opterr;
	extern int			optopt;

	while ((c = getopt(argc, argv, "dr:")) != EOF)
	{
		switch (c) 
		{
		case 'd':
			debugFlag = 1;
			break;
		case 'r':
			documentRoot = optarg;
			break;
		}
	}

	/* Make sure that we have a document root directory, or gripe & croak */
	if (documentRoot == NULL)
	{
		fprintf(stderr, "Usage: %s -r <document root> [-d]\n", argv[0]);
		exit(2);
	}

	time(&startTime);

	debug("Debugging enabled\n");

	/* Create socket to listen on */
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0) 
	{
		perror("opening stream socket");
		exit(1);
	}
	/* Name socket */
	server.sin_family = AF_INET;			/* Use Internet TCP/IP protocols */
	server.sin_addr.s_addr = INADDR_ANY;	/* Use any attached addresses. */
	server.sin_port = htons(MY_SERVER_PORT);

	/* bind() tells the O/S to map the above server values to the socket */
	if (bind(sock, (struct sockaddr *)&server, sizeof(server))) 
	{
		perror("binding stream socket");
		exit(1);
	}

	/* Find out assigned port number and print it out */
	length = sizeof(server);
	if (getsockname(sock, (struct sockaddr *)&server, &length)) 
	{
		perror("getting socket name");
		exit(1);
	}
	fprintf(stderr, "Socket has port #%d\n", ntohs(server.sin_port));

	/* Tell the O/S that we are willing to accept connections */
	listen(sock, 5);

	while(1)
	{

		/* Wait for, and accept any connections from clients */
		msgsock = accept(sock, 0, 0);
		if (msgsock == -1)
		{
			/* Something bad happened in accept() */
			perror("accept");
		}
		else
		{	/* We got a connection, process the request */

			int		status;
			int		pid;

			debug("\nTransaction starting on FD %d\n", msgsock);
			pid = fork();
			
			if (pid < 0)
			{
				debug("ERROR: fork() failed\n");
				status = -1;
			}
			else if (pid == 0)
			{
				signal(SIGPIPE, sigHandler);
				signal(SIGALRM, sigHandler);

				alarm(ALARM_TIME);
				status = handleTransaction(msgsock);
				alarm(0);
				exit(status);
			}
			else
			{
				int		stat;
				int		waitpid;
				waitpid = wait(&stat);
				status = WEXITSTATUS(stat);
				switch(status)
				{
				case 1:
					pipeCount++;
					break;
				case 2:
					timeoutCount++;
					break;
				case 100:
					headCount++;
					break;
				case 110:
					getCount++;
					break;
				default:
					errorCount++;
					break;
				}
			}
			debug("Transaction completed, pid = %d, status = %d\n", pid, status);
			close(msgsock); /* Closes the FD in the parent process */
		}
	}
	/*
	 * Since this program has an infinite loop, the socket "sock" is
	 * never explicitly closed.  However, all sockets will be closed
	 * automatically when a process is killed or terminates normally. 
	 */
}