3 /* file trp (transport) - send/receive a bunch of bytes in SG realm */
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. */
9 /* Authors: Martin Quinson */
10 /* Copyright (C) 2004 Martin Quinson. */
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. */
17 #include "transport_private.h"
18 #include "gras/Virtu/virtu_sg.h"
20 GRAS_LOG_EXTERNAL_CATEGORY(transport);
21 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
26 /* retrieve the port record associated to a numerical port on an host */
27 static gras_error_t find_port(gras_hostdata_t *hd, int port,
28 gras_sg_portrec_t *hpd);
31 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
32 /* OUT */ gras_socket_t *sock);
33 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
34 /* OUT */ gras_socket_t *sock);
35 void gras_trp_sg_socket_close(gras_socket_t *sd);
37 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sd,
41 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sd,
46 *** Specific plugin part
49 int placeholder; /* nothing plugin specific so far */
50 } gras_trp_sg_plug_data_t;
56 static gras_error_t find_port(gras_hostdata_t *hd, int port,
57 gras_sg_portrec_t *hpd) {
61 gras_assert0(hd,"Please run gras_process_init on each process");
63 gras_dynar_foreach(hd->ports, cpt, pr) {
64 if (pr.port == port) {
65 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
69 return mismatch_error;
74 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
76 gras_trp_sg_plug_data_t *data=gras_new(gras_trp_sg_plug_data_t,1);
80 plug->socket_client = gras_trp_sg_socket_client;
81 plug->socket_server = gras_trp_sg_socket_server;
82 plug->socket_close = gras_trp_sg_socket_close;
84 plug->chunk_send = gras_trp_sg_chunk_send;
85 plug->chunk_recv = gras_trp_sg_chunk_recv;
87 plug->flush = NULL; /* nothing cached */
92 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
93 /* OUT */ gras_socket_t *sock){
99 gras_trp_sg_sock_data_t *data;
100 gras_sg_portrec_t pr;
102 /* make sure this socket will reach someone */
103 if (!(peer=MSG_get_host_by_name(sock->peer_name))) {
104 fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",sock->peer_name);
105 return mismatch_error;
107 if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
108 RAISE1(mismatch_error,
109 "can't connect to %s: no process on this host",
112 errcode = find_port(hd,sock->peer_port,&pr);
113 if (errcode != no_error && errcode != mismatch_error)
116 if (errcode == mismatch_error) {
117 RAISE2(mismatch_error,
118 "can't connect to %s:%d, no process listen on this port",
119 sock->peer_name,sock->peer_port);
122 if (pr.raw && !sock->raw) {
123 RAISE2(mismatch_error,
124 "can't connect to %s:%d in regular mode, the process listen "
125 "in raw mode on this port",sock->peer_name,sock->peer_port);
127 if (!pr.raw && sock->raw) {
128 RAISE2(mismatch_error,
129 "can't connect to %s:%d in raw mode, the process listen "
130 "in regular mode on this port",sock->peer_name,sock->peer_port);
133 /* create the socket */
134 data = gras_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;
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->raw?"RAW":"regular",
146 sock->peer_name,sock->peer_port,data->to_PID);
151 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
152 gras_socket_t *sock){
154 gras_error_t errcode;
156 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
157 gras_procdata_t *pd=gras_procdata_get();
158 gras_sg_portrec_t pr;
159 gras_trp_sg_sock_data_t *data;
161 const char *host=MSG_host_get_name(MSG_host_self());
163 gras_assert0(hd,"Please run gras_process_init on each process");
165 sock->accepting = 0; /* no such nuisance in SG */
167 errcode = find_port(hd,sock->port,&pr);
169 case no_error: /* Port already used... */
170 RAISE2(mismatch_error,
171 "can't listen on address %s:%d: port already in use\n.",
174 case mismatch_error: /* Port not used so far. Do it */
175 pr.tochan = sock->raw ? pd->rawChan : pd->chan;
176 pr.port = sock->port;
178 gras_dynar_push(hd->ports,&pr);
184 /* Create the socket */
185 data = gras_new(gras_trp_sg_sock_data_t,1);
187 data->to_PID = MSG_process_self_PID();
188 data->to_host = MSG_host_self();
189 data->to_chan = pd->chan;
193 INFO6("'%s' (%d) ears on %s:%d%s (%p)",
194 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
195 host,sock->port,sock->raw? " (mode RAW)":"",sock);
200 void gras_trp_sg_socket_close(gras_socket_t *sock){
201 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
204 gras_sg_portrec_t pr;
207 gras_assert0(hd,"Please run gras_process_init on each process");
210 gras_free(sock->data);
212 if (sock->incoming) {
213 /* server mode socket. Un register it from 'OS' tables */
214 gras_dynar_foreach(hd->ports, cpt, pr) {
215 DEBUG2("Check pr %d of %lu", cpt, gras_dynar_length(hd->ports));
216 if (pr.port == sock->port) {
217 gras_dynar_cursor_rm(hd->ports, &cpt);
221 WARN0("socket_close called on an unknown socket");
230 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
234 static unsigned int count=0;
236 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
237 sg_task_data_t *task_data;
239 sprintf(name,"Chunk[%d]",count++);
241 task_data=gras_new(sg_task_data_t,1);
242 task_data->data=(void*)gras_malloc(size);
243 task_data->size = size;
244 memcpy(task_data->data,data,size);
246 task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
248 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
249 name, MSG_host_get_name(MSG_host_self()),
250 MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
251 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
252 RAISE0(system_error,"Problem during the MSG_task_put");
258 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
261 gras_procdata_t *pd=gras_procdata_get();
264 sg_task_data_t *task_data;
265 gras_trp_sg_sock_data_t *sock_data = sock->data;
268 DEBUG4("recv chunk on %s -> %s:%d (size=%ld)",
269 MSG_host_get_name(sock_data->to_host),
270 MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
271 if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
272 RAISE0(unknown_error,"Error in MSG_task_get()");
274 task_data = MSG_task_get_data(task);
275 if (task_data->size != size)
276 RAISE5(mismatch_error,
277 "Got %d bytes when %ld where expected (in %s->%s:%d)",
278 task_data->size, size,
279 MSG_host_get_name(sock_data->to_host),
280 MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
281 memcpy(data,task_data->data,size);
282 gras_free(task_data->data);
283 gras_free(task_data);
285 if (MSG_task_destroy(task) != MSG_OK)
286 RAISE0(unknown_error,"Error in MSG_task_destroy()");
292 /* Data exchange over raw sockets */
293 gras_error_t gras_socket_raw_exchange(gras_socket_t *peer,
295 unsigned int timeout,
296 unsigned long int expSize,
297 unsigned long int msgSize) {
298 unsigned int bytesTotal;
299 static unsigned int count=0;
302 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)peer->data;
304 gras_procdata_t *pd=gras_procdata_get();
307 startTime=gras_os_time(); /* used only in sender mode */
309 for(bytesTotal = 0; bytesTotal < expSize; bytesTotal += msgSize) {
313 sprintf(name,"Raw data[%d]",count++);
315 task=MSG_task_create(name,0,((double)msgSize)/(1024.0*1024.0),NULL);
317 DEBUG5("%f:%s: gras_socket_raw_send(%f %s -> %s) BEGIN",
318 gras_os_time(), MSG_process_get_name(MSG_process_self()),
319 ((double)msgSize)/(1024.0*1024.0),
320 MSG_host_get_name( MSG_host_self()), peer->peer_name);
322 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK)
323 RAISE0(system_error,"Problem during the MSG_task_put()");
325 DEBUG5("%f:%s: gras_socket_raw_send(%f %s -> %s) END",
326 gras_os_time(), MSG_process_get_name(MSG_process_self()),
327 ((double)msgSize)/(1024.0*1024.0),
328 MSG_host_get_name( MSG_host_self()), peer->peer_name);
330 } else { /* we are receiver, simulate a select */
333 DEBUG2("%f:%s: gras_socket_raw_recv() BEGIN\n",
334 gras_os_time(), MSG_process_get_name(MSG_process_self()));
336 if (MSG_task_Iprobe((m_channel_t) pd->rawChan)) {
337 if (MSG_task_get(&task, (m_channel_t) pd->rawChan) != MSG_OK) {
338 fprintf(stderr,"GRAS: Error in MSG_task_get()\n");
339 return unknown_error;
342 if (MSG_task_destroy(task) != MSG_OK) {
343 fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
344 return unknown_error;
347 DEBUG2("%f:%s: gras_socket_raw_recv() END\n",
348 gras_os_time(), MSG_process_get_name(MSG_process_self()));
351 MSG_process_sleep(0.0001);
354 } while (gras_os_time() - startTime < timeout);
356 if (gras_os_time() - startTime > timeout)
357 return timeout_error;
358 } /* receiver part */