Пример #1
0
// Read and process a packet (blocking)
void *playerc_client_read(playerc_client_t *client)
{
  void* ret_proxy = NULL;
  int ret;

  // In case we're in PULL mode, first request a round of data.

  // This function call here is changed to peek() as if we have data on the internal queue this will return without requesting data
  // but if we don't, it will request and thus will wait until a SYNC message is received, without thisthe code
  //   if peek()
  //       read()
  // can be blocking if there is data on the internal queue (causing peek to return true) but read will wait until there is new data
  // as read_non block only returns 1 on getting a synch message, or if data hasn't been requested
  if(playerc_client_peek(client, 0) < 0)
    return NULL;
  // now wait until we get a sync, or some data if in push mode
  do
  {
    ret = playerc_client_read_nonblock_withproxy(client, &ret_proxy);
    if (ret < 0 || client->sock < 0)
      break;
    if(ret > 0)
      return ret_proxy;
    // if no data is available, then do a peek with infinite timeout...
    // we cant do this first as we may already have data waiting on the internal queue
  } while (playerc_client_internal_peek(client, -1) >= 0);
  return NULL;
}
Пример #2
0
// Main
int main(int argc, char **argv)
{
  playerc_client_t *client;
  rtk_app_t *app;
  mainwnd_t *mainwnd;
  opt_t *opt;
  const char *host;
  int port;
  int i;
  int count;
  double rate;
  char section[256];
  int device_count;
  device_t devices[PLAYER_MAX_DEVICES];
  device_t *device;
  struct timeval tv, tc = {0, 0};
  struct timespec st = {0, (1.0/GUI_UPDATE_RATE) * 1e9};

  printf("PlayerViewer %s\n", PLAYER_VERSION);

  // Initialise rtk lib (after we have read the program options we
  // want).
  rtk_init(&argc, &argv);

  // Register signal handlers
  signal(SIGINT, sig_quit);
  signal(SIGQUIT, sig_quit);

  // Load program options
  opt = opt_init(argc, argv, NULL);
  if (!opt)
  {
    print_usage();
    return -1;
  }

  // Pick out some important program options
  host = opt_get_string(opt, "", "host", NULL);
  if (!host)
    host = opt_get_string(opt, "", "h", "localhost");

  port = opt_get_int(opt, "", "port", -1);
  if (port < 0)
    port = opt_get_int(opt, "", "p", 6665);

  rate = opt_get_double(opt, "", "rate", 5.0);
  if(rate < 0.0)
    rate = 0.0;

  // Connect to the server
  printf("Connecting to [%s:%d]\n", host, port);
  client = playerc_client_create(NULL, host, port);
  if (playerc_client_connect(client) != 0)
  {
    PRINT_ERR1("%s", playerc_error_str());
    print_usage();
    return -1;
  }

  if(rate == 0.0)
  {
    printf("Setting delivery mode to PLAYER_DATAMODE_PUSH\n");
    // Change the server's data delivery mode.
    if (playerc_client_set_replace_rule(client, -1, -1, -1, -1, 0) != 0)
    {
      PRINT_ERR1("%s", playerc_error_str());
      return -1;
    }

    // Change the server's data delivery mode.
    // PLAYERC_DATAMODE_PUSH, PLAYERC_DATAMODE_PULL
    if (playerc_client_datamode(client, PLAYERC_DATAMODE_PUSH) != 0)
    {
      PRINT_ERR1("%s", playerc_error_str());
      return -1;
    }
  }

  // Get the available devices.
  if (playerc_client_get_devlist(client) != 0)
  {
    PRINT_ERR1("%s", playerc_error_str());
    return -1;
  }

  // Create gui
  app = rtk_app_create();

  // Create a window for most of the sensor data
  mainwnd = mainwnd_create(app, host, port);
  if (!mainwnd)
    return -1;

  // Create a list of available devices, with their gui proxies.
  device_count = 0;
  for (i = 0; i < client->devinfo_count; i++)
  {
    device = devices + device_count;

    device->addr = client->devinfos[i].addr;
    device->drivername = strdup(client->devinfos[i].drivername);

    // See if the device should be subscribed immediately.
    snprintf(section, sizeof(section), "%s:%d",
             interf_to_str(device->addr.interf), device->addr.index);
    device->subscribe = opt_get_int(opt, section, "", 0);
    device->subscribe = opt_get_int(opt, section, "subscribe", device->subscribe);
    if (device->addr.index == 0)
    {
      snprintf(section, sizeof(section), "%s",
               interf_to_str(device->addr.interf));
      device->subscribe = opt_get_int(opt, section, "", device->subscribe);
      device->subscribe = opt_get_int(opt, section, "subscribe", device->subscribe);
    }

    // Allow for --position instead of --position2d
    if(device->addr.interf == PLAYER_POSITION2D_CODE)
    {
      snprintf(section, sizeof(section), "%s:%d",
               PLAYER_POSITION2D_STRING, device->addr.index);
      device->subscribe = opt_get_int(opt, section, "", device->subscribe);
      device->subscribe = opt_get_int(opt, section, "subscribe", device->subscribe);
      if (device->addr.index == 0)
      {
        snprintf(section, sizeof(section), "%s", PLAYER_POSITION2D_STRING);
        device->subscribe = opt_get_int(opt, section, "", device->subscribe);
        device->subscribe = opt_get_int(opt, section, "subscribe", device->subscribe);
      }
    }

    // Create the GUI proxy for this device.
    create_proxy(device, opt, mainwnd, client);

    device_count++;
  }

  // Print the list of available devices.
  printf("Available devices: %s:%d\n", host, port);
  for (i = 0; i < device_count; i++)
  {
    device = devices + i;
    snprintf(section, sizeof(section), "%s:%d",
             interf_to_str(device->addr.interf), device->addr.index);
    printf("%-16s %-40s", section, device->drivername);
    if (device->proxy)
    {
      if (device->subscribe)
        printf("subscribed");
      else
        printf("ready");
    }
    else
      printf("unsupported");
    printf("\n");
  }

  // Print out a list of unused options.
  opt_warn_unused(opt);

  // Start the gui; dont run in a separate thread and dont let it do
  // its own updates.
  rtk_app_main_init(app);

  // start out timer if in pull mode
  if(rate > 0.0)
    gettimeofday(&tv, NULL);

  while (!quit)
  {
    // Let gui process messages
    rtk_app_main_loop(app);

    if(rate == 0.0)  // if we're in push mode
    {
      // see if there's data
      count = playerc_client_peek(client, 50);
      if (count < 0)
      {
        PRINT_ERR1("%s", playerc_error_str());
        break;
      }
      if (count > 0)
      {
        /*proxy = */playerc_client_read_nonblock(client);
      }
    }
    else // we're in pull mode
    {
      // we only want to request new data at the target rate
      gettimeofday(&tc, NULL);
      if(((tc.tv_sec - tv.tv_sec) + (tc.tv_usec - tv.tv_usec)/1e6) > 1.0/rate)
      {
        tv = tc;
        // this requests a round of data from the server to be read
        playerc_client_requestdata(client);
        playerc_client_read_nonblock(client);
       }
       else
       {
        // sleep for the minimum time we can, so we don't use up too much
        // processor
        nanosleep(&st, NULL);
       }
    }


    // Update the devices
    for (i = 0; i < device_count; i++)
    {
      device = devices + i;
      if(device->proxy)
        (*(device->fnupdate)) (device->proxy);
    }
    // Update the main window
    if (mainwnd_update(mainwnd) != 0)
      break;
  }

  // Stop the gui
  rtk_app_main_term(app);

  // Destroy devices
  for (i = 0; i < device_count; i++)
  {
    device = devices + i;
    if (device->proxy)
      (*(device->fndestroy)) (device->proxy);
    free(device->drivername);
  }

  // Disconnect from server
  if (playerc_client_disconnect(client) != 0)
  {
    PRINT_ERR1("%s", playerc_error_str());
    return -1;
  }
  playerc_client_destroy(client);

  // For some reason, either of the following calls makes the program
  // segfault on exit.  I haven't figured out why, so I'm commenting them out.  - BPG

  // Destroy the windows
  //mainwnd_destroy(mainwnd);

  // Destroy the gui
  //rtk_app_destroy(app);

  opt_term(opt);

  return 0;
}
Пример #3
0
int main(int argc, const char **argv)
{
	int i;
	playerc_client_t *client;
	nsdnet_t *device;

	// Takes a parameter for the index.
	int index = 0;
	if (argc < 2)
	{
		printf("Needs one parameter.\n");
		return 0;
	}
	else
	{
		index = atoi(argv[1]);
		printf("Using index %d\n", index);
	}

	// Create a client and connect it to the server.
	client = playerc_client_create(NULL, "localhost", 6665);
	if (0 != playerc_client_connect(client))
	{
		printf("Could not connect\n");
		return -1;
	}

	// Load the plugin interface
	if (playerc_add_xdr_ftable (player_plugininterf_gettable (), 0) < 0)
		printf("Could not add xdr functions\n");

	// Create and subscribe to a device using the interface.
	device = nsdnet_create(client, index);
	if (nsdnet_subscribe(device, PLAYER_OPEN_MODE) != 0)
	{
		printf("Could not subscribe\n");
		return -1;
	}

	// Get our clientid
	if (nsdnet_property_get(device, "self.id"))
	{
		printf("Failed to get registered device id...\n");
		return -1;
	}

	// Print out what we get...
	printf("Client id: %s\n", device->propval);

	// Get the list of clients
	if (nsdnet_get_listclients(device))
	{
		printf("Failed to get list of clients...\n");
		return -1;
	}

	// Print out what we get...
	printf("Clients: ");
	for (i = 0; i < device->listclients_count; i++)
	{
		printf("%s ", device->listclients[i]);
	}
	printf("\n");

	// Endless loop...
	for (i = 0; i < 1000; i++)
	{
		if (playerc_client_peek(client, 0) == 1)
		{
			playerc_client_read(client);
			while (device->queue_head != device->queue_tail)
			{
				nsdmsg_t *msg; int err;
				if ((err = nsdnet_receive_message(device, &msg)))
				{
					if (err < 0)
						printf("ERROR\n");
					else
						printf("%s: %s [%d]\n", msg->clientid, msg->msg, i);
				}
			}
			usleep(0);
		}
		else
		{
			printf("Sending Hello World\n");
			if (nsdnet_send_message(device, NULL, 12, "Hello World"))
			{
				printf("Could not broadcast 'Hello World'\n");
				return -1;
			}
			usleep(100000);
		}
	}

	// Shutdown
	nsdnet_unsubscribe(device);
	nsdnet_destroy(device);
	playerc_client_disconnect(client);
	playerc_client_destroy(client);

	return 0;
}