Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
port the SG part to the new stuff intended to work on AIX
[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 "gras/Virtu/virtu_sg.h"
20
21 GRAS_LOG_EXTERNAL_CATEGORY(transport);
22 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-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                                     const 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 /***
50  *** Specific plugin part
51  ***/
52 typedef struct {
53   int placeholder; /* nothing plugin specific so far */
54 } gras_trp_sg_plug_data_t;
55
56
57 /***
58  *** Code
59  ***/
60 static gras_error_t find_port(gras_hostdata_t *hd, int port,
61                               gras_sg_portrec_t *hpd) {
62   int cpt;
63   gras_sg_portrec_t pr;
64
65   gras_assert0(hd,"Please run gras_process_init on each process");
66   
67   gras_dynar_foreach(hd->ports, cpt, pr) {
68     if (pr.port == port) {
69       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
70       return no_error;
71     }
72   }
73   return mismatch_error;
74 }
75
76
77 gras_error_t
78 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
79
80   gras_trp_sg_plug_data_t *data=gras_new(gras_trp_sg_plug_data_t,1);
81
82   if (!data)
83     RAISE_MALLOC;
84
85   plug->data      = data; 
86
87   plug->socket_client = gras_trp_sg_socket_client;
88   plug->socket_server = gras_trp_sg_socket_server;
89   plug->socket_close  = gras_trp_sg_socket_close;
90
91   plug->chunk_send    = gras_trp_sg_chunk_send;
92   plug->chunk_recv    = gras_trp_sg_chunk_recv;
93
94   plug->flush         = NULL; /* nothing cached */
95
96   return no_error;
97 }
98
99 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
100                                        const char *host,
101                                        unsigned short port,
102                                        /* OUT */ gras_socket_t *sock){
103
104   gras_error_t errcode;
105
106   m_host_t peer;
107   gras_hostdata_t *hd;
108   gras_trp_sg_sock_data_t *data;
109   gras_sg_portrec_t pr;
110
111   /* make sure this socket will reach someone */
112   if (!(peer=MSG_get_host_by_name(host))) {
113       fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
114       return mismatch_error;
115   }
116   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
117     RAISE1(mismatch_error,
118            "can't connect to %s: no process on this host",
119            host);
120   }
121   errcode = find_port(hd,port,&pr);
122   if (errcode != no_error && errcode != mismatch_error) 
123     return errcode;
124
125   if (errcode == mismatch_error) {
126     RAISE2(mismatch_error,
127            "can't connect to %s:%d, no process listen on this port",
128            host,port);
129   } 
130
131   if (pr.raw && !sock->raw) {
132     RAISE2(mismatch_error,
133            "can't connect to %s:%d in regular mode, the process listen "
134            "in raw mode on this port",host,port);
135   }
136   if (!pr.raw && sock->raw) {
137     RAISE2(mismatch_error,
138            "can't connect to %s:%d in raw mode, the process listen "
139            "in regular mode on this port",host,port);
140   }
141
142   /* create the socket */
143   if (!(data = gras_new(gras_trp_sg_sock_data_t,1)))
144     RAISE_MALLOC;
145   
146   data->from_PID     = MSG_process_self_PID();
147   data->to_PID       = hd->proc[ pr.tochan ];
148   data->to_host      = peer;
149   data->to_chan      = pr.tochan;
150   
151   sock->data = data;
152   sock->incoming = 1;
153
154   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
155           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
156           sock->raw?"RAW":"regular",host,port,data->to_PID);
157
158   return no_error;
159 }
160
161 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
162                                        unsigned short port,
163                                        gras_socket_t *sock){
164
165   gras_error_t errcode;
166
167   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
168   gras_procdata_t *pd=gras_procdata_get();
169   gras_sg_portrec_t pr;
170   gras_trp_sg_sock_data_t *data;
171   
172   const char *host=MSG_host_get_name(MSG_host_self());
173
174   gras_assert0(hd,"Please run gras_process_init on each process");
175
176   sock->accepting = 0; /* no such nuisance in SG */
177
178   errcode = find_port(hd,port,&pr);
179   switch (errcode) {
180   case no_error: /* Port already used... */
181     RAISE2(mismatch_error,
182            "can't listen on address %s:%d: port already in use\n.",
183            host,port);
184
185   case mismatch_error: /* Port not used so far. Do it */
186     pr.tochan = sock->raw ? pd->rawChan : pd->chan;
187     pr.port   = port;
188     pr.raw    = sock->raw;
189     TRY(gras_dynar_push(hd->ports,&pr));
190     
191   default:
192     return errcode;
193   }
194   
195   /* Create the socket */
196   if (!(data = gras_new(gras_trp_sg_sock_data_t,1)))
197     RAISE_MALLOC;
198   
199   data->from_PID     = -1;
200   data->to_PID       = MSG_process_self_PID();
201   data->to_host      = MSG_host_self();
202   data->to_chan      = pd->chan;
203   
204   sock->data = data;
205
206   INFO6("'%s' (%d) ears on %s:%d%s (%p)",
207     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
208     host,port,sock->raw? " (mode RAW)":"",sock);
209
210   return no_error;
211 }
212
213 void gras_trp_sg_socket_close(gras_socket_t *sock){
214   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
215   int cpt;
216   
217   gras_sg_portrec_t pr;
218
219   if (!sock) return;
220   gras_assert0(hd,"Please run gras_process_init on each process");
221
222   if (sock->data)
223     gras_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       DEBUG2("Check pr %d of %lu", cpt, gras_dynar_length(hd->ports));
229       if (pr.port == sock->port) {
230         gras_dynar_cursor_rm(hd->ports, &cpt);
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                                     const char *data,
245                                     long int 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=gras_new(sg_task_data_t,1)))
257     RAISE_MALLOC;
258   if (!(task_data->data=(void*)gras_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   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
266          name, MSG_host_get_name(MSG_host_self()),
267          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
268   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
269     RAISE0(system_error,"Problem during the MSG_task_put");
270   }
271
272   return no_error;
273 }
274
275 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
276                                     char *data,
277                                     long int size){
278   gras_procdata_t *pd=gras_procdata_get();
279
280   m_task_t task=NULL;
281   sg_task_data_t *task_data;
282   gras_trp_sg_sock_data_t *sock_data = sock->data;
283
284   GRAS_IN;
285   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
286          MSG_host_get_name(sock_data->to_host),
287          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
288   if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
289     RAISE0(unknown_error,"Error in MSG_task_get()");
290
291   task_data = MSG_task_get_data(task);
292   if (task_data->size != size)
293     RAISE5(mismatch_error,
294            "Got %d bytes when %ld where expected (in %s->%s:%d)",
295            task_data->size, size,
296            MSG_host_get_name(sock_data->to_host),
297            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
298   memcpy(data,task_data->data,size);
299   gras_free(task_data->data);
300   gras_free(task_data);
301
302   if (MSG_task_destroy(task) != MSG_OK)
303     RAISE0(unknown_error,"Error in MSG_task_destroy()");
304
305   GRAS_OUT;
306   return no_error;
307 }