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