Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
729920570b2231a67241a8d1f3564dd514a59042
[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 "gras/Virtu/virtu_sg.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
15
16 /* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
17 gras_socket_t _gras_lastly_selected_socket = NULL;
18
19 /**     
20  * gras_trp_select:
21  *
22  * Returns the next socket to service having a message awaiting.
23  *
24  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
25  *
26  * if timeout=0, do not wait for new message, only handle the ones already there.
27  *
28  * if timeout>0 and no message there, wait at most that amount of time before giving up.
29  */
30 gras_socket_t gras_trp_select(double timeout) {
31   gras_socket_t res;
32   gras_trp_procdata_t pd = 
33     (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
34   gras_trp_sg_sock_data_t *sockdata;
35   gras_trp_plugin_t trp;
36   gras_socket_t active_socket;
37   gras_trp_sg_sock_data_t *active_socket_data;
38   gras_socket_t sock_iter; /* iterating over all sockets */
39   int cursor;
40
41   DEBUG0("Trying to get the lock pd, trp_select");
42   SIMIX_mutex_lock(pd->msg_select_mutex);
43   DEBUG3("select on %s@%s with timeout=%f",
44          SIMIX_process_get_name(SIMIX_process_self()),
45          SIMIX_host_get_name(SIMIX_host_self()),
46          timeout);
47
48   if (xbt_fifo_size(pd->msg_selectable_sockets) == 0) {
49     /* message didn't arrive yet, wait */
50     SIMIX_cond_wait_timeout(pd->msg_select_cond,pd->msg_select_mutex,timeout);
51   }
52
53   if (xbt_fifo_size(pd->msg_selectable_sockets) == 0) {
54     DEBUG0("TIMEOUT");
55     SIMIX_mutex_unlock(pd->msg_select_mutex);
56     THROW0(timeout_error,0,"Timeout");
57   }
58   active_socket = xbt_fifo_shift(pd->msg_selectable_sockets);
59   active_socket_data = (gras_trp_sg_sock_data_t*)active_socket->data;
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   DEBUG1("Open sockets size %lu",xbt_dynar_length(pd->sockets));
65   xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
66     gras_trp_sg_sock_data_t *sock_data;
67     DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
68     
69     if (sock_iter->meas || !sock_iter->outgoing)
70       continue;
71     sock_data = ((gras_trp_sg_sock_data_t*)sock_iter->data);
72
73     if ( (sock_data->to_socket == active_socket) && 
74          (sock_data->to_host == SIMIX_process_get_host(active_socket_data->from_process)) ) {
75       SIMIX_mutex_unlock(pd->msg_select_mutex);
76       return sock_iter;
77     }
78   }
79
80   /* Socket to expeditor not created yet */
81   DEBUG0("Create a socket to the expeditor");
82
83   trp = gras_trp_plugin_get_by_name("sg");
84
85   gras_trp_socket_new(1,&res);
86   res->plugin   = trp;
87
88   res->incoming  = 1;
89   res->outgoing  = 1;
90   res->accepting = 0;
91   res->sd        = -1;
92
93   res->port = -1;
94
95   /* initialize the ports */
96   //res->peer_port = active_socket->port;
97   res->port = active_socket->peer_port;
98   
99   /* create sockdata */
100   sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
101   sockdata->from_process = SIMIX_process_self();
102   sockdata->to_process   = active_socket_data->from_process;
103         
104   res->peer_port = 
105     ((gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sockdata->to_process))->myport;
106   sockdata->to_socket = active_socket;
107   /*update the peer to_socket  variable */
108   active_socket_data->to_socket = res;
109   sockdata->cond = SIMIX_cond_init();
110   sockdata->mutex = SIMIX_mutex_init();
111
112   sockdata->to_host = SIMIX_process_get_host(active_socket_data->from_process);
113
114   res->data = sockdata;
115   res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
116
117   gras_trp_buf_init_sock(res);
118
119   DEBUG4("Create socket to process:%s(Port %d) from process: %s(Port %d)",
120          SIMIX_process_get_name(sockdata->from_process),
121          res->peer_port,
122          SIMIX_process_get_name(sockdata->to_process), res->port);
123
124   SIMIX_mutex_unlock(pd->msg_select_mutex);
125   return res;
126 }
127
128   
129 /* dummy implementations of the functions used in RL mode */
130
131 void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
132 void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
133 void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
134
135 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
136