mockup-service.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. #include <stdlib.h>
  18. #include "platform.h"
  19. #include "gnunet_disk_lib.h"
  20. #include "gnunet_getopt_lib.h"
  21. #include "gnunet_protocols.h"
  22. #include "gnunet_service_lib.h"
  23. #include "gnunet_statistics_service.h"
  24. #include "gnunet_strings_lib.h"
  25. #include "gnunet_time_lib.h"
  26. static size_t
  27. transmit_shutdown_ack (void *cls, size_t size, void *buf)
  28. {
  29. struct GNUNET_SERVER_Client *client = cls;
  30. struct GNUNET_MessageHeader *msg;
  31. if (size < sizeof (struct GNUNET_MessageHeader))
  32. {
  33. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  34. _("Failed to transmit shutdown ACK.\n"));
  35. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  36. return 0; /* client disconnected */
  37. }
  38. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  39. _("Transmitting shutdown ACK.\n"));
  40. msg = (struct GNUNET_MessageHeader *) buf;
  41. msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK);
  42. msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  43. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  44. GNUNET_SERVER_client_drop(client);
  45. return sizeof (struct GNUNET_MessageHeader);
  46. }
  47. /**
  48. * Handler for SHUTDOWN message.
  49. *
  50. * @param cls closure (refers to service)
  51. * @param client identification of the client
  52. * @param message the actual message
  53. */
  54. static void
  55. handle_shutdown (void *cls,
  56. struct GNUNET_SERVER_Client *client,
  57. const struct GNUNET_MessageHeader *message)
  58. {
  59. GNUNET_SERVER_client_keep(client);
  60. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  61. _("Initiating shutdown as requested by client.\n"));
  62. GNUNET_SERVER_notify_transmit_ready (client,
  63. sizeof(struct GNUNET_MessageHeader),
  64. GNUNET_TIME_UNIT_FOREVER_REL,
  65. &transmit_shutdown_ack, client);
  66. GNUNET_SERVER_client_persist_ (client);
  67. GNUNET_SCHEDULER_shutdown ();
  68. }
  69. static void
  70. run (void *cls,
  71. struct GNUNET_SERVER_Handle *server,
  72. const struct GNUNET_CONFIGURATION_Handle *cfg)
  73. {
  74. static const struct GNUNET_SERVER_MessageHandler handlers[] = {
  75. {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN,
  76. sizeof (struct GNUNET_MessageHeader)},
  77. {NULL, NULL, 0, 0}
  78. };
  79. /* process client requests */
  80. GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
  81. GNUNET_SERVER_add_handlers (server, handlers);
  82. }
  83. int main(int argc, char *const *argv)
  84. {
  85. int ret;
  86. ret = (GNUNET_OK ==
  87. GNUNET_SERVICE_run (argc,
  88. argv,
  89. "do-nothing", GNUNET_SERVICE_OPTION_NONE,
  90. &run, NULL)) ? 0 : 1;
  91. return ret;
  92. }