aboutsummaryrefslogtreecommitdiff
path: root/src/ext/gnunet-service-ext.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ext/gnunet-service-ext.c')
-rw-r--r--src/ext/gnunet-service-ext.c135
1 files changed, 93 insertions, 42 deletions
diff --git a/src/ext/gnunet-service-ext.c b/src/ext/gnunet-service-ext.c
index 7876047..d292eac 100644
--- a/src/ext/gnunet-service-ext.c
+++ b/src/ext/gnunet-service-ext.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 3 Copyright (C) 20xx GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -28,24 +28,55 @@
28#include "gnunet_protocols_ext.h" 28#include "gnunet_protocols_ext.h"
29 29
30/** 30/**
31 * Some state we track per client.
32 */
33struct ClientContext
34{
35 /**
36 * For telling service to continue processing more messages.
37 */
38 struct GNUNET_SERVICE_Client *c;
39
40 /**
41 * For sending messages to the client.
42 */
43 struct GNUNET_MQ_Handle *mq;
44
45 /**
46 * Sample state.
47 */
48 uint32_t state;
49};
50
51
52/**
31 * Our configuration. 53 * Our configuration.
32 */ 54 */
33static const struct GNUNET_CONFIGURATION_Handle *cfg; 55static const struct GNUNET_CONFIGURATION_Handle *cfg;
34 56
57
35/** 58/**
36 * Handle EXT-message. 59 * Handle EXT-message.
37 * 60 *
38 * @param cls closure 61 * @param cls identification of the client
39 * @param client identification of the client
40 * @param message the actual message 62 * @param message the actual message
41 */ 63 */
42static void 64static void
43handle_ext (void *cls, 65handle_ext (void *cls,
44 struct GNUNET_SERVER_Client *client,
45 const struct GNUNET_MessageHeader *message) 66 const struct GNUNET_MessageHeader *message)
46{ 67{
47 GNUNET_SERVER_receive_done (client, 68 struct ClientContext *cc = cls;
48 GNUNET_OK); 69 struct GNUNET_MQ_Envelope *env;
70 struct GNUNET_MessageHeader *response;
71
72 /* Send same type of message back... */
73 env = GNUNET_MQ_msg (response,
74 GNUNET_MESSAGE_TYPE_EXT);
75 GNUNET_MQ_send (cc->mq,
76 env);
77
78 /* Continue processing more messages from client */
79 GNUNET_SERVICE_client_continue (cc->c);
49} 80}
50 81
51 82
@@ -57,19 +88,7 @@ handle_ext (void *cls,
57static void 88static void
58shutdown_task (void *cls) 89shutdown_task (void *cls)
59{ 90{
60} 91 /* Clean up whatever #run() setup here. */
61
62
63/**
64 * A client disconnected. Remove all of its data structure entries.
65 *
66 * @param cls closure, NULL
67 * @param client identification of the client
68 */
69static void
70handle_client_disconnect (void *cls,
71 struct GNUNET_SERVER_Client * client)
72{
73} 92}
74 93
75 94
@@ -85,37 +104,69 @@ run (void *cls,
85 struct GNUNET_SERVER_Handle *server, 104 struct GNUNET_SERVER_Handle *server,
86 const struct GNUNET_CONFIGURATION_Handle *c) 105 const struct GNUNET_CONFIGURATION_Handle *c)
87{ 106{
88 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
89 {&handle_ext, NULL, GNUNET_MESSAGE_TYPE_EXT, 0},
90 {NULL, NULL, 0, 0}
91 };
92 cfg = c; 107 cfg = c;
93 GNUNET_SERVER_add_handlers (server, handlers); 108 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
94 GNUNET_SERVER_disconnect_notify (server, 109 NULL);
95 &handle_client_disconnect, 110}
96 NULL); 111
97 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 112
98 &shutdown_task, 113/**
99 NULL); 114 * Callback called when a client connects to the service.
115 *
116 * @param cls closure for the service
117 * @param c the new client that connected to the service
118 * @param mq the message queue used to send messages to the client
119 * @return @a c
120 */
121static void *
122client_connect_cb (void *cls,
123 struct GNUNET_SERVICE_Client *c,
124 struct GNUNET_MQ_Handle *mq)
125{
126 struct ClientContext *cc;
127
128 cc = GNUNET_new (struct ClientContext);
129 cc->c = c;
130 cc->mq = mq;
131 /* setup more for new client here */
132 return cc;
100} 133}
101 134
102 135
103/** 136/**
104 * The main function for the ext service. 137 * Callback called when a client disconnected from the service
105 * 138 *
106 * @param argc number of arguments from the command line 139 * @param cls closure for the service
107 * @param argv command line arguments 140 * @param c the client that disconnected
108 * @return 0 ok, 1 on error 141 * @param internal_cls our `struct ClientContext`
109 */ 142 */
110int 143static void
111main (int argc, char *const *argv) 144client_disconnect_cb (void *cls,
145 struct GNUNET_SERVICE_Client *c,
146 void *internal_cls)
112{ 147{
113 return (GNUNET_OK == 148 struct ClientContext *cc = internal_cls;
114 GNUNET_SERVICE_run (argc, 149
115 argv, 150 GNUNET_assert (cc->c == c);
116 "ext", 151 /* Tear down rest of client here */
117 GNUNET_SERVICE_OPTION_NONE, 152 GNUNET_free (cc);
118 &run, NULL)) ? 0 : 1;
119} 153}
120 154
155
156/**
157 * Define "main" method using service macro.
158 */
159GNUNET_SERVICE_MAIN
160("ext",
161 GNUNET_SERVICE_OPTION_NONE,
162 &run,
163 &client_connect_cb,
164 &client_disconnect_cb,
165 NULL,
166 GNUNET_MQ_hd_fixed_size (ext,
167 GNUNET_MESSAGE_TYPE_EXT,
168 struct GNUNET_MessageHeader,
169 NULL),
170 GNUNET_MQ_handler_end ());
171
121/* end of gnunet-service-ext.c */ 172/* end of gnunet-service-ext.c */