Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MSG is freeing the pointer it gives me as argv. That's thus a bad idea to realloc it
[simgrid.git] / src / gras / Transport / sg_transport.c
1 /* $Id$ */
2
3 /* sg_transport - SG specific functions for transport                       */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "Transport/transport_private.h"
12 #include <msg.h>
13 #include "Virtu/virtu_sg.h"
14
15 GRAS_LOG_EXTERNAL_CATEGORY(transport);
16 GRAS_LOG_DEFAULT_CATEGORY(transport);
17
18 /**
19  * gras_trp_select:
20  *
21  * Returns the next socket to service having a message awaiting.
22  *
23  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
24  *
25  * if timeout=0, do not wait for new message, only handle the ones already there.
26  *
27  * if timeout>0 and no message there, wait at most that amount of time before giving up.
28  */
29 gras_error_t 
30 gras_trp_select(double timeout, 
31                 gras_socket_t **dst) {
32
33   gras_error_t errcode;
34   double startTime=gras_time();
35   gras_procdata_t *pd=gras_procdata_get();
36   gras_trp_sg_sock_data_t *sockdata;
37   gras_trp_plugin_t *trp;
38
39   gras_socket_t *sock_iter; /* iterating over all sockets */
40   int cursor;               /* iterating over all sockets */
41
42   int r_pid, cpt;
43   gras_hostdata_t *remote_hd;
44
45   DEBUG1("select with timeout=%d",timeout);
46   do {
47     r_pid = MSG_task_probe_from((m_channel_t) pd->chan);
48     if (r_pid >= 0) {
49       /* Try to reuse an already openned socket to that expeditor */
50       gras_dynar_foreach(pd->sockets,cursor,sock_iter) {
51         DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
52         sockdata = sock_iter->data;
53
54         if (sock_iter->raw || !sock_iter->outgoing)
55           continue;
56
57         if (sockdata->from_PID == r_pid) {
58           *dst=sock_iter;
59           return no_error;
60         }
61       }
62
63       /* Socket to expeditor not created yet */
64       DEBUG0("Create a socket to the expeditor");
65
66       TRY(gras_trp_plugin_get_by_name("sg",&trp));
67
68       TRY(gras_trp_socket_new(1,dst));
69       (*dst)->plugin   = trp;
70
71       (*dst)->incoming  = 1;
72       (*dst)->outgoing  = 1;
73       (*dst)->accepting = 0;
74       (*dst)->sd        = -1;
75
76       (*dst)->port      = -1;
77
78       if (!(sockdata = malloc(sizeof(gras_trp_sg_sock_data_t))))
79         RAISE_MALLOC;
80
81       sockdata->from_PID = r_pid;
82       sockdata->to_PID = MSG_process_self_PID();
83       sockdata->to_host = MSG_process_get_host(MSG_process_from_PID(r_pid));
84       (*dst)->data = sockdata;
85
86       (*dst)->peer_port = -1;
87       (*dst)->peer_name = strdup(MSG_host_get_name(sockdata->to_host));
88
89       remote_hd=(gras_hostdata_t *)MSG_host_get_data(sockdata->to_host);
90       gras_assert0(remote_hd,"Run gras_process_init!!");
91
92       sockdata->to_chan = -1;
93       for (cpt=0; cpt< GRAS_MAX_CHANNEL; cpt++)
94         if (r_pid == remote_hd->proc[cpt])
95           sockdata->to_chan = cpt;
96
97       gras_assert0(sockdata->to_chan>0,
98                    "Got a message from a process without channel");
99
100       return no_error;
101     } else {
102       MSG_process_sleep(0.01);
103     }
104   } while (gras_time()-startTime < timeout
105            || MSG_task_Iprobe((m_channel_t) pd->chan));
106
107   return timeout_error;
108
109 }
110
111   
112 /* dummy implementations of the functions used in RL mode */
113
114 gras_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
115   return mismatch_error;
116 }
117 gras_error_t gras_trp_file_setup(gras_trp_plugin_t *plug) {
118   return mismatch_error;
119 }