d2a702b44c098c8cae5a0e6deb82f36b2e9360b8
[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 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   connection = g_socket_client_connect_to_host (client,
101                                                 argv[1],
102                                                 7777,
103                                                 cancellable, &error);
104   if (connection == NULL)
105     {
106       g_printerr ("%s can't connect: %s\n", argv[0], error->message);
107       return 1;
108     }
109
110   address = g_socket_connection_get_remote_address (connection, &error);
111   if (!address)
112     {
113       g_printerr ("Error getting remote address: %s\n",
114                   error->message);
115       return 1;
116     }
117   g_print ("Connected to address: %s\n",
118            socket_address_to_string (address));
119   g_object_unref (address);
120
121   if (graceful)
122     g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);
123
124   out = g_io_stream_get_output_stream (G_IO_STREAM (connection));
125
126   while (fgets(buffer, sizeof (buffer), stdin) != NULL)
127     {
128       if (!g_output_stream_write_all (out, buffer, strlen (buffer),
129                                       NULL, cancellable, &error))
130         {
131           g_warning ("send error: %s\n",  error->message);
132           g_error_free (error);
133           error = NULL;
134         }
135     }
136
137   g_print ("closing stream\n");
138   if (async)
139     {
140       GAsyncResult *res;
141       g_io_stream_close_async (G_IO_STREAM (connection),
142                                0, cancellable, async_cb, &res);
143       g_main_loop_run (loop);
144       if (!g_io_stream_close_finish (G_IO_STREAM (connection),
145                                      res, &error))
146         {
147           g_object_unref (res);
148           g_warning ("close error: %s\n",  error->message);
149           return 1;
150         }
151       g_object_unref (res);
152     }
153   else
154     {
155       if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
156         {
157           g_warning ("close error: %s\n",  error->message);
158           return 1;
159         }
160     }
161
162   return 0;
163 }