aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/examples/c-server/server.c
blob: a04d60e9b15e277b8a7fa460371c3efea0b241dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1998-2016. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#ifdef __WIN32__
#include <winsock2.h>
#include <direct.h>
#include <windows.h>
#include <winbase.h>
#else /* not __WIN32__ */
#include <errno.h> 
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
#include <netdb.h>
#endif
#include "rmod_random__s.h"
#include "ei_connect.h"

/* Used functions */
extern int gethostname(char *buf, int buflen);
static int getport(int sockd);
static int getlisten(int port);
static int init(ei_cnode *ec, int *sd, int *portnr, int *epmd_fd);
void terminate(int *fd, int *sd, int *epmd_fd);
static void server_loop(ei_cnode *ec, int fd, int sd);

/* change these, or even better, make command-line args to program... */
#define COOKIE "flash"
#define SERVER "babbis"
#define NODENAMESZ 512
#define HOSTNAMESZ 256
#define INBUFSZ 1024
#define OUTBUFSZ 1024


int main(int argc, char **argv)
{
    int sd;
    int portnr;
    int epmd_fd;
    ei_cnode ec;

    /* crate file descriptors */
    if (init(&ec, &sd, &portnr, &epmd_fd) < 0)
	return -1;

    /* start server loop */
    server_loop(&ec, sd, epmd_fd);

    return 0;
}



static void server_loop(ei_cnode *ec, int sd, int epmd_fd)
{
    ErlConnect conn;
    erlang_msg msg;
    int status=1;
    CORBA_Environment *env;
  
    /* Create and init CORBA_Environment */
    env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ);
  
    while (status >= 0) {
	status = 1;
    
	if ((env->_fd = ei_accept(ec, sd, &conn)) < 0) {       
	    /* error */
	    fprintf(stderr,"Accept failed: %s\n",strerror(errno));
	} else {
	    /* connection */
	    fprintf(stderr,"Accepted connection from %s\n",conn.nodename);
      
	    while (status >= 0) {

		/* write message to buffer */
		status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, &msg, &env->_iin); 
		switch(status) {
		case ERL_SEND:
		case ERL_REG_SEND :
		    /* do transaction with fd */
		    rmod_random__switch(NULL,env);
	  
		    switch(env->_major) {
		    case CORBA_NO_EXCEPTION: /* Success */
			break;
		    case CORBA_SYSTEM_EXCEPTION: /* System exception */
			printf("Request failure, reason : %s\n",(char *) CORBA_exception_value(env));
			CORBA_exception_free(env);
			break;
		    default:	/* Should not come here */
			CORBA_exception_free(env);
			break;
		    }
	  
		    /* send outdata */
		    if (env->_iout > 0) 
			ei_send_encoded(env->_fd,&env->_caller,env->_outbuf,env->_iout);
		    break;
	  
		case ERL_TICK :
		    break;
		default :	/* < 0 */
		    printf("Connection terminated\n");
		    break;
		}  
	    }
	}
	status=0;		/* restart */
    }
  
    /* close file descriptors */
    terminate(&env->_fd, &sd, &epmd_fd);
  
    /* Free env & buffers */
    CORBA_free(env->_inbuf);
    CORBA_free(env->_outbuf);
    CORBA_free(env);
} 



static int init(int *sd, int *portnr, int *epmd_fd)
{
    char host[HOSTNAMESZ];
    char servernode[NODENAMESZ];
    struct hostent *h;

    /* get the host name */
    if ((gethostname(host,HOSTNAMESZ)))
	fprintf(stderr,"can't find own hostname\n");
    else {
	/* identify host */
	if (!(h = erl_gethostbyname(host)))
	    fprintf(stdout,"can't find own ip address\n");
	else {

	    /* get a listen port. 0 means let system choose port number */
	    *sd = getlisten(0);
      
	    /* what port did we get? */
	    /* this call not necessary if we specified port in call to getlisten() */
	    *portnr = getport(*sd);
      
	    /* make the nodename server@host */
	    sprintf(servernode,"%s@%s",SERVER,host);
      
	    /* initiate */
	    /* cnode, host, alive, alive@host, addr, cookie, creation */
	    if (ei_connect_xinit(ec, host, SERVER, servernode,
				 (Erl_IpAddr)(h->h_addr_list[0]),
				 COOKIE, 0) == 0) {
		/* let epmd know we are here */
		*epmd_fd = ei_publish(ec, *portnr);
		if (*epmd_fd >= 0)
		    return 0;
	    }
	} 
    }
    return -1;
}


void terminate(int *fd, int *sd, int *epmd_fd) {

    close(*fd);

    /* remove info from epnd */
    close(*epmd_fd);

    /* return socket */
    close(*sd);

}



/* tells you what port you are using on given socket */
static int getport(int sockd)
{
    struct sockaddr_in addr;
    int namelen = sizeof(addr);
    int i;

    memset(&addr,0,sizeof(addr));
  
    if ((i = getsockname(sockd,(struct sockaddr *)&addr,&namelen))<0)
	return i;
  
    return ntohs(addr.sin_port);
}



/* return a listen socket, bound to given port */
/* specify port = 0 to let system assign port */
static int getlisten(int port)
{
    int sockd;
    struct sockaddr_in inaddr;
    int opt = 1;
    int i;

    /* get listen socket */
    if ((sockd = socket(AF_INET,SOCK_STREAM,0)) < 0) return sockd;
  
    if ((i=setsockopt(sockd,SOL_SOCKET,SO_REUSEADDR,(void *)&opt,sizeof(opt)))<0) 
	return i;

    /* bind to requested port */
    memset(&inaddr,0,sizeof(inaddr));
    inaddr.sin_family = AF_INET;              
    inaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    inaddr.sin_port = htons(port);

    if ((i = bind(sockd,(struct sockaddr*) &inaddr, sizeof(inaddr))) < 0)
	return i;

    listen(sockd,5);

    return sockd;
}