3 /* rl_transport - RL specific functions for transport */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
12 #include "gras/Transport/transport_private.h"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
18 * Returns the next socket to service because it receives a message.
20 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
22 * if timeout=0, do not wait for new message, only handle the ones already there.
24 * if timeout>0 and no message there, wait at most that amount of time before giving up.
26 gras_socket_t gras_trp_select(double timeout) {
27 xbt_dynar_t sockets= ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
29 double wakeup = gras_os_time() + timeout;
31 /* nextToService used to make sure socket with high number do not starve */
32 /* static int nextToService = 0; */
33 struct timeval tout, *p_tout;
35 int max_fds=0; /* first arg of select: number of existing sockets */
36 /* but accept() of winsock returns sockets bigger than the limit, so don't bother
37 with this tiny optimisation on BillWare */
39 int ready; /* return of select: number of socket ready to be serviced */
40 int fd_setsize; /* FD_SETSIZE not always defined. Get this portably */
42 gras_socket_t sock_iter; /* iterating over all sockets */
43 int cursor; /* iterating over all sockets */
46 /* Compute FD_SETSIZE */
48 fd_setsize = sysconf( _SC_OPEN_MAX );
50 # ifdef HAVE_GETDTABLESIZE
51 fd_setsize = getdtablesize();
53 fd_setsize = FD_SETSIZE;
54 # endif /* !USE_SYSCONF */
58 if (timeout > 0) { /* did we timeout already? */
60 DEBUG2("wakeup=%f now=%f",wakeup, now);
61 if (now == -1 || now >= wakeup) {
62 /* didn't find anything */
63 THROW1(timeout_error,0,
64 "Timeout (%f) elapsed with selecting for incomming connexions",
69 /* construct the set of socket to ear from */
72 xbt_dynar_foreach(sockets,cursor,sock_iter) {
73 if (!sock_iter->valid)
76 if (sock_iter->incoming) {
77 DEBUG1("Considering socket %d for select",sock_iter->sd);
78 #ifndef HAVE_WINSOCK_H
79 if (max_fds < sock_iter->sd)
80 max_fds = sock_iter->sd;
82 FD_SET(sock_iter->sd, &FDS);
84 DEBUG1("Not considering socket %d for select",sock_iter->sd);
90 DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
91 gras_os_sleep(timeout);
92 THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
94 DEBUG0("No socket to select onto. Return directly.");
95 THROW0(timeout_error,0, "No socket to select onto. Return directly.");
99 #ifndef HAVE_WINSOCK_H
100 /* we cannot have more than FD_SETSIZE sockets
101 ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
102 if (++max_fds > fd_setsize && fd_setsize > 0) {
103 WARN1("too many open sockets (%d).",max_fds);
108 max_fds = fd_setsize;
112 /* set the timeout */
113 tout.tv_sec = (unsigned long)(wakeup - now);
114 tout.tv_usec = ((wakeup -now) - ((unsigned long)(wakeup - now))) * 1000000;
116 } else if (timeout == 0) {
121 /* we just do one loop around */
124 /* no timeout: good luck! */
128 DEBUG2("Selecting over %d socket(s); timeout=%f", max_fds-1,timeout);
129 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
130 DEBUG1("select returned %d", ready);
133 case EINTR: /* a signal we don't care about occured. we don't care */
134 /* if we cared, we would have set an handler */
136 case EINVAL: /* invalid value */
137 THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
138 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
140 xbt_assert0(0,"Malloc error during the select");
142 THROW2(system_error,errno,"Error during select: %s (%d)",
143 strerror(errno),errno);
146 } else if (ready == 0) {
147 continue; /* this was a timeout */
150 xbt_dynar_foreach(sockets,cursor,sock_iter) {
151 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
155 /* Got a socket to serve */
158 if ( sock_iter->accepting
159 && sock_iter->plugin->socket_accept) {
160 /* not a socket but an ear. accept on it and serve next socket */
161 gras_socket_t accepted=NULL;
163 accepted = (sock_iter->plugin->socket_accept)(sock_iter);
164 DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
165 accepted->meas = sock_iter->meas;
167 } else if (sock_iter->recv_ok) {
168 /* Make sure the socket is still alive by reading the first byte */
172 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
174 WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
175 /* done with this socket */
176 gras_socket_close(sock_iter);
178 } else if (recvd == 0) {
179 /* Connection reset (=closed) by peer. */
180 DEBUG1("Connection %d reset by peer", sock_iter->sd);
181 sock_iter->valid=0; /* don't close it. User may keep references to it */
183 /* Got a suited socket ! */
189 /* This is a file socket. Cannot recv() on it, but it must be alive */
195 /* if we're here, the socket we found wasn't really ready to be served */
196 if (ready == 0) /* exausted all sockets given by select. Request new ones */
202 /* No socket found. Maybe we had timeout=0 and nothing to do */
204 THROW0(timeout_error,0,"Timeout");
207 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
208 THROW0(mismatch_error,0,"No SG transport on live platforms");