4
* Copyright Ericsson AB 2004-2011. All Rights Reserved.
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/.
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
20
/* C-server for test of IC.
22
* The C-node implemented here connects to its peer node, waits for
23
* one message, evaluates the message, returns an result message, and
28
* 1. XXX #includes for VxWorks, Windows
42
# include <sys/timeb.h>
45
# include <sys/times.h>
47
# include <sys/time.h>
53
# include <winsock2.h>
56
# include <sys/types.h>
57
# include <sys/socket.h>
58
# include <netinet/in.h>
59
# include <arpa/inet.h>
65
#include "erl_interface.h"
76
typedef struct timeval MyTimeval;
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);
83
#define HOSTNAMESZ 256
84
#define NODENAMESZ 512
89
static char *progname;
93
int c_server(int argc, char **argv)
95
int main(int argc, char **argv)
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;
105
char host[HOSTNAMESZ + 1];
106
char this_node[NODENAMESZ + 1];
111
WORD wVersionRequested;
114
wVersionRequested = MAKEWORD(2, 0);
116
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
117
fprintf(stderr, "Could not load winsock2 v2.0 compatible DLL");
123
host[HOSTNAMESZ] = '\0';
124
if (gethostname(host, HOSTNAMESZ) < 0) {
125
fprintf(stderr, "Can't find own hostname\n");
128
if ((hp = gethostbyname(host)) == 0) {
129
fprintf(stderr, "Can't get ip address for host %s\n", host);
132
for (i = 1; i < argc; i++) {
133
if (strcmp(argv[i], "-help") == 0) {
136
} else if (strcmp(argv[i], "-this-node-name") == 0) {
138
this_node_name = argv[i];
139
} else if (strcmp(argv[i], "-peer-node") == 0) {
142
} else if (strcmp(argv[i], "-cookie") == 0) {
146
fprintf(stderr, "Error : invalid argument \"%s\"\n", argv[i]);
152
if (this_node_name == NULL || peer_node == NULL || cookie == NULL) {
153
fprintf(stderr, "Error: missing option\n");
158
/* Behead hostname at first dot */
159
for (i=0; host[i] != '\0'; i++) {
160
if (host[i] == '.') { host[i] = '\0'; break; }
162
sprintf(this_node, "%s@%s", this_node_name, host);
164
fprintf(stderr, "c_server: this node: \"%s\"\n", this_node);
165
fprintf(stderr, "c_server: peer node: \"%s\"\n", peer_node);
167
/* initialize erl_interface */
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,
175
fprintf(stderr, "c_server: erl_connect_xinit(): %d\n", ires);
177
fd = erl_connect(peer_node);
178
fprintf(stderr, "c_server: erl_connect(): %d\n", fd);
181
fprintf(stderr, "c_server: cannot connect, retrying\n");
184
fprintf(stderr, "c_server: cannot connect, exiting\n");
187
env = CORBA_Environment_alloc(INBUFSZ, OUTBUFSZ);
192
my_gettimeofday(&start);
193
while (status >= 0 && loop > 0) {
194
status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz,
200
m_i__switch(NULL, env);
201
switch(env->_major) {
202
case CORBA_NO_EXCEPTION:
204
case CORBA_SYSTEM_EXCEPTION:
205
fprintf(stderr, "Request failure, reason : %s\n",
206
(char *) CORBA_exception_value(env));
207
CORBA_exception_free(env);
209
default: /* Should not happen */
210
CORBA_exception_free(env);
213
/* send back result data */
215
ei_send_encoded(env->_fd, &env->_caller, env->_outbuf,
223
fprintf(stderr, "Status negative: %d\n", status);
229
my_gettimeofday(&stop);
230
showtime(&start, &stop);
232
erl_close_connection(fd);
234
CORBA_free(env->_inbuf);
235
CORBA_free(env->_outbuf);
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);
251
static void done(int r)
259
static void showtime(MyTimeval *start, MyTimeval *stop)
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) {
267
elapsed.tv_usec += 1000000;
269
fprintf(stderr,"%ld.%06ld seconds\n",elapsed.tv_sec, elapsed.tv_usec);
274
static void my_gettimeofday(MyTimeval *tv)
276
#define EPOCH_JULIAN_DIFF 11644473600i64
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);
288
#elif defined VXWORKS
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;
297
gettimeofday(tv, NULL);