Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MSG is freeing the pointer it gives me as argv. That's thus a bad idea to realloc it
[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 /***
62  *** Code
63  ***/
64 static gras_error_t find_port(gras_hostdata_t *hd, int port,
65                               gras_sg_portrec_t *hpd) {
66   int cpt;
67   gras_sg_portrec_t pd;
68
69   gras_assert0(hd,"Please run gras_process_init on each process");
70   
71   gras_dynar_foreach(hd->ports, cpt, pd) {
72     if (pd.port == port) {
73       memcpy(hpd,&pd,sizeof(gras_sg_portrec_t));
74       return no_error;
75     }
76   }
77   return mismatch_error;
78 }
79
80
81 gras_error_t
82 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
83
84   gras_trp_sg_plug_data_t *data=malloc(sizeof(gras_trp_sg_plug_data_t));
85
86   if (!data)
87     RAISE_MALLOC;
88
89   plug->data      = data; 
90
91   plug->socket_client = gras_trp_sg_socket_client;
92   plug->socket_server = gras_trp_sg_socket_server;
93   plug->socket_close  = gras_trp_sg_socket_close;
94
95   plug->chunk_send    = gras_trp_sg_chunk_send;
96   plug->chunk_recv    = gras_trp_sg_chunk_recv;
97
98
99   return no_error;
100 }
101
102 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
103                                        const char *host,
104                                        unsigned short port,
105                                        /* OUT */ gras_socket_t *sock){
106
107   gras_error_t errcode;
108
109   m_host_t peer;
110   gras_hostdata_t *hd;
111   gras_trp_sg_sock_data_t *data;
112   gras_sg_portrec_t pr;
113
114   /* make sure this socket will reach someone */
115   if (!(peer=MSG_get_host_by_name(host))) {
116       fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
117       return mismatch_error;
118   }
119   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
120     RAISE1(mismatch_error,
121            "can't connect to %s: no process on this host",
122            host);
123   }
124   errcode = find_port(hd,port,&pr);
125   if (errcode != no_error && errcode != mismatch_error) 
126     return errcode;
127
128   if (errcode == mismatch_error) {
129     RAISE2(mismatch_error,
130            "can't connect to %s:%d, no process listen on this port",
131            host,port);
132   } 
133
134   if (pr.raw && !sock->raw) {
135     RAISE2(mismatch_error,
136            "can't connect to %s:%d in regular mode, the process listen "
137            "in raw mode on this port",host,port);
138   }
139   if (!pr.raw && sock->raw) {
140     RAISE2(mismatch_error,
141            "can't connect to %s:%d in raw mode, the process listen "
142            "in regular mode on this port",host,port);
143   }
144
145   /* create the socket */
146   if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
147     RAISE_MALLOC;
148   
149   data->from_PID     = MSG_process_self_PID();
150   data->to_PID       = hd->proc[ pr.tochan ];
151   data->to_host      = peer;
152   data->to_chan      = pr.tochan;
153   
154   sock->data = data;
155
156   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
157           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
158           sock->raw?"RAW":"regular",host,port,data->to_PID);
159
160   return no_error;
161 }
162
163 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
164                                        unsigned short port,
165                                        gras_socket_t *sock){
166
167   gras_error_t errcode;
168
169   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
170   gras_procdata_t *pd=gras_procdata_get();
171   gras_sg_portrec_t pr;
172   gras_trp_sg_sock_data_t *data;
173   
174   const char *host=MSG_host_get_name(MSG_host_self());
175
176   gras_assert0(hd,"Please run gras_process_init on each process");
177
178   sock->accepting = 0; /* no such nuisance in SG */
179
180   errcode = find_port(hd,port,&pr);
181   switch (errcode) {
182   case no_error: /* Port already used... */
183     RAISE2(mismatch_error,
184            "can't listen on address %s:%d: port already in use\n.",
185            host,port);
186
187   case mismatch_error: /* Port not used so far. Do it */
188     pr.tochan = sock->raw ? pd->rawChan : pd->chan;
189     pr.port   = port;
190     pr.raw    = sock->raw;
191     TRY(gras_dynar_push(hd->ports,&pr));
192     
193   default:
194     return errcode;
195   }
196   
197   /* Create the socket */
198   if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
199     RAISE_MALLOC;
200   
201   data->from_PID     = -1;
202   data->to_PID       = MSG_process_self_PID();
203   data->to_host      = MSG_host_self();
204   data->to_chan      = pd->chan;
205   
206   sock->data = data;
207
208   INFO6("'%s' (%d) ears on %s:%d%s (%p)",
209     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
210     host,port,sock->raw? " (mode RAW)":"",*sock);
211
212   return no_error;
213 }
214
215 void gras_trp_sg_socket_close(gras_socket_t *sock){
216   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
217   int cpt;
218   gras_sg_portrec_t *pr;
219
220   if (!sock) return;
221   gras_assert0(hd,"Please run gras_process_init on each process");
222
223   free(sock->data);
224
225   if (sock->incoming) {
226     /* server mode socket. Un register it from 'OS' tables */
227     gras_dynar_foreach(hd->ports, cpt, pr) {
228       if (pr->port == sock->port) {
229         gras_dynar_cursor_rm(hd->ports, &cpt);
230
231         return;
232       }
233     }
234     WARN0("socket_close called on an unknown socket");
235   }
236 }
237
238 typedef struct {
239   int size;
240   void *data;
241 } sg_task_data_t;
242
243 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
244                                     char *data,
245                                     size_t size) {
246   m_task_t task=NULL;
247   static unsigned int count=0;
248   char name[256];
249   gras_trp_sg_sock_data_t *sock_data;
250   sg_task_data_t *task_data;
251   
252   sock_data = (gras_trp_sg_sock_data_t *)sock->data;
253
254   sprintf(name,"Chunk[%d]",count++);
255
256   if (!(task_data=malloc(sizeof(sg_task_data_t)))) 
257     RAISE_MALLOC;
258   if (!(task_data->data=malloc(size)))
259     RAISE_MALLOC;
260   task_data->size = size;
261   memcpy(task_data->data,data,size);
262
263   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
264
265   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
266     RAISE0(system_error,"Problem during the MSG_task_put");
267   }
268
269   return no_error;
270 }
271
272 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
273                                     char *data,
274                                     size_t size){
275   gras_procdata_t *pd=gras_procdata_get();
276
277   m_task_t task=NULL;
278   sg_task_data_t *task_data;
279
280   if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
281     RAISE0(unknown_error,"Error in MSG_task_get()");
282
283   task_data = MSG_task_get_data(task);
284   gras_assert2(task_data->size == size,
285                "Got %d bytes when %d where expected",
286                task_data->size, size);
287   memcpy(data,task_data->data,size);
288   free(task_data->data);
289   free(task_data);
290
291   if (MSG_task_destroy(task) != MSG_OK)
292     RAISE0(unknown_error,"Error in MSG_task_destroy()");
293
294   return no_error;
295 }
296
297 /*FIXME
298
299 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){
300   RAISE_UNIMPLEMENTED;
301 }
302 */