3 /* Thread in charge of listening the network and queuing incoming messages */
5 /* Copyright (c) 2007 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. */
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");
15 #include "xbt/queue.h"
16 #include "xbt/synchro.h"
18 #include "gras/Transport/transport_interface.h" /* gras_select */
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;
28 static void listener_function(void *p) {
29 gras_msg_listener_t me = (gras_msg_listener_t)p;
32 gras_msgtype_t msg_wakeup_listener_t = gras_msgtype_by_name("_wakeup_listener");
33 DEBUG0("I'm the listener");
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);
42 DEBUG0("Asked to get awake");
44 /* empty the list of sockets to trash */
48 xbt_queue_shift_timed(me->socks_to_close,&sock,0);
49 if(tcp_close(sock) < 0) {
50 WARN3("error while closing tcp socket %d: %d (%s)\n",
51 sock, sock_errno, sock_errstr(sock_errno));
55 if (e.category != timeout_error)
63 gras_msg_listener_launch(xbt_queue_t msg_exchange){
64 gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t,1);
67 DEBUG0("Launch listener");
68 arg->incomming_messages = msg_exchange;
69 arg->socks_to_close = xbt_queue_new(0,sizeof(int));
71 /* get a free socket for the receiving part of the listener, taking care that it does not get saved as "myport" number */
72 my_port = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport;
73 arg->wakeup_sock_listener_side=gras_socket_server_range(5000,6000,-1,0);
74 ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = my_port;
75 /* Connect the other part of the socket */
76 arg->wakeup_sock_master_side=gras_socket_client(gras_os_myname(),gras_socket_my_port(arg->wakeup_sock_listener_side));
78 /* declare the message used to awake the listener from the master */
79 gras_msgtype_declare("_wakeup_listener",gras_datadesc_by_name("char"));
81 /* actually start the thread */
82 arg->listener = xbt_thread_create("listener",listener_function,arg);
83 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 */
87 void gras_msg_listener_shutdown(gras_msg_listener_t l) {
88 DEBUG0("Listener quit");
89 xbt_thread_cancel(l->listener);
90 xbt_queue_free(&l->incomming_messages);
91 xbt_queue_free(&l->socks_to_close);
95 #include "gras/Virtu/virtu_private.h" /* procdata_t content */
96 void gras_msg_listener_awake(){
100 DEBUG0("Awaking the listener");
101 pd = gras_procdata_get();
103 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&c);
106 void gras_msg_listener_close_socket(int sd) {
107 gras_procdata_t *pd = gras_procdata_get();
109 xbt_queue_push(pd->listener->socks_to_close,&sd);
110 gras_msg_listener_awake();