Logo AND Algorithmique Numérique Distribuée

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