Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4448111a872a6e04c8d873b5cfb9cda9586e991b
[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 "gras/Transport/transport_private.h"
11 #include "msg/msg.h"
12 #include "gras/Virtu/virtu_sg.h"
13
14 XBT_LOG_EXTERNAL_CATEGORY(transport);
15 XBT_LOG_DEFAULT_CATEGORY(transport);
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 xbt_error_t 
29 gras_trp_select(double timeout, 
30                 gras_socket_t *dst) {
31
32   xbt_error_t errcode;
33   double startTime=gras_os_time();
34   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
35   gras_trp_sg_sock_data_t *sockdata;
36   gras_trp_plugin_t trp;
37
38   gras_socket_t sock_iter; /* iterating over all sockets */
39   int cursor,cpt;
40
41   gras_sg_portrec_t pr;     /* iterating to find the chanel of expeditor */
42
43   int r_pid;
44   gras_hostdata_t *remote_hd;
45
46   DEBUG3("select on %s@%s with timeout=%f",
47          MSG_process_get_name(MSG_process_self()),
48          MSG_host_get_name(MSG_host_self()),
49          timeout);
50
51   TRYOLD(MSG_channel_select_from((m_channel_t) pd->chan, timeout, &r_pid));
52   
53   if (r_pid < 0) {
54     DEBUG0("TIMEOUT");
55     return timeout_error;
56   }
57
58   /* Ok, got something. Open a socket back to the expeditor */
59
60   /* Try to reuse an already openned socket to that expeditor */
61   xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
62     DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
63     sockdata = sock_iter->data;
64     
65     if (sock_iter->meas || !sock_iter->outgoing)
66       continue;
67     
68     if (sockdata->to_PID == r_pid) {
69       *dst=sock_iter;
70       return no_error;
71     }
72   }
73   
74   /* Socket to expeditor not created yet */
75   DEBUG0("Create a socket to the expeditor");
76   
77   TRYOLD(gras_trp_plugin_get_by_name("buf",&trp));
78   
79   gras_trp_socket_new(1,dst);
80   (*dst)->plugin   = trp;
81   
82   (*dst)->incoming  = 1;
83   (*dst)->outgoing  = 1;
84   (*dst)->accepting = 0;
85   (*dst)->sd        = -1;
86   
87   (*dst)->port      = -1;
88   
89   sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
90   sockdata->from_PID = MSG_process_self_PID();
91   sockdata->to_PID   = r_pid;
92   sockdata->to_host  = MSG_process_get_host(MSG_process_from_PID(r_pid));
93   (*dst)->data = sockdata;
94   gras_trp_buf_init_sock(*dst);
95   
96   (*dst)->peer_name = strdup(MSG_host_get_name(sockdata->to_host));
97   
98   remote_hd=(gras_hostdata_t *)MSG_host_get_data(sockdata->to_host);
99   xbt_assert0(remote_hd,"Run gras_process_init!!");
100   
101   sockdata->to_chan = -1;
102   (*dst)->peer_port = -10;
103   for (cursor=0; cursor<XBT_MAX_CHANNEL; cursor++) {
104     if (remote_hd->proc[cursor] == r_pid) {
105       sockdata->to_chan = cursor;
106       DEBUG2("Chan %d on %s is for my pal",
107              cursor,(*dst)->peer_name);
108       
109       xbt_dynar_foreach(remote_hd->ports, cpt, pr) {
110         if (sockdata->to_chan == pr.tochan) {
111           if (pr.meas) {
112             DEBUG0("Damn, it's for measurement");
113             continue;
114           }
115           
116           (*dst)->peer_port = pr.port;
117           DEBUG1("Cool, it points to port %d", pr.port);
118           break;
119         } else {
120           DEBUG2("Wrong port (tochan=%d, looking for %d)\n",
121                  pr.tochan,sockdata->to_chan);
122         }
123       }
124       if ((*dst)->peer_port == -10) {
125         /* was for measurement */
126         sockdata->to_chan = -1;
127       } else {
128         /* found it, don't let it override by meas */
129         break;
130       }
131     }
132   }
133   xbt_assert0(sockdata->to_chan != -1,
134               "Got a message from a process without channel");
135   
136   return no_error;
137 }
138
139   
140 /* dummy implementations of the functions used in RL mode */
141
142 xbt_error_t gras_trp_tcp_setup(gras_trp_plugin_t plug) {
143   return mismatch_error;
144 }
145 xbt_error_t gras_trp_file_setup(gras_trp_plugin_t plug) {
146   return mismatch_error;
147 }
148
149
150