Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8c81ca2ad36e31d16c6d7484ca9aca802916336e
[simgrid.git] / src / gras / Transport / transport_plugin_sg.c
1 /* $Id$ */
2
3 /* file trp (transport) - send/receive a bunch of bytes in SG realm         */
4
5 /* Note that this is only used to debug other parts of GRAS since message   */
6 /*  exchange in SG realm is implemented directly without mimicing real life */
7 /*  This would be terribly unefficient.                                     */
8
9 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14 #include "xbt/ex.h" 
15
16 #include "msg/msg.h"
17
18 #include "transport_private.h"
19 #include "gras/Virtu/virtu_sg.h"
20
21 XBT_LOG_EXTERNAL_CATEGORY(transport);
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
23
24 /***
25  *** Prototypes 
26  ***/
27 void hexa_print(unsigned char *data, int size);   /* in gras.c */
28
29 /* retrieve the port record associated to a numerical port on an host */
30 static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd);
31
32
33 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
34                                /* OUT */ gras_socket_t sock);
35 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
36                                /* OUT */ gras_socket_t sock);
37 void gras_trp_sg_socket_close(gras_socket_t sd);
38
39 void gras_trp_sg_chunk_send(gras_socket_t sd,
40                             const char *data,
41                             unsigned long int size);
42
43 void gras_trp_sg_chunk_recv(gras_socket_t sd,
44                             char *data,
45                             unsigned long int size,
46                             unsigned long int bufsize);
47
48 /***
49  *** Specific plugin part
50  ***/
51 typedef struct {
52   int placeholder; /* nothing plugin specific so far */
53 } gras_trp_sg_plug_data_t;
54
55
56 /***
57  *** Code
58  ***/
59 static void find_port(gras_hostdata_t *hd, int port,
60                       gras_sg_portrec_t *hpd) {
61   int cpt;
62   gras_sg_portrec_t pr;
63
64   xbt_assert0(hd,"Please run gras_process_init on each process");
65   
66   xbt_dynar_foreach(hd->ports, cpt, pr) {
67     if (pr.port == port) {
68       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
69       return;
70     }
71   }
72   THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
73 }
74
75
76 void
77 gras_trp_sg_setup(gras_trp_plugin_t plug) {
78
79   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
80
81   plug->data      = data; 
82
83   plug->socket_client = gras_trp_sg_socket_client;
84   plug->socket_server = gras_trp_sg_socket_server;
85   plug->socket_close  = gras_trp_sg_socket_close;
86
87   plug->chunk_send    = gras_trp_sg_chunk_send;
88   plug->chunk_recv    = gras_trp_sg_chunk_recv;
89
90   plug->flush         = NULL; /* nothing cached */
91 }
92
93 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
94                                /* OUT */ gras_socket_t sock){
95   xbt_ex_t e;
96
97   m_host_t peer;
98   gras_hostdata_t *hd;
99   gras_trp_sg_sock_data_t *data;
100   gras_sg_portrec_t pr;
101
102   /* make sure this socket will reach someone */
103   if (!(peer=MSG_get_host_by_name(sock->peer_name))) 
104     THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
105
106   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) 
107     THROW1(mismatch_error,0,
108            "can't connect to %s: no process on this host",
109            sock->peer_name);
110   
111   TRY {
112     find_port(hd,sock->peer_port,&pr);
113   } CATCH(e) {
114     if (e.category == mismatch_error) {
115       xbt_ex_free(e);
116       THROW2(mismatch_error,0,
117              "can't connect to %s:%d, no process listen on this port",
118              sock->peer_name,sock->peer_port);
119     } 
120     RETHROW;
121   }
122
123   if (pr.meas && !sock->meas) {
124     THROW2(mismatch_error,0,
125            "can't connect to %s:%d in regular mode, the process listen "
126            "in meas mode on this port",sock->peer_name,sock->peer_port);
127   }
128   if (!pr.meas && sock->meas) {
129     THROW2(mismatch_error,0,
130            "can't connect to %s:%d in meas mode, the process listen "
131            "in regular mode on this port",sock->peer_name,sock->peer_port);
132   }
133
134   /* create the socket */
135   data = xbt_new(gras_trp_sg_sock_data_t,1);
136   data->from_PID     = MSG_process_self_PID();
137   data->to_PID       = hd->proc[ pr.tochan ];
138   data->to_host      = peer;
139   data->to_chan      = pr.tochan;
140   
141   sock->data = data;
142   sock->incoming = 1;
143
144   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
145           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
146           sock->meas?"meas":"regular",
147          sock->peer_name,sock->peer_port,data->to_PID);
148 }
149
150 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
151                                gras_socket_t sock){
152
153   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
154   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
155   gras_sg_portrec_t pr;
156   gras_trp_sg_sock_data_t *data;
157   int found;
158   
159   const char *host=MSG_host_get_name(MSG_host_self());
160
161   xbt_ex_t e;
162
163   xbt_assert0(hd,"Please run gras_process_init on each process");
164
165   sock->accepting = 0; /* no such nuisance in SG */
166
167   found = 0;
168   TRY {
169     find_port(hd,sock->port,&pr);
170     found = 1;
171   } CATCH(e) {
172     if (e.category == mismatch_error)
173       xbt_ex_free(e);
174     else
175       RETHROW;
176   }
177    
178   if (found) 
179     THROW2(mismatch_error,0,
180            "can't listen on address %s:%d: port already in use.",
181            host,sock->port);
182
183   pr.tochan = sock->meas ? pd->measChan : pd->chan;
184   pr.port   = sock->port;
185   pr.meas    = sock->meas;
186   xbt_dynar_push(hd->ports,&pr);
187   
188   /* Create the socket */
189   data = xbt_new(gras_trp_sg_sock_data_t,1);
190   data->from_PID     = -1;
191   data->to_PID       = MSG_process_self_PID();
192   data->to_host      = MSG_host_self();
193   data->to_chan      = pd->chan;
194   
195   sock->data = data;
196
197   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
198     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
199     host,sock->port,sock->meas? " (mode meas)":"",sock);
200 }
201
202 void gras_trp_sg_socket_close(gras_socket_t sock){
203   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
204   int cpt;
205   
206   gras_sg_portrec_t pr;
207
208   XBT_IN1(" (sock=%p)",sock);
209    
210   if (!sock) return;
211   xbt_assert0(hd,"Please run gras_process_init on each process");
212
213   if (sock->data)
214     free(sock->data);
215
216   if (sock->incoming && sock->port >= 0) {
217     /* server mode socket. Unregister it from 'OS' tables */
218     xbt_dynar_foreach(hd->ports, cpt, pr) {
219       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
220       if (pr.port == sock->port) {
221         xbt_dynar_cursor_rm(hd->ports, &cpt);
222         XBT_OUT;
223         return;
224       }
225     }
226     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
227           sock,sock->port);
228   }
229   XBT_OUT;
230 }
231
232 typedef struct {
233   int size;
234   void *data;
235 } sg_task_data_t;
236
237 void gras_trp_sg_chunk_send(gras_socket_t sock,
238                             const char *data,
239                             unsigned long int size) {
240   m_task_t task=NULL;
241   static unsigned int count=0;
242   char name[256];
243   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
244   sg_task_data_t *task_data;
245   
246   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
247
248   sprintf(name,"Chunk[%d]",count++);
249
250   task_data=xbt_new(sg_task_data_t,1);
251   task_data->data=(void*)xbt_malloc(size);
252   task_data->size = size;
253   memcpy(task_data->data,data,size);
254
255   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
256
257   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
258          name, MSG_host_get_name(MSG_host_self()),
259          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
260   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
261     THROW0(system_error,0,"Problem during the MSG_task_put");
262   }
263 }
264
265 void gras_trp_sg_chunk_recv(gras_socket_t sock,
266                             char *data,
267                             unsigned long int size,
268                             unsigned long int bufsize){
269   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
270
271   m_task_t task=NULL;
272   sg_task_data_t *task_data;
273   gras_trp_sg_sock_data_t *sock_data = sock->data;
274
275   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
276   XBT_IN;
277   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
278          MSG_host_get_name(sock_data->to_host),
279          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
280   if (MSG_task_get(&task, (sock->meas ? pd->measChan : pd->chan)) != MSG_OK)
281     THROW0(system_error,0,"Error in MSG_task_get()");
282   DEBUG1("Got chuck %s",MSG_task_get_name(task));
283
284   task_data = MSG_task_get_data(task);
285   if (size != -1) {     
286      if (task_data->size != size)
287        THROW5(mismatch_error,0,
288               "Got %d bytes when %ld where expected (in %s->%s:%d)",
289               task_data->size, size,
290               MSG_host_get_name(sock_data->to_host),
291               MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
292      memcpy(data,task_data->data,size);
293   } else {
294      /* damn, the size is embeeded at the begining of the chunk */
295      int netsize;
296      
297      memcpy((char*)&netsize,task_data->data,4);
298      DEBUG1("netsize embeeded = %d",netsize);
299
300      memcpy(data,task_data->data,netsize+4);
301   }
302   free(task_data->data);
303   free(task_data);
304
305   if (MSG_task_destroy(task) != MSG_OK)
306     THROW0(system_error,0,"Error in MSG_task_destroy()");
307
308   XBT_OUT;
309 }
310