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 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
18 #include "transport_private.h"
19 #include "gras/Virtu/virtu_sg.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg,gras_trp,"SimGrid pseudo-transport");
27 /* retrieve the port record associated to a numerical port on an host */
28 static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd);
31 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
32 /* OUT */ gras_socket_t sock);
33 void 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 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
39 unsigned long int size);
40 void gras_trp_sg_chunk_send(gras_socket_t sd,
42 unsigned long int size,
45 int gras_trp_sg_chunk_recv(gras_socket_t sd,
47 unsigned long int size);
50 *** Specific plugin part
53 int placeholder; /* nothing plugin specific so far */
54 } gras_trp_sg_plug_data_t;
60 static void find_port(gras_hostdata_t *hd, int port,
61 gras_sg_portrec_t *hpd) {
65 xbt_assert0(hd,"Please run gras_process_init on each process");
67 xbt_dynar_foreach(hd->ports, cpt, pr) {
68 if (pr.port == port) {
69 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
73 THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
78 gras_trp_sg_setup(gras_trp_plugin_t plug) {
80 gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
84 plug->socket_client = gras_trp_sg_socket_client;
85 plug->socket_server = gras_trp_sg_socket_server;
86 plug->socket_close = gras_trp_sg_socket_close;
88 plug->raw_send = gras_trp_sg_chunk_send_raw;
89 plug->send = gras_trp_sg_chunk_send;
90 plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
92 plug->flush = NULL; /* nothing cached */
95 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
96 /* OUT */ gras_socket_t sock){
101 gras_trp_sg_sock_data_t *data;
102 gras_sg_portrec_t pr;
104 /* make sure this socket will reach someone */
105 if (!(peer=MSG_get_host_by_name(sock->peer_name)))
106 THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
108 if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer)))
109 THROW1(mismatch_error,0,
110 "can't connect to %s: no process on this host",
114 find_port(hd,sock->peer_port,&pr);
116 if (e.category == mismatch_error) {
118 THROW2(mismatch_error,0,
119 "can't connect to %s:%d, no process listen on this port",
120 sock->peer_name,sock->peer_port);
125 if (pr.meas && !sock->meas) {
126 THROW2(mismatch_error,0,
127 "can't connect to %s:%d in regular mode, the process listen "
128 "in meas mode on this port",sock->peer_name,sock->peer_port);
130 if (!pr.meas && sock->meas) {
131 THROW2(mismatch_error,0,
132 "can't connect to %s:%d in meas mode, the process listen "
133 "in regular mode on this port",sock->peer_name,sock->peer_port);
136 /* create the socket */
137 data = xbt_new(gras_trp_sg_sock_data_t,1);
138 data->from_PID = MSG_process_self_PID();
139 data->to_PID = hd->proc[ pr.tochan ];
140 data->to_host = peer;
141 data->to_chan = pr.tochan;
146 DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
147 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
148 sock->meas?"meas":"regular",
149 sock->peer_name,sock->peer_port,data->to_PID);
152 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
155 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
156 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
157 gras_sg_portrec_t pr;
158 gras_trp_sg_sock_data_t *data;
161 const char *host=MSG_host_get_name(MSG_host_self());
165 xbt_assert0(hd,"Please run gras_process_init on each process");
167 sock->accepting = 0; /* no such nuisance in SG */
171 find_port(hd,sock->port,&pr);
174 if (e.category == mismatch_error)
181 THROW2(mismatch_error,0,
182 "can't listen on address %s:%d: port already in use.",
185 pr.tochan = sock->meas ? pd->measChan : pd->chan;
186 pr.port = sock->port;
187 pr.meas = sock->meas;
188 xbt_dynar_push(hd->ports,&pr);
190 /* Create the socket */
191 data = xbt_new(gras_trp_sg_sock_data_t,1);
193 data->to_PID = MSG_process_self_PID();
194 data->to_host = MSG_host_self();
195 data->to_chan = pd->chan;
199 VERB6("'%s' (%d) ears on %s:%d%s (%p)",
200 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
201 host,sock->port,sock->meas? " (mode meas)":"",sock);
204 void gras_trp_sg_socket_close(gras_socket_t sock){
205 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
208 gras_sg_portrec_t pr;
210 XBT_IN1(" (sock=%p)",sock);
213 xbt_assert0(hd,"Please run gras_process_init on each process");
218 if (sock->incoming && sock->port >= 0) {
219 /* server mode socket. Unregister it from 'OS' tables */
220 xbt_dynar_foreach(hd->ports, cpt, pr) {
221 DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
222 if (pr.port == sock->port) {
223 xbt_dynar_cursor_rm(hd->ports, &cpt);
228 WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
239 void gras_trp_sg_chunk_send(gras_socket_t sock,
241 unsigned long int size,
242 int stable_ignored) {
243 gras_trp_sg_chunk_send_raw(sock,data,size);
246 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
248 unsigned long int size) {
250 static unsigned int count=0;
252 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
253 sg_task_data_t *task_data;
255 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
257 sprintf(name,"Chunk[%d]",count++);
259 task_data=xbt_new(sg_task_data_t,1);
260 task_data->size = size;
262 task_data->data=(void*)xbt_malloc(size);
263 memcpy(task_data->data,data,size);
265 task_data->data = NULL;
268 task=MSG_task_create(name,0,((double)size),task_data);
270 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
271 name, MSG_host_get_name(MSG_host_self()),
272 MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
273 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
274 THROW0(system_error,0,"Problem during the MSG_task_put");
278 int gras_trp_sg_chunk_recv(gras_socket_t sock,
280 unsigned long int size){
281 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
284 sg_task_data_t *task_data;
285 gras_trp_sg_sock_data_t *sock_data = sock->data;
287 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
289 DEBUG4("recv chunk on %s -> %s:%d (size=%ld)",
290 MSG_host_get_name(sock_data->to_host),
291 MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
292 if (MSG_task_get_with_time_out(&task,
293 (sock->meas ? pd->measChan : pd->chan),
295 THROW0(system_error,0,"Error in MSG_task_get()");
296 DEBUG1("Got chuck %s",MSG_task_get_name(task));
298 task_data = MSG_task_get_data(task);
299 if (task_data->size != size)
300 THROW5(mismatch_error,0,
301 "Got %d bytes when %ld where expected (in %s->%s:%d)",
302 task_data->size, size,
303 MSG_host_get_name(sock_data->to_host),
304 MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
306 memcpy(data,task_data->data,size);
308 free(task_data->data);
311 if (MSG_task_destroy(task) != MSG_OK)
312 THROW0(system_error,0,"Error in MSG_task_destroy()");