Logo AND Algorithmique Numérique Distribuée

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