Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Examples / connect.c
1 /*
2  *  WINGs connect.c: example how to create a network client using WMConnection
3  *
4  *  Copyright (c) 1999-2003 Dan Pascu
5  *
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include <WINGs/WINGs.h>
14
15 static int initialized = 0;
16
17 static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr);
18
19 static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr);
20
21 static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr);
22
23 static ConnectionDelegate socketDelegate = {
24         NULL,                   /* data */
25         NULL,                   /* canResumeSending */
26         NULL,                   /* didCatchException */
27         connectionDidDie,       /* didDie */
28         didInitialize,          /* didInitialize */
29         didReceiveInput,        /* didReceiveInput */
30         NULL                    /* didTimeout */
31 };
32
33 void wAbort(Bool foo)
34 {
35         exit(1);
36 }
37
38 static char *getMessage(WMConnection * cPtr)
39 {
40         char *buffer;
41         WMData *aData;
42         int length;
43
44         aData = WMGetConnectionAvailableData(cPtr);
45         if (!aData)
46                 return NULL;
47         if ((length = WMGetDataLength(aData)) == 0) {
48                 WMReleaseData(aData);
49                 return NULL;
50         }
51
52         buffer = (char *)wmalloc(length + 1);
53         WMGetDataBytes(aData, buffer);
54         buffer[length] = '\0';
55         WMReleaseData(aData);
56
57         return buffer;
58 }
59
60 static void inputHandler(int fd, int mask, void *clientData)
61 {
62         WMConnection *cPtr = (WMConnection *) clientData;
63         WMData *aData;
64         char buf[4096];
65         int n;
66
67         if (!initialized)
68                 return;
69
70         n = read(fd, buf, 4096);
71         if (n > 0) {
72                 aData = WMCreateDataWithBytes(buf, n);
73                 WMSendConnectionData(cPtr, aData);
74                 WMReleaseData(aData);
75         }
76 }
77
78 static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr)
79 {
80         char *buffer;
81
82         buffer = getMessage(cPtr);
83         if (!buffer) {
84                 fprintf(stderr, "Connection closed by peer.\n");
85                 exit(0);
86         }
87
88         printf("%s", buffer);
89
90         wfree(buffer);
91 }
92
93 static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr)
94 {
95         WMCloseConnection(cPtr);
96
97         fprintf(stderr, "Connection closed by peer.\n");
98         exit(0);
99 }
100
101 static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr)
102 {
103         int state = WMGetConnectionState(cPtr);
104         WMHost *host;
105
106         if (state == WCConnected) {
107                 host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
108                 fprintf(stderr, "connected to '%s:%s'\n",
109                         host ? WMGetHostName(host) : WMGetConnectionAddress(cPtr), WMGetConnectionService(cPtr));
110                 initialized = 1;
111                 if (host)
112                         WMReleaseHost(host);
113                 return;
114         } else {
115                 wsyserrorwithcode(WCErrorCode, "Unable to connect");
116                 exit(1);
117         }
118 }
119
120 int main(int argc, char **argv)
121 {
122         char *ProgName, *host, *port;
123         int i;
124         WMConnection *sPtr;
125
126         wsetabort(wAbort);
127
128         WMInitializeApplication("connect", &argc, argv);
129
130         ProgName = strrchr(argv[0], '/');
131         if (!ProgName)
132                 ProgName = argv[0];
133         else
134                 ProgName++;
135
136         host = NULL;
137         port = "34567";
138
139         if (argc > 1) {
140                 for (i = 1; i < argc; i++) {
141                         if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
142                                 printf("usage: %s [host [port]]\n\n", ProgName);
143                                 exit(0);
144                         } else {
145                                 if (!host)
146                                         host = argv[i];
147                                 else
148                                         port = argv[i];
149                         }
150                 }
151         }
152
153         printf("Trying to make connection to '%s:%s'\n", host ? host : "localhost", port);
154
155         sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
156         if (!sPtr) {
157                 wfatal("could not create connection. exiting");
158                 exit(1);
159         }
160
161         WMSetConnectionDelegate(sPtr, &socketDelegate);
162
163         /* watch what user types and send it over the connection */
164         WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
165
166         while (1) {
167                 WHandleEvents();
168         }
169
170         return 0;
171
172 }