glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / arm / mockup-service.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2007, 2008, 2009, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 #include <stdlib.h>
17 #include "platform.h"
18 #include "gnunet_util_lib.h"
19 #include "gnunet_protocols.h"
20
21
22 static int special_ret = 0;
23
24 /**
25  * Handler for STOP message.
26  *
27  * @param cls client identification of the client
28  * @param message the actual message
29  */
30 static void
31 handle_stop (void *cls,
32              const struct GNUNET_MessageHeader *message)
33 {
34   struct GNUNET_SERVICE_Client *client = cls;
35
36   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
37               _("Initiating shutdown as requested by client.\n"));
38   GNUNET_SERVICE_client_persist (client);
39   GNUNET_SCHEDULER_shutdown ();
40   /* ARM won't exponentially increase restart delay if we
41    * terminate normally. This changes the return code.
42    */
43   special_ret = 1;
44 }
45
46
47 /**
48  * Callback called when a client connects to the service.
49  *
50  * @param cls closure for the service
51  * @param c the new client that connected to the service
52  * @param mq the message queue used to send messages to the client
53  * @return @a c
54  */
55 static void *
56 client_connect_cb (void *cls,
57                    struct GNUNET_SERVICE_Client *c,
58                    struct GNUNET_MQ_Handle *mq)
59 {
60   return c;
61 }
62
63
64 /**
65  * Callback called when a client disconnected from the service
66  *
67  * @param cls closure for the service
68  * @param c the client that disconnected
69  * @param internal_cls should be equal to @a c
70  */
71 static void
72 client_disconnect_cb (void *cls,
73                       struct GNUNET_SERVICE_Client *c,
74                       void *internal_cls)
75 {
76   GNUNET_assert (c == internal_cls);
77 }
78
79
80 static void
81 run (void *cls,
82      const struct GNUNET_CONFIGURATION_Handle *cfg,
83      struct GNUNET_SERVICE_Handle *service)
84 {
85   /* nothing to do */
86 }
87
88
89 /**
90  * Define "main" method using service macro.
91  */
92 GNUNET_SERVICE_MAIN
93 ("do-nothing",
94  GNUNET_SERVICE_OPTION_NONE,
95  &run,
96  &client_connect_cb,
97  &client_disconnect_cb,
98  NULL,
99  GNUNET_MQ_hd_fixed_size (stop,
100                           GNUNET_MESSAGE_TYPE_ARM_STOP,
101                           struct GNUNET_MessageHeader,
102                           NULL),
103  GNUNET_MQ_handler_end ());
104
105
106 /**
107  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
108  */
109 void __attribute__ ((destructor))
110 GNUNET_mockup_done ()
111 {
112   _exit (special_ret);
113 }