3 /* gras_rl - implementation of GRAS on top of the SimGrid simulator */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
15 XBT_LOG_DEFAULT_CATEGORY(GRAS);
17 /* **************************************************************************
18 * Openning/Maintaining/Closing connexions (private functions for both raw
19 * and regular sockets)
20 * **************************************************************************/
22 _gras_sock_server_open(unsigned short startingPort, unsigned short endingPort,
23 int raw, unsigned int bufSize, /* OUT */ gras_sock_t **sock) {
25 gras_hostdata_t *hd=hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
26 grasProcessData_t *pd=(grasProcessData_t *)MSG_process_get_data(MSG_process_self());
28 const char *host=MSG_host_get_name(MSG_host_self());
30 xbt_assert0(hd,"HostData=NULL !! Please run grasInit on each process\n");
31 xbt_assert0(pd,"ProcessData=NULL !! Please run grasInit on each process\n");
33 for (port=startingPort ; port <= endingPort ; port++) {
34 for (i=0; i<hd->portLen && hd->port[i] != port; i++);
35 if (i<hd->portLen && hd->port[i] == port)
38 /* port not used so far. Do it */
39 if (i == hd->portLen) {
40 /* need to enlarge the tables */
42 hd->port2chan=(int*)realloc(hd->port2chan,hd->portLen*sizeof(int));
43 hd->port =(int*)realloc(hd->port ,hd->portLen*sizeof(int));
44 hd->raw =(int*)realloc(hd->raw ,hd->portLen*sizeof(int));
46 hd->port2chan=(int*)malloc(hd->portLen*sizeof(int));
47 hd->port =(int*)malloc(hd->portLen*sizeof(int));
48 hd->raw =(int*)malloc(hd->portLen*sizeof(int));
50 if (!hd->port2chan || !hd->port || !hd->raw) {
51 fprintf(stderr,"GRAS: PANIC: A malloc error did lose all ports attribution on this host\n");
56 hd->port2chan[ i ]=raw ? pd->rawChan : pd->chan;
60 /* Create the socket */
61 if (!(*sock=(gras_sock_t*)malloc(sizeof(gras_sock_t)))) {
62 fprintf(stderr,"GRAS: openServerSocket: out of memory\n");
66 (*sock)->server_sock = 1;
67 (*sock)->raw_sock = raw;
68 (*sock)->from_PID = -1;
69 (*sock)->to_PID = MSG_process_self_PID();
70 (*sock)->to_host = MSG_host_self();
71 (*sock)->to_port = port;
72 (*sock)->to_chan = pd->chan;
75 fprintf(stderr,"GRAS: '%s' (%d) ears on %s:%d%s (%p).\n",
76 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
77 host,port,raw? " (mode RAW)":"",*sock);
81 /* if we go out of the previous for loop, that's that we didn't find any
85 "GRAS: can't find an empty port between %d and %d to open a socket on host %s\n.",
86 startingPort,endingPort,host);
87 return mismatch_error;
91 _gras_sock_client_open(const char *host, short port, int raw, unsigned int bufSize,
92 /* OUT */ gras_sock_t **sock) {
97 /* make sure this socket will reach someone */
98 if (!(peer=MSG_get_host_by_name(host))) {
99 fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",host);
100 return mismatch_error;
102 if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
103 fprintf(stderr,"GRAS: can't connect to %s: no process on this host.\n",host);
104 return mismatch_error;
106 for (i=0; i<hd->portLen && port != hd->port[i]; i++);
107 if (i == hd->portLen) {
108 fprintf(stderr,"GRAS: can't connect to %s:%d, no process listen on this port.\n",host,port);
109 return mismatch_error;
112 if (hd->raw[i] && !raw) {
113 fprintf(stderr,"GRAS: can't connect to %s:%d in regular mode, the process listen in raw mode on this port.\n",host,port);
114 return mismatch_error;
116 if (!hd->raw[i] && raw) {
117 fprintf(stderr,"GRAS: can't connect to %s:%d in raw mode, the process listen in regular mode on this port.\n",host,port);
118 return mismatch_error;
122 /* Create the socket */
123 if (!(*sock=(gras_sock_t*)malloc(sizeof(gras_sock_t)))) {
124 fprintf(stderr,"GRAS: openClientSocket: out of memory\n");
128 (*sock)->server_sock = 0;
129 (*sock)->raw_sock = raw;
130 (*sock)->from_PID = MSG_process_self_PID();
131 (*sock)->to_PID = hd->proc[ hd->port2chan[i] ];
132 (*sock)->to_host = peer;
133 (*sock)->to_port = port;
134 (*sock)->to_chan = hd->port2chan[i];
137 fprintf(stderr,"GRAS: %s (PID %d) connects in %s mode to %s:%d (to_PID=%d).\n",
138 MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
139 raw?"RAW":"regular",host,port,(*sock)->to_PID);
144 xbt_error_t _gras_sock_close(int raw, gras_sock_t *sd) {
145 gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
148 xbt_assert0(hd,"HostData=NULL !! Please run grasInit on each process\n");
150 if (!sd) return no_error;
151 if (raw && !sd->raw_sock) {
152 fprintf(stderr,"GRAS: gras_rawsock_close: Was passed a regular socket. Please use gras_sock_close()\n");
154 if (!raw && sd->raw_sock) {
155 fprintf(stderr,"GRAS: grasSockClose: Was passed a raw socket. Please use gras_rawsock_close()\n");
157 if (sd->server_sock) {
158 /* server mode socket. Un register it from 'OS' tables */
160 i<hd->portLen && sd->to_port != hd->port[i];
163 if (i==hd->portLen) {
164 fprintf(stderr,"GRAS: closeSocket: The host does not know this server socket.\n");
166 memmove(&(hd->port[i]), &(hd->port[i+1]), (hd->portLen -i -1) * sizeof(int));
167 memmove(&(hd->raw[i]), &(hd->raw[i+1]), (hd->portLen -i -1) * sizeof(int));
168 memmove(&(hd->port2chan[i]), &(hd->port2chan[i+1]), (hd->portLen -i -1) * sizeof(int));
176 /* **************************************************************************
177 * Creating/Using regular sockets
178 * **************************************************************************/
180 gras_sock_server_open(unsigned short startingPort, unsigned short endingPort,
181 /* OUT */ gras_sock_t **sock) {
182 return _gras_sock_server_open(startingPort,endingPort,0,0,sock);
186 gras_sock_client_open(const char *host, short port,
187 /* OUT */ gras_sock_t **sock) {
188 return _gras_sock_client_open(host,port,0,0,sock);
191 xbt_error_t gras_sock_close(gras_sock_t *sd) {
192 return _gras_sock_close(0,sd);
196 gras_sock_get_my_port(gras_sock_t *sd) {
197 if (!sd || !sd->server_sock) return -1;
202 gras_sock_get_peer_port(gras_sock_t *sd) {
203 if (!sd || sd->server_sock) return -1;
208 gras_sock_get_peer_name(gras_sock_t *sd) {
211 if (!sd) return NULL;
212 if ((proc=MSG_process_from_PID(sd->to_PID))) {
213 return (char*) MSG_host_get_name(MSG_process_get_host(proc));
215 fprintf(stderr,"GRAS: try to access hostname of unknown process %d\n",sd->to_PID);
216 return (char*) "(dead or unknown host)";
219 /* **************************************************************************
220 * Creating/Using raw sockets
221 * **************************************************************************/
222 xbt_error_t gras_rawsock_server_open(unsigned short startingPort, unsigned short endingPort,
223 unsigned int bufSize, gras_rawsock_t **sock) {
224 return _gras_sock_server_open(startingPort,endingPort,1,bufSize,(gras_sock_t**)sock);
227 xbt_error_t gras_rawsock_client_open(const char *host, short port,
228 unsigned int bufSize, gras_rawsock_t **sock) {
229 return _gras_sock_client_open(host,port,1,bufSize,(gras_sock_t**)sock);
232 xbt_error_t gras_rawsock_close(gras_rawsock_t *sd) {
233 return _gras_sock_close(1,(gras_sock_t*)sd);
237 gras_rawsock_get_peer_port(gras_rawsock_t *sd) {
238 if (!sd || !sd->server_sock) return -1;
243 gras_rawsock_send(gras_rawsock_t *sock, unsigned int expSize, unsigned int msgSize) {
244 unsigned int bytesTotal;
245 static unsigned int count=0;
249 xbt_assert0(sock->raw_sock,"Asked to send raw data on a regular socket\n");
252 bytesTotal < expSize;
253 bytesTotal += msgSize) {
255 sprintf(name,"Raw data[%d]",count++);
257 task=MSG_task_create(name,0,((double)msgSize)/(1024.0*1024.0),NULL);
259 fprintf(stderr, "%f:%s: gras_rawsock_send(%f %s -> %s) BEGIN\n",
261 MSG_process_get_name(MSG_process_self()),
262 ((double)msgSize)/(1024.0*1024.0),
263 MSG_host_get_name( MSG_host_self()),
264 MSG_host_get_name( sock->to_host));
266 if (MSG_task_put(task, sock->to_host,sock->to_chan) != MSG_OK) {
267 fprintf(stderr,"GRAS: msgSend: Problem during the MSG_task_put()\n");
268 return unknown_error;
270 /*fprintf(stderr, "%f:%s: gras_rawsock_send(%f -> %s) END\n",
272 MSG_process_get_name(MSG_process_self()),
273 ((double)msgSize)/(1024.0*1024.0),
274 MSG_host_get_name( sock->to_host));*/
280 gras_rawsock_recv(gras_rawsock_t *sock, unsigned int expSize, unsigned int msgSize,
281 unsigned int timeout) {
282 grasProcessData_t *pd=(grasProcessData_t *)MSG_process_get_data(MSG_process_self());
283 unsigned int bytesTotal;
287 xbt_assert0(sock->raw_sock,"Asked to receive raw data on a regular socket\n");
289 startTime=gras_time();
291 bytesTotal < expSize;
292 bytesTotal += msgSize) {
296 fprintf(stderr, "%f:%s: gras_rawsock_recv() BEGIN\n",
298 MSG_process_get_name(MSG_process_self()));
301 if (MSG_task_Iprobe((m_channel_t) pd->rawChan)) {
302 if (MSG_task_get(&task, (m_channel_t) pd->rawChan) != MSG_OK) {
303 fprintf(stderr,"GRAS: Error in MSG_task_get()\n");
304 return unknown_error;
306 if (MSG_task_destroy(task) != MSG_OK) {
307 fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
308 return unknown_error;
311 fprintf(stderr, "%f:%s: gras_rawsock_recv() END\n",
313 MSG_process_get_name(MSG_process_self()));
317 MSG_process_sleep(0.0001);
319 } while (gras_time() - startTime < timeout);
321 if (gras_time() - startTime > timeout)
322 return timeout_error;
328 /* **************************************************************************
329 * Actually exchanging messages
330 * **************************************************************************/
333 grasMsgRecv(gras_msg_t **msg,
336 double startTime=gras_time();
337 grasProcessData_t *pd=grasProcessDataGet();
341 if (MSG_task_Iprobe((m_channel_t) pd->chan)) {
342 if (MSG_task_get(&task, (m_channel_t) pd->chan) != MSG_OK) {
343 fprintf(stderr,"GRAS: Error in MSG_task_get()\n");
344 return unknown_error;
347 *msg=(gras_msg_t*)MSG_task_get_data(task);
351 gras_msg_t *__dm_=*msg;
353 fprintf(stderr, "grasMsgRecv(%s) = %d seq (",
354 __dm_->entry->name, __dm_->entry->seqCount );
356 for (i=0; i<__dm_->entry->seqCount; i++) {
357 fprintf(stderr,"%d elem {",__dm_->dataCount[i]);
358 for (j=0; j<__dm_->dataCount[i]; j++) {
359 fprintf(stderr,"%p; ",__dm_->data[i]);
361 fprintf(stderr,"},");
363 fprintf(stderr, ")\n");
367 if (MSG_task_destroy(task) != MSG_OK) {
368 fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
369 return unknown_error;
374 MSG_process_sleep(1);
376 } while (gras_time()-startTime < timeOut || MSG_task_Iprobe((m_channel_t) pd->chan));
377 return timeout_error;
381 gras_msg_send(gras_sock_t *sd,
383 e_xbt_free_directive_t freeDirective) {
385 grasProcessData_t *pd=grasProcessDataGet();
392 /* arg validity checks */
393 xbt_assert0(msg,"Trying to send NULL message");
394 xbt_assert0(sd ,"Trying to send over a NULL socket");
396 if (!(answer=(gras_sock_t*)malloc(sizeof(gras_sock_t)))) {
399 answer->server_sock=0;
401 answer->from_PID = sd->to_PID;
402 answer->to_PID = MSG_process_self_PID();
403 answer->to_host = MSG_host_self();
405 answer->to_chan = pd->chan;
407 sprintf(name,"%s[%d]",_msg->entry->name,count++);
408 /* if freeDirective == free_never, we have to build a copy of the message */
409 if (freeDirective == free_never) {
410 msg=gras_msg_copy(_msg);
417 fprintf(stderr,"Send %s with answer(%p)=",msg->entry->name,msg->sock);
418 fprintf(stderr,"(server=%d,raw=%d,fromPID=%d,toPID=%d,toHost=%p,toPort=%d,toChan=%d)\n",
419 msg->sock->server_sock,msg->sock->raw_sock,msg->sock->from_PID,
420 msg->sock->to_PID,msg->sock->to_host,msg->sock->to_port,msg->sock->to_chan);
421 fprintf(stderr,"Send over %p=(server=%d,raw=%d,fromPID=%d,toPID=%d,toHost=%p,toPort=%d,toChan=%d)\n",
422 sd,sd->server_sock,sd->raw_sock,sd->from_PID,sd->to_PID,sd->to_host,sd->to_port,sd->to_chan);
428 gras_msg_t *__dm_=msg;
430 fprintf(stderr, "gras_msg_send(%s) = %d seq (",
431 __dm_->entry->name, __dm_->entry->seqCount );
433 for (i=0; i<__dm_->entry->seqCount; i++) {
434 fprintf(stderr,"%d elem {",__dm_->dataCount[i]);
435 for (j=0; j<__dm_->dataCount[i]; j++) {
436 fprintf(stderr,"%p; ",__dm_->data[i]);
438 fprintf(stderr,"},");
440 fprintf(stderr, ")\n");
445 task=MSG_task_create(name,0,((double)msg->header->dataSize)/(1024.0*1024.0),msg);
446 if (MSG_task_put(task, sd->to_host,sd->to_chan) != MSG_OK) {
447 fprintf(stderr,"GRAS: msgSend: Problem during the MSG_task_put()\n");
448 return unknown_error;
453 gras_sock_t *gras_sock_new(void) {
454 return malloc(sizeof(gras_sock_t));
457 void grasSockFree(gras_sock_t *s) {
461 /* **************************************************************************
463 * **************************************************************************/
464 grasProcessData_t *grasProcessDataGet() {
465 return (grasProcessData_t *)MSG_process_get_data(MSG_process_self());
468 /* **************************************************************************
469 * Wrappers over OS functions
470 * **************************************************************************/
472 return MSG_getClock();
475 void gras_sleep(unsigned long sec, unsigned long usec) {
476 MSG_process_sleep((double)sec + ((double)usec)/1000000);