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_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
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 DEBUG2("wakeup=%f now=%f",wakeup, now);
62 if (now == -1 || now >= wakeup) {
63 done = 0; /* didn't find anything */
68 /* construct the set of socket to ear from */
71 xbt_dynar_foreach(sockets,cursor,sock_iter) {
72 if (sock_iter->incoming) {
73 DEBUG1("Considering socket %d for select",sock_iter->sd);
74 #ifndef HAVE_WINSOCK_H
75 if (max_fds < sock_iter->sd)
76 max_fds = sock_iter->sd;
78 FD_SET(sock_iter->sd, &FDS);
80 DEBUG1("Not considering socket %d for select",sock_iter->sd);
86 DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
87 gras_os_sleep(timeout);
88 THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
90 DEBUG0("No socket to select onto. Return directly.");
91 THROW0(timeout_error,0, "No socket to select onto. Return directly.");
95 #ifndef HAVE_WINSOCK_H
96 /* we cannot have more than FD_SETSIZE sockets
97 ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
98 if (++max_fds > fd_setsize && fd_setsize > 0) {
99 WARN1("too many open sockets (%d).",max_fds);
104 max_fds = fd_setsize;
108 /* set the timeout */
109 tout.tv_sec = (unsigned long)(wakeup - now);
110 tout.tv_usec = ((wakeup -now) - ((unsigned long)(wakeup - now))) * 1000000;
112 } else if (timeout == 0) {
117 /* we just do one loop around */
120 /* no timeout: good luck! */
124 DEBUG2("Selecting over %d socket(s); timeout=%f", max_fds-1,timeout);
125 ready = select(max_fds, &FDS, NULL, NULL, p_tout);
126 DEBUG1("select returned %d", ready);
129 case EINTR: /* a signal we don't care about occured. we don't care */
130 /* if we cared, we would have set an handler */
132 case EINVAL: /* invalid value */
133 THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
134 max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
136 xbt_assert0(0,"Malloc error during the select");
138 THROW2(system_error,errno,"Error during select: %s (%d)",
139 strerror(errno),errno);
142 } else if (ready == 0) {
143 continue; /* this was a timeout */
146 xbt_dynar_foreach(sockets,cursor,sock_iter) {
147 if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
151 /* Got a socket to serve */
154 if ( sock_iter->accepting
155 && sock_iter->plugin->socket_accept) {
156 /* not a socket but an ear. accept on it and serve next socket */
157 gras_socket_t accepted=NULL;
159 accepted = (sock_iter->plugin->socket_accept)(sock_iter);
160 DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
161 accepted->meas = sock_iter->meas;
164 FIXME: this fails of files. quite logical
165 /* Make sure the socket is still alive by reading the first byte */
169 recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
171 WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
172 /* done with this socket */
173 gras_socket_close(&sock_iter);
175 } else if (recvd == 0) {
176 /* Connection reset (=closed) by peer. */
177 DEBUG1("Connection %d reset by peer", sock_iter->sd);
178 gras_socket_close(&sock_iter);
182 /* Got a suited socket ! */
191 /* if we're here, the socket we found wasn't really ready to be served */
192 if (ready == 0) /* exausted all sockets given by select. Request new ones */
202 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
203 THROW0(mismatch_error,0,"No SG transport on live platforms");