GNUnet  0.11.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 {
41 
46 
51 
55  void *peer_cb_cls;
56 };
57 
58 
65 static void
67 
68 
77 static void
78 handle_mq_error (void *cls, enum GNUNET_MQ_Error error)
79 {
80  struct GNUNET_CORE_MonitorHandle *mh = cls;
81 
82  (void) error;
83  reconnect (mh);
84 }
85 
86 
93 static void
94 handle_receive_info (void *cls, const struct MonitorNotifyMessage *mon_message)
95 {
96  struct GNUNET_CORE_MonitorHandle *mh = cls;
97 
98  mh->peer_cb (mh->peer_cb_cls,
99  &mon_message->peer,
100  (enum GNUNET_CORE_KxState) ntohl (mon_message->state),
101  GNUNET_TIME_absolute_ntoh (mon_message->timeout));
102 }
103 
104 
111 static void
113 {
114  struct GNUNET_MQ_MessageHandler handlers[] =
115  { GNUNET_MQ_hd_fixed_size (receive_info,
117  struct MonitorNotifyMessage,
118  mh),
120  struct GNUNET_MQ_Envelope *env;
121  struct GNUNET_MessageHeader *msg;
122 
123  if (NULL != mh->mq)
124  GNUNET_MQ_destroy (mh->mq);
125  /* FIXME: use backoff? */
126  mh->mq =
127  GNUNET_CLIENT_connect (mh->cfg, "core", handlers, &handle_mq_error, mh);
128  if (NULL == mh->mq)
129  return;
130  /* notify callback about reconnect */
131  if (NULL != mh->peer_cb)
132  mh->peer_cb (mh->peer_cb_cls,
133  NULL,
137  GNUNET_MQ_send (mh->mq, env);
138 }
139 
140 
160  void *peer_cb_cls)
161 {
163 
164  GNUNET_assert (NULL != peer_cb);
166  mh->cfg = cfg;
167  reconnect (mh);
168  mh->peer_cb = peer_cb;
169  mh->peer_cb_cls = peer_cb_cls;
170  if (NULL == mh->mq)
171  {
172  GNUNET_free (mh);
173  return NULL;
174  }
175  return mh;
176 }
177 
178 
184 void
186 {
187  if (NULL != mh->mq)
188  {
189  GNUNET_MQ_destroy (mh->mq);
190  mh->mq = NULL;
191  }
192  GNUNET_free (mh);
193 }
194 
195 
196 /* 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:673
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:1057
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:322
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:312
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:302
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:317