FreeWRL/FreeX3D  3.0.0
EAI_C_Control.c
1 
2 /****************************************************************************
3  This file is part of the FreeWRL/FreeX3D Distribution.
4 
5  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
6 
7  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
19 ****************************************************************************/
20 
21 #include "EAI_C.h"
22 #include "config.h"
23 #include "system.h"
24 #include <stdio.h>
25 #ifndef WIN32
26 #include <sys/errno.h>
27 #endif
28 
29 #define UNUSED(v) ((void) v) // compiler warning mitigation
30 
31 pthread_t readThread;
32 pthread_t EVcallbackThread;
33 pthread_t swigThread;
34 int readThreadInitialized = FALSE;
35 
36 void X3D_initialize(char *hostname) {
37  struct sockaddr_in serv_addr;
38  struct hostent *server;
39  int iret1;
40  int iret2;
41  #ifdef WIN32
42  WORD wVersionRequested;
43  WSADATA wsaData;
44  int err;
45  #endif
46  int loopCount;
47  int constat;
48 
49  UNUSED(iret1); //compiler warning mitigation
50  UNUSED(iret2); //compiler warning mitigation
51 
52 #ifdef WIN32
53  wVersionRequested = MAKEWORD( 2, 2 );
54 
55  err = WSAStartup( wVersionRequested, &wsaData );
56 
57  if ( err != 0 )
58  {
59  int socket_error = WSAGetLastError();
60 
61  switch(socket_error)
62  {
63  case WSASYSNOTREADY:
64  //The underlying network subsystem is not ready for network communication.
65  break;
66  case WSAVERNOTSUPPORTED:
67  //The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation.
68  break;
69  case WSAEINPROGRESS:
70  //A blocking Windows Sockets 1.1 operation is in progress.
71  break;
72  case WSAEPROCLIM:
73  //A limit on the number of tasks supported by the Windows Sockets implementation has been reached.
74  break;
75  case WSAEFAULT:
76  //The lpWSAData parameter is not a valid pointer.
77  break;
78  default:
79  break;
80  }
81  }
82 #endif
83 
84  loopCount = 0;
85  // printf ("X3D_initialize, opening socket\n");
86 
87  while ((_X3D_FreeWRL_FD = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
88  //printf ("X3D_initialize, looping %d\n",loopCount);
89 
90 
91 #ifdef WIN32
92  int socket_error = WSAGetLastError();
93 
94  switch(socket_error)
95  {
96  case (WSANOTINITIALISED):
97  //The network subsystem or the associated service provider has failed.
98  break;
99  case (WSAENETDOWN):
100  //The network subsystem or the associated service provider has failed.
101  break;
102  case (WSAEAFNOSUPPORT):
103  //The specified address family is not supported.
104  break;
105  case (WSAEINPROGRESS):
106  //A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
107  break;
108  case (WSAEMFILE):
109  //No more socket descriptors are available.
110  break;
111  case (WSAENOBUFS):
112  //No buffer space is available. The socket cannot be created.
113  break;
114  case (WSAEPROTONOSUPPORT):
115  //The specified protocol is not supported.
116  break;
117  case (WSAEPROTOTYPE):
118  //The specified protocol is the wrong type for this socket.
119  break;
120  case (WSAESOCKTNOSUPPORT):
121  //The specified socket type is not supported in this address family.
122  break;
123  default:
124  //unspecified error...
125  break;
126  }
127 #endif
128 
129  usleep (100000);
130  loopCount ++;
131  if (loopCount >= 10000) {
132  X3D_error("ERROR opening socket");
133  return;
134  }
135  }
136 
137  usleep (10000); /* let remote end settle down to this interruption */
138 
139  if (strlen(hostname) == 0) hostname = "localhost";
140 
141  server = gethostbyname(hostname);
142  if (server == NULL) {
143  fprintf(stderr,"ERROR, no such host :%s:\n",hostname);
144  exit(0);
145  }
146  // printf ("X3D_initialize, server pointer is %p\n",server);
147 
148  bzero((char *) &serv_addr, sizeof(serv_addr));
149  serv_addr.sin_family = AF_INET;
150  bcopy((char *)server->h_addr,
151  (char *)&serv_addr.sin_addr.s_addr,
152  server->h_length);
153 
154  serv_addr.sin_port = htons(EAIBASESOCKET);
155 
156  loopCount = 0;
157  // printf ("X3D_initialize, connecting to socket %d\n",EAIBASESOCKET);
158 
159  while ((constat = connect(_X3D_FreeWRL_FD,(struct sockaddr *) &serv_addr,sizeof(serv_addr))) < 0) {
160  usleep (100000);
161  loopCount ++;
162  if (loopCount >= 10000) {
163  X3D_error("ERROR connecting to socket - FreeWRL not there?");
164  return;
165  }
166  }
167 
168 
169  /* start up read thread */
170  iret1 = pthread_create( &readThread, NULL, freewrlReadThread, NULL);
171  iret2 = pthread_create( &EVcallbackThread, NULL, freewrlEVcallbackThread, NULL);
172  /* start up thread to allow connections from SWIG (or similar) */
173  /* iret2 = pthread_create(&swigThread, NULL, freewrlSwigThread, NULL); */
174 //#ifdef WIN32
175 #ifdef SWIG
176  iret2 = pthread_create(&swigThread, NULL, freewrlSwigThread, NULL);
177 #endif
178 //#endif
179  // printf ("X3D_initialize, opened, created threads and exiting\n");
180 
181 }
182 
183 
184 
185 /* tell FreeWRL to shut down; don't worry about the return value */
186 void X3D_shutdown() {
187  _X3D_makeShortCommand(STOPFREEWRL);
188 #ifdef WIN32
189  /* cleanup I suspect it never gets here */
190  closesocket(_X3D_FreeWRL_FD);
191  WSACleanup();
192 #endif
193 }