Ejemplo n.º 1
0
int main(int argc, char** argv) {
  int fd = dl_connect(3000);
  if (fd < 0) {
    return -1;
  }

  dl_t dl = dl_new();
  dl->send_packet = my_send_packet;
  dl->on_attach = my_on_attach;
  dl->on_detach = my_on_detach;
  my_t my = (my_t)malloc(sizeof(struct my_struct));
  my->fd = fd;
  dl->state = my;

  if (dl->start(dl)) {
    return -1;
  }

#define BUF_LEN 1024
  char buf[BUF_LEN];
  while (1) {
    ssize_t read_bytes = recv(fd, buf, BUF_LEN, 0);
    if (read_bytes < 0) {
      if (errno == EAGAIN) {
        continue;
      }
      break;
    }
    if (dl->on_recv(dl, buf, read_bytes)) {
      break;
    }
  }
  close(fd);
  free(dl->state);
  dl_free(dl);
  return 0;
}
int iwdpm_subscribe(iwdp_t iwdp) {
  return dl_connect(-1);
}
Ejemplo n.º 3
0
int
main (int argc, char **argv)
{
  DLPacket dlpack;
  char packetdata[MAXPACKETSIZE];
  char timestr[50];
  char *infobuf = 0;
  int infolen;
  int endflag = 0;
  
#ifndef WIN32
  /* Signal handling, use POSIX calls with standardized semantics */
  struct sigaction sa;
  
  sigemptyset (&sa.sa_mask);
  sa.sa_flags   = SA_RESTART;
  
  sa.sa_handler = term_handler;
  sigaction (SIGINT, &sa, NULL);
  sigaction (SIGQUIT, &sa, NULL);
  sigaction (SIGTERM, &sa, NULL);
  
  sa.sa_handler = SIG_IGN;
  sigaction (SIGHUP, &sa, NULL);
  sigaction (SIGPIPE, &sa, NULL);
#endif
  
  /* Process given parameters (command line and parameter file) */
  if ( parameter_proc (argc, argv) < 0 )
    {
      fprintf (stderr, "Parameter processing failed\n\n");
      fprintf (stderr, "Try '-h' for detailed help\n");
      return -1;
    }
  
  /* Connect to server */
  if ( dl_connect (dlconn) < 0 )
    {
      fprintf (stderr, "Error connecting to server\n");
      return -1;
    }
  
  /* Reposition connection */
  if ( dlconn->pktid > 0 )
    {
      if ( dl_position (dlconn, dlconn->pktid, dlconn->pkttime) < 0 )
	return -1;
    }
  
  /* Send match pattern if supplied */
  if ( matchpattern )
    {
      if ( dl_match (dlconn, matchpattern) < 0 )
	return -1;
    }
  
  /* Send reject pattern if supplied */
  if ( rejectpattern )
    {
      if ( dl_reject (dlconn, rejectpattern) < 0 )
	return -1;
    }

  /* Request INFO and print returned XML */
  if ( infotype )
    {
      if ( (infolen = dl_getinfo (dlconn, infotype, matchpattern, &infobuf, 0)) < 0 )
	{
	  dl_log (2, 0, "Problem requesting INFO from server\n");
	  return -1;
	}
      
      printf ("%.*s\n", infolen, infobuf);
      
      if ( infobuf )
	free (infobuf);
    }
  /* Otherwise collect packets in STREAMing mode */
  else
    {
      /* Collect packets in streaming mode */
      while ( dl_collect (dlconn, &dlpack, packetdata, sizeof(packetdata), endflag) == DLPACKET )
	{
	  dl_dltime2seedtimestr (dlpack.datastart, timestr, 1);
	  
	  dl_log (0, 0, "Received %s (%lld), %s, %d\n",
		  dlpack.streamid, dlpack.pktid, timestr, dlpack.datasize);
	}
    }
  
  /* Make sure everything is shut down and save the state file */
  if ( dlconn->link != -1 )
    dl_disconnect (dlconn);
  
  /* Save the state file */
  if ( statefile )
    dl_savestate (dlconn, statefile);

  if ( dlconn )
    dl_freedlcp (dlconn);
  
  return 0;
}  /* End of main() */