1 /* Unix Socket Echo client
2  * Copyright (C) 2001  Mark Ferlatte
3  * Adapted from echoclient.c, Copyright (C) 2000  David Helder
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  */
20 
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <gnet.h>
27 
28 typedef enum { NORMAL, ASYNC} ClientType;
29 
30 
31 static void usage (int status);
32 
33 static void normal_echoclient (gchar* path, gboolean abstract);
34 static void async_echoclient (gchar* path, gboolean abstract);
35 
36 int
main(int argc,char ** argv)37 main(int argc, char** argv)
38 {
39   ClientType client_type = NORMAL;
40   gboolean abstract = FALSE;
41 
42   gnet_init ();
43 
44   if (argc < 2)
45     usage(EXIT_FAILURE);
46   if (argc > 2) {
47     if (strcmp(argv[1], "--async") == 0 || strcmp(argv[2], "--async") == 0)
48       client_type = ASYNC;
49     if (strcmp(argv[1], "--abstract") == 0 ||
50             strcmp(argv[2], "--abstract") == 0)
51       abstract = TRUE;
52   }
53 
54   switch (client_type) {
55   case NORMAL:
56     /* output would confuse our testing scripts */
57     /* g_print ("Normal echo client running\n"); */
58     normal_echoclient(argv[argc - 1], abstract);
59     break;
60   case ASYNC:
61     g_print ("Asynchronous echo client running (UNFINISHED)\n");
62     async_echoclient(argv[argc - 1], abstract);
63     break;
64   default:
65     g_assert_not_reached();
66   }
67 
68   return 0;
69 }
70 
71 static void
usage(int status)72 usage (int status)
73 {
74 	g_print ("usage: echoclient [(nothing)|--async| --abstract] <path>\n");
75 	exit(status);
76 }
77 
78 
79 static void
normal_echoclient(gchar * path,gboolean abstract)80 normal_echoclient(gchar* path, gboolean abstract)
81 {
82   GUnixSocket *socket = NULL;
83   GIOChannel* iochannel = NULL;
84   gchar buffer[1024];
85   gsize n;
86   GIOError e = G_IO_ERROR_NONE;
87 
88   g_assert(path != NULL);
89 
90   /* Create the socket */
91   if (abstract)
92     socket = gnet_unix_socket_new_abstract(path);
93   else
94     socket = gnet_unix_socket_new(path);
95   g_assert (socket != NULL);
96 
97   /* Get the IOChannel */
98   iochannel = gnet_unix_socket_get_io_channel(socket);
99   g_assert (iochannel != NULL);
100 
101   while (fgets(buffer, sizeof(buffer), stdin) != 0) {
102     n = strlen(buffer);
103     e = gnet_io_channel_writen(iochannel, buffer, n, &n);
104     if (e != G_IO_ERROR_NONE)
105       break;
106     e = gnet_io_channel_readn(iochannel, buffer, n, &n);
107     if (e != G_IO_ERROR_NONE)
108       break;
109     fwrite(buffer, n, 1, stdout);
110   }
111 
112   if (e != G_IO_ERROR_NONE)
113     g_print ("IO error (%d)\n", e);
114   return;
115 }
116 
117 static void
async_echoclient(gchar * path,gboolean abstract)118 async_echoclient(gchar *path, gboolean abstract)
119 {
120   g_print("Sorry, there is no async echoclient yet.\n");
121   return;
122 }
123