int main (int argc, char** argv) { gchar* hostname; gint port; GMainLoop* main_loop; GConn* conn; gnet_init (); /* Parse args */ if (argc != 4) { g_print ("usage: %s <server> <port> <output-directory>\n", argv[0]); exit(EXIT_FAILURE); } hostname = argv[1]; port = atoi(argv[2]); out_dir = argv[3]; check_dir (out_dir); update_filename (); /* Create the main loop */ main_loop = g_main_new(FALSE); /* Create connection object */ conn = gnet_conn_new (hostname, port, ob_conn_func, NULL); g_assert (conn); /* Connect */ gnet_conn_connect (conn); gnet_conn_set_watch_error (conn, TRUE); gnet_conn_timeout (conn, 30000); /* 30 second timeout */ //open the xml file init_xml (); atexit (flush_xml); /* Start the main loop */ g_main_run (main_loop); exit (EXIT_SUCCESS); return 0; }
int main(int argc, char** argv) { gchar* hostname; gint port; GMainLoop* main_loop; GConn* conn; GIOChannel* in; gnet_init (); /* Parse args */ if (argc != 3) { g_print ("usage: %s <server> <port>\n", argv[0]); exit(EXIT_FAILURE); } hostname = argv[1]; port = atoi(argv[2]); /* Create the main loop */ main_loop = g_main_new(FALSE); /* Create connection object */ conn = gnet_conn_new (hostname, port, ob_conn_func, NULL); g_assert (conn); /* Connect */ gnet_conn_connect (conn); gnet_conn_set_watch_error (conn, TRUE); gnet_conn_timeout (conn, 30000); /* 30 second timeout */ /* Read from stdin */ in = g_io_channel_unix_new (fileno(stdin)); g_io_add_watch(in, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, ob_in_iofunc, conn); /* Start the main loop */ g_main_run (main_loop); exit (EXIT_SUCCESS); return 0; }
/* Handle GConn events */ static void ob_conn_func (GConn* conn, GConnEvent* event, gpointer user_data) { switch (event->type) { case GNET_CONN_CONNECT: { gnet_conn_timeout (conn, 0); /* reset timeout */ printf ("Connected to server\n"); gnet_conn_read (conn); break; } case GNET_CONN_READ: { /* Write line out */ net_event_header_t hdr; light_temp_event_t ev; gnet_unpack ("!iiliii", event->buffer, sizeof (hdr) + sizeof (ev), &hdr.size, &hdr.event_type, &hdr.time_seconds, &ev.node_id, &ev.light, &ev.temp); printf ("Got an event: %d %d %ld %d %d %d\n", hdr.size, hdr.event_type, hdr.time_seconds, ev.node_id, ev.light, ev.temp); xml_write_node (ev.node_id, ev.light, ev.temp, hdr.time_seconds); /* Check if done */ if (read_eof) { exit (EXIT_SUCCESS); } /* Read another line */ gnet_conn_read (conn); break; } case GNET_CONN_WRITE: { /* do nothing */ break; } case GNET_CONN_CLOSE: { gnet_conn_delete (conn); exit (EXIT_SUCCESS); break; } case GNET_CONN_TIMEOUT: { gnet_conn_delete (conn); fprintf (stderr, "Connection timeout\n"); exit (EXIT_FAILURE); break; } case GNET_CONN_ERROR: { gnet_conn_delete (conn); fprintf (stderr, "Connection failure\n"); exit (EXIT_FAILURE); break; } default: g_assert_not_reached (); } }
/* Handle GConn events */ static void ob_conn_func (GConn* conn, GConnEvent* event, gpointer user_data) { switch (event->type) { case GNET_CONN_CONNECT: { gnet_conn_timeout (conn, 0); /* reset timeout */ gnet_conn_readline (conn); break; } case GNET_CONN_READ: { /* Write line out */ event->buffer[event->length - 1] = '\n'; if (fwrite (event->buffer, event->length, 1, stdout) != 1) { fprintf (stderr, "Error: fwrite to stdout failed: %s\n", g_strerror (errno)); } /* Check if done */ lines_pending--; if (lines_pending == 0 && read_eof) exit (EXIT_SUCCESS); /* Read another line */ gnet_conn_readline (conn); break; } case GNET_CONN_WRITE: { ; /* do nothing */ break; } case GNET_CONN_CLOSE: { gnet_conn_delete (conn); exit (EXIT_SUCCESS); break; } case GNET_CONN_TIMEOUT: { gnet_conn_delete (conn); fprintf (stderr, "Connection timeout\n"); exit (EXIT_FAILURE); break; } case GNET_CONN_ERROR: { gnet_conn_delete (conn); fprintf (stderr, "Connection failure\n"); exit (EXIT_FAILURE); break; } default: g_assert_not_reached (); } }