Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e777b589a79176d784d74b5e4f312d23fb99ca15
[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
47 /***
48  *** Specific plugin part
49  ***/
50 typedef struct {
51   int placeholder; /* nothing plugin specific so far */
52 } gras_trp_sg_plug_data_t;
53
54
55 /***
56  *** Code
57  ***/
58 static void find_port(gras_hostdata_t *hd, int port,
59                       gras_sg_portrec_t *hpd) {
60   int cpt;
61   gras_sg_portrec_t pr;
62
63   xbt_assert0(hd,"Please run gras_process_init on each process");
64   
65   xbt_dynar_foreach(hd->ports, cpt, pr) {
66     if (pr.port == port) {
67       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
68       return;
69     }
70   }
71   THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
72 }
73
74
75 void
76 gras_trp_sg_setup(gras_trp_plugin_t plug) {
77
78   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
79
80   plug->data      = data; 
81
82   plug->socket_client = gras_trp_sg_socket_client;
83   plug->socket_server = gras_trp_sg_socket_server;
84   plug->socket_close  = gras_trp_sg_socket_close;
85
86   plug->chunk_send    = gras_trp_sg_chunk_send;
87   plug->chunk_recv    = gras_trp_sg_chunk_recv;
88
89   plug->flush         = NULL; /* nothing cached */
90 }
91
92 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
93                                /* OUT */ gras_socket_t sock){
94   xbt_ex_t e;
95
96   m_host_t peer;
97   gras_hostdata_t *hd;
98   gras_trp_sg_sock_data_t *data;
99   gras_sg_portrec_t pr;
100
101   /* make sure this socket will reach someone */
102   if (!(peer=MSG_get_host_by_name(sock->peer_name))) 
103     THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
104
105   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) 
106     THROW1(mismatch_error,0,
107            "can't connect to %s: no process on this host",
108            sock->peer_name);
109   
110   TRY {
111     find_port(hd,sock->peer_port,&pr);
112   } CATCH(e) {
113     if (e.category == mismatch_error) {
114       xbt_ex_free(e);
115       THROW2(mismatch_error,0,
116              "can't connect to %s:%d, no process listen on this port",
117              sock->peer_name,sock->peer_port);
118     } 
119     RETHROW;
120   }
121
122   if (pr.meas && !sock->meas) {
123     THROW2(mismatch_error,0,
124            "can't connect to %s:%d in regular mode, the process listen "
125            "in meas mode on this port",sock->peer_name,sock->peer_port);
126   }
127   if (!pr.meas && sock->meas) {
128     THROW2(mismatch_error,0,
129            "can't connect to %s:%d in meas mode, the process listen "
130            "in regular mode on this port",sock->peer_name,sock->peer_port);
131   }
132
133   /* create the socket */
134   data = xbt_new(gras_trp_sg_sock_data_t,1);
135   data->from_PID     = MSG_process_self_PID();
136   data->to_PID       = hd->proc[ pr.tochan ];
137   data->to_host      = peer;
138   data->to_chan      = pr.tochan;
139   
140   sock->data = data;
141   sock->incoming = 1;
142
143   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
144           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
145           sock->meas?"meas":"regular",
146          sock->peer_name,sock->peer_port,data->to_PID);
147 }
148
149 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
150                                gras_socket_t sock){
151
152   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
153   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
154   gras_sg_portrec_t pr;
155   gras_trp_sg_sock_data_t *data;
156   int found;
157   
158   const char *host=MSG_host_get_name(MSG_host_self());
159
160   xbt_ex_t e;
161
162   xbt_assert0(hd,"Please run gras_process_init on each process");
163
164   sock->accepting = 0; /* no such nuisance in SG */
165
166   found = 0;
167   TRY {
168     find_port(hd,sock->port,&pr);
169     found = 1;
170   } CATCH(e) {
171     if (e.category == mismatch_error)
172       xbt_ex_free(e);
173     else
174       RETHROW;
175   }
176    
177   if (found) 
178     THROW2(mismatch_error,0,
179            "can't listen on address %s:%d: port already in use.",
180            host,sock->port);
181
182   pr.tochan = sock->meas ? pd->measChan : pd->chan;
183   pr.port   = sock->port;
184   pr.meas    = sock->meas;
185   xbt_dynar_push(hd->ports,&pr);
186   
187   /* Create the socket */
188   data = xbt_new(gras_trp_sg_sock_data_t,1);
189   data->from_PID     = -1;
190   data->to_PID       = MSG_process_self_PID();
191   data->to_host      = MSG_host_self();
192   data->to_chan      = pd->chan;
193   
194   sock->data = data;
195
196   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
197     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
198     host,sock->port,sock->meas? " (mode meas)":"",sock);
199 }
200
201 void gras_trp_sg_socket_close(gras_socket_t sock){
202   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
203   int cpt;
204   
205   gras_sg_portrec_t pr;
206
207   XBT_IN1(" (sock=%p)",sock);
208    
209   if (!sock) return;
210   xbt_assert0(hd,"Please run gras_process_init on each process");
211
212   if (sock->data)
213     free(sock->data);
214
215   if (sock->incoming && sock->port >= 0) {
216     /* server mode socket. Unregister it from 'OS' tables */
217     xbt_dynar_foreach(hd->ports, cpt, pr) {
218       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
219       if (pr.port == sock->port) {
220         xbt_dynar_cursor_rm(hd->ports, &cpt);
221         XBT_OUT;
222         return;
223       }
224     }
225     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
226           sock,sock->port);
227   }
228   XBT_OUT;
229 }
230
231 typedef struct {
232   int size;
233   void *data;
234 } sg_task_data_t;
235
236 void gras_trp_sg_chunk_send(gras_socket_t sock,
237                             const char *data,
238                             unsigned long int size) {
239   m_task_t task=NULL;
240   static unsigned int count=0;
241   char name[256];
242   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
243   sg_task_data_t *task_data;
244   
245   sprintf(name,"Chunk[%d]",count++);
246
247   task_data=xbt_new(sg_task_data_t,1);
248   task_data->data=(void*)xbt_malloc(size);
249   task_data->size = size;
250   memcpy(task_data->data,data,size);
251
252   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
253
254   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
255          name, MSG_host_get_name(MSG_host_self()),
256          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
257   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
258     THROW0(system_error,0,"Problem during the MSG_task_put");
259   }
260 }
261
262 void gras_trp_sg_chunk_recv(gras_socket_t sock,
263                             char *data,
264                             unsigned long int size){
265   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
266
267   m_task_t task=NULL;
268   sg_task_data_t *task_data;
269   gras_trp_sg_sock_data_t *sock_data = sock->data;
270
271   XBT_IN;
272   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
273          MSG_host_get_name(sock_data->to_host),
274          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
275   if (MSG_task_get(&task, (sock->meas ? pd->measChan : pd->chan)) != MSG_OK)
276     THROW0(system_error,0,"Error in MSG_task_get()");
277   DEBUG1("Got chuck %s",MSG_task_get_name(task));
278
279   task_data = MSG_task_get_data(task);
280   if (size != -1) {     
281      if (task_data->size != size)
282        THROW5(mismatch_error,0,
283               "Got %d bytes when %ld where expected (in %s->%s:%d)",
284               task_data->size, size,
285               MSG_host_get_name(sock_data->to_host),
286               MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
287      memcpy(data,task_data->data,size);
288   } else {
289      /* damn, the size is embeeded at the begining of the chunk */
290      int netsize;
291      
292      memcpy((char*)&netsize,task_data->data,4);
293      DEBUG1("netsize embeeded = %d",netsize);
294
295      memcpy(data,task_data->data,netsize+4);
296   }
297   free(task_data->data);
298   free(task_data);
299
300   if (MSG_task_destroy(task) != MSG_OK)
301     THROW0(system_error,0,"Error in MSG_task_destroy()");
302
303   XBT_OUT;
304 }
305