1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1999-2016. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  *
20  */
21 
22 /* Just include the interface function */
23 #include "rmod_random.h"
24 
25 
26 /* Assign your own node name here */
27 #define CLNODENAME "c50"
28 #define SNODENAME "babbis"
29 #define SREGNAME "rmod_random_impl"
30 #define COOKIE "flash"
31 #define INBUFSZ 1024
32 #define OUTBUFSZ 1024
33 #define HOSTNAMESZ 256
34 
35 
36 
37 /* Stopping node */
client_exit(CORBA_Environment * env)38 void client_exit(CORBA_Environment *env) {
39 
40   /* Free env & buffers */
41   CORBA_free(env->_inbuf);
42   CORBA_free(env->_outbuf);
43   CORBA_free(env);
44 
45   erl_close_connection(env->_fd);
46   exit(1);
47 }
48 
49 
main()50 int main(){
51 
52   double result=0;
53   int i=0;
54   int error = 0;
55   erlang_pid pid;
56   char host[HOSTNAMESZ];
57   char server_node[HOSTNAMESZ];
58   char client_node[HOSTNAMESZ];
59   CORBA_Environment *env;
60   ei_cnode ec;
61 
62   /* Initiate names */
63 #ifdef __WIN32__
64   WORD wVersionRequested;
65   WSADATA wsaData;
66 
67   wVersionRequested = MAKEWORD(1, 1);
68   if ((error = WSAStartup(wVersionRequested, &wsaData))) {
69       fprintf(stderr,"Can't initialize windows sockets: %d",error);
70       return 0;
71   }
72 #endif
73   error = gethostname(host,HOSTNAMESZ);
74   if (error) {
75 #ifdef __WIN32__
76       fprintf(stderr,"can't find own hostname (error = %ld) !\n",WSAGetLastError());
77 #else /* not __WIN32__ */
78       fprintf(stderr,"can't find own hostname !\n");
79 #endif
80   }
81   sprintf(client_node,"%s@%s",CLNODENAME,host);
82   sprintf(server_node,"%s@%s",SNODENAME,host);
83 
84   /* Create and init CORBA_Environment */
85   env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ);
86 
87   /* Initiating the connection */
88   ei_init();
89   ei_connect_init(&ec, 50,COOKIE,0);
90 
91   /* Initiating pid*/
92   strcpy(pid.node,client_node);
93   pid.num = 99;
94   pid.serial = 0;
95   pid.creation = 0;
96 
97   /* Fixing environment variable */
98   env->_fd=ei_connect(&ec, server_node);
99   strcpy(env->_regname,SREGNAME);
100   env->_to_pid = NULL;
101   env->_from_pid = &pid;
102 
103   if (env->_fd < 0) {
104     fprintf(stderr,"Error : Cannot connect to Server\n");
105 
106     /* Free env & buffers */
107     CORBA_free(env->_inbuf);
108     CORBA_free(env->_outbuf);
109     CORBA_free(env);
110     exit(1);
111   }
112 
113   /* Calling the init function */
114   rmod_random_init(NULL, 1, 2, 3, env);
115 
116   switch(env->_major) {
117   case CORBA_NO_EXCEPTION: /* Success */
118     printf("Init complete !\n");
119     break;
120   case CORBA_SYSTEM_EXCEPTION: /* System exception */
121     printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env));
122     CORBA_exception_free(env);
123     client_exit(env);
124   default: /* Should not come here */
125     client_exit(env);
126   }
127 
128   /* Calling the produce function */
129   for(i=1; i<=10; i++) {
130     result = rmod_random_produce(NULL, env);
131 
132     switch(env->_major) {
133     case CORBA_NO_EXCEPTION: /* Success */
134       break;
135     case CORBA_SYSTEM_EXCEPTION: /* System exception */
136       printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env));
137       CORBA_exception_free(env);
138       client_exit(env);
139     default: /* Should not come here */
140       client_exit(env);
141     }
142 
143     printf("the random number nr%d is %f\n",i,result);
144   }
145 
146   /* Closing the connection */
147   ei_close_connection(env->_fd);
148 
149   /* Free env & buffers */
150   CORBA_free(env->_inbuf);
151   CORBA_free(env->_outbuf);
152   CORBA_free(env);
153 
154   return 0;
155 }
156