Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b231827b9af931aa006a32b142fbff6abd8970c
[simgrid.git] / src / gras / Transport / sg_transport.c
1 /* $Id$ */
2
3 /* sg_transport - SG 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 "Transport/transport_private.h"
12 #include <msg.h>
13 #include "Virtu/virtu_sg.h"
14
15 GRAS_LOG_EXTERNAL_CATEGORY(transport);
16 GRAS_LOG_DEFAULT_CATEGORY(transport);
17
18 /**
19  * gras_trp_select:
20  *
21  * Returns the next socket to service having a message awaiting.
22  *
23  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
24  *
25  * if timeout=0, do not wait for new message, only handle the ones already there.
26  *
27  * if timeout>0 and no message there, wait at most that amount of time before giving up.
28  */
29 gras_error_t 
30 gras_trp_select(double timeout, 
31                 gras_socket_t **dst) {
32
33   gras_error_t errcode;
34   double startTime=gras_os_time();
35   gras_procdata_t *pd=gras_procdata_get();
36   gras_trp_sg_sock_data_t *sockdata;
37   gras_trp_plugin_t *trp;
38
39   gras_socket_t *sock_iter; /* iterating over all sockets */
40   int cursor,cpt;
41
42   gras_sg_portrec_t pr;     /* iterating to find the chanel of expeditor */
43
44   int r_pid;
45   gras_hostdata_t *remote_hd;
46
47   DEBUG3("select on %s@%s with timeout=%f",
48          MSG_process_get_name(MSG_process_self()),
49          MSG_host_get_name(MSG_host_self()),
50          timeout);
51   do {
52     r_pid = MSG_task_probe_from((m_channel_t) pd->chan);
53     if (r_pid >= 0) {
54       /* Try to reuse an already openned socket to that expeditor */
55       gras_dynar_foreach(pd->sockets,cursor,sock_iter) {
56         DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
57         sockdata = sock_iter->data;
58
59         if (sock_iter->raw || !sock_iter->outgoing)
60           continue;
61
62         if (sockdata->from_PID == r_pid) {
63           *dst=sock_iter;
64           return no_error;
65         }
66       }
67
68       /* Socket to expeditor not created yet */
69       DEBUG0("Create a socket to the expeditor");
70
71       TRY(gras_trp_plugin_get_by_name("sg",&trp));
72
73       TRY(gras_trp_socket_new(1,dst));
74       (*dst)->plugin   = trp;
75
76       (*dst)->incoming  = 1;
77       (*dst)->outgoing  = 1;
78       (*dst)->accepting = 0;
79       (*dst)->sd        = -1;
80
81       (*dst)->port      = -1;
82
83       if (!(sockdata = malloc(sizeof(gras_trp_sg_sock_data_t))))
84         RAISE_MALLOC;
85
86       sockdata->from_PID = MSG_process_self_PID();
87       sockdata->to_PID   = r_pid;
88       sockdata->to_host  = MSG_process_get_host(MSG_process_from_PID(r_pid));
89       (*dst)->data = sockdata;
90
91       (*dst)->peer_name = strdup(MSG_host_get_name(sockdata->to_host));
92
93       remote_hd=(gras_hostdata_t *)MSG_host_get_data(sockdata->to_host);
94       gras_assert0(remote_hd,"Run gras_process_init!!");
95
96       sockdata->to_chan = -1;
97       (*dst)->peer_port = -10;
98       for (cursor=0; cursor<GRAS_MAX_CHANNEL; cursor++) {
99         if (remote_hd->proc[cursor] == r_pid) {
100           sockdata->to_chan = cursor;
101           DEBUG2("Chan %d on %s is for my pal",
102                  cursor,(*dst)->peer_name);
103
104           gras_dynar_foreach(remote_hd->ports, cpt, pr) {
105             if (sockdata->to_chan == pr.tochan) {
106               if (pr.raw) {
107                 DEBUG0("Damn, it's raw");
108                 continue;
109               }
110
111               (*dst)->peer_port = pr.port;
112               DEBUG1("Cool, it points to port %d", pr.port);
113               break;
114             } else {
115               DEBUG2("Wrong port (tochan=%d, looking for %d)\n",
116                      pr.tochan,sockdata->to_chan);
117             }
118           }
119           if ((*dst)->peer_port == -10) {
120             /* was raw */
121             sockdata->to_chan = -1;
122           } else {
123             /* found it, don't let it override by raw */
124             break;
125           }
126         }
127       }
128       gras_assert0(sockdata->to_chan != -1,
129                    "Got a message from a process without channel");
130
131       return no_error;
132     } else {
133       /*
134       DEBUG2("Select on %s@%s did not find anything yet",
135              MSG_process_get_name(MSG_process_self()),
136              MSG_host_get_name(MSG_host_self()));
137       */
138       // MSG_process_sleep(1);
139       MSG_process_sleep(0.01);
140     }
141   } while (gras_os_time()-startTime < timeout
142            || MSG_task_Iprobe((m_channel_t) pd->chan));
143
144   return timeout_error;
145
146 }
147
148   
149 /* dummy implementations of the functions used in RL mode */
150
151 gras_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
152   return mismatch_error;
153 }
154 gras_error_t gras_trp_file_setup(gras_trp_plugin_t *plug) {
155   return mismatch_error;
156 }