Logo AND Algorithmique Numérique Distribuée

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