Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
971870fae95a6bed55e2373312c8b863c0923c96
[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 /* Authors: Martin Quinson                                                  */
10 /* Copyright (C) 2004 Martin Quinson.                                       */
11
12 /* This program is free software; you can redistribute it and/or modify it
13    under the terms of the license (GNU LGPL) which comes with this package. */
14
15 #include <msg.h>
16
17 #include "gras_private.h"
18 #include "transport_private.h"
19 #include "Virtu/virtu_sg.h"
20
21 GRAS_LOG_EXTERNAL_CATEGORY(transport);
22 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport);
23
24 /***
25  *** Prototypes 
26  ***/
27 /* retrieve the port record associated to a numerical port on an host */
28 static gras_error_t find_port(gras_hostdata_t *hd, int port,
29                               gras_sg_portrec_t *hpd);
30
31
32 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
33                                        const char *host,
34                                        unsigned short port,
35                                        /* OUT */ gras_socket_t *sock);
36 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
37                                        unsigned short port,
38                                        /* OUT */ gras_socket_t *sock);
39 void         gras_trp_sg_socket_close(gras_socket_t *sd);
40
41 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sd,
42                                     char *data,
43                                     size_t size);
44
45 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sd,
46                                     char *data,
47                                     size_t size);
48
49 /* FIXME
50   gras_error_t gras_trp_sg_flush(gras_socket_t *sd);
51 */
52
53 /***
54  *** Specific plugin part
55  ***/
56 typedef struct {
57   int placeholder; /* nothing plugin specific so far */
58 } gras_trp_sg_plug_data_t;
59
60 /***
61  *** Specific socket part
62  ***/
63 typedef struct {
64   int from_PID;    /* process which sent this message */
65   int to_PID;      /* process to which this message is destinated */
66
67   m_host_t to_host;   /* Who's on other side */
68   m_channel_t to_chan;/* Channel on which the other side is earing */
69 } gras_trp_sg_sock_data_t;
70
71
72
73 /***
74  *** Code
75  ***/
76 static gras_error_t find_port(gras_hostdata_t *hd, int port,
77                               gras_sg_portrec_t *hpd) {
78   int cpt;
79   gras_sg_portrec_t pd;
80
81   gras_assert0(hd,"Please run gras_process_init on each process");
82   
83   gras_dynar_foreach(hd->ports, cpt, pd) {
84     if (pd.port == port) {
85       memcpy(hpd,&pd,sizeof(gras_sg_portrec_t));
86       return no_error;
87     }
88   }
89   return mismatch_error;
90 }
91
92
93 gras_error_t
94 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
95
96   gras_trp_sg_plug_data_t *data=malloc(sizeof(gras_trp_sg_plug_data_t));
97
98   if (!data)
99     RAISE_MALLOC;
100
101   plug->data      = data; 
102
103   plug->socket_client = gras_trp_sg_socket_client;
104   plug->socket_server = gras_trp_sg_socket_server;
105   plug->socket_close  = gras_trp_sg_socket_close;
106
107   plug->chunk_send    = gras_trp_sg_chunk_send;
108   plug->chunk_recv    = gras_trp_sg_chunk_recv;
109
110
111   return no_error;
112 }
113
114 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
115                                        const char *host,
116                                        unsigned short port,
117                                        /* OUT */ gras_socket_t *sock){
118
119   gras_error_t errcode;
120
121   m_host_t peer;
122   gras_hostdata_t *hd;
123   gras_trp_sg_sock_data_t *data;
124   gras_sg_portrec_t pr;
125
126   /* make sure this socket will reach someone */
127   if (!(peer=MSG_get_host_by_name(host))) {
128       fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
129       return mismatch_error;
130   }
131   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
132     RAISE1(mismatch_error,
133            "can't connect to %s: no process on this host",
134            host);
135   }
136   errcode = find_port(hd,port,&pr);
137   if (errcode != no_error && errcode != mismatch_error) 
138     return errcode;
139
140   if (errcode == mismatch_error) {
141     RAISE2(mismatch_error,
142            "can't connect to %s:%d, no process listen on this port",
143            host,port);
144   } 
145
146   if (pr.raw && !sock->raw) {
147     RAISE2(mismatch_error,
148            "can't connect to %s:%d in regular mode, the process listen "
149            "in raw mode on this port",host,port);
150   }
151   if (!pr.raw && sock->raw) {
152     RAISE2(mismatch_error,
153            "can't connect to %s:%d in raw mode, the process listen "
154            "in regular mode on this port",host,port);
155   }
156
157   /* create the socket */
158   if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
159     RAISE_MALLOC;
160   
161   data->from_PID     = MSG_process_self_PID();
162   data->to_PID       = hd->proc[ pr.tochan ];
163   data->to_host      = peer;
164   data->to_chan      = pr.tochan;
165   
166   sock->data = data;
167
168   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
169           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
170           sock->raw?"RAW":"regular",host,port,data->to_PID);
171
172   return no_error;
173 }
174
175 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
176                                        unsigned short port,
177                                        gras_socket_t *sock){
178
179   gras_error_t errcode;
180
181   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
182   gras_procdata_t *pd=gras_procdata_get();
183   gras_sg_portrec_t pr;
184   gras_trp_sg_sock_data_t *data;
185   
186   const char *host=MSG_host_get_name(MSG_host_self());
187
188   gras_assert0(hd,"Please run gras_process_init on each process");
189
190   sock->accepting = 0; /* no such nuisance in SG */
191
192   errcode = find_port(hd,port,&pr);
193   switch (errcode) {
194   case no_error: /* Port already used... */
195     RAISE2(mismatch_error,
196            "can't listen on address %s:%d: port already in use\n.",
197            host,port);
198
199   case mismatch_error: /* Port not used so far. Do it */
200     pr.tochan = sock->raw ? pd->rawChan : pd->chan;
201     pr.port   = port;
202     pr.raw    = sock->raw;
203     TRY(gras_dynar_push(hd->ports,&pr));
204     
205     if (sock->raw) {
206       if (pd->rawSock) 
207         WARN1("asked to open two raw server sockets on %s, first one lost",
208               MSG_host_get_name(MSG_host_self()));
209       pd->rawSock = sock;
210     } else {
211       if (pd->sock) 
212         WARN1("asked to open two server sockets on %s, first one lost",
213               MSG_host_get_name(MSG_host_self()));
214       pd->sock = sock;
215     }
216
217   default:
218     return errcode;
219   }
220   
221   /* Create the socket */
222   if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
223     RAISE_MALLOC;
224   
225   data->from_PID     = -1;
226   data->to_PID       = MSG_process_self_PID();
227   data->to_host      = MSG_host_self();
228   data->to_chan      = pd->chan;
229   
230   sock->data = data;
231
232   INFO6("'%s' (%d) ears on %s:%d%s (%p)",
233     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
234     host,port,sock->raw? " (mode RAW)":"",*sock);
235
236   return no_error;
237 }
238
239 void gras_trp_sg_socket_close(gras_socket_t *sock){
240   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
241   gras_procdata_t *pd=gras_procdata_get();
242   int cpt;
243   gras_sg_portrec_t *pr;
244
245   if (!sock) return;
246   gras_assert0(hd,"Please run gras_process_init on each process");
247
248   free(sock->data);
249
250   if (sock->incoming) {
251     /* server mode socket. Un register it from 'OS' tables */
252     gras_dynar_foreach(hd->ports, cpt, pr) {
253       if (pr->port == sock->port) {
254         gras_dynar_cursor_rm(hd->ports, &cpt);
255
256         if (sock->raw) {
257           pd->rawSock = NULL;
258         } else {
259           pd->sock = NULL;
260         }
261         return;
262       }
263     }
264     WARN0("socket_close called on an unknown socket");
265   }
266 }
267
268 typedef struct {
269   int size;
270   void *data;
271 } sg_task_data_t;
272
273 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
274                                     char *data,
275                                     size_t size) {
276   m_task_t task=NULL;
277   static unsigned int count=0;
278   char name[256];
279   gras_trp_sg_sock_data_t *sock_data;
280   sg_task_data_t *task_data;
281   
282   sock_data = (gras_trp_sg_sock_data_t *)sock->data;
283
284   sprintf(name,"Chunk[%d]",count++);
285
286   if (!(task_data=malloc(sizeof(sg_task_data_t)))) 
287     RAISE_MALLOC;
288   if (!(task_data->data=malloc(size)))
289     RAISE_MALLOC;
290   task_data->size = size;
291   memcpy(task_data->data,data,size);
292
293   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
294
295   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
296     RAISE0(system_error,"Problem during the MSG_task_put");
297   }
298
299   return no_error;
300 }
301
302 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
303                                     char *data,
304                                     size_t size){
305   gras_procdata_t *pd=gras_procdata_get();
306
307   m_task_t task=NULL;
308   sg_task_data_t *task_data;
309
310   if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
311     RAISE0(unknown_error,"Error in MSG_task_get()");
312
313   task_data = MSG_task_get_data(task);
314   gras_assert2(task_data->size == size,
315                "Got %d bytes when %d where expected",
316                task_data->size, size);
317   memcpy(data,task_data->data,size);
318   free(task_data->data);
319   free(task_data);
320
321   if (MSG_task_destroy(task) != MSG_OK)
322     RAISE0(unknown_error,"Error in MSG_task_destroy()");
323
324   return no_error;
325 }
326
327 /*FIXME
328
329 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){
330   RAISE_UNIMPLEMENTED;
331 }
332 */