~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/ic/test/erl_client_c_server_proto_SUITE_data/c_server.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2004-2011. All Rights Reserved.
 
5
 * 
 
6
 * The contents of this file are subject to the Erlang Public License,
 
7
 * Version 1.1, (the "License"); you may not use this file except in
 
8
 * compliance with the License. You should have received a copy of the
 
9
 * Erlang Public License along with this software. If not, it can be
 
10
 * retrieved online at http://www.erlang.org/.
 
11
 * 
 
12
 * Software distributed under the License is distributed on an "AS IS"
 
13
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
 * the License for the specific language governing rights and limitations
 
15
 * under the License.
 
16
 * 
 
17
 * %CopyrightEnd%
 
18
 *
 
19
 */
 
20
/* C-server for test of IC.
 
21
 *
 
22
 * The C-node implemented here connects to its peer node, waits for
 
23
 * one message, evaluates the message, returns an result message, and 
 
24
 * terminates.
 
25
 * 
 
26
 * TODO:
 
27
 *
 
28
 * 1. XXX #includes for VxWorks, Windows
 
29
 */
 
30
 
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
 
 
34
#ifndef __WIN32__
 
35
#  include <unistd.h>
 
36
#endif
 
37
 
 
38
#include <string.h>
 
39
 
 
40
#ifdef __WIN32__
 
41
#  include <time.h>
 
42
#  include <sys/timeb.h>
 
43
#elif defined VXWORKS
 
44
#  include <time.h>
 
45
#  include <sys/times.h>
 
46
#else
 
47
#  include <sys/time.h>
 
48
#endif
 
49
 
 
50
#include <ctype.h>
 
51
 
 
52
#ifdef __WIN32__
 
53
#  include <winsock2.h>
 
54
#  include <windows.h>
 
55
#else
 
56
#  include <sys/types.h>
 
57
#  include <sys/socket.h>
 
58
#  include <netinet/in.h>
 
59
#  include <arpa/inet.h>
 
60
#  include <netdb.h>
 
61
#endif
 
62
 
 
63
#include "ic.h"
 
64
#include "ei.h"
 
65
#include "erl_interface.h"
 
66
#include "eicode.h"
 
67
#include "m_i__s.h"
 
68
#include "m__s.h"
 
69
 
 
70
#ifdef __WIN32__
 
71
typedef struct {
 
72
    long tv_sec;
 
73
    long tv_usec;
 
74
} MyTimeval;
 
75
#else
 
76
typedef struct timeval MyTimeval;
 
77
#endif
 
78
static void my_gettimeofday(MyTimeval *tv);
 
79
static void showtime(MyTimeval *start, MyTimeval *stop);
 
80
static void usage(void);
 
81
static void done(int r);
 
82
 
 
83
#define HOSTNAMESZ      256
 
84
#define NODENAMESZ      512
 
85
#define INBUFSZ          10
 
86
#define OUTBUFSZ          0
 
87
#define MAXTRIES          5
 
88
 
 
89
static char *progname;
 
90
 
 
91
/* main */
 
92
#ifdef VXWORKS
 
93
int c_server(int argc, char **argv)
 
94
#else
 
95
int main(int argc, char **argv)
 
96
#endif
 
