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 
40 
45 
50 
54  void *peer_cb_cls;
55 };
56 
57 
64 static void
66 
67 
76 static void
77 handle_mq_error(void *cls, enum GNUNET_MQ_Error error)
78 {
79  struct GNUNET_CORE_MonitorHandle *mh = cls;
80 
81  (void)error;
82  reconnect(mh);
83 }
84 
85 
92 static void
93 handle_receive_info(void *cls, const struct MonitorNotifyMessage *mon_message)
94 {
95  struct GNUNET_CORE_MonitorHandle *mh = cls;
96 
97  mh->peer_cb(mh->peer_cb_cls,
98  &mon_message->peer,
99  (enum GNUNET_CORE_KxState)ntohl(mon_message->state),
100  GNUNET_TIME_absolute_ntoh(mon_message->timeout));
101 }
102 
103 
110 static void
112 {
113  struct GNUNET_MQ_MessageHandler handlers[] =
114  { GNUNET_MQ_hd_fixed_size(receive_info,
116  struct MonitorNotifyMessage,
117  mh),
119  struct GNUNET_MQ_Envelope *env;
120  struct GNUNET_MessageHeader *msg;
121 
122  if (NULL != mh->mq)
123  GNUNET_MQ_destroy(mh->mq);
124  /* FIXME: use backoff? */
125  mh->mq =
126  GNUNET_CLIENT_connect(mh->cfg, "core", handlers, &handle_mq_error, mh);
127  if (NULL == mh->mq)
128  return;
129  /* notify callback about reconnect */
130  if (NULL != mh->peer_cb)
131  mh->peer_cb(mh->peer_cb_cls,
132  NULL,
136  GNUNET_MQ_send(mh->mq, env);
137 }
138 
139 
159  void *peer_cb_cls)
160 {
162 
163  GNUNET_assert(NULL != peer_cb);
165  mh->cfg = cfg;
166  reconnect(mh);
167  mh->peer_cb = peer_cb;
168  mh->peer_cb_cls = peer_cb_cls;
169  if (NULL == mh->mq)
170  {
171  GNUNET_free(mh);
172  return NULL;
173  }
174  return mh;
175 }
176 
177 
183 void
185 {
186  if (NULL != mh->mq)
187  {
188  GNUNET_MQ_destroy(mh->mq);
189  mh->mq = NULL;
190  }
191  GNUNET_free(mh);
192 }
193 
194 
195 /* 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:671
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:900
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:313
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:84
void GNUNET_CORE_monitor_stop(struct GNUNET_CORE_MonitorHandle *mh)
Stop monitoring CORE activity.
configuration data
Definition: configuration.c:83
void * peer_cb_cls
Closure for peer_cb.
uint32_t state
New peer state, an enum GNUNET_CORE_KxState in NBO.
Definition: core.h:303
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:821
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:351
Message sent by the service to monitor clients to notify them about a peer changing status...
Definition: core.h:294
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:308