Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
435174e3afaffdc023a62f92c4dbeaa7fbd2bc9e
[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                                     long int size);
44
45 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sd,
46                                     char *data,
47                                     long int 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 pr;
68
69   gras_assert0(hd,"Please run gras_process_init on each process");
70   
71   gras_dynar_foreach(hd->ports, cpt, pr) {
72     if (pr.port == port) {
73       memcpy(hpd,&pr,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   sock->incoming = 1;
156
157   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
158           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
159           sock->raw?"RAW":"regular",host,port,data->to_PID);
160
161   return no_error;
162 }
163
164 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
165                                        unsigned short port,
166                                        gras_socket_t *sock){
167
168   gras_error_t errcode;
169
170   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
171   gras_procdata_t *pd=gras_procdata_get();
172   gras_sg_portrec_t pr;
173   gras_trp_sg_sock_data_t *data;
174   
175   const char *host=MSG_host_get_name(MSG_host_self());
176
177   gras_assert0(hd,"Please run gras_process_init on each process");
178
179   sock->accepting = 0; /* no such nuisance in SG */
180
181   errcode = find_port(hd,port,&pr);
182   switch (errcode) {
183   case no_error: /* Port already used... */
184     RAISE2(mismatch_error,
185            "can't listen on address %s:%d: port already in use\n.",
186            host,port);
187
188   case mismatch_error: /* Port not used so far. Do it */
189     pr.tochan = sock->raw ? pd->rawChan : pd->chan;
190     pr.port   = port;
191     pr.raw    = sock->raw;
192     TRY(gras_dynar_push(hd->ports,&pr));
193     
194   default:
195     return errcode;
196   }
197   
198   /* Create the socket */
199   if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
200     RAISE_MALLOC;
201   
202   data->from_PID     = -1;
203   data->to_PID       = MSG_process_self_PID();
204   data->to_host      = MSG_host_self();
205   data->to_chan      = pd->chan;
206   
207   sock->data = data;
208
209   INFO6("'%s' (%d) ears on %s:%d%s (%p)",
210     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
211     host,port,sock->raw? " (mode RAW)":"",sock);
212
213   return no_error;
214 }
215
216 void gras_trp_sg_socket_close(gras_socket_t *sock){
217   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
218   int cpt;
219   
220   gras_sg_portrec_t pr;
221
222   if (!sock) return;
223   gras_assert0(hd,"Please run gras_process_init on each process");
224
225   if (sock->data)
226     free(sock->data);
227
228   if (sock->incoming) {
229     /* server mode socket. Un register it from 'OS' tables */
230     gras_dynar_foreach(hd->ports, cpt, pr) {
231       DEBUG2("Check pr %d of %d", cpt, gras_dynar_length(hd->ports));
232       if (pr.port == sock->port) {
233         gras_dynar_cursor_rm(hd->ports, &cpt);
234         return;
235       }
236     }
237     WARN0("socket_close called on an unknown socket");
238   }
239 }
240
241 typedef struct {
242   int size;
243   void *data;
244 } sg_task_data_t;
245
246 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
247                                     char *data,
248                                     long int size) {
249   m_task_t task=NULL;
250   static unsigned int count=0;
251   char name[256];
252   gras_trp_sg_sock_data_t *sock_data;
253   sg_task_data_t *task_data;
254   
255   sock_data = (gras_trp_sg_sock_data_t *)sock->data;
256
257   sprintf(name,"Chunk[%d]",count++);
258
259   if (!(task_data=malloc(sizeof(sg_task_data_t)))) 
260     RAISE_MALLOC;
261   if (!(task_data->data=malloc(size)))
262     RAISE_MALLOC;
263   task_data->size = size;
264   memcpy(task_data->data,data,size);
265
266   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
267
268   DEBUG4("send chunk %s from %s to %s on channel %d",
269          name, MSG_host_get_name(MSG_host_self()),
270          MSG_host_get_name(sock_data->to_host), sock_data->to_chan);
271   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
272     RAISE0(system_error,"Problem during the MSG_task_put");
273   }
274
275   return no_error;
276 }
277
278 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
279                                     char *data,
280                                     long int size){
281   gras_procdata_t *pd=gras_procdata_get();
282
283   m_task_t task=NULL;
284   sg_task_data_t *task_data;
285   gras_trp_sg_sock_data_t *sock_data = sock->data;
286
287   DEBUG3("recv chunk on %s from %s on channel %d",
288          MSG_host_get_name(MSG_host_self()),
289          MSG_host_get_name(sock_data->to_host), sock_data->to_chan);
290   if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
291     RAISE0(unknown_error,"Error in MSG_task_get()");
292
293   task_data = MSG_task_get_data(task);
294   gras_assert2(task_data->size == size,
295                "Got %d bytes when %ld where expected",
296                task_data->size, size);
297   memcpy(data,task_data->data,size);
298   free(task_data->data);
299   free(task_data);
300
301   if (MSG_task_destroy(task) != MSG_OK)
302     RAISE0(unknown_error,"Error in MSG_task_destroy()");
303
304   return no_error;
305 }
306
307 /*FIXME
308
309 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){
310   RAISE_UNIMPLEMENTED;
311 }
312 */