97
{
 
98
    struct hostent *hp;
 
99
    MyTimeval start, stop;
 
100
    int i, fd, ires, tries;
 
101
    CORBA_Environment *env;
 
102
    char *this_node_name = NULL;
 
103
    char *peer_node = NULL;
 
104
    char *cookie = NULL;
 
105
    char host[HOSTNAMESZ + 1];
 
106
    char this_node[NODENAMESZ + 1];
 
107
    erlang_msg msg;
 
108
    int status, loop; 
 
109
    
 
110
#ifdef __WIN32__
 
111
    WORD wVersionRequested;
 
112
    WSADATA wsaData;
 
113
    
 
114
    wVersionRequested = MAKEWORD(2, 0);
 
115
    
 
116
    if (WSAStartup(wVersionRequested, &wsaData) != 0) {
 
117
        fprintf(stderr, "Could not load winsock2 v2.0 compatible DLL");
 
118
        exit(1);
 
119
    }
 
120
#endif
 
121
    
 
122
    progname = argv[0];
 
123
    host[HOSTNAMESZ] = '\0';
 
124
    if (gethostname(host, HOSTNAMESZ) < 0) {
 
125
        fprintf(stderr, "Can't find own hostname\n");
 
126
        done(1);
 
127
    } 
 
128
    if ((hp = gethostbyname(host)) == 0) {
 
129
        fprintf(stderr, "Can't get ip address for host %s\n", host);
 
130
        done(1);
 
131
    }
 
132
    for (i = 1; i < argc; i++) {
 
133
        if (strcmp(argv[i], "-help") == 0) {
 
134
            usage();
 
135
            done(0);
 
136
        } else if (strcmp(argv[i], "-this-node-name") == 0) {
 
137
            i++;
 
138
            this_node_name = argv[i];
 
139
        } else if (strcmp(argv[i], "-peer-node") == 0) {
 
140
            i++;
 
141
            peer_node = argv[i];
 
142
        } else if (strcmp(argv[i], "-cookie") == 0) {
 
143
            i++;
 
144
            cookie = argv[i];
 
145
        } else {
 
146
            fprintf(stderr, "Error : invalid argument \"%s\"\n", argv[i]);
 
147
            usage();
 
148
            done(1);
 
149
        }
 
150
    }
 
151
 
 
152
    if (this_node_name == NULL || peer_node == NULL || cookie == NULL) {
 
153
        fprintf(stderr, "Error: missing option\n");
 
154
        usage();
 
155
        done(1);
 
156
    }
 
157
 
 
158
    /* Behead hostname at first dot */
 
159
    for (i=0; host[i] != '\0'; i++) {
 
160
        if (host[i] == '.') { host[i] = '\0'; break; }
 
161
    }
 
162
    sprintf(this_node, "%s@%s", this_node_name, host);
 
163
 
 
164
    fprintf(stderr, "c_server: this node: \"%s\"\n", this_node);
 
165
    fprintf(stderr, "c_server: peer node: \"%s\"\n", peer_node);
 
166
 
 
167
    /* initialize erl_interface */
 
168
    erl_init(NULL, 0);
 
169
 
 
170
    for (tries = 0; tries < MAXTRIES; tries++) {
 
171
        /* connect to peer node */ 
 
172
        ires = erl_connect_xinit(host, this_node_name, this_node, 
 
173
                                 (struct in_addr *)*hp->h_addr_list, 
 
174
                                 cookie, 0);
 
175
        fprintf(stderr, "c_server: erl_connect_xinit(): %d\n", ires);
 
176
    
 
177
        fd = erl_connect(peer_node);
 
178
        fprintf(stderr, "c_server: erl_connect(): %d\n", fd);
 
179
        if (fd >= 0) 
 
180
            break;
 
181
        fprintf(stderr, "c_server: cannot connect, retrying\n");
 
182
    }
 
183
    if (fd < 0) {
 
184
        fprintf(stderr, "c_server: cannot connect, exiting\n");
 
185
        done(1);
 
186
    }
 
187
    env = CORBA_Environment_alloc(INBUFSZ, OUTBUFSZ);
 
188
    env->_fd = fd; 
 
189
 
 
190
    status = 1;
 
191
    loop = 1;
 
192
    my_gettimeofday(&start);
 
193
    while (status >= 0 && loop > 0) {
 
194
        status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, 
 
195
                                    &msg, &env->_iin); 
 
196
        switch(status) {
 
197
        case ERL_SEND:
 
198
        case ERL_REG_SEND:
 
199
            /* get result */
 
200
            m_i__switch(NULL, env);
 
201
            switch(env->_major) {
 
202
            case CORBA_NO_EXCEPTION:
 
203
                break;
 
204
            case CORBA_SYSTEM_EXCEPTION:
 
205
                fprintf(stderr, "Request failure, reason : %s\n", 
 
206
                        (char *) CORBA_exception_value(env));
 
207
                CORBA_exception_free(env);
 
208
                break;
 
209
            default: /* Should not happen */
 
210
                CORBA_exception_free(env);
 
211
                break;
 
212
            }
 
213
            /* send back result data */
 
214
            if (env->_iout > 0) 
 
215
                ei_send_encoded(env->_fd, &env->_caller, env->_outbuf, 
 
216
                                env->_iout);
 
217
            loop = 0;
 
218
            break;
 
219
        case ERL_TICK:
 
220
            break;
 
221
        default: 
 
222
            if (status < 0) {
 
223
                fprintf(stderr, "Status negative: %d\n", status);
 
224
                loop = 0;
 
225
            }
 
226
            break;
 
227
        }
 
228
    }   
 
