misc. changes
[wmaker-crm.git] / WINGs / connect.c
blobc7b2d3b30efc92b5fc6d2aef0122df770f82f85c
1 /*
2 * WINGs connect.c: example how to create a network 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 char*
61 getMessage(WMConnection *cPtr)
63 char *buffer;
64 WMData *aData;
65 int length;
67 aData = WMGetConnectionAvailableData(cPtr);
68 if (!aData)
69 return NULL;
70 if ((length=WMGetDataLength(aData))==0) {
71 WMReleaseData(aData);
72 return NULL;
75 buffer = (char*)wmalloc(length+1);
76 WMGetDataBytes(aData, buffer);
77 buffer[length]= '\0';
78 WMReleaseData(aData);
80 return buffer;
84 static void
85 inputHandler(int fd, int mask, void *clientData)
87 WMConnection *cPtr = (WMConnection*)clientData;
88 WMData *aData;
89 char buf[4096];
90 int n;
92 if (!initialized)
93 return;
95 n = read(fd, buf, 4096);
96 if (n>0) {
97 aData = WMCreateDataWithBytes(buf, n);
98 WMSendConnectionData(cPtr, aData);
99 WMReleaseData(aData);
104 static void
105 didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
107 char *buffer;
109 buffer = getMessage(cPtr);
110 if (!buffer) {
111 fprintf(stderr, "Connection closed by peer.\n");
112 exit(0);
115 printf("%s", buffer);
117 wfree(buffer);
121 static void
122 connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
124 WMCloseConnection(cPtr);
126 fprintf(stderr, "Connection closed by peer.\n");
127 exit(0);
131 static void
132 didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
134 int state = WMGetConnectionState(cPtr);
135 WMHost *host;
137 if (state == WCConnected) {
138 host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
139 fprintf(stderr, "connected to '%s:%s'\n",
140 host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
141 WMGetConnectionService(cPtr));
142 initialized = 1;
143 if (host)
144 WMReleaseHost(host);
145 return;
146 } else {
147 wsyserrorwithcode(WCErrorCode, "Unable to connect");
148 exit(1);
154 main(int argc, char **argv) /*FOLD00*/
156 char *ProgName, *host, *port;
157 int i;
158 WMConnection *sPtr;
160 wsetabort(wAbort);
162 WMInitializeApplication("connect", &argc, argv);
164 ProgName = strrchr(argv[0],'/');
165 if (!ProgName)
166 ProgName = argv[0];
167 else
168 ProgName++;
170 host = NULL;
171 port = "7000";
173 if (argc>1) {
174 for (i=1; i<argc; i++) {
175 if (strcmp(argv[i], "--help")==0 || strcmp(argv[i], "-h")==0) {
176 printf("usage: %s [host [port]]\n\n", ProgName);
177 exit(0);
178 } else {
179 if (!host)
180 host = argv[i];
181 else
182 port = argv[i];
187 printf("Trying to make connection to '%s:%s'\n",
188 host?host:"localhost", port);
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;