Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27649e99d862276c7919def9c8db1381117ebc38
[simgrid.git] / src / gras / Msg / gras_msg_listener.c
1 /* $Id$ */
2
3 /* Thread in charge of listening the network and queuing incoming messages  */
4
5 /* Copyright (c) 2007 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #include "gras/Msg/msg_private.h"
11 #include "gras/Transport/transport_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_read,gras_msg,"Message reader thread");
13
14 #include "xbt/ex.h"
15 #include "xbt/queue.h"
16 #include "xbt/synchro.h"
17
18 #include "gras/Transport/transport_interface.h" /* gras_select */
19
20 typedef struct s_gras_msg_listener_ {
21   xbt_queue_t incomming_messages;
22   xbt_queue_t socks_to_close; /* let the listener close the sockets, since it may be selecting on them. Darwin don't like this trick */
23   gras_socket_t wakeup_sock_listener_side;
24   gras_socket_t wakeup_sock_master_side;
25   xbt_thread_t listener;
26 } s_gras_msg_listener_t;
27
28 static void listener_function(void *p) {
29   gras_msg_listener_t me = (gras_msg_listener_t)p;
30   s_gras_msg_t msg;
31   xbt_ex_t e;
32   gras_msgtype_t msg_wakeup_listener_t = gras_msgtype_by_name("_wakeup_listener");
33   DEBUG0("I'm the listener");
34   while (1) {
35     DEBUG0("Selecting");
36     msg.expe = gras_trp_select(-1);
37     DEBUG0("Select returned something");
38     gras_msg_recv(msg.expe, &msg);
39     if (msg.type!=msg_wakeup_listener_t)
40         xbt_queue_push(me->incomming_messages, &msg);
41     else  {
42        char got = *(char*)msg.payl;
43        if (got == '1') {
44           VERB0("Asked to get awake");
45        } else {
46           VERB0("Asked to die");
47           return ;
48        }               
49     }
50      /* empty the list of sockets to trash */
51      TRY {
52         while (1) {
53            int sock;
54            xbt_queue_shift_timed(me->socks_to_close,&sock,0);
55            if(tcp_close(sock) < 0) {
56               WARN3("error while closing tcp socket %d: %d (%s)\n",
57                     sock, sock_errno, sock_errstr(sock_errno));
58            }
59         }
60      } CATCH(e) {
61         if (e.category != timeout_error)
62           RETHROW;
63         xbt_ex_free(e);
64      }         
65   }
66 }
67
68 gras_msg_listener_t
69 gras_msg_listener_launch(xbt_queue_t msg_exchange){
70   gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t,1);
71   int my_port;
72
73   DEBUG0("Launch listener");
74   arg->incomming_messages = msg_exchange;
75   arg->socks_to_close = xbt_queue_new(0,sizeof(int));
76
77   /* get a free socket for the receiving part of the listener, taking care that it does not get saved as "myport" number */
78   my_port = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport;
79   arg->wakeup_sock_listener_side=gras_socket_server_range(5000,6000,-1,0);
80   ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = my_port;
81   /* Connect the other part of the socket */
82   arg->wakeup_sock_master_side=gras_socket_client(gras_os_myname(),gras_socket_my_port(arg->wakeup_sock_listener_side));
83
84   /* declare the message used to awake the listener from the master */
85   gras_msgtype_declare("_wakeup_listener",gras_datadesc_by_name("char"));
86
87   /* actually start the thread */
88   arg->listener =  xbt_thread_create("listener",listener_function,arg);
89   gras_os_sleep(0); /* TODO: useless? give the listener a chance to initialize even if the main is empty and we cancel it right afterward */
90   return arg;
91 }
92
93 #include "gras/Virtu/virtu_private.h" /* procdata_t content */
94 void gras_msg_listener_shutdown(gras_msg_listener_t l) {
95    gras_procdata_t *pd = gras_procdata_get();
96    char kill = '0';
97   DEBUG0("Listener quit");
98
99    
100    if (pd->listener) 
101      gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&kill);
102    
103    /* FIXME: thread_join is not implemented in SG (remove next conditional when fixed)
104     * But I guess it's not a big deal since we're terminating the thread mainly to 
105     * make it free its OS locks on darwin.
106     * darwin is definitly different from the neat & nice SG world */   
107    if (gras_if_RL()) 
108      xbt_thread_join(pd->listener->listener);
109
110   xbt_queue_free(&l->incomming_messages);
111   xbt_queue_free(&l->socks_to_close);
112   xbt_free(l);
113 }
114
115 void gras_msg_listener_awake(){
116         gras_procdata_t *pd;
117         char c = '1';
118
119         DEBUG0("Awaking the listener");
120         pd = gras_procdata_get();
121         if (pd->listener) {
122                 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&c);
123         }
124 }
125 void  gras_msg_listener_close_socket(int sd) {
126    gras_procdata_t *pd = gras_procdata_get();
127    if (pd->listener) {
128       xbt_queue_push(pd->listener->socks_to_close,&sd);
129       gras_msg_listener_awake();
130    } else {
131       /* do it myself */
132       tcp_close(sd);
133    }    
134 }