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 "gras_private.h"
18 #include "transport_private.h"
19 #include "gras/Virtu/virtu_sg.h"
21 GRAS_LOG_EXTERNAL_CATEGORY(transport);
22 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
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);
32 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
35 /* OUT */ gras_socket_t *sock);
36 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
38 /* OUT */ gras_socket_t *sock);
39 void gras_trp_sg_socket_close(gras_socket_t *sd);
41 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sd,
45 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sd,
50 *** Specific plugin part
53 int placeholder; /* nothing plugin specific so far */
54 } gras_trp_sg_plug_data_t;
60 static gras_error_t find_port(gras_hostdata_t *hd, int port,
61 gras_sg_portrec_t *hpd) {
65 gras_assert0(hd,"Please run gras_process_init on each process");
67 gras_dynar_foreach(hd->ports, cpt, pr) {
68 if (pr.port == port) {
69 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
73 return mismatch_error;
78 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
80 gras_trp_sg_plug_data_t *data=gras_new(gras_trp_sg_plug_data_t,1);
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;
91 plug->chunk_send = gras_trp_sg_chunk_send;
92 plug->chunk_recv = gras_trp_sg_chunk_recv;
94 plug->flush = NULL; /* nothing cached */
99 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
102 /* OUT */ gras_socket_t *sock){
104 gras_error_t errcode;
108 gras_trp_sg_sock_data_t *data;
109 gras_sg_portrec_t pr;
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;
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",
121 errcode = find_port(hd,port,&pr);
122 if (errcode != no_error && errcode != mismatch_error)
125 if (errcode == mismatch_error) {
126 RAISE2(mismatch_error,
127 "can't connect to %s:%d, no process listen on this port",
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);
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);
142 /* create the socket */
143 if (!(data = gras_new(gras_trp_sg_sock_data_t,1)))
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;
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);
161 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
163 gras_socket_t *sock){
165 gras_error_t errcode;
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;
172 const char *host=MSG_host_get_name(MSG_host_self());
174 gras_assert0(hd,"Please run gras_process_init on each process");
176 sock->accepting = 0; /* no such nuisance in SG */
178 errcode = find_port(hd,port,&pr);
180 case no_error: /* Port already used... */
181 RAISE2(mismatch_error,
182 "can't listen on address %s:%d: port already in use\n.",
185 case mismatch_error: /* Port not used so far. Do it */
186 pr.tochan = sock->raw ? pd->rawChan : pd->chan;
189 TRY(gras_dynar_push(hd->ports,&pr));
195 /* Create the socket */
196 if (!(data = gras_new(gras_trp_sg_sock_data_t,1)))
200 data->to_PID = MSG_process_self_PID();
201 data->to_host = MSG_host_self();
202 data->to_chan = pd->chan;
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);
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());
217 gras_sg_portrec_t pr;
220 gras_assert0(hd,"Please run gras_process_init on each process");
223 gras_free(sock->data);
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);
234 WARN0("socket_close called on an unknown socket");
243 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
247 static unsigned int count=0;
249 gras_trp_sg_sock_data_t *sock_data;
250 sg_task_data_t *task_data;
252 sock_data = (gras_trp_sg_sock_data_t *)sock->data;
254 sprintf(name,"Chunk[%d]",count++);
256 if (!(task_data=gras_new(sg_task_data_t,1)))
258 if (!(task_data->data=(void*)gras_malloc(size)))
260 task_data->size = size;
261 memcpy(task_data->data,data,size);
263 task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
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");
275 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
278 gras_procdata_t *pd=gras_procdata_get();
281 sg_task_data_t *task_data;
282 gras_trp_sg_sock_data_t *sock_data = sock->data;
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()");
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);
302 if (MSG_task_destroy(task) != MSG_OK)
303 RAISE0(unknown_error,"Error in MSG_task_destroy()");