Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / send-data.c
1 #include <gio/gio.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 GMainLoop *loop;
6
7 int cancel_timeout = 0;
8 int io_timeout = 0;
9 gboolean async = FALSE;
10 gboolean graceful = FALSE;
11 static GOptionEntry cmd_entries[] = {
12   {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
13    "Cancel any op after the specified amount of seconds", NULL},
14   {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
15    "Use async ops", NULL},
16   {"graceful-disconnect", 'g', 0, G_OPTION_ARG_NONE, &graceful,
17    "Use graceful disconnect", NULL},
18   {"timeout", 't', 0, G_OPTION_ARG_INT, &io_timeout,
19    "Time out socket I/O after the specified number of seconds", NULL},
20   {NULL}
21 };
22
23 static gpointer
24 cancel_thread (gpointer data)
25 {
26   GCancellable *cancellable = data;
27
28   g_usleep (1000*1000*cancel_timeout);
29   g_print ("Cancelling\n");
30   g_cancellable_cancel (cancellable);
31   return NULL;
32 }
33
34 static char *
35 socket_address_to_string (GSocketAddress *address)
36 {
37   GInetAddress *inet_address;
38   char *str, *res;
39   int port;
40
41   inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
42   str = g_inet_address_to_string (inet_address);
43   port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
44   res = g_strdup_printf ("%s:%d", str, port);
45   g_free (str);
46   return res;
47 }
48
49 static void
50 async_cb (GObject *source_object,
51           GAsyncResult *res,
52           gpointer user_data)
53 {
54   GAsyncResult **resp = user_data;
55   *resp = g_object_ref (res);
56   g_main_loop_quit (loop);
57 }
58
59
60 int
61 main (int argc, char *argv[])
62 {
63   GOptionContext *context;
64   GSocketClient *client;
65   GSocketConnection *connection;
66   GSocketAddress *address;
67   GCancellable *cancellable;
68   GOutputStream *out;
69   GError *error = NULL;
70   char buffer[1000];
71
72   g_type_init ();
73   g_thread_init (NULL);
74
75   context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
76   g_option_context_add_main_entries (context, cmd_entries, NULL);
77   if (!g_option_context_parse (context, &argc, &argv, &error))
78     {
79       g_printerr ("%s: %s\n", argv[0], error->message);
80       return 1;
81     }
82
83   if (argc != 2)
84     {
85       g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
86       return 1;
87     }
88
89   if (async)
90     loop = g_main_loop_new (NULL, FALSE);
91
92   if (cancel_timeout)
93     {
94       cancellable = g_cancellable_new ();
95       g_thread_create (cancel_thread, cancellable, FALSE, NULL);
96     }
97   else
98     {
99       cancellable = NULL;
100     }
101
102   client = g_socket_client_new ();
103   if (io_timeout)
104     g_socket_client_set_timeout (client, io_timeout);
105
106   if (async)
107     {
108       GAsyncResult *res;
109       g_socket_client_connect_to_host_async (client, argv[1], 7777,
110                                              cancellable, async_cb, &res);
111       g_main_loop_run (loop);
112       connection = g_socket_client_connect_to_host_finish (client, res, &error);
113       g_object_unref (res);
114     }
115   else
116     {
117       connection = g_socket_client_connect_to_host (client,
118                                                     argv[1],
119                                                     7777,
120                                                     cancellable, &error);
121     }
122   if (connection == NULL)
123     {
124       g_printerr ("%s can't connect: %s\n", argv[0], error->message);
125       return 1;
126     }
127   g_object_unref (client);
128
129   address = g_socket_connection_get_remote_address (connection, &error);
130   if (!address)
131     {
132       g_printerr ("Error getting remote address: %s\n",
133                   error->message);
134       return 1;
135     }
136   g_print ("Connected to address: %s\n",
137            socket_address_to_string (address));
138   g_object_unref (address);
139
140   if (graceful)
141     g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
142
143   out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
144
145   while (fgets(buffer, sizeof (buffer), stdin) != NULL)
146     {
147       /* FIXME if (async) */
148       if (!g_output_stream_write_all (out, buffer, strlen (buffer),
149                                       NULL, cancellable, &error))
150         {
151           g_warning ("send error: %s\n",  error->message);
152           g_error_free (error);
153           error = NULL;
154         }
155     }
156
157   g_print ("closing stream\n");
158   if (async)
159     {
160       GAsyncResult *res;
161       g_io_stream_close_async (G_IO_STREAM (connection),
162                                0, cancellable, async_cb, &res);
163       g_main_loop_run (loop);
164       if (!g_io_stream_close_finish (G_IO_STREAM (connection),
165                                      res, &error))
166         {
167           g_object_unref (res);
168           g_warning ("close error: %s\n",  error->message);
169           return 1;
170         }
171       g_object_unref (res);
172     }
173   else
174     {
175       if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
176         {
177           g_warning ("close error: %s\n",  error->message);
178           return 1;
179         }
180     }
181
182   g_object_unref (connection);
183
184   return 0;
185 }