示例#1
0
文件: pscp.c 项目: JonBau/pscom
void doServer(void)
{
    int forward_id, numClients;
    ps_send_info_t sinfo;
    FILE *input = NULL;
    PSnodes_ID_t node;
    PSP_PortH_t porth;
    char *buf, *buf2;

    buf = malloc(arg_maxmsize);
    if (!buf) { perror("malloc(buf)"); exit(1); }
    buf2 = malloc(arg_maxmsize);
    if (!buf2) { perror("malloc(buf2)"); exit(1); }

    input = stdin;
    if (arg_ifile) {
	input = fopen(arg_ifile, "r");
	if (!input) {
	    fprintf(stderr, "Cant open file '%s' for reading : %s\n",
		    arg_ifile, strerror(errno));
	    exit(1);
	}
    } else if (arg_icmd) {
	input = popen(arg_icmd, "r");
	if (!input) {
	    fprintf(stderr, "Cant start input command '%s' : %s\n",
		    arg_icmd, strerror(errno));
	    exit(1);
	}
    }

    porth = start_server();

    if (arg_manual) {
	numClients = arg_manual;
    } else {
	int clientRank = 1;

	if (arg_hosts) {
	    nodeList = getNLFromHosts(arg_hosts);
	} else if (arg_nodes) {
	    nodeList = getNLFromNodes(arg_nodes);
	} else {
	    nodeList = getNLFromNodes("all");
	}

	if (!nodeList && !arg_manual) {
	    fprintf(stderr, "Unknown clients\n");
	    exit(1);
	}

	/* Start clients */
	for (node=0; node<PSC_getNrOfNodes(); node++) {
	    if (node == PSC_getMyID()) continue;
	    if (nodeList[node]) {
		int ret = PSE_spawnAdmin(node, clientRank,
					 rem_argc, rem_argv, 0);
		if (!ret) clientRank++;
	    }
	}
	numClients = clientRank - 1;
	if (arg_verbose)
	    fprintf(stderr, "Distribute to %d clients\n", numClients);
    }

    forward_id = assign_clients(porth, numClients);
    PSP_StopListen(porth);
    ps_send_info_init(&sinfo, porth, forward_id);

    stat_time_start = getusec();
    // read from stdin, forward to forward_id
    while (1) {
	int len;
	char *tmp;

	len = read(fileno(input), buf, arg_maxmsize);
	if (len <= 0) break;

	ps_send(&sinfo, buf, len);
	// swap buffers (ps_send use PSP_ISend. We can read more
	// data, while we transmit the old data.)
	tmp = buf; buf = buf2; buf2 = tmp;

	if (timer_called) {
	    print_stat(arg_cp ? 1 : 0);
	    timer_called = 0;
	}
    }

    if (arg_ifile) {
	fclose(input);
    } else if (arg_icmd) {
	pclose(input);
    }

    // Send eof:
    ps_send(&sinfo, NULL, 0);
    ps_send_close(&sinfo);

    free(buf);
    free(buf2);
}
int	main(int ac, char **av)
{
  // doit absolument etre apelle avant d'autres appels a la lib
  init_nettool();
  if (ac > 1) // server mode
    {

      // set handlers
      assign_newclient(process_newclient, NULL);
      assign_deadclient(process_deadclient, NULL);
      assign_clients(process_clients, NULL);

      // open listen connection
      if (init_server_connection(PORT))
	return (1);

      // check messages
      while (1)
	check_select(MAXSELECT);

      // close connection
      close_server_connection();
      close_connection();
    }
  else // client mode
    {
      // connect to server
      if (init_connection("localhost", PORT))
	return (1);

      assign_deadclient(process_drop, NULL);

      printf("step 1\n");fflush(stdout); fflush(stdout);
      // stock messages to send
      stock_remote_msg(WELCOME, strlen("tata"), (void*)"tata");
      printf("step 2\n");fflush(stdout); fflush(stdout);
      stock_remote_msg(HELLOWORLD, 0, NULL);
      printf("step 3\n");fflush(stdout); fflush(stdout);
      stock_remote_msg(HELLOWORLD, 0, NULL);
      printf("step 4\n");fflush(stdout); fflush(stdout);
      stock_remote_msg(HELLOWORLD, 0, NULL);
      printf("step 5\n");fflush(stdout); fflush(stdout);
      stock_remote_msg(HELLOWORLD, 0, NULL);
      printf("step 5\n");fflush(stdout); fflush(stdout);

      printf("step 7\n");fflush(stdout); fflush(stdout);
      // send messages
      while (1)
	check_select(MAXSELECT);

      printf("step 8\n");fflush(stdout); fflush(stdout);
      // close connection
      close_connection();
      printf("step 9\n");fflush(stdout); fflush(stdout);
    }
  // free memory, and close connections
  nettool_quit();
  if (players)
    free(players);
  return (0);
}