Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Examples / connect.c
blob9b4b93a4139bf96a426e55ecad44ae3120884a34
1 /*
2 * WINGs connect.c: example how to create a network client using WMConnection
4 * Copyright (c) 1999-2003 Dan Pascu
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
13 #include <WINGs/WINGs.h>
15 static int initialized = 0;
17 static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr);
19 static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr);
21 static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr);
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 */
33 void wAbort(Bool foo)
35 exit(1);
38 static char *getMessage(WMConnection * cPtr)
40 char *buffer;
41 WMData *aData;
42 int length;
44 aData = WMGetConnectionAvailableData(cPtr);
45 if (!aData)
46 return NULL;
47 if ((length = WMGetDataLength(aData)) == 0) {
48 WMReleaseData(aData);
49 return NULL;
52 buffer = (char *)wmalloc(length + 1);
53 WMGetDataBytes(aData, buffer);
54 buffer[length] = '\0';
55 WMReleaseData(aData);
57 return buffer;
60 static void inputHandler(int fd, int mask, void *clientData)
62 WMConnection *cPtr = (WMConnection *) clientData;
63 WMData *aData;
64 char buf[4096];
65 int n;
67 if (!initialized)
68 return;
70 n = read(fd, buf, 4096);
71 if (n > 0) {
72 aData = WMCreateDataWithBytes(buf, n);
73 WMSendConnectionData(cPtr, aData);
74 WMReleaseData(aData);
78 static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr)
80 char *buffer;
82 buffer = getMessage(cPtr);
83 if (!buffer) {
84 fprintf(stderr, "Connection closed by peer.\n");
85 exit(0);
88 printf("%s", buffer);
90 wfree(buffer);
93 static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr)
95 WMCloseConnection(cPtr);
97 fprintf(stderr, "Connection closed by peer.\n");
98 exit(0);
101 static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr)
103 int state = WMGetConnectionState(cPtr);
104 WMHost *host;
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);
120 int main(int argc, char **argv)
122 char *ProgName, *host, *port;
123 int i;
124 WMConnection *sPtr;
126 wsetabort(wAbort);
128 WMInitializeApplication("connect", &argc, argv);
130 ProgName = strrchr(argv[0], '/');
131 if (!ProgName)
132 ProgName = argv[0];
133 else
134 ProgName++;
136 host = NULL;
137 port = "34567";
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];
153 printf("Trying to make connection to '%s:%s'\n", host ? host : "localhost", port);
155 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
156 if (!sPtr) {
157 wfatal("could not create connection. exiting");
158 exit(1);
161 WMSetConnectionDelegate(sPtr, &socketDelegate);
163 /* watch what user types and send it over the connection */
164 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
166 while (1) {
167 WHandleEvents();
170 return 0;