Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b316d32f0963816d6d85fa3d0023037a63a0dfc2
[simgrid.git] / src / gras_simix / Transport / gras_simix_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_simix/Transport/gras_simix_transport_private.h"
12 //#include "msg/msg.h"
13 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
16
17 /* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
18 gras_socket_t _gras_lastly_selected_socket = NULL;
19
20 /**
21  * gras_trp_select:
22  *
23  * Returns the next socket to service having a message awaiting.
24  *
25  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
26  *
27  * if timeout=0, do not wait for new message, only handle the ones already there.
28  *
29  * if timeout>0 and no message there, wait at most that amount of time before giving up.
30  */
31 gras_socket_t gras_trp_select(double timeout) {
32         gras_socket_t res;
33         res = xbt_new(s_gras_socket_t,1);
34         gras_trp_procdata_t pd = (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
35         gras_trp_sg_sock_data_t *sockdata;
36         gras_trp_plugin_t trp;
37         gras_socket_t sock_iter; /* iterating over all sockets */
38         int cursor;
39
40         //gras_sg_portrec_t pr;     /* iterating to find the chanel of expeditor */
41 //      int r_pid;
42         gras_hostdata_t *remote_hd;
43
44         SIMIX_mutex_lock(pd->mutex);
45         DEBUG3("select on %s@%s with timeout=%f",
46                         SIMIX_process_get_name(SIMIX_process_self()),
47                         SIMIX_host_get_name(SIMIX_host_self()),
48                         timeout);
49         //MSG_channel_select_from((m_channel_t) pd->chan, timeout, &r_pid);
50         SIMIX_cond_wait_timeout(pd->cond,pd->mutex,timeout);
51
52         if (pd->active_socket == NULL) {
53                 DEBUG0("TIMEOUT");
54                 THROW0(timeout_error,0,"Timeout");
55         }
56 /*      if (r_pid < 0) {
57                 DEBUG0("TIMEOUT");
58                 THROW0(timeout_error,0,"Timeout");
59         }*/
60
61         /* Ok, got something. Open a socket back to the expeditor */
62
63         /* Try to reuse an already openned socket to that expeditor */
64         xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
65                 DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
66
67                 if (sock_iter->meas || !sock_iter->outgoing)
68                         continue;
69                 if (sock_iter->peer_port == pd->active_socket->port)
70                         return sock_iter;
71
72         }
73
74         /* Socket to expeditor not created yet */
75         DEBUG0("Create a socket to the expeditor");
76
77         trp = gras_trp_plugin_get_by_name("sg");
78
79         gras_trp_socket_new(1,&res);
80         res->plugin   = trp;
81
82         res->incoming  = 1;
83         res->outgoing  = 1;
84         res->accepting = 0;
85         res->sd        = -1;
86
87         res->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->from_process = SIMIX_process_self();
93         sockdata->to_process   = ((gras_trp_sg_sock_data_t*)(pd->active_socket->data))->from_process;
94         
95         /* complicated*/
96         sockdata->to_host  = SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)(pd->active_socket->data))->from_process);
97         res->data = sockdata;
98         gras_trp_buf_init_sock(res);
99
100         res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
101
102         remote_hd=(gras_hostdata_t *)SIMIX_host_get_data(sockdata->to_host);
103         xbt_assert0(remote_hd,"Run gras_process_init!!");
104
105         //sockdata->to_chan = -1;
106         res->peer_port = pd->active_socket->port;
107         /*
108         for (cursor=0; cursor<XBT_MAX_CHANNEL; cursor++) {
109                 if (remote_hd->proc[cursor] == r_pid) {
110                         sockdata->to_chan = cursor;
111                         DEBUG2("Chan %d on %s is for my pal",
112                                         cursor,res->peer_name);
113
114                         xbt_dynar_foreach(remote_hd->ports, cpt, pr) {
115                                 if (sockdata->to_chan == pr.tochan) {
116                                         if (pr.meas) {
117                                                 DEBUG0("Damn, it's for measurement");
118                                                 continue;
119                                         }
120
121                                         res->peer_port = pr.port;
122                                         DEBUG1("Cool, it points to port %d", pr.port);
123                                         break;
124                                 } else {
125                                         DEBUG2("Wrong port (tochan=%d, looking for %d)\n",
126                                                         pr.tochan,sockdata->to_chan);
127                                 }
128                         }
129                         if (res->peer_port == -10) {
130                                 sockdata->to_chan = -1;
131                         } else {
132                                 break;
133                         }
134                 }
135         }
136         */
137         
138         DEBUG1("New socket: Peer port %d", res->peer_port);
139         pd->active_socket=NULL;
140         SIMIX_mutex_unlock(pd->mutex);
141         return res;
142 }
143
144   
145 /* dummy implementations of the functions used in RL mode */
146
147 void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
148 void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
149 void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
150
151 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
152