~profzoom/ubuntu/quantal/wmaker/bug-1079925

« back to all changes in this revision

Viewing changes to WINGs/Examples/connect.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-11-10 14:05:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041110140530-qpd66b5lm38x7apk
Tags: upstream-0.91.0
ImportĀ upstreamĀ versionĀ 0.91.0

Show diffs side-by-side

added added

removed removed

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