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 "Virtu/virtu_sg.h"
21 GRAS_LOG_EXTERNAL_CATEGORY(transport);
22 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,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 gras_error_t gras_trp_sg_flush(gras_socket_t *sd);
54 *** Specific plugin part
57 int placeholder; /* nothing plugin specific so far */
58 } gras_trp_sg_plug_data_t;
64 static gras_error_t find_port(gras_hostdata_t *hd, int port,
65 gras_sg_portrec_t *hpd) {
69 gras_assert0(hd,"Please run gras_process_init on each process");
71 gras_dynar_foreach(hd->ports, cpt, pr) {
72 if (pr.port == port) {
73 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
77 return mismatch_error;
82 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
84 gras_trp_sg_plug_data_t *data=malloc(sizeof(gras_trp_sg_plug_data_t));
91 plug->socket_client = gras_trp_sg_socket_client;
92 plug->socket_server = gras_trp_sg_socket_server;
93 plug->socket_close = gras_trp_sg_socket_close;
95 plug->chunk_send = gras_trp_sg_chunk_send;
96 plug->chunk_recv = gras_trp_sg_chunk_recv;
102 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
105 /* OUT */ gras_socket_t *sock){
107 gras_error_t errcode;
111 gras_trp_sg_sock_data_t *data;
112 gras_sg_portrec_t pr;
114 /* make sure this socket will reach someone */
115 if (!(peer=MSG_get_host_by_name(host))) {
116 fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
117 return mismatch_error;
119 if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
120 RAISE1(mismatch_error,
121 "can't connect to %s: no process on this host",
124 errcode = find_port(hd,port,&pr);
125 if (errcode != no_error && errcode != mismatch_error)
128 if (errcode == mismatch_error) {
129 RAISE2(mismatch_error,
130 "can't connect to %s:%d, no process listen on this port",
134 if (pr.raw && !sock->raw) {
135 RAISE2(mismatch_error,
136 "can't connect to %s:%d in regular mode, the process listen "
137 "in raw mode on this port",host,port);
139 if (!pr.raw && sock->raw) {
140 RAISE2(mismatch_error,
141 "can't connect to %s:%d in raw mode, the process listen "
142 "in regular mode on this port",host,port);
145 /* create the socket */
146 if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
149 data->from_PID = MSG_process_self_PID();
150 data->to_PID = hd->proc[ pr.tochan ];
151 data->to_host = peer;
152 data->to_chan = pr.tochan;
157 DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
158 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
159 sock->raw?"RAW":"regular",host,port,data->to_PID);
164 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
166 gras_socket_t *sock){
168 gras_error_t errcode;
170 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
171 gras_procdata_t *pd=gras_procdata_get();
172 gras_sg_portrec_t pr;
173 gras_trp_sg_sock_data_t *data;
175 const char *host=MSG_host_get_name(MSG_host_self());
177 gras_assert0(hd,"Please run gras_process_init on each process");
179 sock->accepting = 0; /* no such nuisance in SG */
181 errcode = find_port(hd,port,&pr);
183 case no_error: /* Port already used... */
184 RAISE2(mismatch_error,
185 "can't listen on address %s:%d: port already in use\n.",
188 case mismatch_error: /* Port not used so far. Do it */
189 pr.tochan = sock->raw ? pd->rawChan : pd->chan;
192 TRY(gras_dynar_push(hd->ports,&pr));
198 /* Create the socket */
199 if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
203 data->to_PID = MSG_process_self_PID();
204 data->to_host = MSG_host_self();
205 data->to_chan = pd->chan;
209 INFO6("'%s' (%d) ears on %s:%d%s (%p)",
210 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
211 host,port,sock->raw? " (mode RAW)":"",*sock);
216 void gras_trp_sg_socket_close(gras_socket_t *sock){
217 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
220 gras_sg_portrec_t pr;
223 gras_assert0(hd,"Please run gras_process_init on each process");
228 if (sock->incoming) {
229 /* server mode socket. Un register it from 'OS' tables */
230 gras_dynar_foreach(hd->ports, cpt, pr) {
231 DEBUG2("Check pr %d of %d", cpt, gras_dynar_length(hd->ports));
232 if (pr.port == sock->port) {
233 gras_dynar_cursor_rm(hd->ports, &cpt);
237 WARN0("socket_close called on an unknown socket");
246 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
250 static unsigned int count=0;
252 gras_trp_sg_sock_data_t *sock_data;
253 sg_task_data_t *task_data;
255 sock_data = (gras_trp_sg_sock_data_t *)sock->data;
257 sprintf(name,"Chunk[%d]",count++);
259 if (!(task_data=malloc(sizeof(sg_task_data_t))))
261 if (!(task_data->data=malloc(size)))
263 task_data->size = size;
264 memcpy(task_data->data,data,size);
266 task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
268 DEBUG4("send chunk %s from %s to %s on channel %d",
269 name, MSG_host_get_name(MSG_host_self()),
270 MSG_host_get_name(sock_data->to_host), sock_data->to_chan);
271 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
272 RAISE0(system_error,"Problem during the MSG_task_put");
278 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
281 gras_procdata_t *pd=gras_procdata_get();
284 sg_task_data_t *task_data;
285 gras_trp_sg_sock_data_t *sock_data = sock->data;
287 DEBUG3("recv chunk on %s from %s on channel %d",
288 MSG_host_get_name(MSG_host_self()),
289 MSG_host_get_name(sock_data->to_host), sock_data->to_chan);
290 if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
291 RAISE0(unknown_error,"Error in MSG_task_get()");
293 task_data = MSG_task_get_data(task);
294 gras_assert2(task_data->size == size,
295 "Got %d bytes when %d where expected",
296 task_data->size, size);
297 memcpy(data,task_data->data,size);
298 free(task_data->data);
301 if (MSG_task_destroy(task) != MSG_OK)
302 RAISE0(unknown_error,"Error in MSG_task_destroy()");
309 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){