Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make it work on win32 ; some more debug msgs
[simgrid.git] / src / gras / Transport / rl_transport.c
1 /* $Id$ */
2
3 /* rl_transport - RL specific functions for transport                       */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "portable.h"
11 #include "gras/Transport/transport_private.h"
12 XBT_LOG_EXTERNAL_CATEGORY(transport);
13 XBT_LOG_DEFAULT_CATEGORY(transport);
14
15 /**
16  * gras_trp_select:
17  *
18  * Returns the next socket to service having a message awaiting.
19  *
20  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
21  *
22  * if timeout=0, do not wait for new message, only handle the ones already there.
23  *
24  * if timeout>0 and no message there, wait at most that amount of time before giving up.
25  */
26 xbt_error_t 
27 gras_trp_select(double timeout,
28                 gras_socket_t *dst) {
29
30   xbt_error_t errcode;
31   xbt_dynar_t sockets= gras_socketset_get();
32   int done = -1;
33   double wakeup = gras_os_time() + 1000000*timeout;
34   double now = 0;
35   /* nextToService used to make sure socket with high number do not starve */
36   /*  static int nextToService = 0; */
37   struct timeval tout, *p_tout;
38
39   int max_fds=0; /* first arg of select: number of existing sockets */
40   /* but accept() of winsock returns sockets bigger than the limit, so don't bother 
41      with this tiny optimisation on BillWare */
42   fd_set FDS;
43   int ready; /* return of select: number of socket ready to be serviced */
44   int fd_setsize; /* FD_SETSIZE not always defined. Get this portably */
45
46   gras_socket_t sock_iter; /* iterating over all sockets */
47   int cursor;              /* iterating over all sockets */
48
49    
50   /* Compute FD_SETSIZE */
51 #ifdef HAVE_SYSCONF
52    fd_setsize = sysconf( _SC_OPEN_MAX );
53 #else
54 #  ifdef HAVE_GETDTABLESIZE 
55    fd_setsize = getdtablesize();
56 #  else
57    fd_setsize = FD_SETSIZE;
58 #  endif /* !USE_SYSCONF */
59 #endif
60
61   *dst=NULL;
62   while (done == -1) {
63     if (timeout > 0) { /* did we timeout already? */
64       now = gras_os_time();
65       if (now == -1 || now >= wakeup) {
66         done = 0;       /* didn't find anything */
67         break;
68       }
69     }
70
71     /* construct the set of socket to ear from */
72     FD_ZERO(&FDS);
73     xbt_dynar_foreach(sockets,cursor,sock_iter) {
74       if (sock_iter->incoming) {
75 #ifndef HAVE_WINSOCK_H
76         if (max_fds < sock_iter->sd)
77           max_fds = sock_iter->sd;
78 #endif
79         FD_SET(sock_iter->sd, &FDS);
80       } else {
81         DEBUG1("Not considering socket %d for select",sock_iter->sd);
82       }
83     }
84
85 #ifndef HAVE_WINSOCK_H
86     /* we cannot have more than FD_SETSIZE sockets 
87        ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
88     if (++max_fds > fd_setsize && fd_setsize > 0) {
89       WARN1("too many open sockets (%d).",max_fds);
90       done = 0;
91       break;
92     }
93 #else
94     max_fds = fd_setsize;
95 #endif
96
97     if (timeout > 0) { 
98       /* set the timeout */
99       tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
100       tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
101       p_tout = &tout;
102     } else if (timeout == 0) {
103       /* polling only */
104       tout.tv_sec = 0;
105       tout.tv_usec = 0;
106       p_tout = &tout;
107       /* we just do one loop around */
108       done = 0;
109     } else { 
110       /* no timeout: good luck! */
111       p_tout = NULL;
112     }
113
114     DEBUG1("Selecting over %d socket(s)", max_fds-1);
115     ready = select(max_fds, &FDS, NULL, NULL, p_tout);
116     if (ready == -1) {
117       switch (errno) {
118       case  EINTR: /* a signal we don't care about occured. we don't care */
119         /* if we cared, we would have set an handler */
120         continue;
121       case EINVAL: /* invalid value */
122         RAISE3(system_error,"invalid select: nb fds: %d, timeout: %d.%d",
123                max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
124       case ENOMEM: 
125         xbt_assert0(0,"Malloc error during the select");
126       default:
127         RAISE2(system_error,"Error during select: %s (%d)",
128                strerror(errno),errno);
129       }
130       RAISE_IMPOSSIBLE;
131     } else if (ready == 0) {
132       continue;  /* this was a timeout */
133     }
134
135     xbt_dynar_foreach(sockets,cursor,sock_iter) { 
136        if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
137         continue;
138        }
139        
140        /* Got a socket to serve */
141        ready--;
142
143        if (   sock_iter->accepting
144            && sock_iter->plugin->socket_accept) { 
145          /* not a socket but an ear. accept on it and serve next socket */
146          gras_socket_t accepted=NULL;
147          
148          DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
149          TRY((sock_iter->plugin->socket_accept)(sock_iter,&accepted));
150          accepted->raw = sock_iter->raw;
151        } else {
152 #if 0 
153        FIXME: this fails of files. quite logical
154          /* Make sure the socket is still alive by reading the first byte */
155          char lookahead;
156          int recvd;
157
158          recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
159          if (recvd < 0) {
160            WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
161            /* done with this socket */
162            gras_socket_close(&sock_iter);
163            cursor--;
164          } else if (recvd == 0) {
165            /* Connection reset (=closed) by peer. */
166            DEBUG1("Connection %d reset by peer", sock_iter->sd);
167            gras_socket_close(&sock_iter); 
168            cursor--; 
169          } else { 
170 #endif
171            /* Got a suited socket ! */
172            *dst = sock_iter;
173            XBT_OUT;
174            return no_error;
175 #if 0
176          }
177 #endif
178        }
179
180        
181        /* if we're here, the socket we found wasn't really ready to be served */
182        if (ready == 0) /* exausted all sockets given by select. Request new ones */
183          break; 
184     }
185
186   }
187
188   XBT_OUT;
189   return timeout_error;
190 }
191
192 xbt_error_t gras_trp_sg_setup(gras_trp_plugin_t *plug) {
193   return mismatch_error;
194 }
195