Ejemplo n.º 1
0
int connectionHandler()
{
  requests = 0;

  logTest("connectionHandler", "Going to create a new dispatcher");

  dispatcher = new Dispatcher();

  dispatcher -> start();

  logTest("connectionHandler", "Going to create a new listener");

  listener = new TcpListener();

  listener -> setPort(6001);
  listener -> setBacklog(32);

  //
  // Not needed for the early tests.
  //
  // listener -> setAccept("127.0.0.1");
  //

  listener -> start();

  logTest("connectionHandler", "Going to add the listener to the set");

  dispatcher -> addReadFd(listener -> getFd());

  logTest("connectionHandler", "Going to enter the main loop");

  T_timestamp start = getTimestamp();
  T_timestamp now   = getTimestamp();

  int result;
  int fd;

  T_timestamp timeout;

  while (diffTimestamp(start, now) < 1000000000)
  {
    logTest("connectionHandler", "Going to wait for new events");

    timeout = nullTimestamp();

    result = dispatcher -> waitEvent(timeout);

    logTest("connectionHandler", "Dispatcher returned %d", result);

    while ((fd = dispatcher -> nextReadEvent()) != -1)
    {
      if ((fd = listener -> accept()) != -1)
      {
        requests++;

        logTest("connectionHandler", "Going to handle request %d",
                    requests);

        processHandler(fd);
      }
    }

    processCheck();

    now = getTimestamp();
  }

  logTest("connectionHandler", "Exiting with %ld seconds elapsed since start",
              diffTimestamp(start, now) / 1000);

  dispatcher -> removeReadFd(listener -> getFd());

  dispatcher -> end();

  delete dispatcher;

  logTest("connectionHandler", "Deleted the dispatcher");

  listener -> end();

  delete listener;

  logTest("connectionHandler", "Deleted the listener");

  processWait();

  return requests;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {

	if (argc <= 3) {
		fprintf( stderr, "Usage: %s time word file1 file2 ...\n", argv[0]);
		exit(1);
	}

	noFiles = 0;

	int TIME = atoi(argv[1]);
	char* word = argv[2];
	numFiles = argc-3;


	//files
	pid_t temp[numFiles];
	pidsFile = temp;

	unsigned i;
	for(i = 0; i < numFiles; i++) {
		
		pidsFile[i] = fork();

		if(pidsFile[i] < 0) {
			fprintf(stderr, "Error while forking\n");
			exit(1);
		}
		else if (pidsFile[i] > 0) {
			//PARENT
		}
		else {
			//SON
			searchWord(argv[i+3], word);
		}
	}

	pidFileMonitor = fork();

	if(pidFileMonitor < 0) {
		fprintf(stderr, "Error while forking\n");
		exit(1);
	}
	//child process
	else if(pidFileMonitor > 0) {
		//parent process
	}

	else {
		//child process
		char* files[numFiles];


		for(i = 0;i<numFiles;i++)
			files[i] = argv[i+3];

		fileMonitor(files);
	}


	struct sigaction action;

	// prepare the 'sigaction struct'
	action.sa_handler = handler;
	sigemptyset(&action.sa_mask);
	action.sa_flags = 0;
	
	// install the handler
	if (sigaction(SIGUSR1,&action,NULL)<0 || sigaction(SIGUSR2,&action,NULL)<0 || sigaction(SIGCHLD,&action,NULL) < 0) {
		fprintf(stderr, "Error while instaling handlers...");
		exit(1);
	}

	while(TIME > 0 && noFiles == 0)
		TIME = sleep(TIME);

	noFiles = 1;

	if(noFiles == 1)
		processHandler();

	return 0;
}
Ejemplo n.º 3
0
/** @brief check select pool and process client reqeusts
 *
 *  @param pool select pool
 *  @return void
 */
void SelPool::check_clients() 
{
    int connfd;

    vector<ClientConnection*>::iterator it = clients.begin();
    while ( it != clients.end() )
    {
        ClientConnection *client = *it;
        connfd = client->getFd();
        printf("Handling %d\n", connfd);

        /* Client Connection State Machine */
        switch ( client->getState() )
        {
            case ClientConnection::Ready_ForRead:
            {
                printf("Client State: Ready_ForRead\n");
                /* read ready client socket */
                if (FD_ISSET(connfd, &read_set))
                {
                    readHandler(client);
                    if (client->getState() == ClientConnection::Request_Parsed)
                    {
                        processHandler(client);
                        /* if the client is closed after processing */
                        if (client->getState() == ClientConnection::Closed)
                        {
                            Socket::Close_Socket(connfd);
                            FD_CLR(connfd, &read_set);
                            delete client;
                            clients.erase(it);
                            continue;
                        }
                        writeHandler(client);
                    }
                }
                break;
            }

            case ClientConnection::Request_Parsed: {
                printf("Client State: Request_Parsed\n");
                break;
            }

            case ClientConnection::Writing_Response: 
            {
                printf("Client State: Writing_Response\n");
                if (FD_ISSET(connfd, &write_set)) 
                {
                    if (!client->getRequest()->isCGIRequest())
                    {
                        processHandler(client);
                        writeHandler(client);
                    }
                    else if (FD_ISSET(client->getResponse()->getCGIout(), &read_set))
                    {
                        /* CGI request : if CGIout is also ready for reading */
                        pipeHandler(client);
                        writeHandler(client);
                    }
                }
                break;
            }

            case ClientConnection::Done_Response: {
                printf("Client State: Done_Response\n");
                break;
            }

            default: {
                break;
            }
        }
        it++;
    }
}