Exemplo n.º 1
0
static void *
http_eventloop_proc(void *arg)
{
  task_t *request;    
  
  proxy_debug_2(DBG_HTTP, "(HTTP): Accepting connections");

  while (1) {
    newconn = saccept(gSock);
    if (newconn == -1) {
      MON_SEND_2(MON_ERR,"(HTTP): Accept connection: %s", strerror(errno));
      continue;
    }
    /* Put the connection on the queue. */

    proxy_debug_3(DBG_HTTP, "(HTTP): Queueing connection %d", n_httpreqs);
    new_task(&request);
    SET_TASK_ID(request,n_httpreqs);
    n_httpreqs++;
    SET_TASK_GO_PROC(request, http_go_proc);
    /*
     *  Set parent, child_index, and metadata to null, to indicate this request
     *  is direct from client. 
     */
    SET_TASK_PARENT(request,0);
    SET_TASK_CHILD_INDEX(request,0);
    SET_TASK_METADATA(request,NULL);
    /*
     *  Set task_data to the file descriptor for this socket.  When a
     *  worker thread picks up this task, it will change task_data to a
     *  pointer to the Request struct describing this request.
     *  That structure includs a field for the file descriptor (among
     *  other thigns), but we can't fill in the structure here because
     *  the structure is managed per worker thread.
     */
    SET_TASK_DATA(request, newconn);
    assert(dispatch(request) == 0);
  }
}
Exemplo n.º 2
0
static void *harvest_http_eventloop_proc(void *arg)
{
  int newconn;
  task_t *request;    

  proxy_debug_2(DBG_HTTP, "Accepting connections");

  while (1) {
    newconn = saccept(gSock2);
    if (newconn == -1) {
      proxy_errlog_2("Accept connection: %s", strerror(errno));
      continue;
    }
    /* Put the connection on the queue. */

    new_task(&request);
    SET_TASK_ID(request,nreqs2);
    SET_TASK_GO_PROC(request, harvest_http_go_proc);
    SET_TASK_DATA(request, newconn);
    assert(dispatch(request) == 0);
    nreqs2++;
  }
}
int main(int argc, char **argv) {
	char buf[1000];
	int socket_listen;
	int socket_talk;
	int dummy, len;
	threadpool pool;
	struct timeval then, now, diff;
	if (argc != 2) {
		fprintf(stderr, "(SERVER): Invoke as  './server socknum'\n");
		fprintf(stderr, "(SERVER): for example, './server 4434'\n");
		exit(-1);
	}

	/*
	 * Set up the 'listening socket'.  This establishes a network
	 * IP_address:port_number that other programs can connect with.
	 */
	pool = create_threadpool(POOL_SIZE,NULL);
	socket_listen = setup_listen(argv[1]);

	/*
	 * Here's the main loop of our program.  Inside the loop, the
	 * one thread in the server performs the following steps:
	 *
	 *  1) Wait on the socket for a new connection to arrive.  This
	 *     is done using the "accept" library call.  The return value
	 *     of "accept" is a file descriptor for a new data socket associated
	 *     with the new connection.  The 'listening socket' still exists,
	 *     so more connections can be made to it later.
	 *
	 *  2) Read a request off of the listening socket.  Requests
	 *     are, by definition, REQUEST_SIZE bytes long.
	 *
	 *  3) Process the request.
	 *
	 *  4) Write a response back to the client.
	 *
	 *  5) Close the data socket associated with the connection
	 */
	gettimeofday(&then, NULL);
	while (1) {

		socket_talk = saccept(socket_listen);  // step 1
		if (socket_talk < 0) {
			fprintf(stderr, "An error occured in the server; a connection\n");
			fprintf(stderr, "failed because of ");
			perror("");
			exit(1);
		}

		dispatch(pool, request_client, (void *) socket_talk);
		count_dispatches++;
		if (count_dispatches == 10) {
			double seconds;  //elapsed time in seconds between then and now
			gettimeofday(&now, NULL);
			timersub(&now, &then, &diff);
			seconds = 1000000*diff.tv_sec + diff.tv_usec;

			fprintf(stdout, "Loops: %6d  Threads: %2d Dispatches/sec: %10.3f\n",
					NUM_LOOPS, POOL_SIZE, seconds/1000);
		}

	}
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
  cache_perf tc;
  unsigned long long cache_size, num_entries;
  unsigned long int report_interval;
  int   accept_socket, dsocket, cntr = 0;
  lf_entry   next_entry;

  if (argc != 5)
    usage();

  if (sscanf(argv[2], "%llu", &cache_size) != 1)
    usage();

  if (sscanf(argv[3], "%llu", &num_entries) != 1)
    usage();

  if (sscanf(argv[4], "%lu", &report_interval) != 1)
    usage();

  accept_socket = slisten(argv[1]);
  if (accept_socket == -1)
    usage();

  fprintf(stderr, "Starting up cache sim server, params:\n");
  fprintf(stderr, "  port %s, cachesize %llu, num_entries %llu, ri %lu\n",
	  argv[1], cache_size, num_entries, report_interval);

  if (!init_cache_el(cache_size, num_entries, &tc)) {
    fprintf(stderr, "initialize_cache failed.\n");
    usage();
  }

  if ( (dsocket = saccept(accept_socket)) < 0) {
    fprintf(stderr, "accept failed.\n");
    usage();
  }

  while(1) {
    int gne_res;

    gne_res = lf_get_next_entry(dsocket, &next_entry, 3);
    if ( (gne_res != 0) && (gne_res != 1)) {
      fprintf(stderr, "get_next_entry failed.  All done!\n");
      exit(0);
    }

    process_log_entry(&tc, &next_entry);
    cntr++;
    if (gne_res == 1) {
      fprintf(stderr, "All done!\n");
      exit(0);
    }

    if ( (cntr % report_interval) == 0)
      dump_cache_stats(&tc, stdout);

    free(next_entry.url);
  }

  exit(0);
}