GNUnet  0.10.x
core_api_monitor_peers.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2014, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
27 #include "platform.h"
28 #include "gnunet_core_service.h"
29 #include "core.h"
30 
31 
36 {
37 
42 
47 
52 
56  void *peer_cb_cls;
57 };
58 
59 
66 static void
68 
69 
78 static void
79 handle_mq_error (void *cls, enum GNUNET_MQ_Error error)
80 {
81  struct GNUNET_CORE_MonitorHandle *mh = cls;
82 
83  (void) error;
84  reconnect (mh);
85 }
86 
87 
94 static void
95 handle_receive_info (void *cls, const struct MonitorNotifyMessage *mon_message)
96 {
97  struct GNUNET_CORE_MonitorHandle *mh = cls;
98 
99  mh->peer_cb (mh->peer_cb_cls,
100  &mon_message->peer,
101  (enum GNUNET_CORE_KxState) ntohl (mon_message->state),
102  GNUNET_TIME_absolute_ntoh (mon_message->timeout));
103 }
104 
105 
112 static void
114 {
115  struct GNUNET_MQ_MessageHandler handlers[] =
116  {GNUNET_MQ_hd_fixed_size (receive_info,
118  struct MonitorNotifyMessage,
119  mh),
121  struct GNUNET_MQ_Envelope *env;
122  struct GNUNET_MessageHeader *msg;
123 
124  if (NULL != mh->mq)
125  GNUNET_MQ_destroy (mh->mq);
126  /* FIXME: use backoff? */
127  mh->mq =
128  GNUNET_CLIENT_connect (mh->cfg, "core", handlers, &handle_mq_error, mh);
129  if (NULL == mh->mq)
130  return;
131  /* notify callback about reconnect */
132  if (NULL != mh->peer_cb)
133  mh->peer_cb (mh->peer_cb_cls,
134  NULL,
138  GNUNET_MQ_send (mh->mq, env);
139 }
140 
141 
161  void *peer_cb_cls)
162 {
164 
165  GNUNET_assert (NULL != peer_cb);
167  mh->cfg = cfg;
168  reconnect (mh);
169  mh->peer_cb = peer_cb;
170  mh->peer_cb_cls = peer_cb_cls;
171  if (NULL == mh->mq)
172  {
173  GNUNET_free (mh);
174  return NULL;
175  }
176  return mh;
177 }
178 
179 
185 void
187 {
188  if (NULL != mh->mq)
189  {
190  GNUNET_MQ_destroy (mh->mq);
191  mh->mq = NULL;
192  }
193  GNUNET_free (mh);
194 }
195 
196 
197 /* end of core_api_monitor_peers.c */
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void reconnect(struct GNUNET_CORE_MonitorHandle *mh)
Protocol error, reconnect to CORE service and notify client.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:670
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
struct GNUNET_MQ_Handle * mq
Our connection to the service.
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
Handle to a CORE monitoring operation.
struct GNUNET_TIME_AbsoluteNBO timeout
How long will we stay in this state (if nothing else happens)?
Definition: core.h:324
common internal definitions for core service
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
GNUNET_CORE_KxState
State machine for our P2P encryption handshake.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS
Request for connection monitoring from CORE service.
void(* GNUNET_CORE_MonitorCallback)(void *cls, const struct GNUNET_PeerIdentity *pid, enum GNUNET_CORE_KxState state, struct GNUNET_TIME_Absolute timeout)
Function called by the monitor callback whenever a peer&#39;s connection status changes.
struct GNUNET_CORE_MonitorHandle * GNUNET_CORE_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CORE_MonitorCallback peer_cb, void *peer_cb_cls)
Monitor connectivity and KX status of all peers known to CORE.
Message handler for a specific message type.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Handle to a message queue.
Definition: mq.c:85
void GNUNET_CORE_monitor_stop(struct GNUNET_CORE_MonitorHandle *mh)
Stop monitoring CORE activity.
configuration data
Definition: configuration.c:85
void * peer_cb_cls
Closure for peer_cb.
uint32_t state
New peer state, an enum GNUNET_CORE_KxState in NBO.
Definition: core.h:314
GNUNET_CORE_MonitorCallback peer_cb
Function called with the peer.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
Message sent by the service to monitor clients to notify them about a peer changing status...
Definition: core.h:304
static void handle_receive_info(void *cls, const struct MonitorNotifyMessage *mon_message)
Receive reply from CORE service with information about a peer.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
This is not a state in a peer&#39;s state machine, but a special value used with the GNUNET_CORE_MonitorC...
static void handle_mq_error(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY
Reply for monitor by CORE service.
struct GNUNET_PeerIdentity peer
Identity of the peer.
Definition: core.h:319