2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file namestore/namestore_api_monitor.c
23 * @brief API to monitor changes in the NAMESTORE
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_signatures.h"
34 #include "gnunet_namestore_service.h"
35 #include "namestore.h"
39 * Handle for a monitoring activity.
41 struct GNUNET_NAMESTORE_ZoneMonitor
44 * Configuration (to reconnect).
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle to namestore service.
51 struct GNUNET_CLIENT_Connection *h;
54 * Function to call on events.
56 GNUNET_NAMESTORE_RecordMonitor monitor;
59 * Closure for 'monitor'.
64 * Transmission handle to client.
66 struct GNUNET_CLIENT_TransmitHandle *th;
71 struct GNUNET_CRYPTO_ShortHashCode zone;
74 * GNUNET_YES if we monitor all zones, GNUNET_NO if we only monitor 'zone'.
81 * Send our request to start monitoring to the service.
83 * @param cls the monitor handle
84 * @param size number of bytes available in buf
85 * @param buf where to copy the message to the service
86 * @return number of bytes copied to buf
89 transmit_monitor_message (void *cls,
95 * Reconnect to the namestore service.
97 * @param zm monitor to reconnect
100 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
103 GNUNET_CLIENT_disconnect (zm->h);
104 zm->monitor (zm->monitor_cls,
106 GNUNET_TIME_UNIT_ZERO_ABS,
107 NULL, 0, NULL, NULL);
108 GNUNET_assert (NULL != (zm->h = GNUNET_CLIENT_connect ("namestore", zm->cfg)));
109 zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
110 sizeof (struct ZoneMonitorStartMessage),
111 GNUNET_TIME_UNIT_FOREVER_REL,
113 &transmit_monitor_message,
119 * We've received a notification about a change to our zone.
120 * Forward to monitor callback.
122 * @param cls the zone monitor handle
123 * @param msg the message from the service.
126 handle_updates (void *cls,
127 const struct GNUNET_MessageHeader *msg)
129 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
136 // FIXME: parse, validate
138 GNUNET_CLIENT_receive (zm->h,
141 GNUNET_TIME_UNIT_FOREVER_REL);
142 // FIXME: call 'monitor'.
143 // zm->monitor (zm->monitor_cls, ...);
148 * Send our request to start monitoring to the service.
150 * @param cls the monitor handle
151 * @param size number of bytes available in buf
152 * @param buf where to copy the message to the service
153 * @return number of bytes copied to buf
156 transmit_monitor_message (void *cls,
160 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
161 struct ZoneMonitorStartMessage sm;
163 if (size < sizeof (struct ZoneMonitorStartMessage))
170 sm.all_zones = htonl (zm->all_zones);
171 memcpy (buf, &sm, sizeof (sm));
172 GNUNET_CLIENT_receive (zm->h,
175 GNUNET_TIME_UNIT_FOREVER_REL);
181 * Begin monitoring a zone for changes. Will first call the 'monitor' function
182 * on all existing records in the selected zone(s) and then call it whenever
185 * @param cfg configuration to use to connect to namestore
186 * @param zone zone to monitor, NULL for all zones
187 * @param monitor function to call on zone changes
188 * @param monitor_cls closure for 'monitor'
189 * @return handle to stop monitoring
191 struct GNUNET_NAMESTORE_ZoneMonitor *
192 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
193 const struct GNUNET_CRYPTO_ShortHashCode *zone,
194 GNUNET_NAMESTORE_RecordMonitor monitor,
197 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
198 struct GNUNET_CLIENT_Connection *client;
200 if (NULL == (client = GNUNET_CLIENT_connect ("namestore", cfg)))
202 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
206 zm->all_zones = GNUNET_YES;
209 zm->monitor = monitor;
210 zm->monitor_cls = monitor_cls;
211 zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
212 sizeof (struct ZoneMonitorStartMessage),
213 GNUNET_TIME_UNIT_FOREVER_REL,
215 &transmit_monitor_message,
222 * Stop monitoring a zone for changes.
224 * @param zm handle to the monitor activity to stop
227 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
231 GNUNET_CLIENT_notify_transmit_ready_cancel (zm->th);
234 GNUNET_CLIENT_disconnect (zm->h);
238 /* end of namestore_api_monitor.c */