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_CATEGORY(transport);
14 XBT_LOG_DEFAULT_CATEGORY(transport);
19 * Returns the next socket to service because it receives a message.
21 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
23 * if timeout=0, do not wait for new message, only handle the ones already there.
25 * if timeout>0 and no message there, wait at most that amount of time before giving up.
27 gras_socket_t gras_trp_select(double timeout) {
28 xbt_dynar_t sockets= gras_socketset_get();
30 double wakeup = gras_os_time() + timeout;
32 /* nextToService used to make sure socket with high number do not starve */
33 /* static int nextToService = 0; */
34 struct timeval tout, *p_tout;
36 int max_fds=0; /* first arg of select: number of existing sockets */
37 /* but accept() of winsock returns sockets bigger than the limit, so don't bother
38 with this tiny optimisation on BillWare */
40 int ready; /* return of select: number of socket ready to be serviced */
41 int fd_setsize; /* FD_SETSIZE not always defined. Get this portably */
43 gras_socket_t sock_iter; /* iterating over all sockets */
44 int cursor; /* iterating over all sockets */
47 /* Compute FD_SETSIZE */
49 fd_setsize = sysconf( _SC_OPEN_MAX );
51 # ifdef HAVE_GETDTABLESIZE
52 fd_setsize = getdtablesize();
54 fd_setsize = FD_SETSIZE;
55 # endif /* !USE_SYSCONF */
59 if (timeout > 0) { /* did we timeout already? */
61 if (now == -1 || now >= wakeup) {
62 done = 0; /* didn't find anything */
67 /* construct the set of socket to ear from */
70 xbt_dynar_foreach(sockets,cursor,sock_iter) {
71 if (sock_iter->incoming) {
72 DEBUG1("Considering socket %d for select",sock_iter->sd);
73 #ifndef HAVE_WINSOCK_H
74 if (max_fds < sock_iter->sd)
75 max_fds = sock_iter->sd;
77 FD_SET(sock_iter->sd, &FDS);
79 DEBUG1("Not considering socket %d for select",sock_iter->sd);
85 DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
86 gras_os_sleep(timeout);
87 THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
89 DEBUG0("No socket to select onto. Return directly.");
90 THROW0(timeout_error,0, "No socket to select onto. Return directly.");
94 #ifndef HAVE_WINSOCK_H
95 /* we cannot have more than FD_SETSIZE sockets
96 ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
97 if (++max_fds > fd_setsize && fd_setsize > 0) {
98 WARN1("too many open sockets (%d).",max_fds);
103 max_fds = fd_setsize;
107 /* set the timeout */
108 tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
109 tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
111 } else if (timeout == 0) {
116 /* we just do one loop around */
119 /* no timeout: good luck! */
123 DEBUG1("Selecting over %d socket(s)", max_fds-1);
124 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
127 case EINTR: /* a signal we don't care about occured. we don't care */
128 /* if we cared, we would have set an handler */
130 case EINVAL: /* invalid value */
131 THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
132 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
134 xbt_assert0(0,"Malloc error during the select");
136 THROW2(system_error,errno,"Error during select: %s (%d)",
137 strerror(errno),errno);
140 } else if (ready == 0) {
141 continue; /* this was a timeout */
144 xbt_dynar_foreach(sockets,cursor,sock_iter) {
145 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
149 /* Got a socket to serve */
152 if ( sock_iter->accepting
153 && sock_iter->plugin->socket_accept) {
154 /* not a socket but an ear. accept on it and serve next socket */
155 gras_socket_t accepted=NULL;
157 accepted = (sock_iter->plugin->socket_accept)(sock_iter);
158 DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
159 accepted->meas = sock_iter->meas;
162 FIXME: this fails of files. quite logical
163 /* Make sure the socket is still alive by reading the first byte */
167 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
169 WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
170 /* done with this socket */
171 gras_socket_close(&sock_iter);
173 } else if (recvd == 0) {
174 /* Connection reset (=closed) by peer. */
175 DEBUG1("Connection %d reset by peer", sock_iter->sd);
176 gras_socket_close(&sock_iter);
180 /* Got a suited socket ! */
189 /* if we're here, the socket we found wasn't really ready to be served */
190 if (ready == 0) /* exausted all sockets given by select. Request new ones */
200 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
201 THROW0(mismatch_error,0,"No SG transport on live platforms");