Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functions gras_trp_sg_chunk_recv and send added.
[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         gras_trp_procdata_t pd = (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_socket_t sock_iter; /* iterating over all sockets */
38         int cursor;
39 //      int i;
40
41         gras_hostdata_t *remote_hd;
42 //      gras_hostdata_t *local_hd;
43         
44         DEBUG0("Trying to get the lock pd, trp_select");
45         SIMIX_mutex_lock(pd->mutex);
46         DEBUG3("select on %s@%s with timeout=%f",
47                         SIMIX_process_get_name(SIMIX_process_self()),
48                         SIMIX_host_get_name(SIMIX_host_self()),
49                         timeout);
50
51         if (xbt_fifo_size(pd->active_socket) == 0) {
52         /* message didn't arrive yet, wait */
53                 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,timeout);
54         }
55
56         if (xbt_fifo_size(pd->active_socket) == 0) {
57                 DEBUG0("TIMEOUT");
58                 SIMIX_mutex_unlock(pd->mutex);
59                 THROW0(timeout_error,0,"Timeout");
60         }
61         active_socket = xbt_fifo_shift(pd->active_socket);
62         
63         /* Ok, got something. Open a socket back to the expeditor */
64
65         /* Try to reuse an already openned socket to that expeditor */
66         DEBUG1("Open sockets size %lu",xbt_dynar_length(pd->sockets));
67         xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
68                 DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
69
70                 if (sock_iter->meas || !sock_iter->outgoing)
71                         continue;
72                 if ((sock_iter->peer_port == active_socket->port) && 
73                                 (((gras_trp_sg_sock_data_t*)sock_iter->data)->to_host == SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)active_socket->data)->from_process))) {
74                         SIMIX_mutex_unlock(pd->mutex);
75                         return sock_iter;
76                 }
77         }
78
79         /* Socket to expeditor not created yet */
80         DEBUG0("Create a socket to the expeditor");
81
82         trp = gras_trp_plugin_get_by_name("sg");
83
84         gras_trp_socket_new(1,&res);
85         res->plugin   = trp;
86
87         res->incoming  = 1;
88         res->outgoing  = 1;
89         res->accepting = 0;
90         res->sd        = -1;
91
92         res->port = -1;
93
94         sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
95         sockdata->from_process = SIMIX_process_self();
96         sockdata->to_process   = ((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process;
97         
98         DEBUG2("Create socket to process:%s from process: %s",SIMIX_process_get_name(sockdata->from_process),SIMIX_process_get_name(sockdata->to_process));
99         /* complicated*/
100         sockdata->to_host  = SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process);
101
102         res->data = sockdata;
103         gras_trp_buf_init_sock(res);
104
105         res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
106
107         remote_hd=(gras_hostdata_t *)SIMIX_host_get_data(sockdata->to_host);
108         xbt_assert0(remote_hd,"Run gras_process_init!!");
109
110         res->peer_port = active_socket->port;
111
112         res->port = active_socket->peer_port;
113         /* search for a free port on the host */
114         /*
115         local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
116         for (i=1;i<65536;i++) {
117                 if (local_hd->cond_port[i] == NULL)
118                         break;
119         }
120         if (i == 65536) {
121                 SIMIX_mutex_unlock(pd->mutex);
122                 THROW0(system_error,0,"No port free");
123         }
124         res->port = i;*/
125         /*initialize the cond and mutex */
126         /*
127         local_hd->cond_port[i] = SIMIX_cond_init();
128         local_hd->mutex_port[i] = SIMIX_mutex_init();
129         */
130         /* update remote peer_port to talk through the new port */
131         active_socket->peer_port = res->port;
132
133
134
135         DEBUG2("New socket: Peer port %d Local port %d", res->peer_port, res->port);
136         SIMIX_mutex_unlock(pd->mutex);
137         return res;
138 }
139
140   
141 /* dummy implementations of the functions used in RL mode */
142
143 void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
144 void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
145 void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
146
147 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
148