-only trigger check config if we actually need it
[oweals/gnunet.git] / src / ats / gnunet-service-ats_feedback.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file ats/gnunet-service-ats_feedback.c
22  * @brief ats service, handling of feedback
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-ats_plugins.h"
28 #include "gnunet-service-ats_feedback.h"
29 #include "ats.h"
30
31
32 /**
33  * Change the preference for a peer
34  *
35  * @param application the client sending this request
36  * @param peer the peer id
37  * @param scope the time interval for this feedback: [now - scope .. now]
38  * @param kind the preference kind to change
39  * @param score_abs the new preference score
40  */
41 static void
42 preference_feedback (struct GNUNET_SERVER_Client *application,
43                      const struct GNUNET_PeerIdentity *peer,
44                      const struct GNUNET_TIME_Relative scope,
45                      enum GNUNET_ATS_PreferenceKind kind,
46                      float score_abs)
47 {
48   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
49               "Received PREFERENCE FEEDBACK for peer `%s'\n",
50               GNUNET_i2s (peer));
51   GAS_plugin_notify_feedback (application,
52                                   peer,
53                                   scope,
54                                   kind,
55                                   score_abs);
56 }
57
58
59 /**
60  * Handle 'preference feedback' messages from clients.
61  *
62  * @param cls unused, NULL
63  * @param client client that sent the request
64  * @param message the request message
65  */
66 void
67 GAS_handle_feedback (void *cls,
68                                 struct GNUNET_SERVER_Client *client,
69                                 const struct GNUNET_MessageHeader *message)
70 {
71   const struct FeedbackPreferenceMessage *msg;
72   const struct PreferenceInformation *pi;
73   uint16_t msize;
74   uint32_t nump;
75   uint32_t i;
76
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78               "Received PREFERENCE_FEEDBACK message\n");
79   msize = ntohs (message->size);
80   if (msize < sizeof (struct FeedbackPreferenceMessage))
81   {
82     GNUNET_break (0);
83     GNUNET_SERVER_receive_done (client,
84                                 GNUNET_SYSERR);
85     return;
86   }
87   msg = (const struct FeedbackPreferenceMessage *) message;
88   nump = ntohl (msg->num_feedback);
89   if (msize !=
90       sizeof (struct FeedbackPreferenceMessage) +
91       nump * sizeof (struct PreferenceInformation))
92   {
93     GNUNET_break (0);
94     GNUNET_SERVER_receive_done (client,
95                                 GNUNET_SYSERR);
96     return;
97   }
98   if (GNUNET_NO ==
99       GNUNET_CONTAINER_multipeermap_contains (GSA_addresses,
100                                               &msg->peer))
101   {
102     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
103                "Received PREFERENCE FEEDBACK for unknown peer `%s'\n",
104                GNUNET_i2s (&msg->peer));
105     return;
106   }
107
108   GNUNET_STATISTICS_update (GSA_stats,
109                             "# preference feedbacks requests processed",
110                             1,
111                             GNUNET_NO);
112   pi = (const struct PreferenceInformation *) &msg[1];
113   for (i = 0; i < nump; i++)
114     preference_feedback (client,
115                          &msg->peer,
116                          GNUNET_TIME_relative_ntoh(msg->scope),
117                          (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
118                          pi[i].preference_value);
119   GNUNET_SERVER_receive_done (client,
120                               GNUNET_OK);
121 }
122
123 /* end of gnunet-service-ats_feedback.c */