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, pd) {
72 if (pd.port == port) {
73 memcpy(hpd,&pd,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;
156 DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
157 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
158 sock->raw?"RAW":"regular",host,port,data->to_PID);
163 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
165 gras_socket_t *sock){
167 gras_error_t errcode;
169 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
170 gras_procdata_t *pd=gras_procdata_get();
171 gras_sg_portrec_t pr;
172 gras_trp_sg_sock_data_t *data;
174 const char *host=MSG_host_get_name(MSG_host_self());
176 gras_assert0(hd,"Please run gras_process_init on each process");
178 sock->accepting = 0; /* no such nuisance in SG */
180 errcode = find_port(hd,port,&pr);
182 case no_error: /* Port already used... */
183 RAISE2(mismatch_error,
184 "can't listen on address %s:%d: port already in use\n.",
187 case mismatch_error: /* Port not used so far. Do it */
188 pr.tochan = sock->raw ? pd->rawChan : pd->chan;
191 TRY(gras_dynar_push(hd->ports,&pr));
197 /* Create the socket */
198 if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
202 data->to_PID = MSG_process_self_PID();
203 data->to_host = MSG_host_self();
204 data->to_chan = pd->chan;
208 INFO6("'%s' (%d) ears on %s:%d%s (%p)",
209 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
210 host,port,sock->raw? " (mode RAW)":"",*sock);
215 void gras_trp_sg_socket_close(gras_socket_t *sock){
216 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
218 gras_sg_portrec_t *pr;
221 gras_assert0(hd,"Please run gras_process_init on each process");
225 if (sock->incoming) {
226 /* server mode socket. Un register it from 'OS' tables */
227 gras_dynar_foreach(hd->ports, cpt, pr) {
228 if (pr->port == sock->port) {
229 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=malloc(sizeof(sg_task_data_t))))
258 if (!(task_data->data=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 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
266 RAISE0(system_error,"Problem during the MSG_task_put");
272 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
275 gras_procdata_t *pd=gras_procdata_get();
278 sg_task_data_t *task_data;
280 if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
281 RAISE0(unknown_error,"Error in MSG_task_get()");
283 task_data = MSG_task_get_data(task);
284 gras_assert2(task_data->size == size,
285 "Got %d bytes when %d where expected",
286 task_data->size, size);
287 memcpy(data,task_data->data,size);
288 free(task_data->data);
291 if (MSG_task_destroy(task) != MSG_OK)
292 RAISE0(unknown_error,"Error in MSG_task_destroy()");
299 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){