229
    my_gettimeofday(&stop);
 
230
    showtime(&start, &stop);
 
231
 
 
232
    erl_close_connection(fd);
 
233
 
 
234
    CORBA_free(env->_inbuf);
 
235
    CORBA_free(env->_outbuf);
 
236
    CORBA_free(env);
 
237
    if (status < 0) 
 
238
        done(-status);
 
239
    else 
 
240
        done(0); 
 
241
}
 
242
 
 
243
static void usage() 
 
244
{
 
245
    fprintf(stderr, "Usage: %s [-help] -this-node-name <name> "
 
246
            "-peer-node <nodename> -cookie <cookie>\n", progname);
 
247
    fprintf(stderr, "Example:\n  %s  -this-node-name kalle "
 
248
            "-peer-node olle@home -cookie oa678er\n", progname);
 
249
}
 
250
 
 
251
static void done(int r) 
 
252
{
 
253
#ifdef __WIN32__
 
254
    WSACleanup();
 
255
#endif
 
256
    exit(r);
 
257
}
 
258
 
 
259
static void showtime(MyTimeval *start, MyTimeval *stop)
 
260
{
 
261
    MyTimeval elapsed;
 
262
 
 
263
    elapsed.tv_sec = stop->tv_sec - start->tv_sec;
 
264
    elapsed.tv_usec = stop->tv_usec - start->tv_usec;
 
265
    while (elapsed.tv_usec < 0) {
 
266
        elapsed.tv_sec -= 1;
 
267
        elapsed.tv_usec += 1000000;
 
268
    }
 
269
    fprintf(stderr,"%ld.%06ld seconds\n",elapsed.tv_sec, elapsed.tv_usec);
 
270
}
 
271
 
 
272
 
 
273
 
 
274
static void my_gettimeofday(MyTimeval *tv)
 
275
#ifdef __WIN32__
 
276
#define EPOCH_JULIAN_DIFF 11644473600i64
 
277
{
 
278
    SYSTEMTIME t;
 
279
    FILETIME ft;
 
280
    LONGLONG lft;
 
281
 
 
282
    GetSystemTime(&t);
 
283
    SystemTimeToFileTime(&t, &ft);
 
284
    memcpy(&lft, &ft, sizeof(lft));
 
285
    tv->tv_usec = (long) ((lft / 10i64) % 1000000i64);
 
286
    tv->tv_sec = (long) ((lft / 10000000i64) - EPOCH_JULIAN_DIFF);
 
287
}
 
288
#elif defined VXWORKS
 
289
{
 
290
    int rate = sysClkRateGet(); /* Ticks per second */
 
291
    unsigned long ctick = tickGet();
 
292
    tv->tv_sec = ctick / rate; /* secs since reboot */
 
293
    tv->tv_usec = ((ctick - (tv->tv_sec * rate))*1000000)/rate;
 
294
}
 
295
#else
 
296
{
 
297
    gettimeofday(tv, NULL);
 
298
}
 
299
#endif