aboutsummaryrefslogtreecommitdiff
path: root/src/dv
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-10-11 20:33:05 +0000
committerChristian Grothoff <christian@grothoff.org>2016-10-11 20:33:05 +0000
commitcb8179ca554d2c6a361828dac8dd7d7d6820210f (patch)
treedbd07db8c03aa7446c13f72ce7a7a0da90f692f9 /src/dv
parentda8797b29f4db485602ecbd25cbafd8f80a9e812 (diff)
downloadgnunet-cb8179ca554d2c6a361828dac8dd7d7d6820210f.tar.gz
gnunet-cb8179ca554d2c6a361828dac8dd7d7d6820210f.zip
converting dv to new service API
Diffstat (limited to 'src/dv')
-rw-r--r--src/dv/gnunet-service-dv.c183
1 files changed, 109 insertions, 74 deletions
diff --git a/src/dv/gnunet-service-dv.c b/src/dv/gnunet-service-dv.c
index 667970b75..fdd871bbd 100644
--- a/src/dv/gnunet-service-dv.c
+++ b/src/dv/gnunet-service-dv.c
@@ -323,7 +323,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
323 * Hopefully this client will never change, although if the plugin 323 * Hopefully this client will never change, although if the plugin
324 * dies and returns for some reason it may happen. 324 * dies and returns for some reason it may happen.
325 */ 325 */
326static struct GNUNET_SERVER_NotificationContext *nc; 326static struct GNUNET_NotificationContext *nc;
327 327
328/** 328/**
329 * Handle for the statistics service. 329 * Handle for the statistics service.
@@ -410,9 +410,9 @@ send_data_to_plugin (const struct GNUNET_MessageHeader *message,
410 received_msg->distance = htonl (distance); 410 received_msg->distance = htonl (distance);
411 received_msg->sender = *origin; 411 received_msg->sender = *origin;
412 GNUNET_memcpy (&received_msg[1], message, ntohs (message->size)); 412 GNUNET_memcpy (&received_msg[1], message, ntohs (message->size));
413 GNUNET_SERVER_notification_context_broadcast (nc, 413 GNUNET_notification_context_broadcast (nc,
414 &received_msg->header, 414 &received_msg->header,
415 GNUNET_YES); 415 GNUNET_YES);
416 GNUNET_free (received_msg); 416 GNUNET_free (received_msg);
417} 417}
418 418
@@ -425,9 +425,9 @@ send_data_to_plugin (const struct GNUNET_MessageHeader *message,
425static void 425static void
426send_control_to_plugin (const struct GNUNET_MessageHeader *message) 426send_control_to_plugin (const struct GNUNET_MessageHeader *message)
427{ 427{
428 GNUNET_SERVER_notification_context_broadcast (nc, 428 GNUNET_notification_context_broadcast (nc,
429 message, 429 message,
430 GNUNET_NO); 430 GNUNET_NO);
431} 431}
432 432
433 433
@@ -1752,36 +1752,50 @@ handle_dv_route_message (void *cls,
1752 1752
1753 1753
1754/** 1754/**
1755 * Service server's handler for message send requests (which come 1755 * Check that @a msg is well-formed
1756 * bubbling up to us through the DV plugin).
1757 * 1756 *
1758 * @param cls closure 1757 * @param cls identification of the client
1759 * @param client identification of the client
1760 * @param message the actual message 1758 * @param message the actual message
1759 * @return #GNUNET_OK if @a msg is well-formed
1761 */ 1760 */
1762static void 1761static int
1763handle_dv_send_message (void *cls, 1762check_dv_send_message (void *cls,
1764 struct GNUNET_SERVER_Client *client, 1763 const struct GNUNET_DV_SendMessage *msg)
1765 const struct GNUNET_MessageHeader *message)
1766{ 1764{
1767 struct Route *route;
1768 const struct GNUNET_DV_SendMessage *msg;
1769 const struct GNUNET_MessageHeader *payload; 1765 const struct GNUNET_MessageHeader *payload;
1770 1766
1771 if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader)) 1767 if (ntohs (msg->header.size) < sizeof (struct GNUNET_DV_SendMessage) +
1768 sizeof (struct GNUNET_MessageHeader))
1772 { 1769 {
1773 GNUNET_break (0); 1770 GNUNET_break (0);
1774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1771 return GNUNET_SYSERR;
1775 return;
1776 } 1772 }
1777 msg = (const struct GNUNET_DV_SendMessage *) message;
1778 payload = (const struct GNUNET_MessageHeader *) &msg[1]; 1773 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1779 if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size)) 1774 if (ntohs (msg->header.size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
1780 { 1775 {
1781 GNUNET_break (0); 1776 GNUNET_break (0);
1782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1777 return GNUNET_SYSERR;
1783 return;
1784 } 1778 }
1779 return GNUNET_OK;
1780}
1781
1782
1783/**
1784 * Service server's handler for message send requests (which come
1785 * bubbling up to us through the DV plugin).
1786 *
1787 * @param cls identification of the client
1788 * @param message the actual message
1789 */
1790static void
1791handle_dv_send_message (void *cls,
1792 const struct GNUNET_DV_SendMessage *msg)
1793{
1794 struct GNUNET_SERVICE_Client *client = cls;
1795 struct Route *route;
1796 const struct GNUNET_MessageHeader *payload;
1797
1798 payload = (const struct GNUNET_MessageHeader *) &msg[1];
1785 route = GNUNET_CONTAINER_multipeermap_get (all_routes, 1799 route = GNUNET_CONTAINER_multipeermap_get (all_routes,
1786 &msg->target); 1800 &msg->target);
1787 if (NULL == route) 1801 if (NULL == route)
@@ -1794,7 +1808,7 @@ handle_dv_send_message (void *cls,
1794 GNUNET_STATISTICS_update (stats, 1808 GNUNET_STATISTICS_update (stats,
1795 "# local messages discarded (no route)", 1809 "# local messages discarded (no route)",
1796 1, GNUNET_NO); 1810 1, GNUNET_NO);
1797 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1811 GNUNET_SERVICE_client_continue (client);
1798 return; 1812 return;
1799 } 1813 }
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1808,7 +1822,7 @@ handle_dv_send_message (void *cls,
1808 &my_identity, 1822 &my_identity,
1809 &msg->target, 1823 &msg->target,
1810 payload); 1824 payload);
1811 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1825 GNUNET_SERVICE_client_continue (client);
1812} 1826}
1813 1827
1814 1828
@@ -1936,7 +1950,7 @@ shutdown_task (void *cls)
1936 GNUNET_CONTAINER_multipeermap_destroy (all_routes); 1950 GNUNET_CONTAINER_multipeermap_destroy (all_routes);
1937 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 1951 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1938 stats = NULL; 1952 stats = NULL;
1939 GNUNET_SERVER_notification_context_destroy (nc); 1953 GNUNET_notification_context_destroy (nc);
1940 nc = NULL; 1954 nc = NULL;
1941 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++) 1955 for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
1942 { 1956 {
@@ -1955,7 +1969,7 @@ shutdown_task (void *cls)
1955/** 1969/**
1956 * Notify newly connected client about an existing route. 1970 * Notify newly connected client about an existing route.
1957 * 1971 *
1958 * @param cls the `struct GNUNET_SERVER_Client *` 1972 * @param cls the `struct GNUNET_SERVICE_Client *`
1959 * @param key peer identity 1973 * @param key peer identity
1960 * @param value the `struct Route *` 1974 * @param value the `struct Route *`
1961 * @return #GNUNET_OK (continue to iterate) 1975 * @return #GNUNET_OK (continue to iterate)
@@ -1965,20 +1979,17 @@ notify_client_about_route (void *cls,
1965 const struct GNUNET_PeerIdentity *key, 1979 const struct GNUNET_PeerIdentity *key,
1966 void *value) 1980 void *value)
1967{ 1981{
1968 struct GNUNET_SERVER_Client *client = cls; 1982 struct GNUNET_SERVICE_Client *client = cls;
1969 struct Route *route = value; 1983 struct Route *route = value;
1970 struct GNUNET_DV_ConnectMessage cm; 1984 struct GNUNET_MQ_Envelope *env;
1971 1985 struct GNUNET_DV_ConnectMessage *cm;
1972 memset (&cm, 0, sizeof (cm)); 1986
1973 cm.header.size = htons (sizeof (cm)); 1987 env = GNUNET_MQ_msg (cm,
1974 cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT); 1988 GNUNET_MESSAGE_TYPE_DV_CONNECT);
1975 cm.distance = htonl (route->target.distance); 1989 cm->distance = htonl (route->target.distance);
1976 cm.peer = route->target.peer; 1990 cm->peer = route->target.peer;
1977 1991 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
1978 GNUNET_SERVER_notification_context_unicast (nc, 1992 env);
1979 client,
1980 &cm.header,
1981 GNUNET_NO);
1982 return GNUNET_OK; 1993 return GNUNET_OK;
1983} 1994}
1984 1995
@@ -1993,11 +2004,13 @@ notify_client_about_route (void *cls,
1993 */ 2004 */
1994static void 2005static void
1995handle_start (void *cls, 2006handle_start (void *cls,
1996 struct GNUNET_SERVER_Client *client,
1997 const struct GNUNET_MessageHeader *message) 2007 const struct GNUNET_MessageHeader *message)
1998{ 2008{
1999 GNUNET_SERVER_notification_context_add (nc, client); 2009 struct GNUNET_SERVICE_Client *client = cls;
2000 GNUNET_SERVER_receive_done (client, GNUNET_OK); 2010
2011 GNUNET_notification_context_add (nc,
2012 GNUNET_SERVICE_client_get_mq (client));
2013 GNUNET_SERVICE_client_continue (client);
2001 GNUNET_CONTAINER_multipeermap_iterate (all_routes, 2014 GNUNET_CONTAINER_multipeermap_iterate (all_routes,
2002 &notify_client_about_route, 2015 &notify_client_about_route,
2003 client); 2016 client);
@@ -2025,13 +2038,13 @@ core_init (void *cls,
2025 * Process dv requests. 2038 * Process dv requests.
2026 * 2039 *
2027 * @param cls closure 2040 * @param cls closure
2028 * @param server the initialized server
2029 * @param c configuration to use 2041 * @param c configuration to use
2042 * @param service the initialized service
2030 */ 2043 */
2031static void 2044static void
2032run (void *cls, 2045run (void *cls,
2033 struct GNUNET_SERVER_Handle *server, 2046 const struct GNUNET_CONFIGURATION_Handle *c,
2034 const struct GNUNET_CONFIGURATION_Handle *c) 2047 struct GNUNET_SERVICE_Handle *service)
2035{ 2048{
2036 struct GNUNET_MQ_MessageHandler core_handlers[] = { 2049 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2037 GNUNET_MQ_hd_var_size (dv_route_message, 2050 GNUNET_MQ_hd_var_size (dv_route_message,
@@ -2040,15 +2053,6 @@ run (void *cls,
2040 NULL), 2053 NULL),
2041 GNUNET_MQ_handler_end () 2054 GNUNET_MQ_handler_end ()
2042 }; 2055 };
2043 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2044 {&handle_start, NULL,
2045 GNUNET_MESSAGE_TYPE_DV_START,
2046 sizeof (struct GNUNET_MessageHeader) },
2047 { &handle_dv_send_message, NULL,
2048 GNUNET_MESSAGE_TYPE_DV_SEND,
2049 0},
2050 {NULL, NULL, 0, 0}
2051 };
2052 in_shutdown = GNUNET_NO; 2056 in_shutdown = GNUNET_NO;
2053 cfg = c; 2057 cfg = c;
2054 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, 2058 direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128,
@@ -2073,35 +2077,66 @@ run (void *cls,
2073 core_api = NULL; 2077 core_api = NULL;
2074 return; 2078 return;
2075 } 2079 }
2076 nc = GNUNET_SERVER_notification_context_create (server, 2080 nc = GNUNET_notification_context_create (MAX_QUEUE_SIZE_PLUGIN);
2077 MAX_QUEUE_SIZE_PLUGIN);
2078 stats = GNUNET_STATISTICS_create ("dv", 2081 stats = GNUNET_STATISTICS_create ("dv",
2079 cfg); 2082 cfg);
2080 GNUNET_SERVER_add_handlers (server,
2081 plugin_handlers);
2082 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 2083 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2083 NULL); 2084 NULL);
2084} 2085}
2085 2086
2086 2087
2087/** 2088/**
2088 * The main function for the dv service. 2089 * Callback called when a client connects to the service.
2089 * 2090 *
2090 * @param argc number of arguments from the command line 2091 * @param cls closure for the service
2091 * @param argv command line arguments 2092 * @param c the new client that connected to the service
2092 * @return 0 ok, 1 on error 2093 * @param mq the message queue used to send messages to the client
2094 * @return @a c
2093 */ 2095 */
2094int 2096static void *
2095main (int argc, 2097client_connect_cb (void *cls,
2096 char *const *argv) 2098 struct GNUNET_SERVICE_Client *c,
2099 struct GNUNET_MQ_Handle *mq)
2097{ 2100{
2098 return (GNUNET_OK == 2101 return c;
2099 GNUNET_SERVICE_run (argc,
2100 argv,
2101 "dv",
2102 GNUNET_SERVICE_OPTION_NONE,
2103 &run,
2104 NULL)) ? 0 : 1;
2105} 2102}
2106 2103
2104
2105/**
2106 * Callback called when a client disconnected from the service
2107 *
2108 * @param cls closure for the service
2109 * @param c the client that disconnected
2110 * @param internal_cls should be equal to @a c
2111 */
2112static void
2113client_disconnect_cb (void *cls,
2114 struct GNUNET_SERVICE_Client *c,
2115 void *internal_cls)
2116{
2117 GNUNET_assert (c == internal_cls);
2118}
2119
2120
2121/**
2122 * Define "main" method using service macro.
2123 */
2124GNUNET_SERVICE_MAIN
2125("dv",
2126 GNUNET_SERVICE_OPTION_NONE,
2127 &run,
2128 &client_connect_cb,
2129 &client_disconnect_cb,
2130 NULL,
2131 GNUNET_MQ_hd_fixed_size (start,
2132 GNUNET_MESSAGE_TYPE_DV_START,
2133 struct GNUNET_MessageHeader,
2134 NULL),
2135 GNUNET_MQ_hd_var_size (dv_send_message,
2136 GNUNET_MESSAGE_TYPE_DV_SEND,
2137 struct GNUNET_DV_SendMessage,
2138 NULL),
2139 GNUNET_MQ_handler_end ());
2140
2141
2107/* end of gnunet-service-dv.c */ 2142/* end of gnunet-service-dv.c */