Added an example of how to create a network client using WMConnection
[wmaker-crm.git] / WINGs / connect.c
blob4202e090bab8a1f0ac35058b588d0528a7ffde6c
1 /*
2 * WINGs connect.c: example how to create a netwrok client using WMConnection
3 *
4 * Copyright (c) 1999 Dan Pascu
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
26 #include "WINGs.h"
30 static int initialized = 0;
34 static void didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr);
36 static void connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr);
38 static void didInitialize(ConnectionDelegate *self, WMConnection *cPtr);
42 static ConnectionDelegate socketDelegate = {
43 NULL, /* data */
44 NULL, /* didCatchException */
45 connectionDidDie, /* didDie */
46 didInitialize, /* didInitialize */
47 didReceiveInput, /* didReceiveInput */
48 NULL /* didTimeout */
53 void
54 wAbort(Bool foo) /*FOLD00*/
56 exit(1);
60 static void
61 printHelp(char *progname) /*FOLD00*/
63 printf("usage: %s [host [port]]\n\n", progname);
64 printf(" --help print this message\n");
68 static char*
69 getMessage(WMConnection *cPtr)
71 char *buffer;
72 WMData *aData;
73 int length;
75 aData = WMGetConnectionAvailableData(cPtr);
76 if (!aData)
77 return NULL;
78 if ((length=WMGetDataLength(aData))==0) {
79 WMReleaseData(aData);
80 return NULL;
83 buffer = (char*)wmalloc(length+1);
84 WMGetDataBytes(aData, buffer);
85 buffer[length]= '\0';
86 WMReleaseData(aData);
88 return buffer;
92 static void
93 inputHandler(int fd, int mask, void *clientData)
95 WMConnection *cPtr = (WMConnection*)clientData;
96 WMData *aData;
97 char buf[4096];
98 int n;
100 if (!initialized)
101 return;
103 n = read(fd, buf, 4096);
104 if (n>0) {
105 aData = WMCreateDataWithBytes(buf, n);
106 WMSendConnectionData(cPtr, aData);
107 WMReleaseData(aData);
112 static void
113 didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
115 char *buffer;
117 buffer = getMessage(cPtr);
118 if (!buffer) {
119 fprintf(stderr, "Connection closed by peer.\n");
120 exit(0);
123 printf("%s", buffer);
125 wfree(buffer);
129 static void
130 connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
132 WMCloseConnection(cPtr);
134 fprintf(stderr, "Connection closed by peer.\n");
135 exit(0);
139 static void
140 didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
142 int state = WMGetConnectionState(cPtr);
144 if (state == WCConnected) {
145 fprintf(stderr, "connected to '%s:%s'\n", WMGetConnectionAddress(cPtr),
146 WMGetConnectionService(cPtr));
147 initialized = 1;
148 return;
149 } else {
150 wsyserrorwithcode(WCErrorCode, "Unable to connect");
151 exit(1);
157 main(int argc, char **argv) /*FOLD00*/
159 char *ProgName, *host, *port;
160 int i;
161 WMConnection *sPtr;
163 wsetabort(wAbort);
165 WMInitializeApplication("connect", &argc, argv);
167 ProgName = strrchr(argv[0],'/');
168 if (!ProgName)
169 ProgName = argv[0];
170 else
171 ProgName++;
173 host = NULL;
174 port = "7000";
176 if (argc>1) {
177 for (i=1; i<argc; i++) {
178 if (strcmp(argv[i], "--help")==0) {
179 printHelp(argv[0]);
180 exit(0);
181 } else {
182 if (!host)
183 host = argv[i];
184 else
185 port = argv[i];
190 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
191 if (!sPtr) {
192 wfatal("could not create connection. exiting");
193 exit(1);
196 WMSetConnectionDelegate(sPtr, &socketDelegate);
198 /* watch what user types and send it over the connection */
199 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
201 while (1) {
202 WHandleEvents();
205 return 0;