Logo AND Algorithmique Numérique Distribuée

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