Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to select on socket 0
[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     max_fds = -1;
74     xbt_dynar_foreach(sockets,cursor,sock_iter) {
75       if (sock_iter->incoming) {
76         DEBUG1("Considering socket %d for select",sock_iter->sd);
77 #ifndef HAVE_WINSOCK_H
78         if (max_fds < sock_iter->sd)
79           max_fds = sock_iter->sd;
80 #endif
81         FD_SET(sock_iter->sd, &FDS);
82       } else {
83         DEBUG1("Not considering socket %d for select",sock_iter->sd);
84       }
85     }
86
87     if (max_fds == -1) {
88        if (timeout > 0) {
89           DEBUG0("No socket to select onto. Sleep instead.");
90           gras_os_sleep(timeout,0);
91           return timeout_error;
92        } else {
93           DEBUG0("No socket to select onto. Return directly.");
94           return timeout_error;
95        }
96     }
97
98 #ifndef HAVE_WINSOCK_H
99     /* we cannot have more than FD_SETSIZE sockets 
100        ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
101     if (++max_fds > fd_setsize && fd_setsize > 0) {
102       WARN1("too many open sockets (%d).",max_fds);
103       done = 0;
104       break;
105     }
106 #else
107     max_fds = fd_setsize;
108 #endif
109
110     if (timeout > 0) { 
111       /* set the timeout */
112       tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
113       tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
114       p_tout = &tout;
115     } else if (timeout == 0) {
116       /* polling only */
117       tout.tv_sec = 0;
118       tout.tv_usec = 0;
119       p_tout = &tout;
120       /* we just do one loop around */
121       done = 0;
122     } else { 
123       /* no timeout: good luck! */
124       p_tout = NULL;
125     }
126      
127     DEBUG1("Selecting over %d socket(s)", max_fds-1);
128     ready = select(max_fds, &FDS, NULL, NULL, p_tout);
129     if (ready == -1) {
130       switch (errno) {
131       case  EINTR: /* a signal we don't care about occured. we don't care */
132         /* if we cared, we would have set an handler */
133         continue;
134       case EINVAL: /* invalid value */
135         RAISE3(system_error,"invalid select: nb fds: %d, timeout: %d.%d",
136                max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
137       case ENOMEM: 
138         xbt_assert0(0,"Malloc error during the select");
139       default:
140         RAISE2(system_error,"Error during select: %s (%d)",
141                strerror(errno),errno);
142       }
143       RAISE_IMPOSSIBLE;
144     } else if (ready == 0) {
145       continue;  /* this was a timeout */
146     }
147
148     xbt_dynar_foreach(sockets,cursor,sock_iter) { 
149        if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
150         continue;
151        }
152        
153        /* Got a socket to serve */
154        ready--;
155
156        if (   sock_iter->accepting
157            && sock_iter->plugin->socket_accept) { 
158          /* not a socket but an ear. accept on it and serve next socket */
159          gras_socket_t accepted=NULL;
160          
161          DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
162          TRY((sock_iter->plugin->socket_accept)(sock_iter,&accepted));
163          accepted->raw = sock_iter->raw;
164        } else {
165 #if 0 
166        FIXME: this fails of files. quite logical
167          /* Make sure the socket is still alive by reading the first byte */
168          char lookahead;
169          int recvd;
170
171          recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
172          if (recvd < 0) {
173            WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
174            /* done with this socket */
175            gras_socket_close(&sock_iter);
176            cursor--;
177          } else if (recvd == 0) {
178            /* Connection reset (=closed) by peer. */
179            DEBUG1("Connection %d reset by peer", sock_iter->sd);
180            gras_socket_close(&sock_iter); 
181            cursor--; 
182          } else { 
183 #endif
184            /* Got a suited socket ! */
185            *dst = sock_iter;
186            XBT_OUT;
187            return no_error;
188 #if 0
189          }
190 #endif
191        }
192
193        
194        /* if we're here, the socket we found wasn't really ready to be served */
195        if (ready == 0) /* exausted all sockets given by select. Request new ones */
196          break; 
197     }
198
199   }
200
201   XBT_OUT;
202   return timeout_error;
203 }
204
205 xbt_error_t gras_trp_sg_setup(gras_trp_plugin_t *plug) {
206   return mismatch_error;
207 }
208