Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initiate win32 cross-port
[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   fd_set FDS;
41   int ready; /* return of select: number of socket ready to be serviced */
42
43   gras_socket_t sock_iter; /* iterating over all sockets */
44   int cursor;              /* iterating over all sockets */
45
46   *dst=NULL;
47   while (done == -1) {
48     if (timeout > 0) { /* did we timeout already? */
49       now = gras_os_time();
50       if (now == -1 || now >= wakeup) {
51         done = 0;       /* didn't find anything */
52         break;
53       }
54     }
55
56     /* construct the set of socket to ear from */
57     FD_ZERO(&FDS);
58     xbt_dynar_foreach(sockets,cursor,sock_iter) {
59       if (sock_iter->incoming) {
60         if (max_fds < sock_iter->sd)
61           max_fds = sock_iter->sd;
62         FD_SET(sock_iter->sd, &FDS);
63       } else {
64         DEBUG1("Not considering socket %d for select",sock_iter->sd);
65       }
66     }
67
68     /* we cannot have more than FD_SETSIZE sockets */
69     if (++max_fds > FD_SETSIZE) {
70       WARN0("too many open sockets.");
71       done = 0;
72       break;
73     }
74
75     if (timeout > 0) { 
76       /* set the timeout */
77       tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
78       tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
79       p_tout = &tout;
80     } else if (timeout == 0) {
81       /* polling only */
82       tout.tv_sec = 0;
83       tout.tv_usec = 0;
84       p_tout = &tout;
85       /* we just do one loop around */
86       done = 0;
87     } else { 
88       /* no timeout: good luck! */
89       p_tout = NULL;
90     }
91
92     DEBUG1("Selecting over %d socket(s)", max_fds-1);
93     ready = select(max_fds, &FDS, NULL, NULL, p_tout);
94     if (ready == -1) {
95       switch (errno) {
96       case  EINTR: /* a signal we don't care about occured. we don't care */
97         /* if we cared, we would have set an handler */
98         continue;
99       case EINVAL: /* invalid value */
100         RAISE3(system_error,"invalid select: nb fds: %d, timeout: %d.%d",
101                max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
102       case ENOMEM: 
103         xbt_assert0(0,"Malloc error during the select");
104       default:
105         RAISE2(system_error,"Error during select: %s (%d)",
106                strerror(errno),errno);
107       }
108       RAISE_IMPOSSIBLE;
109     } else if (ready == 0) {
110       continue;  /* this was a timeout */
111     }
112
113     xbt_dynar_foreach(sockets,cursor,sock_iter) { 
114        if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
115         continue;
116        }
117        
118        /* Got a socket to serve */
119        ready--;
120
121        if (   sock_iter->accepting
122            && sock_iter->plugin->socket_accept) { 
123          /* not a socket but an ear. accept on it and serve next socket */
124          gras_socket_t accepted;
125
126          TRY(sock_iter->plugin->socket_accept(sock_iter,&accepted));
127          accepted->raw = sock_iter->raw;
128        } else {
129 #if 0 
130        FIXME: this fails of files. quite logical
131          /* Make sure the socket is still alive by reading the first byte */
132          char lookahead;
133          int recvd;
134
135          recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
136          if (recvd < 0) {
137            WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
138            /* done with this socket */
139            gras_socket_close(&sock_iter);
140            cursor--;
141          } else if (recvd == 0) {
142            /* Connection reset (=closed) by peer. */
143            DEBUG1("Connection %d reset by peer", sock_iter->sd);
144            gras_socket_close(&sock_iter); 
145            cursor--; 
146          } else { 
147 #endif
148            /* Got a suited socket ! */
149            *dst = sock_iter;
150            return no_error;
151 #if 0
152          }
153 #endif
154        }
155
156        
157        /* if we're here, the socket we found wasn't really ready to be served */
158        if (ready == 0) /* exausted all sockets given by select. Request new ones */
159          break; 
160     }
161
162   }
163
164   return timeout_error;
165 }
166
167 xbt_error_t gras_trp_sg_setup(gras_trp_plugin_t *plug) {
168   return mismatch_error;
169 }