Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Detect socket closed by peer in the transport layer, so that such things don't polute...
[simgrid.git] / src / gras / Transport / sg_transport.c
1 /* $Id$ */
2
3 /* sg_transport - SG specific functions for transport                       */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #include "xbt/ex.h"
11 #include "gras/Transport/transport_private.h"
12 #include "msg/msg.h"
13 #include "gras/Virtu/virtu_sg.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
16
17 /**
18  * gras_trp_select:
19  *
20  * Returns the next socket to service having a message awaiting.
21  *
22  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
23  *
24  * if timeout=0, do not wait for new message, only handle the ones already there.
25  *
26  * if timeout>0 and no message there, wait at most that amount of time before giving up.
27  */
28 gras_socket_t gras_trp_select(double timeout) {
29   
30   gras_socket_t res;
31   gras_trp_procdata_t pd = 
32     (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
33   gras_trp_sg_sock_data_t *sockdata;
34   gras_trp_plugin_t trp;
35
36   gras_socket_t sock_iter; /* iterating over all sockets */
37   int cursor,cpt;
38
39   gras_sg_portrec_t pr;     /* iterating to find the chanel of expeditor */
40
41   int r_pid;
42   gras_hostdata_t *remote_hd;
43
44   DEBUG3("select on %s@%s with timeout=%f",
45          MSG_process_get_name(MSG_process_self()),
46          MSG_host_get_name(MSG_host_self()),
47          timeout);
48
49   MSG_channel_select_from((m_channel_t) pd->chan, timeout, &r_pid);
50   
51   if (r_pid < 0) {
52     DEBUG0("TIMEOUT");
53     THROW0(timeout_error,0,"Timeout");
54   }
55
56   /* Ok, got something. Open a socket back to the expeditor */
57
58   /* Try to reuse an already openned socket to that expeditor */
59   xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
60     DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
61     
62     if (sock_iter->meas || !sock_iter->outgoing)
63       continue;
64     
65     sockdata = sock_iter->data;
66     if (sockdata->to_PID == r_pid) {
67       return sock_iter;
68     }
69   }
70   
71   /* Socket to expeditor not created yet */
72   DEBUG0("Create a socket to the expeditor");
73   
74   trp = gras_trp_plugin_get_by_name("sg");
75   
76   gras_trp_socket_new(1,&res);
77   res->plugin   = trp;
78   
79   res->incoming  = 1;
80   res->outgoing  = 1;
81   res->accepting = 0;
82   res->sd        = -1;
83   
84   res->port = -1;
85   
86   sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
87   sockdata->from_PID = MSG_process_self_PID();
88   sockdata->to_PID   = r_pid;
89   sockdata->to_host  = MSG_process_get_host(MSG_process_from_PID(r_pid));
90   res->data = sockdata;
91   gras_trp_buf_init_sock(res);
92    
93   res->peer_name = strdup(MSG_host_get_name(sockdata->to_host));
94   
95   remote_hd=(gras_hostdata_t *)MSG_host_get_data(sockdata->to_host);
96   xbt_assert0(remote_hd,"Run gras_process_init!!");
97   
98   sockdata->to_chan = -1;
99   res->peer_port = -10;
100   for (cursor=0; cursor<XBT_MAX_CHANNEL; cursor++) {
101     if (remote_hd->proc[cursor] == r_pid) {
102       sockdata->to_chan = cursor;
103       DEBUG2("Chan %d on %s is for my pal",
104              cursor,res->peer_name);
105       
106       xbt_dynar_foreach(remote_hd->ports, cpt, pr) {
107         if (sockdata->to_chan == pr.tochan) {
108           if (pr.meas) {
109             DEBUG0("Damn, it's for measurement");
110             continue;
111           }
112           
113           res->peer_port = pr.port;
114           DEBUG1("Cool, it points to port %d", pr.port);
115           break;
116         } else {
117           DEBUG2("Wrong port (tochan=%d, looking for %d)\n",
118                  pr.tochan,sockdata->to_chan);
119         }
120       }
121       if (res->peer_port == -10) {
122         /* was for measurement */
123         sockdata->to_chan = -1;
124       } else {
125         /* found it, don't let it override by meas */
126         break;
127       }
128     }
129   }
130   res->peer_port = ((gras_trp_procdata_t) gras_libdata_by_name_from_remote("gras_trp",MSG_process_from_PID(r_pid)))->myport;
131   xbt_assert0(sockdata->to_chan != -1,
132               "Got a message from a process without channel");
133   
134   return res;
135 }
136
137   
138 /* dummy implementations of the functions used in RL mode */
139
140 void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
141 void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
142 void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
143
144 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
145