aboutsummaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats.c
blob: 045a5bb67307748a0134526874e84a42a8af903f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
     This file is part of GNUnet.
     Copyright (C) 2011 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file ats/gnunet-service-ats.c
 * @brief ats service
 * @author Matthias Wachs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet-service-ats.h"
#include "gnunet-service-ats_addresses.h"
#include "gnunet-service-ats_connectivity.h"
#include "gnunet-service-ats_feedback.h"
#include "gnunet-service-ats_normalization.h"
#include "gnunet-service-ats_performance.h"
#include "gnunet-service-ats_preferences.h"
#include "gnunet-service-ats_scheduling.h"
#include "gnunet-service-ats_reservations.h"
#include "gnunet-service-ats_plugins.h"
#include "ats.h"

/**
 * Handle for statistics.
 */
struct GNUNET_STATISTICS_Handle *GSA_stats;

/**
 * Handle to the ATS server.
 */
static struct GNUNET_SERVER_Handle *GSA_server;


/**
 * We have received a `struct ClientStartMessage` from a client.  Find
 * out which type of client it is and notify the respective subsystem.
 *
 * @param cls closure, unused
 * @param client handle to the client
 * @param message the start message
 */
static void
handle_ats_start (void *cls,
                  struct GNUNET_SERVER_Client *client,
                  const struct GNUNET_MessageHeader *message)
{
  const struct ClientStartMessage *msg =
      (const struct ClientStartMessage *) message;
  enum StartFlag flag;

  flag = ntohl (msg->start_flag);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received ATS_START (%d) message\n",
              (int) flag);
  switch (flag)
  {
  case START_FLAG_SCHEDULING:
    if (GNUNET_OK != GAS_scheduling_add_client (client))
    {
      GNUNET_SERVER_receive_done (client,
                                  GNUNET_SYSERR);
      return;
    }
    break;
  case START_FLAG_PERFORMANCE_WITH_PIC:
    GAS_performance_add_client (client,
                                flag);
    break;
  case START_FLAG_PERFORMANCE_NO_PIC:
    GAS_performance_add_client (client,
                                flag);
    break;
  case START_FLAG_CONNECTION_SUGGESTION:
    /* This client won't receive messages from us, no need to 'add' */
    break;
  default:
    GNUNET_break (0);
    GNUNET_SERVER_receive_done (client,
                                GNUNET_SYSERR);
    return;
  }
  GNUNET_SERVER_receive_done (client,
                              GNUNET_OK);
}


/**
 * A client disconnected from us.  Tear down the local client
 * record.
 *
 * @param cls unused
 * @param client handle of the client
 */
static void
client_disconnect_handler (void *cls,
                           struct GNUNET_SERVER_Client *client)
{
  if (NULL == client)
    return;
  GAS_scheduling_remove_client (client);
  GAS_connectivity_remove_client (client);
  GAS_preference_client_disconnect (client);
}


/**
 * Task run during shutdown.
 *
 * @param cls unused
 */
static void
cleanup_task (void *cls)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "ATS shutdown initiated\n");
  GAS_connectivity_done ();
  GAS_addresses_done ();
  GAS_plugin_done ();
  GAS_normalization_stop ();
  GAS_scheduling_done ();
  GAS_performance_done ();
  GAS_preference_done ();
  GAS_reservations_done ();
  if (NULL != GSA_stats)
  {
    GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
    GSA_stats = NULL;
  }
}


/**
 * Process template requests.
 *
 * @param cls closure
 * @param server the initialized server
 * @param cfg configuration to use
 */
static void
run (void *cls,
     struct GNUNET_SERVER_Handle *server,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
    {&handle_ats_start, NULL,
     GNUNET_MESSAGE_TYPE_ATS_START,
     sizeof (struct ClientStartMessage)},
    {&GAS_handle_request_address, NULL,
     GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
     sizeof (struct RequestAddressMessage)},
    {&GAS_handle_request_address_cancel, NULL,
     GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
     sizeof (struct RequestAddressMessage)},
    {&GAS_handle_request_address_list, NULL,
     GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
     sizeof (struct AddressListRequestMessage)},
    {&GAS_handle_address_add, NULL,
     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD, 0},
    {&GAS_handle_address_update, NULL,
     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE,
     sizeof (struct AddressUpdateMessage) },
    {&GAS_handle_address_destroyed, NULL,
     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED,
     sizeof (struct AddressDestroyedMessage) },
    {&GAS_handle_reservation_request, NULL,
     GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
     sizeof (struct ReservationRequestMessage)},
    {&GAS_handle_preference_change, NULL,
     GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
    {&GAS_handle_feedback, NULL,
     GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK, 0},
    {NULL, NULL, 0, 0}
  };
  GSA_server = server;
  GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
  GAS_reservations_init (server);
  GAS_connectivity_init ();
  GAS_preference_init ();
  GAS_normalization_start ();
  GAS_addresses_init (server);
  if (GNUNET_OK !=
      GAS_plugin_init (cfg))
  {
    GNUNET_break (0);
    GAS_addresses_done ();
    GAS_normalization_stop ();
    GAS_reservations_done ();
    GAS_connectivity_done ();
    GAS_preference_done ();
    if (NULL != GSA_stats)
    {
      GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
      GSA_stats = NULL;
    }
    return;
  }
  GAS_performance_init (server);
  GAS_scheduling_init (server);

  GNUNET_SERVER_disconnect_notify (server,
                                   &client_disconnect_handler,
				   NULL);
  GNUNET_SERVER_add_handlers (server, handlers);
  GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
				 NULL);
}


/**
 * The main function for the ats service.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  return (GNUNET_OK ==
          GNUNET_SERVICE_run (argc, argv, "ats",
                              GNUNET_SERVICE_OPTION_NONE,
                              &run, NULL)) ? 0 : 1;
}

/* end of gnunet-service-ats.c */