diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-07-08 21:44:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-07-08 21:44:05 +0000 |
commit | 8f31d00ecb420926db5363a2882b5302d8635246 (patch) | |
tree | 93cbc42a2d86539716fa508ed34748d6a84c370b /src/namestore/namestore_api_monitor.c | |
parent | 8096356e5dab41fa45cedfaf1f5bb41603cba89a (diff) | |
download | gnunet-8f31d00ecb420926db5363a2882b5302d8635246.tar.gz gnunet-8f31d00ecb420926db5363a2882b5302d8635246.zip |
-skeleton for monitor API
Diffstat (limited to 'src/namestore/namestore_api_monitor.c')
-rw-r--r-- | src/namestore/namestore_api_monitor.c | 165 |
1 files changed, 162 insertions, 3 deletions
diff --git a/src/namestore/namestore_api_monitor.c b/src/namestore/namestore_api_monitor.c index 4c242d17d..e2caea8a5 100644 --- a/src/namestore/namestore_api_monitor.c +++ b/src/namestore/namestore_api_monitor.c | |||
@@ -35,16 +35,149 @@ | |||
35 | #include "namestore.h" | 35 | #include "namestore.h" |
36 | 36 | ||
37 | 37 | ||
38 | |||
39 | /** | 38 | /** |
40 | * Handle for a monitoring activity. | 39 | * Handle for a monitoring activity. |
41 | */ | 40 | */ |
42 | struct GNUNET_NAMESTORE_ZoneMonitor | 41 | struct GNUNET_NAMESTORE_ZoneMonitor |
43 | { | 42 | { |
43 | /** | ||
44 | * Configuration (to reconnect). | ||
45 | */ | ||
46 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
47 | |||
48 | /** | ||
49 | * Handle to namestore service. | ||
50 | */ | ||
51 | struct GNUNET_CLIENT_Connection *h; | ||
52 | |||
53 | /** | ||
54 | * Function to call on events. | ||
55 | */ | ||
56 | GNUNET_NAMESTORE_RecordMonitor monitor; | ||
57 | |||
58 | /** | ||
59 | * Closure for 'monitor'. | ||
60 | */ | ||
61 | void *monitor_cls; | ||
62 | |||
63 | /** | ||
64 | * Transmission handle to client. | ||
65 | */ | ||
66 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
67 | |||
68 | /** | ||
69 | * Monitored zone. | ||
70 | */ | ||
71 | struct GNUNET_CRYPTO_ShortHashCode zone; | ||
72 | |||
73 | /** | ||
74 | * GNUNET_YES if we monitor all zones, GNUNET_NO if we only monitor 'zone'. | ||
75 | */ | ||
76 | int all_zones; | ||
44 | }; | 77 | }; |
45 | 78 | ||
46 | 79 | ||
47 | /** | 80 | /** |
81 | * Send our request to start monitoring to the service. | ||
82 | * | ||
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 | ||
87 | */ | ||
88 | static size_t | ||
89 | transmit_monitor_message (void *cls, | ||
90 | size_t size, | ||
91 | void *buf); | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Reconnect to the namestore service. | ||
96 | * | ||
97 | * @param zm monitor to reconnect | ||
98 | */ | ||
99 | static void | ||
100 | reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm) | ||
101 | { | ||
102 | if (NULL != zm->h) | ||
103 | GNUNET_CLIENT_disconnect (zm->h); | ||
104 | zm->monitor (zm->monitor_cls, | ||
105 | NULL, | ||
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, | ||
112 | GNUNET_YES, | ||
113 | &transmit_monitor_message, | ||
114 | zm); | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * We've received a notification about a change to our zone. | ||
120 | * Forward to monitor callback. | ||
121 | * | ||
122 | * @param cls the zone monitor handle | ||
123 | * @param msg the message from the service. | ||
124 | */ | ||
125 | static void | ||
126 | handle_updates (void *cls, | ||
127 | const struct GNUNET_MessageHeader *msg) | ||
128 | { | ||
129 | struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls; | ||
130 | |||
131 | if (NULL == msg) | ||
132 | { | ||
133 | reconnect (zm); | ||
134 | return; | ||
135 | } | ||
136 | // FIXME: parse, validate | ||
137 | |||
138 | GNUNET_CLIENT_receive (zm->h, | ||
139 | &handle_updates, | ||
140 | zm, | ||
141 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
142 | // FIXME: call 'monitor'. | ||
143 | // zm->monitor (zm->monitor_cls, ...); | ||
144 | } | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Send our request to start monitoring to the service. | ||
149 | * | ||
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 | ||
154 | */ | ||
155 | static size_t | ||
156 | transmit_monitor_message (void *cls, | ||
157 | size_t size, | ||
158 | void *buf) | ||
159 | { | ||
160 | struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls; | ||
161 | struct ZoneMonitorStartMessage sm; | ||
162 | |||
163 | if (size < sizeof (struct ZoneMonitorStartMessage)) | ||
164 | { | ||
165 | reconnect (zm); | ||
166 | return 0; | ||
167 | } | ||
168 | |||
169 | sm.zone = zm->zone; | ||
170 | sm.all_zones = htonl (zm->all_zones); | ||
171 | memcpy (buf, &sm, sizeof (sm)); | ||
172 | GNUNET_CLIENT_receive (zm->h, | ||
173 | &handle_updates, | ||
174 | zm, | ||
175 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
176 | return sizeof (sm); | ||
177 | } | ||
178 | |||
179 | |||
180 | /** | ||
48 | * Begin monitoring a zone for changes. Will first call the 'monitor' function | 181 | * Begin monitoring a zone for changes. Will first call the 'monitor' function |
49 | * on all existing records in the selected zone(s) and then call it whenever | 182 | * on all existing records in the selected zone(s) and then call it whenever |
50 | * a record changes. | 183 | * a record changes. |
@@ -61,8 +194,27 @@ GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *c | |||
61 | GNUNET_NAMESTORE_RecordMonitor monitor, | 194 | GNUNET_NAMESTORE_RecordMonitor monitor, |
62 | void *monitor_cls) | 195 | void *monitor_cls) |
63 | { | 196 | { |
64 | GNUNET_break (0); | 197 | struct GNUNET_NAMESTORE_ZoneMonitor *zm; |
65 | return NULL; | 198 | struct GNUNET_CLIENT_Connection *client; |
199 | |||
200 | if (NULL == (client = GNUNET_CLIENT_connect ("namestore", cfg))) | ||
201 | return NULL; | ||
202 | zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor); | ||
203 | zm->cfg = cfg; | ||
204 | zm->h = client; | ||
205 | if (NULL == zone) | ||
206 | zm->all_zones = GNUNET_YES; | ||
207 | else | ||
208 | zm->zone = *zone; | ||
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, | ||
214 | GNUNET_YES, | ||
215 | &transmit_monitor_message, | ||
216 | zm); | ||
217 | return zm; | ||
66 | } | 218 | } |
67 | 219 | ||
68 | 220 | ||
@@ -74,6 +226,13 @@ GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *c | |||
74 | void | 226 | void |
75 | GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm) | 227 | GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm) |
76 | { | 228 | { |
229 | if (NULL != zm->th) | ||
230 | { | ||
231 | GNUNET_CLIENT_notify_transmit_ready_cancel (zm->th); | ||
232 | zm->th = NULL; | ||
233 | } | ||
234 | GNUNET_CLIENT_disconnect (zm->h); | ||
235 | GNUNET_free (zm); | ||
77 | } | 236 | } |
78 | 237 | ||
79 | /* end of namestore_api_monitor.c */ | 238 | /* end of namestore_api_monitor.c */ |