Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Examples / connect.c
Commit [+]AuthorDateLineData
e5a7a883 dan1999-12-14 01:45:16 +00001/*
b33d6351 dan1999-12-14 05:31:49 +00002 * WINGs connect.c: example how to create a network client using WMConnection
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1999-2003 Dan Pascu
6830b057 dan2004-10-12 21:28:27 +00005 *
e5a7a883 dan1999-12-14 01:45:16 +00006 */
7
e5a7a883 dan1999-12-14 01:45:16 +00008#include <stdio.h>
c2ec1cfe dan2002-11-13 15:13:48 +00009#include <stdlib.h>
e5a7a883 dan1999-12-14 01:45:16 +000010#include <unistd.h>
11#include <string.h>
12
a20aebde dan2001-01-18 19:21:56 +000013#include <WINGs/WINGs.h>
e5a7a883 dan1999-12-14 01:45:16 +000014
e5a7a883 dan1999-12-14 01:45:16 +000015static int initialized = 0;
16
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020017static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr);
e5a7a883 dan1999-12-14 01:45:16 +000018
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020019static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr);
e5a7a883 dan1999-12-14 01:45:16 +000020
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020021static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr);
e5a7a883 dan1999-12-14 01:45:16 +000022
23static ConnectionDelegate socketDelegate = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020024 NULL, /* data */
25 NULL, /* canResumeSending */
26 NULL, /* didCatchException */
27 connectionDidDie, /* didDie */
28 didInitialize, /* didInitialize */
29 didReceiveInput, /* didReceiveInput */
30 NULL /* didTimeout */
e5a7a883 dan1999-12-14 01:45:16 +000031};
32
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020033void wAbort(Bool foo)
e5a7a883 dan1999-12-14 01:45:16 +000034{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020035 exit(1);
e5a7a883 dan1999-12-14 01:45:16 +000036}
37
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020038static char *getMessage(WMConnection * cPtr)
e5a7a883 dan1999-12-14 01:45:16 +000039{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020040 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;
e5a7a883 dan1999-12-14 01:45:16 +000058}
59
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020060static void inputHandler(int fd, int mask, void *clientData)
e5a7a883 dan1999-12-14 01:45:16 +000061{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020062 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 }
e5a7a883 dan1999-12-14 01:45:16 +000076}
77
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr)
e5a7a883 dan1999-12-14 01:45:16 +000079{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020080 char *buffer;
e5a7a883 dan1999-12-14 01:45:16 +000081
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020082 buffer = getMessage(cPtr);
83 if (!buffer) {
84 fprintf(stderr, "Connection closed by peer.\n");
85 exit(0);
86 }
e5a7a883 dan1999-12-14 01:45:16 +000087
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088 printf("%s", buffer);
e5a7a883 dan1999-12-14 01:45:16 +000089
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020090 wfree(buffer);
e5a7a883 dan1999-12-14 01:45:16 +000091}
92
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020093static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr)
e5a7a883 dan1999-12-14 01:45:16 +000094{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020095 WMCloseConnection(cPtr);
e5a7a883 dan1999-12-14 01:45:16 +000096
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020097 fprintf(stderr, "Connection closed by peer.\n");
98 exit(0);
e5a7a883 dan1999-12-14 01:45:16 +000099}
100
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200101static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr)
e5a7a883 dan1999-12-14 01:45:16 +0000102{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200103 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 }
e5a7a883 dan1999-12-14 01:45:16 +0000118}
119
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200120int main(int argc, char **argv)
e5a7a883 dan1999-12-14 01:45:16 +0000121{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200122 char *ProgName, *host, *port;
123 int i;
124 WMConnection *sPtr;
e5a7a883 dan1999-12-14 01:45:16 +0000125
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200126 wsetabort(wAbort);
e5a7a883 dan1999-12-14 01:45:16 +0000127
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200128 WMInitializeApplication("connect", &argc, argv);
e5a7a883 dan1999-12-14 01:45:16 +0000129
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200130 ProgName = strrchr(argv[0], '/');
131 if (!ProgName)
132 ProgName = argv[0];
133 else
134 ProgName++;
e5a7a883 dan1999-12-14 01:45:16 +0000135
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200136 host = NULL;
137 port = "34567";
e5a7a883 dan1999-12-14 01:45:16 +0000138
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200139 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 }
e5a7a883 dan1999-12-14 01:45:16 +0000152
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200153 printf("Trying to make connection to '%s:%s'\n", host ? host : "localhost", port);
87368574 dan1999-12-14 04:41:56 +0000154
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200155 sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
156 if (!sPtr) {
157 wfatal("could not create connection. exiting");
158 exit(1);
159 }
e5a7a883 dan1999-12-14 01:45:16 +0000160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161 WMSetConnectionDelegate(sPtr, &socketDelegate);
e5a7a883 dan1999-12-14 01:45:16 +0000162
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200163 /* watch what user types and send it over the connection */
164 WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
e5a7a883 dan1999-12-14 01:45:16 +0000165
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200166 while (1) {
167 WHandleEvents();
168 }
e5a7a883 dan1999-12-14 01:45:16 +0000169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170 return 0;
e5a7a883 dan1999-12-14 01:45:16 +0000171
172}