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;
61 *** Specific socket part
64 int from_PID; /* process which sent this message */
65 int to_PID; /* process to which this message is destinated */
67 m_host_t to_host; /* Who's on other side */
68 m_channel_t to_chan;/* Channel on which the other side is earing */
69 } gras_trp_sg_sock_data_t;
76 static gras_error_t find_port(gras_hostdata_t *hd, int port,
77 gras_sg_portrec_t *hpd) {
81 gras_assert0(hd,"Please run gras_process_init on each process");
83 gras_dynar_foreach(hd->ports, cpt, pd) {
84 if (pd.port == port) {
85 memcpy(hpd,&pd,sizeof(gras_sg_portrec_t));
89 return mismatch_error;
94 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
96 gras_trp_sg_plug_data_t *data=malloc(sizeof(gras_trp_sg_plug_data_t));
103 plug->socket_client = gras_trp_sg_socket_client;
104 plug->socket_server = gras_trp_sg_socket_server;
105 plug->socket_close = gras_trp_sg_socket_close;
107 plug->chunk_send = gras_trp_sg_chunk_send;
108 plug->chunk_recv = gras_trp_sg_chunk_recv;
114 gras_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
117 /* OUT */ gras_socket_t *sock){
119 gras_error_t errcode;
123 gras_trp_sg_sock_data_t *data;
124 gras_sg_portrec_t pr;
126 /* make sure this socket will reach someone */
127 if (!(peer=MSG_get_host_by_name(host))) {
128 fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
129 return mismatch_error;
131 if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
132 RAISE1(mismatch_error,
133 "can't connect to %s: no process on this host",
136 errcode = find_port(hd,port,&pr);
137 if (errcode != no_error && errcode != mismatch_error)
140 if (errcode == mismatch_error) {
141 RAISE2(mismatch_error,
142 "can't connect to %s:%d, no process listen on this port",
146 if (pr.raw && !sock->raw) {
147 RAISE2(mismatch_error,
148 "can't connect to %s:%d in regular mode, the process listen "
149 "in raw mode on this port",host,port);
151 if (!pr.raw && sock->raw) {
152 RAISE2(mismatch_error,
153 "can't connect to %s:%d in raw mode, the process listen "
154 "in regular mode on this port",host,port);
157 /* create the socket */
158 if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
161 data->from_PID = MSG_process_self_PID();
162 data->to_PID = hd->proc[ pr.tochan ];
163 data->to_host = peer;
164 data->to_chan = pr.tochan;
168 DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
169 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
170 sock->raw?"RAW":"regular",host,port,data->to_PID);
175 gras_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
177 gras_socket_t *sock){
179 gras_error_t errcode;
181 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
182 gras_procdata_t *pd=gras_procdata_get();
183 gras_sg_portrec_t pr;
184 gras_trp_sg_sock_data_t *data;
186 const char *host=MSG_host_get_name(MSG_host_self());
188 gras_assert0(hd,"Please run gras_process_init on each process");
190 sock->accepting = 0; /* no such nuisance in SG */
192 errcode = find_port(hd,port,&pr);
194 case no_error: /* Port already used... */
195 RAISE2(mismatch_error,
196 "can't listen on address %s:%d: port already in use\n.",
199 case mismatch_error: /* Port not used so far. Do it */
200 pr.tochan = sock->raw ? pd->rawChan : pd->chan;
203 TRY(gras_dynar_push(hd->ports,&pr));
207 WARN1("asked to open two raw server sockets on %s, first one lost",
208 MSG_host_get_name(MSG_host_self()));
212 WARN1("asked to open two server sockets on %s, first one lost",
213 MSG_host_get_name(MSG_host_self()));
221 /* Create the socket */
222 if (!(data = malloc(sizeof(gras_trp_sg_sock_data_t))))
226 data->to_PID = MSG_process_self_PID();
227 data->to_host = MSG_host_self();
228 data->to_chan = pd->chan;
232 INFO6("'%s' (%d) ears on %s:%d%s (%p)",
233 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
234 host,port,sock->raw? " (mode RAW)":"",*sock);
239 void gras_trp_sg_socket_close(gras_socket_t *sock){
240 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
241 gras_procdata_t *pd=gras_procdata_get();
243 gras_sg_portrec_t *pr;
246 gras_assert0(hd,"Please run gras_process_init on each process");
250 if (sock->incoming) {
251 /* server mode socket. Un register it from 'OS' tables */
252 gras_dynar_foreach(hd->ports, cpt, pr) {
253 if (pr->port == sock->port) {
254 gras_dynar_cursor_rm(hd->ports, &cpt);
264 WARN0("socket_close called on an unknown socket");
273 gras_error_t gras_trp_sg_chunk_send(gras_socket_t *sock,
277 static unsigned int count=0;
279 gras_trp_sg_sock_data_t *sock_data;
280 sg_task_data_t *task_data;
282 sock_data = (gras_trp_sg_sock_data_t *)sock->data;
284 sprintf(name,"Chunk[%d]",count++);
286 if (!(task_data=malloc(sizeof(sg_task_data_t))))
288 if (!(task_data->data=malloc(size)))
290 task_data->size = size;
291 memcpy(task_data->data,data,size);
293 task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
295 if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
296 RAISE0(system_error,"Problem during the MSG_task_put");
302 gras_error_t gras_trp_sg_chunk_recv(gras_socket_t *sock,
305 gras_procdata_t *pd=gras_procdata_get();
308 sg_task_data_t *task_data;
310 if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
311 RAISE0(unknown_error,"Error in MSG_task_get()");
313 task_data = MSG_task_get_data(task);
314 gras_assert2(task_data->size == size,
315 "Got %d bytes when %d where expected",
316 task_data->size, size);
317 memcpy(data,task_data->data,size);
318 free(task_data->data);
321 if (MSG_task_destroy(task) != MSG_OK)
322 RAISE0(unknown_error,"Error in MSG_task_destroy()");
329 gras_error_t gras_trp_sg_flush(gras_socket_t *sd){