diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-08-15 18:44:06 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-08-15 18:44:06 +0000 |
commit | 34e40e006af57ec7cb7c69acf0273321550117dd (patch) | |
tree | 1f4a234675455ab8306dcdfdc8bbc98a3979c6d7 | |
parent | fd6ca7a4a3366b3166bc7c4c5e02af6f11c917ef (diff) | |
download | gnunet-34e40e006af57ec7cb7c69acf0273321550117dd.tar.gz gnunet-34e40e006af57ec7cb7c69acf0273321550117dd.zip |
sensor: test case + fixes
-rw-r--r-- | src/include/gnunet_protocols.h | 5 | ||||
-rw-r--r-- | src/include/gnunet_sensor_service.h | 22 | ||||
-rw-r--r-- | src/include/gnunet_sensor_util_lib.h | 20 | ||||
-rw-r--r-- | src/sensor/Makefile.am | 9 | ||||
-rw-r--r-- | src/sensor/gnunet-sensor.c | 28 | ||||
-rw-r--r-- | src/sensor/gnunet-service-sensor.c | 2 | ||||
-rw-r--r-- | src/sensor/gnunet-service-sensor_reporting.c | 60 | ||||
-rw-r--r-- | src/sensor/sensor_api.c | 139 | ||||
-rw-r--r-- | src/sensor/test_gnunet-service-sensor_reporting.c | 469 | ||||
-rw-r--r-- | src/sensor/test_gnunet-service-sensor_reporting.conf | 13 | ||||
-rw-r--r-- | src/sensordashboard/gnunet-service-sensordashboard.c | 20 |
11 files changed, 755 insertions, 32 deletions
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 176391580..68472a3f9 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h | |||
@@ -2458,6 +2458,11 @@ extern "C" | |||
2458 | */ | 2458 | */ |
2459 | #define GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE 810 | 2459 | #define GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE 810 |
2460 | 2460 | ||
2461 | /** | ||
2462 | * Sensor anomaly report exchanged between peers | ||
2463 | */ | ||
2464 | #define GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT_P2P 811 | ||
2465 | |||
2461 | 2466 | ||
2462 | /******************************************************************************* | 2467 | /******************************************************************************* |
2463 | * PEERSTORE message types | 2468 | * PEERSTORE message types |
diff --git a/src/include/gnunet_sensor_service.h b/src/include/gnunet_sensor_service.h index 59e4248ab..250256cef 100644 --- a/src/include/gnunet_sensor_service.h +++ b/src/include/gnunet_sensor_service.h | |||
@@ -49,6 +49,11 @@ struct GNUNET_SENSOR_Handle; | |||
49 | struct GNUNET_SENSOR_IterateContext; | 49 | struct GNUNET_SENSOR_IterateContext; |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * Context of a force anomaly request | ||
53 | */ | ||
54 | struct GNUNET_SENSOR_ForceAnomalyContext; | ||
55 | |||
56 | /** | ||
52 | * Structure containing brief info about sensor | 57 | * Structure containing brief info about sensor |
53 | */ | 58 | */ |
54 | struct SensorInfoShort | 59 | struct SensorInfoShort |
@@ -147,6 +152,16 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
147 | 152 | ||
148 | 153 | ||
149 | /** | 154 | /** |
155 | * Cancel a force anomaly request. | ||
156 | * | ||
157 | * @param fa Force anomaly context returned by GNUNET_SENSOR_force_anomaly() | ||
158 | */ | ||
159 | void | ||
160 | GNUNET_SENSOR_force_anomaly_cancel (struct GNUNET_SENSOR_ForceAnomalyContext | ||
161 | *fa); | ||
162 | |||
163 | |||
164 | /** | ||
150 | * Force an anomaly status change on a given sensor. If the sensor reporting | 165 | * Force an anomaly status change on a given sensor. If the sensor reporting |
151 | * module is running, this will trigger the usual reporting logic, therefore, | 166 | * module is running, this will trigger the usual reporting logic, therefore, |
152 | * please only use this in a test environment. | 167 | * please only use this in a test environment. |
@@ -157,10 +172,13 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
157 | * @param h Service handle | 172 | * @param h Service handle |
158 | * @param sensor_name Sensor name to set the anomaly status | 173 | * @param sensor_name Sensor name to set the anomaly status |
159 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO | 174 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO |
175 | * @param cont Continuation function to be called after the request is sent | ||
176 | * @param cont_cls Closure for cont | ||
160 | */ | 177 | */ |
161 | void | 178 | struct GNUNET_SENSOR_ForceAnomalyContext * |
162 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, | 179 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, |
163 | int anomalous); | 180 | int anomalous, GNUNET_SENSOR_Continuation cont, |
181 | void *cont_cls); | ||
164 | 182 | ||
165 | #if 0 /* keep Emacsens' auto-indent happy */ | 183 | #if 0 /* keep Emacsens' auto-indent happy */ |
166 | { | 184 | { |
diff --git a/src/include/gnunet_sensor_util_lib.h b/src/include/gnunet_sensor_util_lib.h index 87ae564fa..897fbbaad 100644 --- a/src/include/gnunet_sensor_util_lib.h +++ b/src/include/gnunet_sensor_util_lib.h | |||
@@ -189,6 +189,26 @@ struct GNUNET_SENSOR_SensorInfo | |||
189 | 189 | ||
190 | }; | 190 | }; |
191 | 191 | ||
192 | /** | ||
193 | * Anomaly report received and stored by sensor dashboard. | ||
194 | * Sensor name and peer id are not included because they are part of the | ||
195 | * peerstore key. | ||
196 | */ | ||
197 | struct GNUNET_SENSOR_DashboardAnomalyEntry | ||
198 | { | ||
199 | |||
200 | /** | ||
201 | * New anomaly status | ||
202 | */ | ||
203 | uint16_t anomalous; | ||
204 | |||
205 | /** | ||
206 | * Percentage of neighbors reported the same anomaly | ||
207 | */ | ||
208 | float anomalous_neighbors; | ||
209 | |||
210 | }; | ||
211 | |||
192 | GNUNET_NETWORK_STRUCT_BEGIN | 212 | GNUNET_NETWORK_STRUCT_BEGIN |
193 | 213 | ||
194 | /** | 214 | /** |
diff --git a/src/sensor/Makefile.am b/src/sensor/Makefile.am index f7f713fdd..4c19fc7f4 100644 --- a/src/sensor/Makefile.am +++ b/src/sensor/Makefile.am | |||
@@ -95,6 +95,15 @@ test_sensor_api_SOURCES = \ | |||
95 | test_sensor_api_LDADD = \ | 95 | test_sensor_api_LDADD = \ |
96 | $(top_builddir)/src/util/libgnunetutil.la | 96 | $(top_builddir)/src/util/libgnunetutil.la |
97 | 97 | ||
98 | test_gnunet_service_sensor_reporting_SOURCES = \ | ||
99 | test_gnunet-service-sensor_reporting.c | ||
100 | test_gnunet_service_sensor_reporting_LDADD = \ | ||
101 | libgnunetsensor.la \ | ||
102 | libgnunetsensorutil.la \ | ||
103 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
104 | $(top_builddir)/src/testbed/libgnunettestbed.la \ | ||
105 | $(top_builddir)/src/peerstore/libgnunetpeerstore.la | ||
106 | |||
98 | pkgsensordir = sensors | 107 | pkgsensordir = sensors |
99 | 108 | ||
100 | install-data-local: | 109 | install-data-local: |
diff --git a/src/sensor/gnunet-sensor.c b/src/sensor/gnunet-sensor.c index ec7b5cb9e..7b7be61ef 100644 --- a/src/sensor/gnunet-sensor.c +++ b/src/sensor/gnunet-sensor.c | |||
@@ -39,6 +39,11 @@ static int get_all; | |||
39 | */ | 39 | */ |
40 | static char *get_sensor; | 40 | static char *get_sensor; |
41 | 41 | ||
42 | /** | ||
43 | * option '-f' | ||
44 | */ | ||
45 | static char *force_anomaly; | ||
46 | |||
42 | /* | 47 | /* |
43 | * Handle to sensor service | 48 | * Handle to sensor service |
44 | */ | 49 | */ |
@@ -92,6 +97,21 @@ print_sensor_info (void *cls, const struct SensorInfoShort *sensor, | |||
92 | 97 | ||
93 | 98 | ||
94 | /** | 99 | /** |
100 | * Continuation called after a force anomaly request is sent. | ||
101 | * | ||
102 | * @param cls Closure (unused) | ||
103 | * @param emsg Error message, NULL of no error | ||
104 | */ | ||
105 | void | ||
106 | force_anomaly_cont (void *cls, const char *emsg) | ||
107 | { | ||
108 | if (NULL != emsg) | ||
109 | printf ("Error: %s\n", emsg); | ||
110 | GNUNET_SCHEDULER_shutdown (); | ||
111 | } | ||
112 | |||
113 | |||
114 | /** | ||
95 | * Main function that will be run by the scheduler. | 115 | * Main function that will be run by the scheduler. |
96 | * | 116 | * |
97 | * @param cls closure | 117 | * @param cls closure |
@@ -118,6 +138,11 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
118 | GNUNET_SENSOR_iterate (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL, | 138 | GNUNET_SENSOR_iterate (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL, |
119 | get_sensor, &print_sensor_info, NULL); | 139 | get_sensor, &print_sensor_info, NULL); |
120 | } | 140 | } |
141 | else if (NULL != force_anomaly) | ||
142 | { | ||
143 | GNUNET_SENSOR_force_anomaly (sensor_handle, "nse", GNUNET_YES, | ||
144 | &force_anomaly_cont, NULL); | ||
145 | } | ||
121 | ret = 0; | 146 | ret = 0; |
122 | } | 147 | } |
123 | 148 | ||
@@ -139,6 +164,9 @@ main (int argc, char *const *argv) | |||
139 | {'g', "get-sensor", NULL, | 164 | {'g', "get-sensor", NULL, |
140 | gettext_noop ("Retrieve information about a single sensor"), | 165 | gettext_noop ("Retrieve information about a single sensor"), |
141 | 1, &GNUNET_GETOPT_set_string, &get_sensor}, | 166 | 1, &GNUNET_GETOPT_set_string, &get_sensor}, |
167 | {'f', "force anomaly", NULL, | ||
168 | gettext_noop ("Force an anomaly on a sensor, use only for testing"), | ||
169 | 1, &GNUNET_GETOPT_set_string, &force_anomaly}, | ||
142 | GNUNET_GETOPT_OPTION_END | 170 | GNUNET_GETOPT_OPTION_END |
143 | }; | 171 | }; |
144 | 172 | ||
diff --git a/src/sensor/gnunet-service-sensor.c b/src/sensor/gnunet-service-sensor.c index 02bc00a1a..e8b01ae33 100644 --- a/src/sensor/gnunet-service-sensor.c +++ b/src/sensor/gnunet-service-sensor.c | |||
@@ -130,9 +130,11 @@ handle_anomaly_force (void *cls, struct GNUNET_SERVER_Client *client, | |||
130 | { | 130 | { |
131 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 131 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
132 | "Force anomaly message received for a sensor we don't have.\n"); | 132 | "Force anomaly message received for a sensor we don't have.\n"); |
133 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
133 | return; | 134 | return; |
134 | } | 135 | } |
135 | SENSOR_reporting_anomaly_update (sensor, ntohs (anomaly_msg->anomalous)); | 136 | SENSOR_reporting_anomaly_update (sensor, ntohs (anomaly_msg->anomalous)); |
137 | GNUNET_SERVER_receive_done (client, GNUNET_YES); | ||
136 | } | 138 | } |
137 | 139 | ||
138 | 140 | ||
diff --git a/src/sensor/gnunet-service-sensor_reporting.c b/src/sensor/gnunet-service-sensor_reporting.c index a9d5d2e30..a2015f48e 100644 --- a/src/sensor/gnunet-service-sensor_reporting.c +++ b/src/sensor/gnunet-service-sensor_reporting.c | |||
@@ -472,6 +472,8 @@ get_cadet_peer (struct GNUNET_PeerIdentity pid) | |||
472 | return cadetp; | 472 | return cadetp; |
473 | cadetp = cadetp->next; | 473 | cadetp = cadetp->next; |
474 | } | 474 | } |
475 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating a CADET connection to peer `%s'.\n", | ||
476 | GNUNET_i2s (&pid)); | ||
475 | /* Not found, create struct and channel */ | 477 | /* Not found, create struct and channel */ |
476 | cadetp = GNUNET_new (struct CadetPeer); | 478 | cadetp = GNUNET_new (struct CadetPeer); |
477 | cadetp->peer_id = pid; | 479 | cadetp->peer_id = pid; |
@@ -490,23 +492,26 @@ get_cadet_peer (struct GNUNET_PeerIdentity pid) | |||
490 | * MQ envelope. | 492 | * MQ envelope. |
491 | * | 493 | * |
492 | * @param ai Anomaly info struct to use | 494 | * @param ai Anomaly info struct to use |
495 | * @param type Message type | ||
493 | * @return Envelope with message | 496 | * @return Envelope with message |
494 | */ | 497 | */ |
495 | static struct GNUNET_MQ_Envelope * | 498 | static struct GNUNET_MQ_Envelope * |
496 | create_anomaly_report_message (struct AnomalyInfo *ai) | 499 | create_anomaly_report_message (struct AnomalyInfo *ai, int type) |
497 | { | 500 | { |
498 | struct GNUNET_SENSOR_AnomalyReportMessage *arm; | 501 | struct GNUNET_SENSOR_AnomalyReportMessage *arm; |
499 | struct GNUNET_MQ_Envelope *ev; | 502 | struct GNUNET_MQ_Envelope *ev; |
500 | 503 | ||
501 | ev = GNUNET_MQ_msg (arm, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT); | 504 | ev = GNUNET_MQ_msg (arm, type); |
502 | GNUNET_CRYPTO_hash (ai->sensor->name, strlen (ai->sensor->name) + 1, | 505 | GNUNET_CRYPTO_hash (ai->sensor->name, strlen (ai->sensor->name) + 1, |
503 | &arm->sensorname_hash); | 506 | &arm->sensorname_hash); |
504 | arm->sensorversion_major = htons (ai->sensor->version_major); | 507 | arm->sensorversion_major = htons (ai->sensor->version_major); |
505 | arm->sensorversion_minor = htons (ai->sensor->version_minor); | 508 | arm->sensorversion_minor = htons (ai->sensor->version_minor); |
506 | arm->anomalous = htons (ai->anomalous); | 509 | arm->anomalous = htons (ai->anomalous); |
507 | arm->anomalous_neighbors = | 510 | arm->anomalous_neighbors = |
508 | ((float) GNUNET_CONTAINER_multipeermap_size (ai->anomalous_neighbors)) / | 511 | (0 == |
509 | neighborhood; | 512 | neighborhood) ? 0 : ((float) |
513 | GNUNET_CONTAINER_multipeermap_size | ||
514 | (ai->anomalous_neighbors)) / neighborhood; | ||
510 | return ev; | 515 | return ev; |
511 | } | 516 | } |
512 | 517 | ||
@@ -542,13 +547,20 @@ create_value_message (struct ValueInfo *vi) | |||
542 | * | 547 | * |
543 | * @param mq Message queue to put the message in | 548 | * @param mq Message queue to put the message in |
544 | * @param ai Anomaly info to report | 549 | * @param ai Anomaly info to report |
550 | * @param p2p Is the report sent to a neighboring peer | ||
545 | */ | 551 | */ |
546 | static void | 552 | static void |
547 | send_anomaly_report (struct GNUNET_MQ_Handle *mq, struct AnomalyInfo *ai) | 553 | send_anomaly_report (struct GNUNET_MQ_Handle *mq, struct AnomalyInfo *ai, |
554 | int p2p) | ||
548 | { | 555 | { |
549 | struct GNUNET_MQ_Envelope *ev; | 556 | struct GNUNET_MQ_Envelope *ev; |
557 | int type; | ||
550 | 558 | ||
551 | ev = create_anomaly_report_message (ai); | 559 | type = |
560 | (GNUNET_YES == | ||
561 | p2p) ? GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT_P2P : | ||
562 | GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT; | ||
563 | ev = create_anomaly_report_message (ai, type); | ||
552 | GNUNET_MQ_send (mq, ev); | 564 | GNUNET_MQ_send (mq, ev); |
553 | } | 565 | } |
554 | 566 | ||
@@ -579,8 +591,9 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other, | |||
579 | 591 | ||
580 | arm = (struct GNUNET_SENSOR_AnomalyReportMessage *) message; | 592 | arm = (struct GNUNET_SENSOR_AnomalyReportMessage *) message; |
581 | sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &arm->sensorname_hash); | 593 | sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &arm->sensorname_hash); |
582 | if (NULL == sensor || sensor->version_major != arm->sensorversion_major || | 594 | if (NULL == sensor || |
583 | sensor->version_minor != arm->sensorversion_minor) | 595 | sensor->version_major != ntohs (arm->sensorversion_major) || |
596 | sensor->version_minor != ntohs (arm->sensorversion_minor)) | ||
584 | { | 597 | { |
585 | LOG (GNUNET_ERROR_TYPE_WARNING, | 598 | LOG (GNUNET_ERROR_TYPE_WARNING, |
586 | "I don't have the sensor reported by the peer `%s'.\n", | 599 | "I don't have the sensor reported by the peer `%s'.\n", |
@@ -610,8 +623,11 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other, | |||
610 | if (NULL != ai->sensor->collection_point && | 623 | if (NULL != ai->sensor->collection_point && |
611 | GNUNET_YES == ai->sensor->report_anomalies) | 624 | GNUNET_YES == ai->sensor->report_anomalies) |
612 | { | 625 | { |
626 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
627 | "Sending anomaly report to collection point `%s'.\n", | ||
628 | GNUNET_i2s (ai->sensor->collection_point)); | ||
613 | cadetp = get_cadet_peer (*ai->sensor->collection_point); | 629 | cadetp = get_cadet_peer (*ai->sensor->collection_point); |
614 | send_anomaly_report (cadetp->mq, ai); | 630 | send_anomaly_report (cadetp->mq, ai, GNUNET_NO); |
615 | } | 631 | } |
616 | return GNUNET_OK; | 632 | return GNUNET_OK; |
617 | } | 633 | } |
@@ -675,7 +691,7 @@ core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
675 | corep = corep_head; | 691 | corep = corep_head; |
676 | while (NULL != corep) | 692 | while (NULL != corep) |
677 | { | 693 | { |
678 | if (peer == corep->peer_id) | 694 | if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer, corep->peer_id)) |
679 | { | 695 | { |
680 | GNUNET_CONTAINER_DLL_remove (corep_head, corep_tail, corep); | 696 | GNUNET_CONTAINER_DLL_remove (corep_head, corep_tail, corep); |
681 | destroy_core_peer (corep); | 697 | destroy_core_peer (corep); |
@@ -683,9 +699,6 @@ core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
683 | } | 699 | } |
684 | corep = corep->next; | 700 | corep = corep->next; |
685 | } | 701 | } |
686 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
687 | _("Received disconnect notification from CORE" | ||
688 | " for a peer we didn't know about.\n")); | ||
689 | } | 702 | } |
690 | 703 | ||
691 | 704 | ||
@@ -712,8 +725,11 @@ core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
712 | ai = ai_head; | 725 | ai = ai_head; |
713 | while (NULL != ai) | 726 | while (NULL != ai) |
714 | { | 727 | { |
728 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
729 | "Updating newly connected neighbor `%s' with anomalous sensor.\n", | ||
730 | GNUNET_i2s (peer)); | ||
715 | if (GNUNET_YES == ai->anomalous) | 731 | if (GNUNET_YES == ai->anomalous) |
716 | send_anomaly_report (corep->mq, ai); | 732 | send_anomaly_report (corep->mq, ai, GNUNET_YES); |
717 | ai = ai->next; | 733 | ai = ai->next; |
718 | } | 734 | } |
719 | } | 735 | } |
@@ -773,6 +789,9 @@ cadet_channel_destroyed (void *cls, const struct GNUNET_CADET_Channel *channel, | |||
773 | 789 | ||
774 | if (GNUNET_YES == cadetp->destroying) | 790 | if (GNUNET_YES == cadetp->destroying) |
775 | return; | 791 | return; |
792 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
793 | "CADET channel was destroyed by remote peer `%s' or failed to start.\n", | ||
794 | GNUNET_i2s (&cadetp->peer_id)); | ||
776 | GNUNET_CONTAINER_DLL_remove (cadetp_head, cadetp_tail, cadetp); | 795 | GNUNET_CONTAINER_DLL_remove (cadetp_head, cadetp_tail, cadetp); |
777 | cadetp->channel = NULL; | 796 | cadetp->channel = NULL; |
778 | destroy_cadet_peer (cadetp); | 797 | destroy_cadet_peer (cadetp); |
@@ -801,6 +820,7 @@ SENSOR_reporting_anomaly_update (struct GNUNET_SENSOR_SensorInfo *sensor, | |||
801 | 820 | ||
802 | if (GNUNET_NO == module_running) | 821 | if (GNUNET_NO == module_running) |
803 | return; | 822 | return; |
823 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Received an external anomaly update.\n"); | ||
804 | ai = get_anomaly_info_by_sensor (sensor); | 824 | ai = get_anomaly_info_by_sensor (sensor); |
805 | GNUNET_assert (NULL != ai); | 825 | GNUNET_assert (NULL != ai); |
806 | ai->anomalous = anomalous; | 826 | ai->anomalous = anomalous; |
@@ -808,15 +828,21 @@ SENSOR_reporting_anomaly_update (struct GNUNET_SENSOR_SensorInfo *sensor, | |||
808 | corep = corep_head; | 828 | corep = corep_head; |
809 | while (NULL != corep) | 829 | while (NULL != corep) |
810 | { | 830 | { |
811 | send_anomaly_report (corep->mq, ai); | 831 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
832 | "Sending an anomaly report to neighbor `%s'.\n", | ||
833 | GNUNET_i2s (corep->peer_id)); | ||
834 | send_anomaly_report (corep->mq, ai, GNUNET_YES); | ||
812 | corep = corep->next; | 835 | corep = corep->next; |
813 | } | 836 | } |
814 | /* Report change to collection point if need */ | 837 | /* Report change to collection point if need */ |
815 | if (NULL != ai->sensor->collection_point && | 838 | if (NULL != ai->sensor->collection_point && |
816 | GNUNET_YES == ai->sensor->report_anomalies) | 839 | GNUNET_YES == ai->sensor->report_anomalies) |
817 | { | 840 | { |
841 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
842 | "Sending anomaly report to collection point `%s'.\n", | ||
843 | GNUNET_i2s (ai->sensor->collection_point)); | ||
818 | cadetp = get_cadet_peer (*ai->sensor->collection_point); | 844 | cadetp = get_cadet_peer (*ai->sensor->collection_point); |
819 | send_anomaly_report (cadetp->mq, ai); | 845 | send_anomaly_report (cadetp->mq, ai, GNUNET_NO); |
820 | } | 846 | } |
821 | } | 847 | } |
822 | 848 | ||
@@ -924,7 +950,7 @@ SENSOR_reporting_start (const struct GNUNET_CONFIGURATION_Handle *c, | |||
924 | struct GNUNET_CONTAINER_MultiHashMap *s) | 950 | struct GNUNET_CONTAINER_MultiHashMap *s) |
925 | { | 951 | { |
926 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { | 952 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { |
927 | {&handle_anomaly_report, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT, | 953 | {&handle_anomaly_report, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_REPORT_P2P, |
928 | sizeof (struct GNUNET_SENSOR_AnomalyReportMessage)}, | 954 | sizeof (struct GNUNET_SENSOR_AnomalyReportMessage)}, |
929 | {NULL, 0, 0} | 955 | {NULL, 0, 0} |
930 | }; | 956 | }; |
diff --git a/src/sensor/sensor_api.c b/src/sensor/sensor_api.c index 9a18427d0..628873f91 100644 --- a/src/sensor/sensor_api.c +++ b/src/sensor/sensor_api.c | |||
@@ -46,16 +46,26 @@ struct GNUNET_SENSOR_Handle | |||
46 | struct GNUNET_CLIENT_Connection *client; | 46 | struct GNUNET_CLIENT_Connection *client; |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * Head of iterator DLL. | 49 | * Head of iteration requests DLL. |
50 | */ | 50 | */ |
51 | struct GNUNET_SENSOR_IterateContext *ic_head; | 51 | struct GNUNET_SENSOR_IterateContext *ic_head; |
52 | 52 | ||
53 | /** | 53 | /** |
54 | * Tail of iterator DLL. | 54 | * Tail of iteration requests DLL. |
55 | */ | 55 | */ |
56 | struct GNUNET_SENSOR_IterateContext *ic_tail; | 56 | struct GNUNET_SENSOR_IterateContext *ic_tail; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Head of force anomaly requests | ||
60 | */ | ||
61 | struct GNUNET_SENSOR_ForceAnomalyContext *fa_head; | ||
62 | |||
63 | /** | ||
64 | * Tail of force anomaly requests | ||
65 | */ | ||
66 | struct GNUNET_SENSOR_ForceAnomalyContext *fa_tail; | ||
67 | |||
68 | /** | ||
59 | * Message queue used to send data to service | 69 | * Message queue used to send data to service |
60 | */ | 70 | */ |
61 | struct GNUNET_MQ_Handle *mq; | 71 | struct GNUNET_MQ_Handle *mq; |
@@ -115,6 +125,44 @@ struct GNUNET_SENSOR_IterateContext | |||
115 | 125 | ||
116 | }; | 126 | }; |
117 | 127 | ||
128 | /** | ||
129 | * Context of a force anomaly request | ||
130 | */ | ||
131 | struct GNUNET_SENSOR_ForceAnomalyContext | ||
132 | { | ||
133 | |||
134 | /** | ||
135 | * DLL | ||
136 | */ | ||
137 | struct GNUNET_SENSOR_ForceAnomalyContext *next; | ||
138 | |||
139 | /** | ||
140 | * DLL | ||
141 | */ | ||
142 | struct GNUNET_SENSOR_ForceAnomalyContext *prev; | ||
143 | |||
144 | /** | ||
145 | * Handle to the SENSOR service. | ||
146 | */ | ||
147 | struct GNUNET_SENSOR_Handle *h; | ||
148 | |||
149 | /** | ||
150 | * Envelope containing iterate request. | ||
151 | */ | ||
152 | struct GNUNET_MQ_Envelope *ev; | ||
153 | |||
154 | /** | ||
155 | * User continuation function | ||
156 | */ | ||
157 | GNUNET_SENSOR_Continuation cont; | ||
158 | |||
159 | /** | ||
160 | * Closure for cont | ||
161 | */ | ||
162 | void *cont_cls; | ||
163 | |||
164 | }; | ||
165 | |||
118 | 166 | ||
119 | /** | 167 | /** |
120 | * Notifier of an error encountered by MQ. | 168 | * Notifier of an error encountered by MQ. |
@@ -222,7 +270,8 @@ handle_sensor_info (void *cls, const struct GNUNET_MessageHeader *msg) | |||
222 | 270 | ||
223 | 271 | ||
224 | /** | 272 | /** |
225 | * Disconnect from the sensor service | 273 | * Disconnect from the sensor service. |
274 | * Please disconnect only when all requests sent are complete or canceled. | ||
226 | * | 275 | * |
227 | * @param h handle to disconnect | 276 | * @param h handle to disconnect |
228 | */ | 277 | */ |
@@ -230,16 +279,34 @@ void | |||
230 | GNUNET_SENSOR_disconnect (struct GNUNET_SENSOR_Handle *h) | 279 | GNUNET_SENSOR_disconnect (struct GNUNET_SENSOR_Handle *h) |
231 | { | 280 | { |
232 | struct GNUNET_SENSOR_IterateContext *ic; | 281 | struct GNUNET_SENSOR_IterateContext *ic; |
282 | GNUNET_SENSOR_SensorIterateCB ic_callback; | ||
283 | void *ic_callback_cls; | ||
284 | struct GNUNET_SENSOR_ForceAnomalyContext *fa; | ||
285 | GNUNET_SENSOR_Continuation fa_cont; | ||
286 | void *fa_cont_cls; | ||
233 | 287 | ||
234 | ic = h->ic_head; | 288 | ic = h->ic_head; |
235 | while (NULL != ic) | 289 | while (NULL != ic) |
236 | { | 290 | { |
237 | if (NULL != ic->callback) | 291 | ic_callback = ic->callback; |
238 | ic->callback (ic->callback_cls, NULL, | 292 | ic_callback_cls = ic->callback_cls; |
239 | _("Iterate request canceled due to disconnection.\n")); | ||
240 | GNUNET_SENSOR_iterate_cancel (ic); | 293 | GNUNET_SENSOR_iterate_cancel (ic); |
294 | if (NULL != ic_callback) | ||
295 | ic_callback (ic_callback_cls, NULL, | ||
296 | _("Iterate request canceled due to disconnection.\n")); | ||
241 | ic = h->ic_head; | 297 | ic = h->ic_head; |
242 | } | 298 | } |
299 | fa = h->fa_head; | ||
300 | while (NULL != fa) | ||
301 | { | ||
302 | fa_cont = fa->cont; | ||
303 | fa_cont_cls = fa->cont_cls; | ||
304 | GNUNET_SENSOR_force_anomaly_cancel (fa); | ||
305 | if (NULL != fa_cont) | ||
306 | fa_cont (fa_cont_cls, | ||
307 | _("Force anomaly request canceled due to disconnection.\n")); | ||
308 | fa = h->fa_head; | ||
309 | } | ||
243 | if (NULL != h->mq) | 310 | if (NULL != h->mq) |
244 | { | 311 | { |
245 | GNUNET_MQ_destroy (h->mq); | 312 | GNUNET_MQ_destroy (h->mq); |
@@ -414,6 +481,49 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
414 | 481 | ||
415 | 482 | ||
416 | /** | 483 | /** |
484 | * Callback from MQ when the request has already been sent to the service. | ||
485 | * Now it can not be canelled. | ||
486 | * | ||
487 | * @param cls closure | ||
488 | */ | ||
489 | static void | ||
490 | force_anomaly_sent (void *cls) | ||
491 | { | ||
492 | struct GNUNET_SENSOR_ForceAnomalyContext *fa = cls; | ||
493 | GNUNET_SENSOR_Continuation cont; | ||
494 | void *cont_cls; | ||
495 | |||
496 | fa->ev = NULL; | ||
497 | cont = fa->cont; | ||
498 | cont_cls = fa->cont_cls; | ||
499 | GNUNET_SENSOR_force_anomaly_cancel (fa); | ||
500 | if (NULL != cont) | ||
501 | cont (cont_cls, NULL); | ||
502 | } | ||
503 | |||
504 | |||
505 | /** | ||
506 | * Cancel a force anomaly request. | ||
507 | * | ||
508 | * @param fa Force anomaly context returned by GNUNET_SENSOR_force_anomaly() | ||
509 | */ | ||
510 | void | ||
511 | GNUNET_SENSOR_force_anomaly_cancel (struct GNUNET_SENSOR_ForceAnomalyContext | ||
512 | *fa) | ||
513 | { | ||
514 | struct GNUNET_SENSOR_Handle *h = fa->h; | ||
515 | |||
516 | if (NULL != fa->ev) | ||
517 | { | ||
518 | GNUNET_MQ_send_cancel (fa->ev); | ||
519 | fa->ev = NULL; | ||
520 | } | ||
521 | GNUNET_CONTAINER_DLL_remove (h->fa_head, h->fa_tail, fa); | ||
522 | GNUNET_free (fa); | ||
523 | } | ||
524 | |||
525 | |||
526 | /** | ||
417 | * Force an anomaly status change on a given sensor. If the sensor reporting | 527 | * Force an anomaly status change on a given sensor. If the sensor reporting |
418 | * module is running, this will trigger the usual reporting logic, therefore, | 528 | * module is running, this will trigger the usual reporting logic, therefore, |
419 | * please only use this in a test environment. | 529 | * please only use this in a test environment. |
@@ -424,19 +534,32 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
424 | * @param h Service handle | 534 | * @param h Service handle |
425 | * @param sensor_name Sensor name to set the anomaly status | 535 | * @param sensor_name Sensor name to set the anomaly status |
426 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO | 536 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO |
537 | * @param cont Continuation function to be called after the request is sent | ||
538 | * @param cont_cls Closure for cont | ||
427 | */ | 539 | */ |
428 | void | 540 | struct GNUNET_SENSOR_ForceAnomalyContext * |
429 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, | 541 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, |
430 | int anomalous) | 542 | int anomalous, GNUNET_SENSOR_Continuation cont, |
543 | void *cont_cls) | ||
431 | { | 544 | { |
432 | struct ForceAnomalyMessage *msg; | 545 | struct ForceAnomalyMessage *msg; |
433 | struct GNUNET_MQ_Envelope *ev; | 546 | struct GNUNET_MQ_Envelope *ev; |
547 | struct GNUNET_SENSOR_ForceAnomalyContext *fa; | ||
434 | 548 | ||
435 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE); | 549 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE); |
436 | GNUNET_CRYPTO_hash (sensor_name, strlen (sensor_name) + 1, | 550 | GNUNET_CRYPTO_hash (sensor_name, strlen (sensor_name) + 1, |
437 | &msg->sensor_name_hash); | 551 | &msg->sensor_name_hash); |
438 | msg->anomalous = htons (anomalous); | 552 | msg->anomalous = htons (anomalous); |
439 | GNUNET_MQ_send (h->mq, ev); | 553 | GNUNET_MQ_send (h->mq, ev); |
554 | fa = GNUNET_new (struct GNUNET_SENSOR_ForceAnomalyContext); | ||
555 | |||
556 | fa->h = h; | ||
557 | fa->cont = cont; | ||
558 | fa->cont_cls = cont_cls; | ||
559 | fa->ev = ev; | ||
560 | GNUNET_CONTAINER_DLL_insert_tail (h->fa_head, h->fa_tail, fa); | ||
561 | GNUNET_MQ_notify_sent (ev, &force_anomaly_sent, fa); | ||
562 | return fa; | ||
440 | } | 563 | } |
441 | 564 | ||
442 | 565 | ||
diff --git a/src/sensor/test_gnunet-service-sensor_reporting.c b/src/sensor/test_gnunet-service-sensor_reporting.c new file mode 100644 index 000000000..969ab16ec --- /dev/null +++ b/src/sensor/test_gnunet-service-sensor_reporting.c | |||
@@ -0,0 +1,469 @@ | |||
1 | /* | ||
2 | * This file is part of GNUnet. | ||
3 | * (C) | ||
4 | * | ||
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. | ||
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 | * General Public License for more details. | ||
14 | * | ||
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. | ||
19 | */ | ||
20 | /** | ||
21 | * @file sensor/test_gnunet-service-sensor_reporting.c | ||
22 | * @brief testcase for gnunet-service-sensor_reporting.c | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_testbed_service.h" | ||
27 | #include "gnunet_sensor_util_lib.h" | ||
28 | #include "sensor.h" | ||
29 | #include "gnunet_peerstore_service.h" | ||
30 | #include "gnunet_sensor_service.h" | ||
31 | |||
32 | /** | ||
33 | * Number of peers to start for the test | ||
34 | */ | ||
35 | #define NUM_PEERS 2 | ||
36 | |||
37 | /** | ||
38 | * Test timeout | ||
39 | */ | ||
40 | #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1) | ||
41 | |||
42 | /** | ||
43 | * Information about a test peer | ||
44 | */ | ||
45 | struct TestPeer | ||
46 | { | ||
47 | |||
48 | /** | ||
49 | * DLL | ||
50 | */ | ||
51 | struct TestPeer *prev; | ||
52 | |||
53 | /** | ||
54 | * DLL | ||
55 | */ | ||
56 | struct TestPeer *next; | ||
57 | |||
58 | /** | ||
59 | * TESTBED information about the peer | ||
60 | */ | ||
61 | struct GNUNET_TESTBED_Peer *testbed_peer; | ||
62 | |||
63 | /** | ||
64 | * Peer indentity | ||
65 | */ | ||
66 | struct GNUNET_PeerIdentity peer_id; | ||
67 | |||
68 | /** | ||
69 | * Peerstore watch context for this peer's anomaly reports | ||
70 | */ | ||
71 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
72 | |||
73 | /** | ||
74 | * TESTBED operation connecting us to sensor service | ||
75 | */ | ||
76 | struct GNUNET_TESTBED_Operation *sensor_op; | ||
77 | |||
78 | }; | ||
79 | |||
80 | /** | ||
81 | * Test name | ||
82 | */ | ||
83 | const static char *testname = "test_gnunet-service-sensor_reporting"; | ||
84 | |||
85 | /** | ||
86 | * Name of GNUNET config file used in this test | ||
87 | */ | ||
88 | const static char *cfg_filename = "test_gnunet-service-sensor_reporting.conf"; | ||
89 | |||
90 | /** | ||
91 | * Test sensor name | ||
92 | */ | ||
93 | const static char *sensor_name = "test-sensor-statistics"; | ||
94 | |||
95 | /** | ||
96 | * Path to read test sensor from | ||
97 | */ | ||
98 | const static char *sensor_path_src = "test_sensors/test-sensor-statistics"; | ||
99 | |||
100 | /** | ||
101 | * Path to write new test sensor to | ||
102 | */ | ||
103 | const static char *sensor_path_dest = | ||
104 | "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics"; | ||
105 | |||
106 | /** | ||
107 | * Head of DLL of peers | ||
108 | */ | ||
109 | static struct TestPeer *peer_head; | ||
110 | |||
111 | /** | ||
112 | * Tail of DLL of peers | ||
113 | */ | ||
114 | static struct TestPeer *peer_tail; | ||
115 | |||
116 | /** | ||
117 | * Number of peers started and got information for | ||
118 | */ | ||
119 | static int started_peers = 0; | ||
120 | |||
121 | /** | ||
122 | * TESTBED operation connecting us to peerstore service | ||
123 | */ | ||
124 | struct GNUNET_TESTBED_Operation *peerstore_op; | ||
125 | |||
126 | /** | ||
127 | * Handle to the peerstore service | ||
128 | */ | ||
129 | struct GNUNET_PEERSTORE_Handle *peerstore; | ||
130 | |||
131 | /** | ||
132 | * Task used to shutdown / expire the test | ||
133 | */ | ||
134 | static GNUNET_SCHEDULER_TaskIdentifier shutdown_task; | ||
135 | |||
136 | /** | ||
137 | * Status of the test to be returned by main() | ||
138 | */ | ||
139 | static int ok = 1; | ||
140 | |||
141 | |||
142 | /** | ||
143 | * Shutdown task | ||
144 | * | ||
145 | * @param cls Closure (unused) | ||
146 | * @param tc Task context (unused) | ||
147 | */ | ||
148 | static void | ||
149 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
150 | { | ||
151 | //TODO: destroy list of peers | ||
152 | if (NULL != peerstore_op) | ||
153 | { | ||
154 | GNUNET_TESTBED_operation_done (peerstore_op); | ||
155 | peerstore_op = NULL; | ||
156 | } | ||
157 | GNUNET_SCHEDULER_shutdown (); | ||
158 | } | ||
159 | |||
160 | |||
161 | /** | ||
162 | * Write new temp sensor directory with a sensor updated with collection point | ||
163 | * peer id | ||
164 | */ | ||
165 | void | ||
166 | write_new_sensor_dir (struct TestPeer *cp_peer) | ||
167 | { | ||
168 | struct GNUNET_CONFIGURATION_Handle *sensorcfg; | ||
169 | |||
170 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_test (sensor_path_src)); | ||
171 | sensorcfg = GNUNET_CONFIGURATION_create (); | ||
172 | GNUNET_assert (GNUNET_SYSERR != | ||
173 | GNUNET_CONFIGURATION_parse (sensorcfg, sensor_path_src)); | ||
174 | GNUNET_CONFIGURATION_set_value_string (sensorcfg, sensor_name, | ||
175 | "COLLECTION_POINT", | ||
176 | GNUNET_i2s_full (&cp_peer->peer_id)); | ||
177 | GNUNET_assert (GNUNET_OK == | ||
178 | GNUNET_DISK_directory_create_for_file (sensor_path_dest)); | ||
179 | GNUNET_CONFIGURATION_write (sensorcfg, sensor_path_dest); | ||
180 | GNUNET_CONFIGURATION_destroy (sensorcfg); | ||
181 | } | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Function called by PEERSTORE for each matching record. | ||
186 | * | ||
187 | * @param cls closure | ||
188 | * @param record peerstore record information | ||
189 | * @param emsg error message, or NULL if no errors | ||
190 | * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop | ||
191 | */ | ||
192 | static int | ||
193 | peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, | ||
194 | char *emsg) | ||
195 | { | ||
196 | struct TestPeer *peer = cls; | ||
197 | struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly; | ||
198 | |||
199 | GNUNET_assert (NULL != record); | ||
200 | GNUNET_assert (record->value_size == | ||
201 | sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry)); | ||
202 | anomaly = record->value; | ||
203 | GNUNET_assert (0 == | ||
204 | GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id, | ||
205 | record->peer)); | ||
206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
207 | "Peerstore watch got an anomaly report from peer `%s':\n" | ||
208 | "Anomalous: %d\n" "Anomalous neigbors: %f.\n", | ||
209 | GNUNET_i2s (&peer->peer_id), anomaly->anomalous, | ||
210 | anomaly->anomalous_neighbors); | ||
211 | //TODO | ||
212 | return GNUNET_YES; | ||
213 | } | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Callback to be called when sensor service connect operation is completed | ||
218 | * | ||
219 | * @param cls the callback closure from functions generating an operation | ||
220 | * @param op the operation that has been finished | ||
221 | * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter() | ||
222 | * @param emsg error message in case the operation has failed; will be NULL if | ||
223 | * operation has executed successfully. | ||
224 | */ | ||
225 | static void | ||
226 | sensor_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, | ||
227 | void *ca_result, const char *emsg) | ||
228 | { | ||
229 | struct TestPeer *peer = cls; | ||
230 | struct GNUNET_SENSOR_Handle *sensor = ca_result; | ||
231 | |||
232 | GNUNET_SENSOR_force_anomaly (sensor, (char *) sensor_name, GNUNET_YES, NULL, | ||
233 | NULL); | ||
234 | } | ||
235 | |||
236 | |||
237 | /** | ||
238 | * Adapter function called to establish a connection to sensor service. | ||
239 | * | ||
240 | * @param cls closure | ||
241 | * @param cfg configuration of the peer to connect to; will be available until | ||
242 | * GNUNET_TESTBED_operation_done() is called on the operation returned | ||
243 | * from GNUNET_TESTBED_service_connect() | ||
244 | * @return service handle to return in 'op_result', NULL on error | ||
245 | */ | ||
246 | static void * | ||
247 | sensor_connect_adapter (void *cls, | ||
248 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
249 | { | ||
250 | struct GNUNET_SENSOR_Handle *sensor; | ||
251 | |||
252 | sensor = GNUNET_SENSOR_connect (cfg); | ||
253 | return sensor; | ||
254 | } | ||
255 | |||
256 | |||
257 | /** | ||
258 | * Adapter function called to destroy a connection to sensor service. | ||
259 | * | ||
260 | * @param cls closure | ||
261 | * @param op_result service handle returned from the connect adapter | ||
262 | */ | ||
263 | static void | ||
264 | sensor_disconnect_adapter (void *cls, void *op_result) | ||
265 | { | ||
266 | struct GNUNET_SENSOR_Handle *sensor = op_result; | ||
267 | |||
268 | GNUNET_SENSOR_disconnect (sensor); | ||
269 | } | ||
270 | |||
271 | |||
272 | /** | ||
273 | * Callback to be called when sensor service is started | ||
274 | * | ||
275 | * @param cls the callback closure from functions generating an operation | ||
276 | * @param op the operation that has been finished | ||
277 | * @param emsg error message in case the operation has failed; will be NULL if | ||
278 | * operation has executed successfully. | ||
279 | */ | ||
280 | static void | ||
281 | sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op, | ||
282 | const char *emsg) | ||
283 | { | ||
284 | struct TestPeer *peer = cls; | ||
285 | |||
286 | GNUNET_assert (NULL == emsg); | ||
287 | peer->sensor_op = | ||
288 | GNUNET_TESTBED_service_connect (NULL, peer->testbed_peer, "sensor", | ||
289 | &sensor_connect_cb, peer, | ||
290 | &sensor_connect_adapter, | ||
291 | &sensor_disconnect_adapter, NULL); | ||
292 | GNUNET_TESTBED_operation_done (op); | ||
293 | } | ||
294 | |||
295 | |||
296 | /** | ||
297 | * Callback to be called when peerstore service connect operation is completed | ||
298 | * | ||
299 | * @param cls the callback closure from functions generating an operation | ||
300 | * @param op the operation that has been finished | ||
301 | * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter() | ||
302 | * @param emsg error message in case the operation has failed; will be NULL if | ||
303 | * operation has executed successfully. | ||
304 | */ | ||
305 | static void | ||
306 | peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, | ||
307 | void *ca_result, const char *emsg) | ||
308 | { | ||
309 | struct TestPeer *peer; | ||
310 | |||
311 | peer = peer_head; | ||
312 | while (NULL != peer) | ||
313 | { | ||
314 | GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies", | ||
315 | &peer->peer_id, sensor_name, &peerstore_watch_cb, | ||
316 | peer); | ||
317 | /* Start sensor service */ | ||
318 | GNUNET_TESTBED_peer_manage_service (NULL, peer->testbed_peer, "sensor", | ||
319 | &sensor_service_started, peer, 1); | ||
320 | peer = peer->next; | ||
321 | } | ||
322 | } | ||
323 | |||
324 | |||
325 | /** | ||
326 | * Adapter function called to establish a connection to peerstore service. | ||
327 | * | ||
328 | * @param cls closure | ||
329 | * @param cfg configuration of the peer to connect to; will be available until | ||
330 | * GNUNET_TESTBED_operation_done() is called on the operation returned | ||
331 | * from GNUNET_TESTBED_service_connect() | ||
332 | * @return service handle to return in 'op_result', NULL on error | ||
333 | */ | ||
334 | static void * | ||
335 | peerstore_connect_adapter (void *cls, | ||
336 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
337 | { | ||
338 | peerstore = GNUNET_PEERSTORE_connect (cfg); | ||
339 | GNUNET_assert (NULL != peerstore); | ||
340 | return peerstore; | ||
341 | } | ||
342 | |||
343 | |||
344 | /** | ||
345 | * Adapter function called to destroy a connection to peerstore service. | ||
346 | * | ||
347 | * @param cls closure | ||
348 | * @param op_result service handle returned from the connect adapter | ||
349 | */ | ||
350 | static void | ||
351 | peerstore_disconnect_adapter (void *cls, void *op_result) | ||
352 | { | ||
353 | GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO); | ||
354 | peerstore = NULL; | ||
355 | peerstore_op = NULL; | ||
356 | } | ||
357 | |||
358 | |||
359 | /** | ||
360 | * Callback to be called when dashboard service is started | ||
361 | * | ||
362 | * @param cls the callback closure from functions generating an operation | ||
363 | * @param op the operation that has been finished | ||
364 | * @param emsg error message in case the operation has failed; will be NULL if | ||
365 | * operation has executed successfully. | ||
366 | */ | ||
367 | static void | ||
368 | dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op, | ||
369 | const char *emsg) | ||
370 | { | ||
371 | GNUNET_assert (NULL == emsg); | ||
372 | GNUNET_TESTBED_operation_done (op); | ||
373 | /* Connect to peerstore service on first peer */ | ||
374 | peerstore_op = | ||
375 | GNUNET_TESTBED_service_connect (NULL, peer_head->testbed_peer, | ||
376 | "peerstore", &peerstore_connect_cb, NULL, | ||
377 | &peerstore_connect_adapter, | ||
378 | &peerstore_disconnect_adapter, NULL); | ||
379 | } | ||
380 | |||
381 | |||
382 | /** | ||
383 | * Callback to be called when the requested peer information is available | ||
384 | * | ||
385 | * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information() | ||
386 | * @param op the operation this callback corresponds to | ||
387 | * @param pinfo the result; will be NULL if the operation has failed | ||
388 | * @param emsg error message if the operation has failed; will be NULL if the | ||
389 | * operation is successfull | ||
390 | */ | ||
391 | static void | ||
392 | peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, | ||
393 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
394 | const char *emsg) | ||
395 | { | ||
396 | struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls; | ||
397 | struct TestPeer *peer; | ||
398 | |||
399 | peer = GNUNET_new (struct TestPeer); | ||
400 | |||
401 | peer->testbed_peer = testbed_peer; | ||
402 | GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); | ||
403 | if (NULL == peer_head) /* First peer (collection point) */ | ||
404 | { | ||
405 | /* Rewrite sensor with collection point peer id */ | ||
406 | write_new_sensor_dir (peer); | ||
407 | } | ||
408 | GNUNET_CONTAINER_DLL_insert_tail (peer_head, peer_tail, peer); | ||
409 | started_peers++; | ||
410 | if (NUM_PEERS == started_peers) | ||
411 | { | ||
412 | /* Start dashboard service on first peer */ | ||
413 | GNUNET_TESTBED_peer_manage_service (NULL, peer_head->testbed_peer, | ||
414 | "sensordashboard", &dashboard_started, | ||
415 | NULL, 1); | ||
416 | } | ||
417 | GNUNET_TESTBED_operation_done (op); | ||
418 | } | ||
419 | |||
420 | |||
421 | /** | ||
422 | * Signature of a main function for a testcase. | ||
423 | * | ||
424 | * @param cls closure | ||
425 | * @param h the run handle | ||
426 | * @param num_peers number of peers in 'peers' | ||
427 | * @param peers handle to peers run in the testbed. NULL upon timeout (see | ||
428 | * GNUNET_TESTBED_test_run()). | ||
429 | * @param links_succeeded the number of overlay link connection attempts that | ||
430 | * succeeded | ||
431 | * @param links_failed the number of overlay link connection attempts that | ||
432 | * failed | ||
433 | * @see GNUNET_TESTBED_test_run() | ||
434 | */ | ||
435 | static void | ||
436 | test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, | ||
437 | unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers, | ||
438 | unsigned int links_succeeded, unsigned int links_failed) | ||
439 | { | ||
440 | int i; | ||
441 | |||
442 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
443 | "%d peers started. %d links succeeded. %d links failed.\n", | ||
444 | num_peers, links_succeeded, links_failed); | ||
445 | GNUNET_assert (NUM_PEERS == num_peers); | ||
446 | GNUNET_assert (0 == links_failed); | ||
447 | /* Schedule test timeout */ | ||
448 | shutdown_task = | ||
449 | GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL); | ||
450 | /* Collect peer information */ | ||
451 | for (i = 0; i < num_peers; i++) | ||
452 | { | ||
453 | GNUNET_TESTBED_peer_get_information (peers[i], | ||
454 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
455 | &peer_info_cb, peers[i]); | ||
456 | } | ||
457 | } | ||
458 | |||
459 | |||
460 | int | ||
461 | main (int argc, char *argv[]) | ||
462 | { | ||
463 | GNUNET_log_setup (testname, "INFO", NULL); | ||
464 | if (GNUNET_OK == | ||
465 | GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL, | ||
466 | &test_master, NULL)) | ||
467 | return ok; | ||
468 | return 1; | ||
469 | } | ||
diff --git a/src/sensor/test_gnunet-service-sensor_reporting.conf b/src/sensor/test_gnunet-service-sensor_reporting.conf new file mode 100644 index 000000000..4dc073856 --- /dev/null +++ b/src/sensor/test_gnunet-service-sensor_reporting.conf | |||
@@ -0,0 +1,13 @@ | |||
1 | [testbed] | ||
2 | OVERLAY_TOPOLOGY = CLIQUE | ||
3 | |||
4 | [arm] | ||
5 | DEFAULTSERVICES = core | ||
6 | |||
7 | [sensor] | ||
8 | SENSOR_DIR = /tmp/test-gnunet-service-sensor-reporting/ | ||
9 | |||
10 | START_MONITORING = NO | ||
11 | START_REPORTING = YES | ||
12 | START_ANALYSIS = NO | ||
13 | START_UPDATE = NO \ No newline at end of file | ||
diff --git a/src/sensordashboard/gnunet-service-sensordashboard.c b/src/sensordashboard/gnunet-service-sensordashboard.c index 7dc1b33d6..1af3be332 100644 --- a/src/sensordashboard/gnunet-service-sensordashboard.c +++ b/src/sensordashboard/gnunet-service-sensordashboard.c | |||
@@ -421,9 +421,12 @@ handle_anomaly_report (void *cls, struct GNUNET_CADET_Channel *channel, | |||
421 | struct ClientPeerContext *cp = *channel_ctx; | 421 | struct ClientPeerContext *cp = *channel_ctx; |
422 | struct GNUNET_SENSOR_AnomalyReportMessage *anomaly_msg; | 422 | struct GNUNET_SENSOR_AnomalyReportMessage *anomaly_msg; |
423 | struct GNUNET_SENSOR_SensorInfo *sensor; | 423 | struct GNUNET_SENSOR_SensorInfo *sensor; |
424 | uint16_t anomalous; | 424 | struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly_entry; |
425 | struct GNUNET_TIME_Absolute expiry; | 425 | struct GNUNET_TIME_Absolute expiry; |
426 | 426 | ||
427 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
428 | "Received an anomaly report message from peer `%s'.\n", | ||
429 | GNUNET_i2s (&cp->peerid)); | ||
427 | anomaly_msg = (struct GNUNET_SENSOR_AnomalyReportMessage *) message; | 430 | anomaly_msg = (struct GNUNET_SENSOR_AnomalyReportMessage *) message; |
428 | sensor = | 431 | sensor = |
429 | GNUNET_CONTAINER_multihashmap_get (sensors, | 432 | GNUNET_CONTAINER_multihashmap_get (sensors, |
@@ -433,13 +436,20 @@ handle_anomaly_report (void *cls, struct GNUNET_CADET_Channel *channel, | |||
433 | GNUNET_break_op (0); | 436 | GNUNET_break_op (0); |
434 | return GNUNET_SYSERR; | 437 | return GNUNET_SYSERR; |
435 | } | 438 | } |
436 | anomalous = ntohs (anomaly_msg->anomalous); | 439 | anomaly_entry = GNUNET_new (struct GNUNET_SENSOR_DashboardAnomalyEntry); |
440 | anomaly_entry->anomalous = ntohs (anomaly_msg->anomalous); | ||
441 | anomaly_entry->anomalous_neighbors = anomaly_msg->anomalous_neighbors; | ||
437 | expiry = | 442 | expiry = |
438 | (GNUNET_YES == | 443 | (GNUNET_YES == |
439 | anomalous) ? GNUNET_TIME_UNIT_FOREVER_ABS : GNUNET_TIME_absolute_get (); | 444 | anomaly_entry->anomalous) ? GNUNET_TIME_UNIT_FOREVER_ABS : |
445 | GNUNET_TIME_absolute_get (); | ||
440 | GNUNET_PEERSTORE_store (peerstore, anomalies_subsystem, &cp->peerid, | 446 | GNUNET_PEERSTORE_store (peerstore, anomalies_subsystem, &cp->peerid, |
441 | sensor->name, &anomalous, sizeof (anomalous), expiry, | 447 | sensor->name, anomaly_entry, |
442 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); | 448 | sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry), |
449 | expiry, GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, | ||
450 | NULL); | ||
451 | GNUNET_free (anomaly_entry); | ||
452 | GNUNET_CADET_receive_done (channel); | ||
443 | return GNUNET_OK; | 453 | return GNUNET_OK; |
444 | } | 454 | } |
445 | 455 | ||