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. */
16 //#include "msg/msg.h"
18 #include "gras_simix_transport_private.h"
19 #include "gras_simix/Virtu/gras_simix_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){
102 gras_trp_sg_sock_data_t *data;
103 gras_sg_portrec_t pr;
105 /* make sure this socket will reach someone */
106 /*if (!(peer=SIMIX_get_host_by_name(sock->peer_name)))
107 THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
109 if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer)))
110 THROW1(mismatch_error,0,
111 "can't connect to %s: no process on this host",
115 find_port(hd,sock->peer_port,&pr);
117 if (e.category == mismatch_error) {
119 THROW2(mismatch_error,0,
120 "can't connect to %s:%d, no process listen on this port",
121 sock->peer_name,sock->peer_port);
126 if (pr.meas && !sock->meas) {
127 THROW2(mismatch_error,0,
128 "can't connect to %s:%d in regular mode, the process listen "
129 "in meas mode on this port",sock->peer_name,sock->peer_port);
131 if (!pr.meas && sock->meas) {
132 THROW2(mismatch_error,0,
133 "can't connect to %s:%d in meas mode, the process listen "
134 "in regular mode on this port",sock->peer_name,sock->peer_port);
137 /* create the socket */
138 /*data = xbt_new(gras_trp_sg_sock_data_t,1);
139 //data->from_PID = gras_os_getpid();
140 data->from_process = SIMIX_process_self();
141 data->to_process = pr.process;
142 //data->to_PID = hd->proc[ pr.tochan ];
144 data->to_host = peer;
145 //data->to_chan = pr.tochan;
150 DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
151 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid,
152 sock->meas?"meas":"regular",
153 sock->peer_name,sock->peer_port,data->to_PID);
157 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
160 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
161 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
162 gras_sg_portrec_t pr;
163 gras_trp_sg_sock_data_t *data;
166 const char *host=SIMIX_host_get_name(SIMIX_host_self());
170 xbt_assert0(hd,"Please run gras_process_init on each process");
172 sock->accepting = 0;*/ /* no such nuisance in SG */
176 find_port(hd,sock->port,&pr);
179 if (e.category == mismatch_error)
186 THROW2(mismatch_error,0,
187 "can't listen on address %s:%d: port already in use.",
190 //pr.tochan = sock->meas ? pd->measChan : pd->chan;
191 pr.port = sock->port;
192 pr.meas = sock->meas;
193 pr.process = SIMIX_process_self();
194 xbt_dynar_push(hd->ports,&pr);
196 /* Create the socket */
197 /*data = xbt_new(gras_trp_sg_sock_data_t,1);
198 data->from_process = NULL;
199 data->to_PID = SIMIX_process_self();;
200 //data->to_process = gras_os_getpid();
201 data->to_host = SIMIX_host_self();
203 //data->to_chan = pd->chan;
207 VERB6("'%s' (%d) ears on %s:%d%s (%p)",
208 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
209 host,sock->port,sock->meas? " (mode meas)":"",sock);
213 void gras_trp_sg_socket_close(gras_socket_t sock){
215 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
218 gras_sg_portrec_t pr;
220 XBT_IN1(" (sock=%p)",sock);
223 xbt_assert0(hd,"Please run gras_process_init on each process");
228 if (sock->incoming && sock->port >= 0) {*/
229 /* server mode socket. Unregister it from 'OS' tables */
230 /* xbt_dynar_foreach(hd->ports, cpt, pr) {
231 DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
232 if (pr.port == sock->port) {
233 xbt_dynar_cursor_rm(hd->ports, &cpt);
238 WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
250 void gras_trp_sg_chunk_send(gras_socket_t sock,
252 unsigned long int size,
253 int stable_ignored) {
254 gras_trp_sg_chunk_send_raw(sock,data,size);
257 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
259 unsigned long int size) {
262 static unsigned int count=0;
264 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
265 sg_task_data_t *task_data;
267 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
269 sprintf(name,"Chunk[%d]",count++);
271 task_data=xbt_new(sg_task_data_t,1);
272 task_data->size = size;
274 task_data->data=(void*)xbt_malloc(size);
275 memcpy(task_data->data,data,size);
277 task_data->data = NULL;
280 task=MSG_task_create(name,0,((double)size),task_data);
282 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
283 name, MSG_host_get_name(MSG_host_self()),
284 MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
285 if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
286 THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
290 int gras_trp_sg_chunk_recv(gras_socket_t sock,
292 unsigned long int size){
294 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
297 sg_task_data_t *task_data;
298 gras_trp_sg_sock_data_t *sock_data = sock->data;
300 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
302 DEBUG4("recv chunk on %s -> %s:%d (size=%ld)",
303 MSG_host_get_name(sock_data->to_host),
304 MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
305 if (MSG_task_get_with_time_out(&task,
306 (sock->meas ? pd->measChan : pd->chan),
308 THROW0(system_error,0,"Error in MSG_task_get()");
309 DEBUG1("Got chuck %s",MSG_task_get_name(task));
311 task_data = MSG_task_get_data(task);
312 if (task_data->size != size)
313 THROW5(mismatch_error,0,
314 "Got %d bytes when %ld where expected (in %s->%s:%d)",
315 task_data->size, size,
316 MSG_host_get_name(sock_data->to_host),
317 MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
319 memcpy(data,task_data->data,size);
321 free(task_data->data);
324 if (MSG_task_destroy(task) != MSG_OK)
325 THROW0(system_error,0,"Error in MSG_task_destroy()");