3 <link rel="stylesheet" type="text/css" href="doc.css">
7 <a name="HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON"></a>
8 <h1>HOWTO WRITE a PLUGIN for AFB-DAEMON</h1>
16 <li><a href="#HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON">HOWTO WRITE a PLUGIN for AFB-DAEMON</a>
18 <li><a href="#Summary">Summary</a>
20 <li><a href="#Nature.of.a.plugin">Nature of a plugin</a></li>
21 <li><a href="#Live.cycle.of.a.plugin.within.afb-daemon">Live cycle of a plugin within afb-daemon</a></li>
22 <li><a href="#Content.of.a.plugin">Content of a plugin</a>
24 <li><a href="#The.name.of.the.plugin">The name of the plugin</a></li>
25 <li><a href="#Names.of.verbs">Names of verbs</a></li>
26 <li><a href="#The.initialisation.function">The initialisation function</a></li>
27 <li><a href="#Functions.implementing.verbs">Functions implementing verbs</a>
33 <li><a href="#The.Tic-Tac-Toe.example">The Tic-Tac-Toe example</a></li>
34 <li><a href="#Choosing.names">Choosing names</a>
36 <li><a href="#Names.for.API..plugin.">Names for API (plugin)</a></li>
37 <li><a href="#Names.for.verbs">Names for verbs</a></li>
38 <li><a href="#Names.for.arguments">Names for arguments</a></li>
39 <li><a href="#Forging.names.widely.available">Forging names widely available</a></li>
42 <li><a href="#Options.to.set.when.compiling.plugins">Options to set when compiling plugins</a></li>
43 <li><a href="#Header.files.to.include">Header files to include</a></li>
44 <li><a href="#Writing.a.synchronous.verb.implementation">Writing a synchronous verb implementation</a>
46 <li><a href="#The.incoming.request">The incoming request</a></li>
47 <li><a href="#Associating.an.object.to.the.session.for.the.plugin">Associating an object to the session for the plugin</a></li>
48 <li><a href="#Sending.the.reply.to.a.request">Sending the reply to a request</a></li>
51 <li><a href="#Getting.argument.of.invocation">Getting argument of invocation</a></li>
52 <li><a href="#How.to.build.a.plugin">How to build a plugin</a></li>
57 <a name="Summary"></a>
60 <p>The binder afb-daemon serves files through
61 the HTTP protocol and offers access to API’s through
62 HTTP or WebSocket protocol.</p>
64 <p>The plugins are used to add API’s to afb-daemon.
65 This part describes how to write a plugin for afb-daemon.
66 Excepting this summary, this part is intended to be read
69 <p>Before going into details, through a tiny example,
70 a short overview plugins basis is needed.</p>
72 <a name="Nature.of.a.plugin"></a>
73 <h3>Nature of a plugin</h3>
75 <p>A plugin is a separate piece of code made of a shared library.
76 The plugin is loaded and activated by afb-daemon when afb-daemon
79 <p>Technically, a plugin is not linked to any library of afb-daemon.</p>
81 <a name="Live.cycle.of.a.plugin.within.afb-daemon"></a>
82 <h3>Live cycle of a plugin within afb-daemon</h3>
84 <p>The plugins are loaded and activated when afb-daemon starts.</p>
86 <p>At start, the plugin initialise itself.
87 If it fails to initialise then afb-daemon stops.</p>
89 <p>Conversely, if it success to initialize, it must declare
90 a name, that must be unique, and a list of API’s verbs.</p>
92 <p>When initialized, the functions implementing the API’s verbs
93 of the plugin are activated on call.</p>
95 <p>At the end, nothing special is done by afb-daemon.
96 Consequently, developpers of plugins should use ‘atexit’
97 or ‘on_exit’ during initialisation if they need to
98 perform specific actions when stopping.</p>
100 <a name="Content.of.a.plugin"></a>
101 <h3>Content of a plugin</h3>
103 <p>For afb-daemon, a plugin contains 2 different
104 things: names and functions.</p>
106 <p>There is two kind of names:
107 - the name of the plugin,
108 - the names of the verbs.</p>
110 <p>There is two kind of functions:
111 - the initialisation function
112 - functions implementing verbs</p>
114 <p>Afb-daemon translates the name of the method that is
115 invoked to a pair of API and verb names. For example,
116 the method named <strong>foo/bar</strong> translated to the API
117 name <strong>foo</strong> and the verb name <strong>bar</strong>.
118 To serve it, afb-daemon search the plugin that record
119 the name <strong>foo</strong> and if it also recorded the verb <strong>bar</strong>,
120 it calls the implementation function declared for this verb.</p>
122 <p>Afb-daemon make no distinction between lower case
123 and upper case when searching for a method.
124 Thus, The names <strong>TicTacToe/Board</strong> and <strong>tictactoe/borad</strong>
127 <a name="The.name.of.the.plugin"></a>
128 <h4>The name of the plugin</h4>
130 <p>The name of the plugin is also known as the name
131 of the API that defines the plugin.</p>
133 <p>This name is also known as the prefix.</p>
135 <p>The name of a plugin MUST be unique within afb-daemon.</p>
137 <p>For example, when a client of afb-daemon
138 calls a method named <strong>foo/bar</strong>. Afb-daemon
139 extracts the prefix <strong>foo</strong> and the suffix <strong>bar</strong>.
140 <strong>foo</strong> is the API name and must match a plugin name,
141 the plugin that implements the verb <strong>bar</strong>.</p>
143 <a name="Names.of.verbs"></a>
144 <h4>Names of verbs</h4>
146 <p>Each plugin exposes a set of verbs that can be called
147 by client of afb-daemon.</p>
149 <p>The name of a verb MUST be unique within a plugin.</p>
151 <p>Plugins link verbs to functions that are called
152 when clients emit requests for that verb.</p>
154 <p>For example, when a client of afb-daemon
155 calls a method named <strong>foo/bar</strong>.</p>
157 <a name="The.initialisation.function"></a>
158 <h4>The initialisation function</h4>
160 <p>The initialisation function serves several purposes.</p>
163 <li><p>It allows afb-daemon to check the version
164 of the plugin using the name of the initialisation
165 functions that it found. Currently, the initialisation
166 function is named <strong>pluginAfbV1Register</strong>. It identifies
167 the first version of plugins.</p></li>
168 <li><p>It allows the plugin to initialise itself.</p></li>
169 <li><p>It serves to the plugin to declare names, descriptions,
170 requirements and implmentations of the verbs that it exposes.</p></li>
174 <a name="Functions.implementing.verbs"></a>
175 <h4>Functions implementing verbs</h4>
177 <p>When a method is called, afb-daemon constructs a request
178 object and pass it to the implementation function for verb
179 within the plugin of the API.</p>
181 <p>An implementation function receives a request object that
182 is used to get arguments of the request, to send
183 answer, to store session data.</p>
185 <p>A plugin MUST send an answer to the request.</p>
187 <p>But it is not mandatory to send the answer
188 before to return from the implementing function.
189 This behaviour is important for implementing
190 asynchronous actions.</p>
192 <p>Implementation functions that always reply to the request
193 before returning are named <em>synchronous implementations</em>.
194 Those that don’t always reply to the request before
195 returning are named <em>asynchronous implementations</em>.</p>
197 <p>Asynchronous implementations typically initiate an
198 asynchronous action and record to send the reply
199 on completion of this action.</p>
201 <a name="The.Tic-Tac-Toe.example"></a>
202 <h2>The Tic-Tac-Toe example</h2>
204 <p>This part explains how to write an afb-plugin.
205 For the sake of being practical we will use many
206 examples from the tic-tac-toe example.
207 This plugin example is in <em>plugins/samples/tic-tac-toe.c</em>.</p>
209 <p>This plugin is named <strong><em>tictactoe</em></strong>.</p>
211 <a name="Choosing.names"></a>
212 <h2>Choosing names</h2>
214 <p>The designer of a plugin must defines names for its plugin
215 (or its API) and for the verbs of its API. He also
216 must defines names for arguments given by name.</p>
218 <p>While forging names, the designer should take into account
219 the rules for making valid names and some rules that make
220 the names easy to use across plaforms.</p>
222 <p>The names and strings used ALL are UTF-8 encoded.</p>
224 <a name="Names.for.API..plugin."></a>
225 <h3>Names for API (plugin)</h3>
227 <p>The names of the API are checked.
228 All characters are authorised except:</p>
231 <li>the control characters (\u0000 .. \u001f)</li>
232 <li>the characters of the set { ‘ ’, ‘“’, ‘#’, ‘%’, ‘&’,
233 ‘’‘, ’/‘, ’?‘, ’`‘, ’\x7f' }</li>
237 <p>In other words the set of forbidden characters is
238 { \u0000..\u0020, \u0022, \u0023, \u0025..\u0027,
239 \u002f, \u003f, \u0060, \u007f }.</p>
241 <p>Afb-daemon make no distinction between lower case
242 and upper case when searching for an API by its name.</p>
244 <a name="Names.for.verbs"></a>
245 <h3>Names for verbs</h3>
247 <p>The names of the verbs are not checked.</p>
249 <p>However, the validity rules for verb’s names are the
250 same as for API’s names except that the dot (.) character
253 <p>Afb-daemon make no distinction between lower case
254 and upper case when searching for an API by its name.</p>
256 <a name="Names.for.arguments"></a>
257 <h3>Names for arguments</h3>
259 <p>The names for arguments are not restricted and can be
262 <p>The arguments are searched with the case sensitive
263 string comparison. Thus the names “index” and “Index”
264 are not the same.</p>
266 <a name="Forging.names.widely.available"></a>
267 <h3>Forging names widely available</h3>
269 <p>The key names of javascript object can be almost
270 anything using the arrayed notation:</p>
272 <pre><code>object[key] = value
275 <p>That is not the case with the dot notation:</p>
277 <pre><code>object.key = value
280 <p>Using the dot notation, the key must be a valid javascript
283 <p>For this reason, the chosen names should better be
284 valid javascript identifier.</p>
286 <p>It is also a good practice, even for arguments, to not
287 rely on the case sensitivity and to avoid the use of
288 names different only by the case.</p>
290 <a name="Options.to.set.when.compiling.plugins"></a>
291 <h2>Options to set when compiling plugins</h2>
293 <p>Afb-daemon provides a configuration file for <em>pkg-config</em>.
294 Typing the command</p>
296 <pre><code>pkg-config --cflags afb-daemon
299 <p>will print the flags to use for compiling, like this:</p>
301 <pre><code>$ pkg-config --cflags afb-daemon
302 -I/opt/local/include -I/usr/include/json-c
305 <p>For linking, you should use</p>
307 <pre><code>$ pkg-config --libs afb-daemon
311 <p>As you see, afb-daemon automatically includes dependency to json-c.
312 This is done through the <strong>Requires</strong> keyword of pkg-config.</p>
314 <p>If this behaviour is a problem, let us know.</p>
316 <a name="Header.files.to.include"></a>
317 <h2>Header files to include</h2>
319 <p>The plugin <em>tictactoe</em> has the following lines for its includes:</p>
321 <pre><code>#define _GNU_SOURCE
322 #include <stdio.h>
323 #include <string.h>
324 #include <json-c/json.h>
325 #include <afb/afb-plugin.h>
328 <p>The header <em>afb/afb-plugin.h</em> includes all the features that a plugin
329 needs except two foreign header that must be included by the plugin
333 <li><em>json-c/json.h</em>: this header must be include to handle json objects;</li>
334 <li><em>systemd/sd-event.h</em>: this must be include to access the main loop;</li>
335 <li><em>systemd/sd-bus.h</em>: this may be include to use dbus connections.</li>
339 <p>The <em>tictactoe</em> plugin does not use systemd features so it is not included.</p>
341 <p>When including <em>afb/afb-plugin.h</em>, the macro <strong>_GNU_SOURCE</strong> must be
344 <a name="Writing.a.synchronous.verb.implementation"></a>
345 <h2>Writing a synchronous verb implementation</h2>
347 <p>The verb <strong>tictactoe/board</strong> is a synchronous implementation.
348 Here is its listing:</p>
353 static void board(struct afb_req req)
356 struct json_object *description;
358 /* retrieves the context for the session */
359 board = board_of_req(req);
360 INFO(afbitf, "method 'board' called for boardid %d", board->id);
362 /* describe the board */
363 description = describe(board);
365 /* send the board's description */
366 afb_req_success(req, description, NULL);
370 <p>This examples show many aspects of writing a synchronous
371 verb implementation.</p>
373 <a name="The.incoming.request"></a>
374 <h3>The incoming request</h3>
376 <p>For any implementation, the request is received by a structure of type
377 <strong>struct afb_req</strong>.</p>
379 <p><strong><em>Important: note that this is a PLAIN structure, not a pointer to a structure.</em></strong></p>
381 <p>This structure, here named <em>req</em>, is used</p>
383 <p><em>req</em> is used to get arguments of the request, to send
384 answer, to store session data.</p>
386 <p>This object and its interface is defined and documented
387 in the file names <em>afb/afb-req-itf.h</em></p>
389 <p>The above example uses 2 times the request object <em>req</em>.</p>
391 <p>The first time, it is used for retrieving the board attached to
392 the session of the request.</p>
394 <p>The second time, it is used to send the reply: an object that
395 describes the current board.</p>
397 <a name="Associating.an.object.to.the.session.for.the.plugin"></a>
398 <h3>Associating an object to the session for the plugin</h3>
400 <p>When the plugin <em>tic-tac-toe</em> receives a request, it musts regain
401 the board that describes the game associated to the session.</p>
403 <p>For a plugin, having data associated to a session is a common case.
404 This data is called the context of the plugin for the session.
405 For the plugin <em>tic-tac-toe</em>, the context is the board.</p>
407 <p>The requests <em>afb_req</em> offer four functions for
408 storing and retrieving the context associated to the session.</p>
410 <p>These functions are:</p>
413 <li><p><strong>afb_req_context_get</strong>:
414 retrieves the context data stored for the plugin.</p></li>
415 <li><p><strong>afb_req_context_set</strong>:
416 store the context data of the plugin.</p></li>
417 <li><p><strong>afb_req_context</strong>:
418 retrieves the context data of the plugin,
419 if needed, creates the context and store it.</p></li>
420 <li><p><strong>afb_req_context_clear</strong>:
421 reset the stored data.</p></li>
425 <p>The plugin <em>tictactoe</em> use a convenient function to retrieve
426 its context: the board. This function is <em>board_of_req</em>:</p>
429 * retrieves the board of the request
431 static inline struct board *board_of_req(struct afb_req req)
433 return afb_req_context(req, (void*)get_new_board, (void*)release_board);
437 <p>This function is very simple because it merely wraps
438 a call to the function <strong>afb_req_context</strong>, providing
439 all needed arguments.
440 The casts are required to avoid a warning when compiling.</p>
442 <p>Here is the definition of the function <strong>afb_req_context</strong></p>
445 * Gets the pointer stored by the plugin for the session of 'req'.
446 * If the stored pointer is NULL, indicating that no pointer was
447 * already stored, afb_req_context creates a new context by calling
448 * the function 'create_context' and stores it with the freeing function
451 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
453 void *result = afb_req_context_get(req);
454 if (result == NULL) {
455 result = create_context();
456 afb_req_context_set(req, result, free_context);
462 <p>This powerful function ensures that the context exists and is
463 stored for the session.</p>
465 <p>The function <strong>get_new_board</strong> creates a new board and set its
466 count of use to 1. The boards are counting their count of use
467 to free there ressources when no more used.</p>
469 <p>The function <strong>release_board</strong></p>
471 <a name="Sending.the.reply.to.a.request"></a>
472 <h3>Sending the reply to a request</h3>
474 <a name="Getting.argument.of.invocation"></a>
475 <h2>Getting argument of invocation</h2>
477 <a name="How.to.build.a.plugin"></a>
478 <h2>How to build a plugin</h2>
480 <p>Afb-daemon provides a The packaging of afb-daemon</p>