diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-06-09 14:22:22 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-06-09 14:22:22 +0200 |
commit | 741d4e0505c588f38a64350c1d3c53c74f7ac22c (patch) | |
tree | c5dcbc33bb759d0af7ed44c7b73317b3d7371e1b /doc | |
parent | 16ac46f9966c85f5d8c3b766efb8df1417aa5ce2 (diff) |
documentation: switch to pandoc
Change-Id: I60fcea9d370f5b98efa1e08632f5f9682741b8c1
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/FAQ.html | 34 | ||||
-rw-r--r-- | doc/README.html | 36 | ||||
-rw-r--r-- | doc/afb-application-writing.html | 428 | ||||
-rw-r--r-- | doc/afb-daemon-vocabulary.html | 179 | ||||
-rw-r--r-- | doc/afb-overview.html | 310 | ||||
-rw-r--r-- | doc/afb-plugin-writing.html | 2052 | ||||
-rw-r--r-- | doc/afb-plugin-writing.md | 742 | ||||
-rw-r--r-- | doc/afb-plugins-overview.html | 140 | ||||
-rw-r--r-- | doc/afb-tests-overview.html | 129 | ||||
-rw-r--r-- | doc/afb-tests-overview.md | 9 | ||||
-rwxr-xr-x | doc/updt.sh | 34 |
11 files changed, 1641 insertions, 2452 deletions
diff --git a/doc/FAQ.html b/doc/FAQ.html index 98337815..0cad0b5b 100644 --- a/doc/FAQ.html +++ b/doc/FAQ.html @@ -1,19 +1,31 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="José Bollo"> + <title>Frequently Asked Question about AFB-DAEMON</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Frequently.Asked.Question.about.AFB-DAEMON"></a> -<h1>Frequently Asked Question about AFB-DAEMON</h1> - +<header> +<h1 class="title">Frequently Asked Question about AFB-DAEMON</h1> +<h2 class="author">José Bollo</h2> +<h3 class="date">27 mai 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#frequently-asked-question-about-afb-daemon">Frequently Asked Question about AFB-DAEMON</a></li> +</ul> +</nav> +<h1 id="frequently-asked-question-about-afb-daemon">Frequently Asked Question about AFB-DAEMON</h1> <pre><code>version: 1 Date: 27 mai 2016 -Author: José Bollo -</code></pre> - -<p><ul> - <li><a href="#Frequently.Asked.Question.about.AFB-DAEMON">Frequently Asked Question about AFB-DAEMON</a></li> -</ul></p> +Author: José Bollo</code></pre> </body> </html> diff --git a/doc/README.html b/doc/README.html index 0a7dffa8..a45c16e1 100644 --- a/doc/README.html +++ b/doc/README.html @@ -1,22 +1,28 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <title></title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Inititial.Build"></a> -<h3>Inititial Build</h3> - -<p>mkdir build -cd build -cmake ..</p> - -<a name="Netbeans"></a> -<h3>Netbeans</h3> - -<a name="Copy.nbprojet.at.project.base"></a> -<h1>Copy nbprojet at project base</h1> - +<nav id="TOC"> +<ul> +<li><a href="#inititial-build">Inititial Build</a></li> +<li><a href="#netbeans">Netbeans</a></li> +<li><a href="#copy-nbprojet-at-project-base">Copy nbprojet at project base</a></li> +</ul> +</nav> +<h3 id="inititial-build">Inititial Build</h3> +<p>mkdir build cd build cmake ..</p> +<h3 id="netbeans">Netbeans</h3> +<h1 id="copy-nbprojet-at-project-base">Copy nbprojet at project base</h1> <p>cp -r doc/nbproject.template ./nbproject</p> </body> </html> diff --git a/doc/afb-application-writing.html b/doc/afb-application-writing.html index 3023e2b0..b394e6b9 100644 --- a/doc/afb-application-writing.html +++ b/doc/afb-application-writing.html @@ -1,340 +1,188 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="José Bollo"> + <title>HOWTO WRITE an APPLICATION above AGL FRAMEWORK</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="HOWTO.WRITE.an.APPLICATION.above.AGL.FRAMEWORK"></a> -<h1>HOWTO WRITE an APPLICATION above AGL FRAMEWORK</h1> - +<header> +<h1 class="title">HOWTO WRITE an APPLICATION above AGL FRAMEWORK</h1> +<h2 class="author">José Bollo</h2> +<h3 class="date">09 juin 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#howto-write-an-application-above-agl-framework">HOWTO WRITE an APPLICATION above AGL FRAMEWORK</a><ul> +<li><a href="#programmation-languages-for-applications">Programmation Languages for Applications</a><ul> +<li><a href="#writing-an-html5-application">Writing an HTML5 application</a></li> +<li><a href="#writing-a-qt-application">Writing a Qt application</a></li> +<li><a href="#writing-c-application">Writing "C" application</a></li> +</ul></li> +<li><a href="#handling-sessions-within-applications">Handling sessions within applications</a><ul> +<li><a href="#handling-sessions">Handling sessions</a></li> +<li><a href="#exchanging-tokens">Exchanging tokens</a></li> +<li><a href="#example-of-session-management">Example of session management</a></li> +</ul></li> +<li><a href="#format-of-replies">Format of replies</a><ul> +<li><a href="#field-jtype">Field jtype</a></li> +<li><a href="#field-request">Field request</a></li> +<li><a href="#field-response">Field response</a></li> +<li><a href="#template">Template</a></li> +</ul></li> +</ul></li> +</ul> +</nav> +<h1 id="howto-write-an-application-above-agl-framework">HOWTO WRITE an APPLICATION above AGL FRAMEWORK</h1> <pre><code>version: 1 Date: 09 juin 2016 -Author: José Bollo -</code></pre> - -<p><ul> - <li><a href="#HOWTO.WRITE.an.APPLICATION.above.AGL.FRAMEWORK">HOWTO WRITE an APPLICATION above AGL FRAMEWORK</a> - <ul> - <li><a href="#Programmation.Languages.for.Applications">Programmation Languages for Applications</a> - <ul> - <li><a href="#Writing.an.HTML5.application">Writing an HTML5 application</a></li> - <li><a href="#Writing.a.Qt.application">Writing a Qt application</a></li> - <li><a href="#Writing..C..application">Writing "C" application</a></li> - </ul> - </li> - <li><a href="#Handling.sessions.within.applications">Handling sessions within applications</a> - <ul> - <li><a href="#Handling.sessions">Handling sessions</a></li> - <li><a href="#Exchanging.tokens">Exchanging tokens</a></li> - <li><a href="#Example.of.session.management">Example of session management</a> - <ul> - <li><a href="#Using.curl">Using curl</a></li> - <li><a href="#Using.afb-client-demo">Using afb-client-demo</a> -</li> - </ul> - </li> - </ul> - </li> - <li><a href="#Format.of.replies">Format of replies</a> - <ul> - <li><a href="#Field.jtype">Field jtype</a></li> - <li><a href="#Field.request">Field request</a> - <ul> - <li><a href="#Subfield.request.status">Subfield request.status</a></li> - <li><a href="#Subfield.request.info">Subfield request.info</a></li> - <li><a href="#Subfield.request.token">Subfield request.token</a></li> - <li><a href="#Subfield.request.uuid">Subfield request.uuid</a></li> - <li><a href="#Subfield.request.reqid">Subfield request.reqid</a></li> - </ul> - </li> - <li><a href="#Field.response">Field response</a></li> - <li><a href="#Template">Template</a></li> - </ul> - </li> - </ul> - </li> -</ul></p> - -<a name="Programmation.Languages.for.Applications"></a> -<h2>Programmation Languages for Applications</h2> - -<a name="Writing.an.HTML5.application"></a> -<h3>Writing an HTML5 application</h3> - -<p>Developers of HTML5 applications (client side) can easily create -applications for AGL framework using their preferred -HTML5 framework.</p> - -<p>Developers may also take advantage of powerful server side plugins to improve -application behavior. Server side plugins return an application/json mine-type -and can be accessed though either HTTP or Websockets.</p> - +Author: José Bollo</code></pre> +<h2 id="programmation-languages-for-applications">Programmation Languages for Applications</h2> +<h3 id="writing-an-html5-application">Writing an HTML5 application</h3> +<p>Developers of HTML5 applications (client side) can easily create applications for AGL framework using their preferred HTML5 framework.</p> +<p>Developers may also take advantage of powerful server side plugins to improve application behavior. Server side plugins return an application/json mine-type and can be accessed though either HTTP or Websockets.</p> <p>In a near future, JSON-RPC protocol should be added to complete current x-afb-json1 protocol.</p> - <p>Two examples of HTML5 applications are given:</p> - <ul> -<li><p><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afb-client">afb-client</a> a simple “hello world” application template</p></li> -<li><p><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afm-client">afm-client</a> a simple “Home screen” application template</p></li> +<li><p><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afb-client">afb-client</a> a simple "hello world" application template</p></li> +<li><p><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree;f=afm-client">afm-client</a> a simple "Home screen" application template</p></li> </ul> - - -<a name="Writing.a.Qt.application"></a> -<h3>Writing a Qt application</h3> - +<h3 id="writing-a-qt-application">Writing a Qt application</h3> <p>Writing Qt applications is also supported. Qt offers standard API to send request through HTTP or WebSockets.</p> - <p>It is also possible to write QML applications. A sample QML application [token-websock] is avaliable..</p> - <ul> -<li><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=test/token-websock.qml">token-websock</a> -a simple “hello world” application in QML</li> +<li><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=test/token-websock.qml">token-websock</a> a simple "hello world" application in QML</li> </ul> - - -<a name="Writing..C..application"></a> -<h3>Writing “C” application</h3> - +<h3 id="writing-c-application">Writing "C" application</h3> <p>C applications can use afb-daemon binder through a websocket connection.</p> - -<p>The library <strong>libafbwsc</strong> is provided for C clients that need -to connect with an afb-daemon binder.</p> - -<p>The program <strong>afb-client-demo</strong> is the C example that use -<strong>libafbwsc</strong> library. -Source code is available here -<a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=src/afb-client-demo.c">src/afb-client-demo.c</a>.</p> - -<p>Current implementation relies on libsystemd and file descriptors. -This model might be review in the future to support secure sockets -and free the dependency with libsystemd.</p> - -<a name="Handling.sessions.within.applications"></a> -<h2>Handling sessions within applications</h2> - +<p>The library <strong>libafbwsc</strong> is provided for C clients that need to connect with an afb-daemon binder.</p> +<p>The program <strong>afb-client-demo</strong> is the C example that use <strong>libafbwsc</strong> library. Source code is available here <a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-binder.git;a=blob;f=src/afb-client-demo.c">src/afb-client-demo.c</a>.</p> +<p>Current implementation relies on libsystemd and file descriptors. This model might be review in the future to support secure sockets and free the dependency with libsystemd.</p> +<h2 id="handling-sessions-within-applications">Handling sessions within applications</h2> <p>Applications should understand sessions and tokens management when interacting with afb-daemon binder.</p> - -<p>Applications are communicating with their private binder(afb-daemon) using -a network connection or potentially any other connection channel. While current version -does not yet implement unix domain this feature might be added in a near future. -Developers need to be warn that HTTP protocol is a none connected protocol. This prevents -from using HTTP socket connection to authenticate clients.</p> - -<p>For this reason, the binder should authenticate the application -by using a shared secret. The secret is named “token” and the identification -of client is named “session”.</p> - -<p>The examples <strong>token-websock.qml</strong> and <strong>afb-client</strong> are demonstrating -how authentication and sessions are managed.</p> - -<a name="Handling.sessions"></a> -<h3>Handling sessions</h3> - -<p>Plugins and other binder feature need to keep track of client -instances. This is especially important for plugins running as services -as they may typically have to keep each client’s data separated.</p> - -<p>For HTML5 applications, the web runtime handles the cookie of session -that the binder afb-daemon automatically sets.</p> - -<p>Session identifier can be set using the parameter -<strong>uuid</strong> or <strong>x-afb-uuid</strong> in URI requests. Within current version of the -framework session UUID is supported by both HTTP requests and websocket negotiation.</p> - -<a name="Exchanging.tokens"></a> -<h3>Exchanging tokens</h3> - -<p>At application start, AGL framework communicates a shared secret to both binder -and client application. This initial secret is called the “initial token”.</p> - -<p>For each of its client application, the binder manages a current active -token for session management. This authentication token can be use to restrict -access to some plugin’s methods.</p> - -<p>The token must be included in URI request on HTTP or during websockets -connection using parameter <strong>token</strong> or <strong>x-afb-token</strong>.</p> - +<p>Applications are communicating with their private binder(afb-daemon) using a network connection or potentially any other connection channel. While current version does not yet implement unix domain this feature might be added in a near future. Developers need to be warn that HTTP protocol is a none connected protocol. This prevents from using HTTP socket connection to authenticate clients.</p> +<p>For this reason, the binder should authenticate the application by using a shared secret. The secret is named "token" and the identification of client is named "session".</p> +<p>The examples <strong>token-websock.qml</strong> and <strong>afb-client</strong> are demonstrating how authentication and sessions are managed.</p> +<h3 id="handling-sessions">Handling sessions</h3> +<p>Plugins and other binder feature need to keep track of client instances. This is especially important for plugins running as services as they may typically have to keep each client's data separated.</p> +<p>For HTML5 applications, the web runtime handles the cookie of session that the binder afb-daemon automatically sets.</p> +<p>Session identifier can be set using the parameter <strong>uuid</strong> or <strong>x-afb-uuid</strong> in URI requests. Within current version of the framework session UUID is supported by both HTTP requests and websocket negotiation.</p> +<h3 id="exchanging-tokens">Exchanging tokens</h3> +<p>At application start, AGL framework communicates a shared secret to both binder and client application. This initial secret is called the "initial token".</p> +<p>For each of its client application, the binder manages a current active token for session management. This authentication token can be use to restrict access to some plugin's methods.</p> +<p>The token must be included in URI request on HTTP or during websockets connection using parameter <strong>token</strong> or <strong>x-afb-token</strong>.</p> <p>To ensure security, tokens must be refreshed periodically.</p> - -<a name="Example.of.session.management"></a> -<h3>Example of session management</h3> - +<h3 id="example-of-session-management">Example of session management</h3> <p>In following examples, we suppose that <strong>afb-daemon</strong> is launched with something equivalent to:</p> - -<pre><code>$ afb-daemon --port=1234 --token=123456 [...] -</code></pre> - +<pre><code>$ afb-daemon --port=1234 --token=123456 [...]</code></pre> <p>making the expectation that <strong>AuthLogin</strong> plugin is requested as default.</p> - -<a name="Using.curl"></a> -<h4>Using curl</h4> - +<h4 id="using-curl">Using curl</h4> <p>First, connects with the initial token, 123456:</p> - <pre><code>$ curl http://localhost:1234/api/auth/connect?token=123456 { - "jtype": "afb-reply", - "request": { - "status": "success", - "token": "0aef6841-2ddd-436d-b961-ae78da3b5c5f", - "uuid": "850c4594-1be1-4e9b-9fcc-38cc3e6ff015" + "jtype": "afb-reply", + "request": { + "status": "success", + "token": "0aef6841-2ddd-436d-b961-ae78da3b5c5f", + "uuid": "850c4594-1be1-4e9b-9fcc-38cc3e6ff015" }, - "response": {"token": "A New Token and Session Context Was Created"} -} -</code></pre> - -<p>It returns an answer containing session UUID, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015, -and a refreshed token, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015.</p> - + "response": {"token": "A New Token and Session Context Was Created"} +}</code></pre> +<p>It returns an answer containing session UUID, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015, and a refreshed token, 850c4594-1be1-4e9b-9fcc-38cc3e6ff015.</p> <p>Check if session and token is valid:</p> - -<pre><code>$ curl http://localhost:1234/api/auth/check?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 +<pre><code>$ curl http://localhost:1234/api/auth/check?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 { - "jtype": "afb-reply", - "request": {"status":"success"}, - "response": {"isvalid":true} -} -</code></pre> - + "jtype": "afb-reply", + "request": {"status":"success"}, + "response": {"isvalid":true} +}</code></pre> <p>Refresh the token:</p> - -<pre><code>$ curl http://localhost:1234/api/auth/refresh?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 +<pre><code>$ curl http://localhost:1234/api/auth/refresh?token=0aef6841-2ddd-436d-b961-ae78da3b5c5f\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 { - "jtype": "afb-reply", - "request": { - "status":"success", - "token":"b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9" + "jtype": "afb-reply", + "request": { + "status":"success", + "token":"b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9" }, - "response": {"token":"Token was refreshed"} -} -</code></pre> - + "response": {"token":"Token was refreshed"} +}</code></pre> <p>Close the session:</p> - -<pre><code>curl http://localhost:1234/api/auth/logout?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 +<pre><code>curl http://localhost:1234/api/auth/logout?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 { - "jtype": "afb-reply", - "request": {"status": "success"}, - "response": {"info":"Token and all resources are released"} -} -</code></pre> - + "jtype": "afb-reply", + "request": {"status": "success"}, + "response": {"info":"Token and all resources are released"} +}</code></pre> <p>Checking on closed session for uuid should be refused:</p> - -<pre><code>curl http://localhost:1234/api/auth/check?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 +<pre><code>curl http://localhost:1234/api/auth/check?token=b8ec3ec3-6ffe-448c-9a6c-efda69ad7bd9\&uuid=850c4594-1be1-4e9b-9fcc-38cc3e6ff015 { - "jtype": "afb-reply", - "request": { - "status": "failed", - "info": "invalid token's identity" + "jtype": "afb-reply", + "request": { + "status": "failed", + "info": "invalid token's identity" } -} -</code></pre> - -<a name="Using.afb-client-demo"></a> -<h4>Using afb-client-demo</h4> - +}</code></pre> +<h4 id="using-afb-client-demo">Using afb-client-demo</h4> <p>Here is an example of exchange using <strong>afb-client-demo</strong>:</p> - <pre><code>$ afb-client-demo ws://localhost:1234/api?token=123456 auth connect -ON-REPLY 1:auth/connect: {"jtype":"afb-reply","request":{"status":"success", - "token":"63f71a29-8b52-4f9b-829f-b3028ba46b68","uuid":"5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1"}, - "response":{"token":"A New Token and Session Context Was Created"}} +ON-REPLY 1:auth/connect: {"jtype":"afb-reply","request":{"status":"success", + "token":"63f71a29-8b52-4f9b-829f-b3028ba46b68","uuid":"5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1"}, + "response":{"token":"A New Token and Session Context Was Created"}} auth check -ON-REPLY 2:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} +ON-REPLY 2:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} auth refresh -ON-REPLY 4:auth/refresh: {"jtype":"afb-reply","request":{"status":"success", - "token":"8b8ba8f4-1b0c-48fa-962d-4a00a8c9157e"},"response":{"token":"Token was refreshed"}} +ON-REPLY 4:auth/refresh: {"jtype":"afb-reply","request":{"status":"success", + "token":"8b8ba8f4-1b0c-48fa-962d-4a00a8c9157e"},"response":{"token":"Token was refreshed"}} auth check -ON-REPLY 5:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} +ON-REPLY 5:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} auth refresh -ON-REPLY 6:auth/refresh: {"jtype":"afb-reply","request":{"status":"success", - "token":"e83b36f8-d945-463d-b983-5d8ed73ba529"},"response":{"token":"Token was refreshed"}} -</code></pre> - +ON-REPLY 6:auth/refresh: {"jtype":"afb-reply","request":{"status":"success", + "token":"e83b36f8-d945-463d-b983-5d8ed73ba529"},"response":{"token":"Token was refreshed"}}</code></pre> <p>After closing connection, reconnect as here after:</p> - -<pre><code>$ afb-client-demo ws://localhost:1234/api?token=e83b36f8-d945-463d-b983-5d8ed73ba529&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 auth check -ON-REPLY 1:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} -</code></pre> - +<pre><code>$ afb-client-demo ws://localhost:1234/api?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 auth check +ON-REPLY 1:auth/check: {"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}</code></pre> <p>Same connection check using <strong>curl</strong>:</p> - -<pre><code>$ curl http://localhost:1234/api/auth/check?token=e83b36f8-d945-463d-b983-5d8ed73ba529&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 -{"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}} -</code></pre> - -<a name="Format.of.replies"></a> -<h2>Format of replies</h2> - +<pre><code>$ curl http://localhost:1234/api/auth/check?token=e83b36f8-d945-463d-b983-5d8ed73ba529\&uuid=5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1 +{"jtype":"afb-reply","request":{"status":"success"},"response":{"isvalid":true}}</code></pre> +<h2 id="format-of-replies">Format of replies</h2> <p>Replies use javascript object returned as serialized JSON.</p> - -<p>This object contains at least 2 mandatory fields of name <strong>jtype</strong> and <strong>request</strong> -and one optional field of name <strong>response</strong>.</p> - -<a name="Field.jtype"></a> -<h3>Field jtype</h3> - -<p>The field <strong>jtype</strong> must have a value of type string equal to <strong>“afb-reply”</strong>.</p> - -<a name="Field.request"></a> -<h3>Field request</h3> - -<p>The field <strong>request</strong> must have a value of type object. This request object -has at least one field named <strong>status</strong> and four optional fields named -<strong>info</strong>, <strong>token</strong>, <strong>uuid</strong>, <strong>reqid</strong>.</p> - -<a name="Subfield.request.status"></a> -<h4>Subfield request.status</h4> - -<p><strong>status</strong> must have a value of type string. This string is equal to <strong>“success”</strong> -only in case of success.</p> - -<a name="Subfield.request.info"></a> -<h4>Subfield request.info</h4> - +<p>This object contains at least 2 mandatory fields of name <strong>jtype</strong> and <strong>request</strong> and one optional field of name <strong>response</strong>.</p> +<h3 id="field-jtype">Field jtype</h3> +<p>The field <strong>jtype</strong> must have a value of type string equal to <strong>"afb-reply"</strong>.</p> +<h3 id="field-request">Field request</h3> +<p>The field <strong>request</strong> must have a value of type object. This request object has at least one field named <strong>status</strong> and four optional fields named <strong>info</strong>, <strong>token</strong>, <strong>uuid</strong>, <strong>reqid</strong>.</p> +<h4 id="subfield-request.status">Subfield request.status</h4> +<p><strong>status</strong> must have a value of type string. This string is equal to <strong>"success"</strong> only in case of success.</p> +<h4 id="subfield-request.info">Subfield request.info</h4> <p><strong>info</strong> is of type string and represent optional information added to the reply.</p> - -<a name="Subfield.request.token"></a> -<h4>Subfield request.token</h4> - -<p><strong>token</strong> is of type string. It is sent either at session creation -or when the token is refreshed.</p> - -<a name="Subfield.request.uuid"></a> -<h4>Subfield request.uuid</h4> - +<h4 id="subfield-request.token">Subfield request.token</h4> +<p><strong>token</strong> is of type string. It is sent either at session creation or when the token is refreshed.</p> +<h4 id="subfield-request.uuid">Subfield request.uuid</h4> <p><strong>uuid</strong> is of type string. It is sent at session creation.</p> - -<a name="Subfield.request.reqid"></a> -<h4>Subfield request.reqid</h4> - -<p><strong>reqid</strong> is of type string. It is sent in response to HTTP requests -that added a parameter of name <strong>reqid</strong> or <strong>x-afb-reqid</strong> at request time. -Value returns in the reply has the exact same value as the one received in the request.</p> - -<a name="Field.response"></a> -<h3>Field response</h3> - +<h4 id="subfield-request.reqid">Subfield request.reqid</h4> +<p><strong>reqid</strong> is of type string. It is sent in response to HTTP requests that added a parameter of name <strong>reqid</strong> or <strong>x-afb-reqid</strong> at request time. Value returns in the reply has the exact same value as the one received in the request.</p> +<h3 id="field-response">Field response</h3> <p>This field response optionally contains an object returned when request succeeded.</p> - -<a name="Template"></a> -<h3>Template</h3> - +<h3 id="template">Template</h3> <p>This is a template of replies:</p> - <pre><code>{ - "jtype": "afb-reply", - "request": { - "status": "success", - "info": "informationnal text", - "token": "e83b36f8-d945-463d-b983-5d8ed73ba52", - "uuid": "5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1", - "reqid": "application-generated-id-23456" - }, - "response": ....any response object.... -} -</code></pre> + "jtype": "afb-reply", + "request": { + "status": "success", + "info": "informationnal text", + "token": "e83b36f8-d945-463d-b983-5d8ed73ba52", + "uuid": "5fcc3f3d-4b84-4fc7-ba66-2d8bd34ae7d1", + "reqid": "application-generated-id-23456" + }, + "response": ....any response object.... +}</code></pre> </body> </html> diff --git a/doc/afb-daemon-vocabulary.html b/doc/afb-daemon-vocabulary.html index fadd1dee..d2d4f512 100644 --- a/doc/afb-daemon-vocabulary.html +++ b/doc/afb-daemon-vocabulary.html @@ -1,131 +1,78 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="José Bollo"> + <title>Vocabulary for AFB-DAEMON</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Vocabulary.for.AFB-DAEMON"></a> -<h1>Vocabulary for AFB-DAEMON</h1> - +<header> +<h1 class="title">Vocabulary for AFB-DAEMON</h1> +<h2 class="author">José Bollo</h2> +<h3 class="date">27 mai 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#vocabulary-for-afb-daemon">Vocabulary for AFB-DAEMON</a><ul> +<li><a href="#event">Event</a></li> +<li><a href="#level-of-assurance-loa">Level of assurance (LOA)</a></li> +<li><a href="#plugin">Plugin</a></li> +<li><a href="#request">Request</a></li> +<li><a href="#replyresponse">Reply/Response</a></li> +<li><a href="#service">Service</a></li> +<li><a href="#session">Session</a></li> +<li><a href="#token">Token</a></li> +<li><a href="#uuid">UUID</a></li> +<li><a href="#x-afb-reqid">x-afb-reqid</a></li> +<li><a href="#x-afb-token">x-afb-token</a></li> +<li><a href="#x-afb-uuid">x-afb-uuid</a></li> +</ul></li> +</ul> +</nav> +<h1 id="vocabulary-for-afb-daemon">Vocabulary for AFB-DAEMON</h1> <pre><code>version: 1 Date: 27 mai 2016 -Author: José Bollo -</code></pre> - -<p><ul> - <li><a href="#Vocabulary.for.AFB-DAEMON">Vocabulary for AFB-DAEMON</a> - <ul> - <li><a href="#Event">Event</a></li> - <li><a href="#Level.of.assurance..LOA.">Level of assurance (LOA)</a></li> - <li><a href="#Plugin">Plugin</a></li> - <li><a href="#Request">Request</a></li> - <li><a href="#Reply.Response">Reply/Response</a></li> - <li><a href="#Service">Service</a></li> - <li><a href="#Session">Session</a></li> - <li><a href="#Token">Token</a></li> - <li><a href="#UUID">UUID</a></li> - <li><a href="#x-afb-reqid">x-afb-reqid</a></li> - <li><a href="#x-afb-token">x-afb-token</a></li> - <li><a href="#x-afb-uuid">x-afb-uuid</a></li> - </ul> - </li> -</ul></p> - -<a name="Event"></a> -<h2>Event</h2> - -<p>Message with data propagated from the services to the client and not expecting -any reply.</p> - +Author: José Bollo</code></pre> +<h2 id="event">Event</h2> +<p>Message with data propagated from the services to the client and not expecting any reply.</p> <p>The current implementation allows to widely broadcast events to all clients.</p> - -<a name="Level.of.assurance..LOA."></a> -<h2>Level of assurance (LOA)</h2> - -<p>This level that can be from 0 to 3 represent the level of -assurance that the services can expect from the session.</p> - -<p>The exact definition of the meaning of this levels and of -how to use it remains to be achived.</p> - -<a name="Plugin"></a> -<h2>Plugin</h2> - -<p>A shared library object intended to be plug to an afb-daemon instance -to implement an API.</p> - -<a name="Request"></a> -<h2>Request</h2> - -<p>A request is an invocation by a client to a method of a plugin using a message -transfered through some protocol: HTTP, WebSocket, DBUS… served by afb-daemon</p> - -<a name="Reply.Response"></a> -<h2>Reply/Response</h2> - +<h2 id="level-of-assurance-loa">Level of assurance (LOA)</h2> +<p>This level that can be from 0 to 3 represent the level of assurance that the services can expect from the session.</p> +<p>The exact definition of the meaning of this levels and of how to use it remains to be achived.</p> +<h2 id="plugin">Plugin</h2> +<p>A shared library object intended to be plug to an afb-daemon instance to implement an API.</p> +<h2 id="request">Request</h2> +<p>A request is an invocation by a client to a method of a plugin using a message transfered through some protocol: HTTP, WebSocket, DBUS... served by afb-daemon</p> +<h2 id="replyresponse">Reply/Response</h2> <p>This is a message sent to client as the result of the request.</p> - -<a name="Service"></a> -<h2>Service</h2> - -<p>Service are made of plugins runnning by their side on their binder. -It can serve many client. Each one being attached to one session.</p> - -<p>The framework establishes the connection between the services and -the clients. Using DBus currently.</p> - -<a name="Session"></a> -<h2>Session</h2> - -<p>A session is meant to be the unic context of an instance of client, -identifying that instance across requests.</p> - +<h2 id="service">Service</h2> +<p>Service are made of plugins runnning by their side on their binder. It can serve many client. Each one being attached to one session.</p> +<p>The framework establishes the connection between the services and the clients. Using DBus currently.</p> +<h2 id="session">Session</h2> +<p>A session is meant to be the unic context of an instance of client, identifying that instance across requests.</p> <p>Each session has an identifier. Session identifier generated by afb-daemon are UUIDs.</p> - -<p>Internally, afb-daemon offers a mechanism to attach data to sessions. -When the session is closed or disappears, the data attached to that session -are freed.</p> - -<a name="Token"></a> -<h2>Token</h2> - +<p>Internally, afb-daemon offers a mechanism to attach data to sessions. When the session is closed or disappears, the data attached to that session are freed.</p> +<h2 id="token">Token</h2> <p>The token is an identifier that the the client must give to be authentificated.</p> - -<p>At start, afb-daemon get an initial token. This initial token must be presented -incoming client to be authentificated.</p> - +<p>At start, afb-daemon get an initial token. This initial token must be presented incoming client to be authentificated.</p> <p>A token is valid only for a period.</p> - -<p>The token must be renewed periodically. When the token is renewed, afb-daemon -sends the new token to the client.</p> - +<p>The token must be renewed periodically. When the token is renewed, afb-daemon sends the new token to the client.</p> <p>Tokens generated by afb-daemon are UUIDs.</p> - -<a name="UUID"></a> -<h2>UUID</h2> - +<h2 id="uuid">UUID</h2> <p>It stand for Universal Unic IDentifier.</p> - -<p>Its is designed to create identifier in a way that avoid has much as possible conflicts. -It means that if two differents instance create a UUID, the probability that they create the same UUID is very low, near to zero.</p> - -<a name="x-afb-reqid"></a> -<h2>x-afb-reqid</h2> - -<p>Argument name that can be used with HTTP request. -When this argument is given, it is automatically added to the “request” object of the -answer.</p> - -<a name="x-afb-token"></a> -<h2>x-afb-token</h2> - -<p>Argument name for giving the token without ambiguity. -You can also use the name <strong>token</strong> but it may conflicts with other arguments.</p> - -<a name="x-afb-uuid"></a> -<h2>x-afb-uuid</h2> - -<p>Argument name for giving explicitely the session identifier without ambiguity. -You can also use the name <strong>uuid</strong> but it may conflicts with other arguments.</p> +<p>Its is designed to create identifier in a way that avoid has much as possible conflicts. It means that if two differents instance create a UUID, the probability that they create the same UUID is very low, near to zero.</p> +<h2 id="x-afb-reqid">x-afb-reqid</h2> +<p>Argument name that can be used with HTTP request. When this argument is given, it is automatically added to the "request" object of the answer.</p> +<h2 id="x-afb-token">x-afb-token</h2> +<p>Argument name for giving the token without ambiguity. You can also use the name <strong>token</strong> but it may conflicts with other arguments.</p> +<h2 id="x-afb-uuid">x-afb-uuid</h2> +<p>Argument name for giving explicitely the session identifier without ambiguity. You can also use the name <strong>uuid</strong> but it may conflicts with other arguments.</p> </body> </html> diff --git a/doc/afb-overview.html b/doc/afb-overview.html index 839d606c..e8050f9a 100644 --- a/doc/afb-overview.html +++ b/doc/afb-overview.html @@ -1,50 +1,51 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="José Bollo"> + <title>Overview of AFB-DAEMON</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Overview.of.AFB-DAEMON"></a> -<h1>Overview of AFB-DAEMON</h1> - +<header> +<h1 class="title">Overview of AFB-DAEMON</h1> +<h2 class="author">José Bollo</h2> +<h3 class="date">30 mai 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#overview-of-afb-daemon">Overview of AFB-DAEMON</a><ul> +<li><a href="#roles-of-afb-daemon">Roles of afb-daemon</a></li> +<li><a href="#use-cases-of-the-binder-afb-daemon">Use cases of the binder afb-daemon</a><ul> +<li><a href="#remotely-running-application">Remotely running application</a></li> +<li><a href="#adding-native-features-to-html5qml-applications">Adding native features to HTML5/QML applications</a></li> +<li><a href="#offering-services-to-the-system">Offering services to the system</a></li> +</ul></li> +<li><a href="#the-plugins-of-the-binder-afb-daemon">The plugins of the binder afb-daemon</a></li> +<li><a href="#launching-the-binder-afb-daemon">Launching the binder afb-daemon</a></li> +<li><a href="#future-development-of-afb-daemon">Future development of afb-daemon</a></li> +</ul></li> +</ul> +</nav> +<h1 id="overview-of-afb-daemon">Overview of AFB-DAEMON</h1> <pre><code>version: 1 Date: 30 mai 2016 -Author: José Bollo -</code></pre> - -<p><ul> - <li><a href="#Overview.of.AFB-DAEMON">Overview of AFB-DAEMON</a> - <ul> - <li><a href="#Roles.of.afb-daemon">Roles of afb-daemon</a></li> - <li><a href="#Use.cases.of.the.binder.afb-daemon">Use cases of the binder afb-daemon</a> - <ul> - <li><a href="#Remotely.running.application">Remotely running application</a></li> - <li><a href="#Adding.native.features.to.HTML5.QML.applications">Adding native features to HTML5/QML applications</a></li> - <li><a href="#Offering.services.to.the.system">Offering services to the system</a></li> - </ul> - </li> - <li><a href="#The.plugins.of.the.binder.afb-daemon">The plugins of the binder afb-daemon</a></li> - <li><a href="#Launching.the.binder.afb-daemon">Launching the binder afb-daemon</a></li> - <li><a href="#Future.development.of.afb-daemon">Future development of afb-daemon</a></li> - </ul> - </li> -</ul></p> - -<a name="Roles.of.afb-daemon"></a> -<h2>Roles of afb-daemon</h2> - -<p>The name <strong>afb-daemon</strong> stands for <em>Application -Framework Binder Daemon</em>. That is why afb-daemon -is also named <strong><em>the binder</em></strong>.</p> - -<p><strong>Afb-daemon</strong> is in charge to bind one instance of -an application to the AGL framework and AGL system.</p> - -<p>On the following figure, you can use a typical use -of afb-daemon:</p> - -<p><a id="binder-fig-basis"><h4>Figure: binder afb-daemon, basis</h4></a></p> - +Author: José Bollo</code></pre> +<h2 id="roles-of-afb-daemon">Roles of afb-daemon</h2> +<p>The name <strong>afb-daemon</strong> stands for <em>Application Framework Binder Daemon</em>. That is why afb-daemon is also named <strong><em>the binder</em></strong>.</p> +<p><strong>Afb-daemon</strong> is in charge to bind one instance of an application to the AGL framework and AGL system.</p> +<p>On the following figure, you can use a typical use of afb-daemon:</p> +<a id="binder-fig-basis"> +<h4> +Figure: binder afb-daemon, basis +</h4> +<p></a></p> <pre><code>. . . . . . . . . . . . . . . . . . . . . . . . . . . Isolated security context . . . @@ -65,43 +66,24 @@ of afb-daemon:</p> . . . . . . . . . . . . | . . . . . . . . . . . . . | v - AGL SYSTEM -</code></pre> - -<p>The application and its companion binder run in secured and isolated -environment set for them. Applications are intended to access to AGL -system through the binder.</p> - + AGL SYSTEM</code></pre> +<p>The application and its companion binder run in secured and isolated environment set for them. Applications are intended to access to AGL system through the binder.</p> <p>The binder afb-daemon serves multiple purposes:</p> - -<ol> +<ol type="1"> <li><p>It acts as a gateway for the application to access the system;</p></li> <li><p>It acts as an HTTP server for serving files to HTML5 applications;</p></li> -<li><p>It allows HTML5 applications to have native extensions subject -to security enforcement for accessing hardware ressources or -for speeding parts of algorithm.</p></li> +<li><p>It allows HTML5 applications to have native extensions subject to security enforcement for accessing hardware ressources or for speeding parts of algorithm.</p></li> </ol> - - -<a name="Use.cases.of.the.binder.afb-daemon"></a> -<h2>Use cases of the binder afb-daemon</h2> - -<p>This section tries to give a better understanding of the binder -usage through several use cases.</p> - -<a name="Remotely.running.application"></a> -<h3>Remotely running application</h3> - -<p>One of the most interresting aspect of using the binder afb-daemon -is the ability to run applications remotely. This feature is -possible because the binder afb-daemon implements native web -protocols.</p> - -<p>So the <a href="#binder-fig-1">figure binder, basis</a> would become -when the application is run remotely:</p> - -<p><a id="binder-fig-remote"><h4>Figure: binder afb-daemon and remotely running application</h4></a></p> - +<h2 id="use-cases-of-the-binder-afb-daemon">Use cases of the binder afb-daemon</h2> +<p>This section tries to give a better understanding of the binder usage through several use cases.</p> +<h3 id="remotely-running-application">Remotely running application</h3> +<p>One of the most interresting aspect of using the binder afb-daemon is the ability to run applications remotely. This feature is possible because the binder afb-daemon implements native web protocols.</p> +<p>So the <a href="#binder-fig-1">figure binder, basis</a> would become when the application is run remotely:</p> +<a id="binder-fig-remote"> +<h4> +Figure: binder afb-daemon and remotely running application +</h4> +<p></a></p> <pre><code> +------------------------------+ | | | A P P L I C A T I O N | @@ -131,35 +113,22 @@ when the application is run remotely:</p> . . . . . . . . . . . . . . | . . . . . . . . . . . . . . | v - AGL SYSTEM -</code></pre> - -<a name="Adding.native.features.to.HTML5.QML.applications"></a> -<h3>Adding native features to HTML5/QML applications</h3> - -<p>Applications can provide with their packaged delivery a plugin. -That plugin will be instanciated for each application instance. -The methods of the plugin will be accessible by applications and -will be excuted within the security context.</p> - -<a name="Offering.services.to.the.system"></a> -<h3>Offering services to the system</h3> - -<p>It is possible to run the binder afb-daemon as a daemon that provides the -API of its plugins.</p> - + AGL SYSTEM</code></pre> +<h3 id="adding-native-features-to-html5qml-applications">Adding native features to HTML5/QML applications</h3> +<p>Applications can provide with their packaged delivery a plugin. That plugin will be instanciated for each application instance. The methods of the plugin will be accessible by applications and will be excuted within the security context.</p> +<h3 id="offering-services-to-the-system">Offering services to the system</h3> +<p>It is possible to run the binder afb-daemon as a daemon that provides the API of its plugins.</p> <p>This will be used for:</p> - -<ol> +<ol type="1"> <li><p>offering common APIs</p></li> -<li><p>provide application’s services (services provided as application)</p></li> +<li><p>provide application's services (services provided as application)</p></li> </ol> - - <p>In that case, the figure showing the whole aspects is</p> - -<p><a id="binder-fig-remote"><h4>Figure: binder afb-daemon for services</h4></a></p> - +<a id="binder-fig-remote"> +<h4> +Figure: binder afb-daemon for services +</h4> +<p></a></p> <pre><code>. . . . . . . . . . . . . . . . . . . . . . . Isolated security context application . . . @@ -195,176 +164,151 @@ API of its plugins.</p> . +------------------------------------+ . . +------------------------------------+ . . . . . . Isolated security context B . . Isolated security context C . -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . -</code></pre> - -<p>For this case, the binder afb-daemon takes care to attribute one single session -context to each client instance. It allows plugins to store and retrieve data -associated to each of its client.</p> - -<a name="The.plugins.of.the.binder.afb-daemon"></a> -<h2>The plugins of the binder afb-daemon</h2> - -<p>The binder can instanciate plugins. The primary use of plugins -is to add native methods that can be accessed by applications -written with any language through web technologies ala JSON RPC.</p> - +. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .</code></pre> +<p>For this case, the binder afb-daemon takes care to attribute one single session context to each client instance. It allows plugins to store and retrieve data associated to each of its client.</p> +<h2 id="the-plugins-of-the-binder-afb-daemon">The plugins of the binder afb-daemon</h2> +<p>The binder can instanciate plugins. The primary use of plugins is to add native methods that can be accessed by applications written with any language through web technologies ala JSON RPC.</p> <p>This simple idea is declined to serves multiple purposes:</p> - -<ol> +<ol type="1"> <li><p>add native feature to applications</p></li> <li><p>add common API available by any applications</p></li> <li><p>provide customers services</p></li> </ol> - - -<p>A specific document explains how to write an afb-daemon binder plugin: -<a href="afb-plugin-writing.html">HOWTO WRITE a PLUGIN for AFB-DAEMON</a></p> - -<a name="Launching.the.binder.afb-daemon"></a> -<h2>Launching the binder afb-daemon</h2> - +<p>A specific document explains how to write an afb-daemon binder plugin: <a href="afb-plugin-writing.html">HOWTO WRITE a PLUGIN for AFB-DAEMON</a></p> +<h2 id="launching-the-binder-afb-daemon">Launching the binder afb-daemon</h2> <p>The launch options for binder <strong>afb-daemon</strong> are:</p> - <pre><code> --help - Prints help with available options + Prints help with available options --version - Display version and copyright + Display version and copyright --verbose - Increases the verbosity, can be repeated + Increases the verbosity, can be repeated --port=xxxx - HTTP listening TCP port [default 1234] + HTTP listening TCP port [default 1234] --rootdir=xxxx - HTTP Root Directory [default $AFBDIR or else $HOME/.AFB] + HTTP Root Directory [default $AFBDIR or else $HOME/.AFB] --rootbase=xxxx - Angular Base Root URL [default /opa] + Angular Base Root URL [default /opa] - This is used for any application of kind OPA (one page application). - When set, any missing document whose url has the form /opa/zzz - is translated to /opa/#!zzz + This is used for any application of kind OPA (one page application). + When set, any missing document whose url has the form /opa/zzz + is translated to /opa/#!zzz --rootapi=xxxx - HTML Root API URL [default /api] + HTML Root API URL [default /api] - The plugins are available within that url. + The plugins are available within that url. --alias=xxxx - Maps a path located anywhere in the file system to the - a subdirectory. The syntax for mapping a PATH to the - subdirectory NAME is: --alias=/NAME:PATH. + Maps a path located anywhere in the file system to the + a subdirectory. The syntax for mapping a PATH to the + subdirectory NAME is: --alias=/NAME:PATH. - Example: --alias=/icons:/usr/share/icons maps the - content of /usr/share/icons within the subpath /icons. + Example: --alias=/icons:/usr/share/icons maps the + content of /usr/share/icons within the subpath /icons. - This option can be repeated. + This option can be repeated. --apitimeout=xxxx - Plugin API timeout in seconds [default 20] + Plugin API timeout in seconds [default 20] - Defines how many seconds maximum a method is allowed to run. - 0 means no limit. + Defines how many seconds maximum a method is allowed to run. + 0 means no limit. --cntxtimeout=xxxx - Client Session Timeout in seconds [default 3600] + Client Session Timeout in seconds [default 3600] --cache-eol=xxxx - Client cache end of live [default 100000 that is 27,7 hours] + Client cache end of live [default 100000 that is 27,7 hours] --sessiondir=xxxx - Sessions file path [default rootdir/sessions] + Sessions file path [default rootdir/sessions] --session-max=xxxx - Maximum count of simultaneous sessions [default 10] + Maximum count of simultaneous sessions [default 10] --ldpaths=xxxx - Load Plugins from given paths separated by colons - as for dir1:dir2:plugin1.so:... [default = $libdir/afb] + Load Plugins from given paths separated by colons + as for dir1:dir2:plugin1.so:... [default = $libdir/afb] - You can mix path to directories and to plugins. - The sub-directories of the given directories are searched - recursively. + You can mix path to directories and to plugins. + The sub-directories of the given directories are searched + recursively. - The plugins are the files terminated by '.so' (the extension - so denotes shared object) that contain the public entry symbol. + The plugins are the files terminated by '.so' (the extension + so denotes shared object) that contain the public entry symbol. --plugin=xxxx - Load the plugin of given path. + Load the plugin of given path. --token=xxxx - Initial Secret token to authenticate. + Initial Secret token to authenticate. - If not set, no client can authenticate. + If not set, no client can authenticate. - If set to the empty string, then any initial token is accepted. + If set to the empty string, then any initial token is accepted. --mode=xxxx - Set the mode: either local, remote or global. + Set the mode: either local, remote or global. - The mode indicate if the application is run locally on the host - or remotely through network. + The mode indicate if the application is run locally on the host + or remotely through network. --readyfd=xxxx - Set the #fd to signal when ready + Set the #fd to signal when ready - If set, the binder afb-daemon will write "READY=1\n" on the file - descriptor whose number if given (/proc/self/fd/xxx). + If set, the binder afb-daemon will write "READY=1\n" on the file + descriptor whose number if given (/proc/self/fd/xxx). --dbus-client=xxxx - Transparent binding to a binder afb-daemon service through dbus. + Transparent binding to a binder afb-daemon service through dbus. - It creates an API of name xxxx that is implemented remotely - and queried via DBUS. + It creates an API of name xxxx that is implemented remotely + and queried via DBUS. --dbus-server=xxxx - Provides a binder afb-daemon service through dbus. + Provides a binder afb-daemon service through dbus. - The name xxxx must be the name of an API defined by a plugin. - This API is exported through DBUS. + The name xxxx must be the name of an API defined by a plugin. + This API is exported through DBUS. --foreground - Get all in foreground mode (default) + Get all in foreground mode (default) --daemon - Get all in background mode -</code></pre> - -<a name="Future.development.of.afb-daemon"></a> -<h2>Future development of afb-daemon</h2> - + Get all in background mode</code></pre> +<h2 id="future-development-of-afb-daemon">Future development of afb-daemon</h2> <ul> <li><p>The binder afb-daemon would launch the applications directly.</p></li> -<li><p>The current setting of mode (local/remote/global) might be reworked to a -mechanism for querying configuration variables.</p></li> -<li><p>Implements “one-shot” initial token. It means that after its first -authenticated use, the initial token is removed and no client can connect -anymore.</p></li> +<li><p>The current setting of mode (local/remote/global) might be reworked to a mechanism for querying configuration variables.</p></li> +<li><p>Implements "one-shot" initial token. It means that after its first authenticated use, the initial token is removed and no client can connect anymore.</p></li> <li><p>Creates some intrinsic APIs.</p></li> <li><p>Make the service connection using WebSocket not DBUS.</p></li> <li><p>Management of targetted events.</p></li> diff --git a/doc/afb-plugin-writing.html b/doc/afb-plugin-writing.html index 84a23573..eaa7a370 100644 --- a/doc/afb-plugin-writing.html +++ b/doc/afb-plugin-writing.html @@ -1,1255 +1,805 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="José Bollo"> + <title>HOWTO WRITE a PLUGIN for AFB-DAEMON</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <style type="text/css"> +table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode { + margin: 0; padding: 0; vertical-align: baseline; border: none; } +table.sourceCode { width: 100%; line-height: 100%; } +td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; } +td.sourceCode { padding-left: 5px; } +code > span.kw { color: #007020; font-weight: bold; } +code > span.dt { color: #902000; } +code > span.dv { color: #40a070; } +code > span.bn { color: #40a070; } +code > span.fl { color: #40a070; } +code > span.ch { color: #4070a0; } +code > span.st { color: #4070a0; } +code > span.co { color: #60a0b0; font-style: italic; } +code > span.ot { color: #007020; } +code > span.al { color: #ff0000; font-weight: bold; } +code > span.fu { color: #06287e; } +code > span.er { color: #ff0000; font-weight: bold; } + </style> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON"></a> -<h1>HOWTO WRITE a PLUGIN for AFB-DAEMON</h1> - +<header> +<h1 class="title">HOWTO WRITE a PLUGIN for AFB-DAEMON</h1> +<h2 class="author">José Bollo</h2> +<h3 class="date">30 mai 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#howto-write-a-plugin-for-afb-daemon">HOWTO WRITE a PLUGIN for AFB-DAEMON</a><ul> +<li><a href="#summary">Summary</a><ul> +<li><a href="#nature-of-a-plugin">Nature of a plugin</a></li> +<li><a href="#class-of-plugins">Class of plugins</a></li> +<li><a href="#live-cycle-of-plugins-within-afb-daemon">Live cycle of plugins within afb-daemon</a></li> +<li><a href="#plugin-contend">Plugin Contend</a></li> +</ul></li> +<li><a href="#the-tic-tac-toe-example">The Tic-Tac-Toe example</a></li> +<li><a href="#dependencies-when-compiling">Dependencies when compiling</a></li> +<li><a href="#header-files-to-include">Header files to include</a></li> +<li><a href="#choosing-names">Choosing names</a><ul> +<li><a href="#names-for-api-plugin">Names for API (plugin)</a></li> +<li><a href="#names-for-methods">Names for methods</a></li> +<li><a href="#names-for-arguments">Names for arguments</a></li> +<li><a href="#forging-names-widely-available">Forging names widely available</a></li> +</ul></li> +<li><a href="#writing-a-synchronous-method-implementation">Writing a synchronous method implementation</a><ul> +<li><a href="#the-incoming-request">The incoming request</a></li> +<li><a href="#associating-a-client-context-to-a-session">Associating a client context to a session</a></li> +<li><a href="#sending-reply-to-a-request">Sending reply to a request</a></li> +</ul></li> +<li><a href="#getting-argument-of-invocation">Getting argument of invocation</a><ul> +<li><a href="#basic-functions-for-querying-arguments">Basic functions for querying arguments</a></li> +<li><a href="#arguments-for-received-files">Arguments for received files</a></li> +<li><a href="#arguments-as-a-json-object">Arguments as a JSON object</a></li> +</ul></li> +<li><a href="#initialisation-of-the-plugin-and-declaration-of-methods">Initialisation of the plugin and declaration of methods</a></li> +<li><a href="#sending-messages-to-the-log-system">Sending messages to the log system</a><ul> +<li><a href="#verbs-for-logging-messages">Verbs for logging messages</a></li> +<li><a href="#managing-verbosity">Managing verbosity</a></li> +<li><a href="#output-format-and-destination">Output format and destination</a></li> +</ul></li> +<li><a href="#sending-events">Sending events</a></li> +<li><a href="#writing-an-asynchronous-method-implementation">Writing an asynchronous method implementation</a></li> +<li><a href="#how-to-build-a-plugin">How to build a plugin</a><ul> +<li><a href="#example-for-cmake-meta-build-system">Example for cmake meta build system</a></li> +<li><a href="#exporting-the-function-pluginafbv1register">Exporting the function pluginAfbV1Register</a></li> +<li><a href="#building-within-yocto">Building within yocto</a></li> +</ul></li> +</ul></li> +</ul> +</nav> +<h1 id="howto-write-a-plugin-for-afb-daemon">HOWTO WRITE a PLUGIN for AFB-DAEMON</h1> <pre><code>version: 1 Date: 30 mai 2016 -Author: José Bollo -</code></pre> - -<p><ul> - <li><a href="#HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON">HOWTO WRITE a PLUGIN for AFB-DAEMON</a> - <ul> - <li><a href="#Summary">Summary</a> - <ul> - <li><a href="#Nature.of.a.plugin">Nature of a plugin</a></li> - <li><a href="#Class.of.plugins">Class of plugins</a> - <ul> - <li><a href="#Application-plugins">Application-plugins</a></li> - <li><a href="#Service-plugins">Service-plugins</a></li> - </ul> - </li> - <li><a href="#Live.cycle.of.plugins.within.afb-daemon">Live cycle of plugins within afb-daemon</a></li> - <li><a href="#Plugin.Contend">Plugin Contend</a> - <ul> - <li><a href="#The.name.of.the.plugin">The name of the plugin</a></li> - <li><a href="#Names.of.methods">Names of methods</a></li> - <li><a href="#Initialisation.function">Initialisation function</a></li> - <li><a href="#Functions.instantiation.of.API.VERBs">Functions instantiation of API/VERBs</a> -</li> - </ul> - </li> - </ul> - </li> - <li><a href="#The.Tic-Tac-Toe.example">The Tic-Tac-Toe example</a></li> - <li><a href="#Dependencies.when.compiling">Dependencies when compiling</a></li> - <li><a href="#Header.files.to.include">Header files to include</a></li> - <li><a href="#Choosing.names">Choosing names</a> - <ul> - <li><a href="#Names.for.API..plugin.">Names for API (plugin)</a></li> - <li><a href="#Names.for.methods">Names for methods</a></li> - <li><a href="#Names.for.arguments">Names for arguments</a></li> - <li><a href="#Forging.names.widely.available">Forging names widely available</a></li> - </ul> - </li> - <li><a href="#Writing.a.synchronous.method.implementation">Writing a synchronous method implementation</a> - <ul> - <li><a href="#The.incoming.request">The incoming request</a></li> - <li><a href="#Associating.a.client.context.to.a.session">Associating a client context to a session</a></li> - <li><a href="#Sending.reply.to.a.request">Sending reply to a request</a></li> - </ul> - </li> - <li><a href="#Getting.argument.of.invocation">Getting argument of invocation</a> - <ul> - <li><a href="#Basic.functions.for.querying.arguments">Basic functions for querying arguments</a></li> - <li><a href="#Arguments.for.received.files">Arguments for received files</a></li> - <li><a href="#Arguments.as.a.JSON.object">Arguments as a JSON object</a></li> - </ul> - </li> - <li><a href="#Initialisation.of.the.plugin.and.declaration.of.methods">Initialisation of the plugin and declaration of methods</a></li> - <li><a href="#Sending.messages.to.the.log.system">Sending messages to the log system</a> - <ul> - <li><a href="#Verbs.for.logging.messages">Verbs for logging messages</a></li> - <li><a href="#Managing.verbosity">Managing verbosity</a></li> - <li><a href="#Output.format.and.destination">Output format and destination</a></li> - </ul> - </li> - <li><a href="#Sending.events">Sending events</a></li> - <li><a href="#Writing.an.asynchronous.method.implementation">Writing an asynchronous method implementation</a></li> - <li><a href="#How.to.build.a.plugin">How to build a plugin</a> - <ul> - <li><a href="#Example.for.cmake.meta.build.system">Example for cmake meta build system</a></li> - <li><a href="#Exporting.the.function.pluginAfbV1Register">Exporting the function pluginAfbV1Register</a></li> - <li><a href="#Building.within.yocto">Building within yocto</a></li> - </ul> - </li> - </ul> - </li> -</ul></p> - -<a name="Summary"></a> -<h2>Summary</h2> - -<p>Afb-daemon binders serve files through HTTP protocol -and offers to developers the capability to expose application API methods through -HTTP or WebSocket protocol.</p> - -<p>Binder plugins are used to add API to afb-daemon. -This part describes how to write a plugin for afb-daemon.</p> - +Author: José Bollo</code></pre> +<h2 id="summary">Summary</h2> +<p>Afb-daemon binders serve files through HTTP protocol and offers to developers the capability to expose application API methods through HTTP or WebSocket protocol.</p> +<p>Binder plugins are used to add API to afb-daemon. This part describes how to write a plugin for afb-daemon.</p> <p>Excepting this summary, this document target developers.</p> - -<p>Before moving further through an example, here after -a short overview of binder plugins fundamentals.</p> - -<a name="Nature.of.a.plugin"></a> -<h3>Nature of a plugin</h3> - -<p>A plugin is an independent piece of software. A plugin is self contain and exposes application logic as sharable library. -A plugin is intended to be dynamically loaded by afb-daemon to expose application API.</p> - +<p>Before moving further through an example, here after a short overview of binder plugins fundamentals.</p> +<h3 id="nature-of-a-plugin">Nature of a plugin</h3> +<p>A plugin is an independent piece of software. A plugin is self contain and exposes application logic as sharable library. A plugin is intended to be dynamically loaded by afb-daemon to expose application API.</p> <p>Technically, a binder plugin does not reference and is not linked with any afb-daemon library.</p> - -<a name="Class.of.plugins"></a> -<h3>Class of plugins</h3> - -<p>Application binder supports two kinds of plugins: application plugins and service plugins. -Technically both class of plugin are equivalent are use the same coding convention. Only sharing mode and security context diverge.</p> - -<a name="Application-plugins"></a> -<h4>Application-plugins</h4> - -<p>Application-plugins implements the glue in between application’s UI and services. Every AGL application -has a corresponding binder that typically activates one or many plugins to interface the application logic with lower platform services. -When an application is started by the AGL application framework, a dedicate binder is started that loads/activates application plugin(s). -API expose by application-plugin are executed within corresponding application security context.</p> - -<p>Application plugins generally handle a unique context for a unique client. As the application framework start -a dedicated instance of afb_daemon for each AGL application, if a given plugin is used within multiple application each of those -application get a new and private instance of eventually “shared” plugin.</p> - -<a name="Service-plugins"></a> -<h4>Service-plugins</h4> - -<p>Service-plugins enable API activation within corresponding service security context and not within calling application context. -Service-plugins are intended to run as a unique instance. Service-plugins can be shared in between multiple clients.</p> - +<h3 id="class-of-plugins">Class of plugins</h3> +<p>Application binder supports two kinds of plugins: application plugins and service plugins. Technically both class of plugin are equivalent are use the same coding convention. Only sharing mode and security context diverge.</p> +<h4 id="application-plugins">Application-plugins</h4> +<p>Application-plugins implements the glue in between application's UI and services. Every AGL application has a corresponding binder that typically activates one or many plugins to interface the application logic with lower platform services. When an application is started by the AGL application framework, a dedicate binder is started that loads/activates application plugin(s). API expose by application-plugin are executed within corresponding application security context.</p> +<p>Application plugins generally handle a unique context for a unique client. As the application framework start a dedicated instance of afb_daemon for each AGL application, if a given plugin is used within multiple application each of those application get a new and private instance of eventually "shared" plugin.</p> +<h4 id="service-plugins">Service-plugins</h4> +<p>Service-plugins enable API activation within corresponding service security context and not within calling application context. Service-plugins are intended to run as a unique instance. Service-plugins can be shared in between multiple clients.</p> <p>Service-plugins can either be stateless or manage client context. When managing context each client get a private context.</p> - <p>Sharing may either be global to the platform (ie: GPS service) or dedicated to a given user (ie: user preferences)</p> - -<a name="Live.cycle.of.plugins.within.afb-daemon"></a> -<h3>Live cycle of plugins within afb-daemon</h3> - +<h3 id="live-cycle-of-plugins-within-afb-daemon">Live cycle of plugins within afb-daemon</h3> <p>Application and service plugins are loaded and activated each time a new afb-daemon is started.</p> - -<p>At launch time, every loaded plugin initialise itself. -If a single plugin initialisation fail corresponding instance of afb-daemon self aborts.</p> - -<p>Conversely, when a plugin initialisation succeeds, it should register -its unique name as well as the list of verbs attached to the methods it exposes.</p> - -<p>When initialised, on request from application clients to the right API/verb, plugin methods -are activated by the afb-daemon attached to the application or service.</p> - -<p>At exit time, no special action is enforced by afb-daemon. When a specific actions is required at afb-daemon stop, -developers should use ‘atexit/on_exit’ during plugin initialisation sequence to register a custom exit function.</p> - -<a name="Plugin.Contend"></a> -<h3>Plugin Contend</h3> - -<p>Afb-daemon’s plugin register two classes of objects: names and functions.</p> - -<p>Plugins declare categories of names: - - A unique plugin name to access all API expose by this plugin, - - One name for each methods/verbs provided by this plugin.</p> - -<p>Plugins declare two categories of functions: - - function use for the initialisation - - functions implementing exposed API methods</p> - -<p>Afb-daemon parses URI requests to extract the API(plugin name) and the VERB(method to activate). -As an example, URI <strong>foo/bar</strong> translates to plugin named <strong>foo</strong> and method named <strong>bar</strong>. -To serve such a request, afb-daemon looks for an active plugin named <strong>foo</strong> and then within this plugin for a method named <strong>bar</strong>. -When find afb-daemon calls corresponding method with attached parameter if any.</p> - +<p>At launch time, every loaded plugin initialise itself. If a single plugin initialisation fail corresponding instance of afb-daemon self aborts.</p> +<p>Conversely, when a plugin initialisation succeeds, it should register its unique name as well as the list of verbs attached to the methods it exposes.</p> +<p>When initialised, on request from application clients to the right API/verb, plugin methods are activated by the afb-daemon attached to the application or service.</p> +<p>At exit time, no special action is enforced by afb-daemon. When a specific actions is required at afb-daemon stop, developers should use 'atexit/on_exit' during plugin initialisation sequence to register a custom exit function.</p> +<h3 id="plugin-contend">Plugin Contend</h3> +<p>Afb-daemon's plugin register two classes of objects: names and functions.</p> +<p>Plugins declare categories of names: - A unique plugin name to access all API expose by this plugin, - One name for each methods/verbs provided by this plugin.</p> +<p>Plugins declare two categories of functions: - function use for the initialisation - functions implementing exposed API methods</p> +<p>Afb-daemon parses URI requests to extract the API(plugin name) and the VERB(method to activate). As an example, URI <strong>foo/bar</strong> translates to plugin named <strong>foo</strong> and method named <strong>bar</strong>. To serve such a request, afb-daemon looks for an active plugin named <strong>foo</strong> and then within this plugin for a method named <strong>bar</strong>. When find afb-daemon calls corresponding method with attached parameter if any.</p> <p>Afb-daemon ignores letter case when parsing URI. Thus <strong>TicTacToe/Board</strong> and <strong>tictactoe/board</strong> are equivalent.</p> - -<a name="The.name.of.the.plugin"></a> -<h4>The name of the plugin</h4> - -<p>The name of a given plugin is also known as the name -of the API prefix that defines the plugin.</p> - +<h4 id="the-name-of-the-plugin">The name of the plugin</h4> +<p>The name of a given plugin is also known as the name of the API prefix that defines the plugin.</p> <p>The name of a plugin SHOULD be unique within a given afb-daemon instance.</p> - -<p>For example, when a client of afb-daemon calls a URI named <strong>foo/bar</strong>. Afb-daemon -extracts the prefix <strong>foo</strong> and the suffix <strong>bar</strong>. <strong>foo</strong> must match a plugin name and <strong>bar</strong> a VERB attached to some method.</p> - -<a name="Names.of.methods"></a> -<h4>Names of methods</h4> - -<p>Each plugin exposes a set of methods that can be called -by the clients of a given afb-daemon.</p> - -<p>VERB’s name attached to a given plugin (API) MUST be unique within a plugin.</p> - -<p>Plugins static declaration link VERBS to corresponding methods. -When clients emit requests on a given API/VERB corresponding method is called by afb-daemon.</p> - -<a name="Initialisation.function"></a> -<h4>Initialisation function</h4> - -<p>Plugin’s initialisation function serves several purposes.</p> - -<ol> -<li><p>It allows afb-daemon to control plugin version depending on initialisation function name. -As today, the only supported initialisation function is <strong>pluginAfbV1Register</strong>. This identifies -version “one” of plugins.</p></li> +<p>For example, when a client of afb-daemon calls a URI named <strong>foo/bar</strong>. Afb-daemon extracts the prefix <strong>foo</strong> and the suffix <strong>bar</strong>. <strong>foo</strong> must match a plugin name and <strong>bar</strong> a VERB attached to some method.</p> +<h4 id="names-of-methods">Names of methods</h4> +<p>Each plugin exposes a set of methods that can be called by the clients of a given afb-daemon.</p> +<p>VERB's name attached to a given plugin (API) MUST be unique within a plugin.</p> +<p>Plugins static declaration link VERBS to corresponding methods. When clients emit requests on a given API/VERB corresponding method is called by afb-daemon.</p> +<h4 id="initialisation-function">Initialisation function</h4> +<p>Plugin's initialisation function serves several purposes.</p> +<ol type="1"> +<li><p>It allows afb-daemon to control plugin version depending on initialisation function name. As today, the only supported initialisation function is <strong>pluginAfbV1Register</strong>. This identifies version "one" of plugins.</p></li> <li><p>It allows plugins to initialise itself.</p></li> <li><p>It enables names declarations: descriptions, requirements and implementations of exposed API/VERB.</p></li> </ol> - - -<a name="Functions.instantiation.of.API.VERBs"></a> -<h4>Functions instantiation of API/VERBs</h4> - -<p>When an API/VERB is called, afb-daemon constructs a request object. Then it -passes this request object to the implementation function corresponding to requested method, this -within attached API plugin.</p> - -<p>An implementation function receives a request object that -is used to: get arguments of the request, send -answer, store session data.</p> - +<h4 id="functions-instantiation-of-apiverbs">Functions instantiation of API/VERBs</h4> +<p>When an API/VERB is called, afb-daemon constructs a request object. Then it passes this request object to the implementation function corresponding to requested method, this within attached API plugin.</p> +<p>An implementation function receives a request object that is used to: get arguments of the request, send answer, store session data.</p> <p>A plugin MUST set an answer to every received requests.</p> - -<p>Nevertheless it is not mandatory to set the answer -before returning from API/VERB implementing function. -This behaviour is important for asynchronous actions.</p> - -<p>API/VERB implementation that set an answer before returning are called <em>synchronous implementations</em>. -Those that do not systematically set an answer before returning are called <em>asynchronous implementations</em>.</p> - -<p>Asynchronous implementations typically launch asynchronous actions. They record some context at -request time and provide answer to the request only at completion of asynchronous actions.</p> - -<a name="The.Tic-Tac-Toe.example"></a> -<h2>The Tic-Tac-Toe example</h2> - -<p>This part explains how to write an afb-plugin. -For the sake of being practical it uses many -examples based on tic-tac-toe. -This plugin example is in <em>plugins/samples/tic-tac-toe.c</em>.</p> - +<p>Nevertheless it is not mandatory to set the answer before returning from API/VERB implementing function. This behaviour is important for asynchronous actions.</p> +<p>API/VERB implementation that set an answer before returning are called <em>synchronous implementations</em>. Those that do not systematically set an answer before returning are called <em>asynchronous implementations</em>.</p> +<p>Asynchronous implementations typically launch asynchronous actions. They record some context at request time and provide answer to the request only at completion of asynchronous actions.</p> +<h2 id="the-tic-tac-toe-example">The Tic-Tac-Toe example</h2> +<p>This part explains how to write an afb-plugin. For the sake of being practical it uses many examples based on tic-tac-toe. This plugin example is in <em>plugins/samples/tic-tac-toe.c</em>.</p> <p>This plugin is named <strong><em>tictactoe</em></strong>.</p> - -<a name="Dependencies.when.compiling"></a> -<h2>Dependencies when compiling</h2> - -<p>Afb-daemon provides a configuration file for <em>pkg-config</em>. -Typing the command</p> - -<pre><code>pkg-config --cflags afb-daemon -</code></pre> - +<h2 id="dependencies-when-compiling">Dependencies when compiling</h2> +<p>Afb-daemon provides a configuration file for <em>pkg-config</em>. Typing the command</p> +<pre><code>pkg-config --cflags afb-daemon</code></pre> <p>Print flags use for compilation:</p> - <pre><code>$ pkg-config --cflags afb-daemon --I/opt/local/include -I/usr/include/json-c -</code></pre> - +-I/opt/local/include -I/usr/include/json-c </code></pre> <p>For linking, you should use</p> - <pre><code>$ pkg-config --libs afb-daemon --ljson-c -</code></pre> - -<p>Afb-daemon automatically includes dependency to json-c. -This is activated through <strong>Requires</strong> keyword in pkg-config. -While almost every plugin replies on <strong>json-c</strong> this is not a must have dependency.</p> - -<p>Internally, afb-daemon relies on <strong>libsystemd</strong> for its event loop, as well -as for its binding to D-Bus. -Plugins developers are encouraged to leverage <strong>libsystemd</strong> when possible. -Nevertheless there is no hard dependency to <strong>libsystemd</strong> if ever -you rather not use it, feel free to do so.</p> - -<blockquote><p>Afb-daemon plugin are fully self contain. They do not enforce dependency on any libraries from the application framework. -Afb-daemon dependencies requirer to run AGL plugins are given at runtime through pointers leveraging read-only -memory feature.</p></blockquote> - -<a name="Header.files.to.include"></a> -<h2>Header files to include</h2> - +-ljson-c</code></pre> +<p>Afb-daemon automatically includes dependency to json-c. This is activated through <strong>Requires</strong> keyword in pkg-config. While almost every plugin replies on <strong>json-c</strong> this is not a must have dependency.</p> +<p>Internally, afb-daemon relies on <strong>libsystemd</strong> for its event loop, as well as for its binding to D-Bus. Plugins developers are encouraged to leverage <strong>libsystemd</strong> when possible. Nevertheless there is no hard dependency to <strong>libsystemd</strong> if ever you rather not use it, feel free to do so.</p> +<blockquote> +<p>Afb-daemon plugin are fully self contain. They do not enforce dependency on any libraries from the application framework. Afb-daemon dependencies requirer to run AGL plugins are given at runtime through pointers leveraging read-only memory feature.</p> +</blockquote> +<h2 id="header-files-to-include">Header files to include</h2> <p>Plugin <em>tictactoe</em> has following includes:</p> - -<pre><code>#define _GNU_SOURCE -#include <stdio.h> -#include <string.h> -#include <json-c/json.h> -#include <afb/afb-plugin.h> -</code></pre> - -<p>Header <em>afb/afb-plugin.h</em> is the only hard dependency, it includes all features -that a plugin MUST HAVE. Outside of includes used to support application logic, -common external headers used within plugins are:</p> - +<pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#define _GNU_SOURCE</span> +<span class="ot">#include <stdio.h></span> +<span class="ot">#include <string.h></span> +<span class="ot">#include <json-c/json.h></span> +<span class="ot">#include <afb/afb-plugin.h></span></code></pre> +<p>Header <em>afb/afb-plugin.h</em> is the only hard dependency, it includes all features that a plugin MUST HAVE. Outside of includes used to support application logic, common external headers used within plugins are:</p> <ul> <li><em>json-c/json.h</em>: should be include to handle json objects;</li> <li><em>systemd/sd-event.h</em>: should be include to access event main loop;</li> <li><em>systemd/sd-bus.h</em>: should be include for dbus connections.</li> </ul> - - -<p>The <em>tictactoe</em> plugin does not leverage systemd features, also only json.h -is used on top of mandatory afb/afb-plugin.h.</p> - -<p>When including <em>afb/afb-plugin.h</em>, the macro <strong>_GNU_SOURCE</strong> MUST be -defined.</p> - -<a name="Choosing.names"></a> -<h2>Choosing names</h2> - -<p>Designers of plugins should define a unique name for every API plugin -as well as for methods VERBs. They should also define names for request -arguments passed as name/value pair in URI.</p> - -<p>While forging names, designers should respect few rules to -ensure that created names are valid and easy to use across platforms.</p> - +<p>The <em>tictactoe</em> plugin does not leverage systemd features, also only json.h is used on top of mandatory afb/afb-plugin.h.</p> +<p>When including <em>afb/afb-plugin.h</em>, the macro **_GNU_SOURCE** MUST be defined.</p> +<h2 id="choosing-names">Choosing names</h2> +<p>Designers of plugins should define a unique name for every API plugin as well as for methods VERBs. They should also define names for request arguments passed as name/value pair in URI.</p> +<p>While forging names, designers should respect few rules to ensure that created names are valid and easy to use across platforms.</p> <p>All names and strings are UTF-8 encoded.</p> - -<a name="Names.for.API..plugin."></a> -<h3>Names for API (plugin)</h3> - -<p>Plugin API name are checked. -All characters are authorised except:</p> - +<h3 id="names-for-api-plugin">Names for API (plugin)</h3> +<p>Plugin API name are checked. All characters are authorised except:</p> <ul> -<li>the control characters (\u0000 .. \u001f)</li> -<li>the characters of the set { ‘ ’, ‘“’, ‘#’, ‘%’, ‘&’, -‘’‘, ’/‘, ’?‘, ’`‘, ’\x7f' }</li> +<li>the control characters (000 .. 01f)</li> +<li>the characters of the set { ' ', '"', '#', '%', '&', ''', '/', '?', '`', '7f' }</li> </ul> - - -<p>In other words the set of forbidden characters is -{ \u0000..\u0020, \u0022, \u0023, \u0025..\u0027, - \u002f, \u003f, \u0060, \u007f }.</p> - -<p>Afb-daemon makes no distinction between lower case -and upper case when searching for API/VERB.</p> - -<a name="Names.for.methods"></a> -<h3>Names for methods</h3> - +<p>In other words the set of forbidden characters is { 000..020, 022, 023, 025..027, 02f, 03f, 060, 07f }.</p> +<p>Afb-daemon makes no distinction between lower case and upper case when searching for API/VERB.</p> +<h3 id="names-for-methods">Names for methods</h3> <p>The names of methods VERBs are totally free and not checked.</p> - -<p>However, the validity rules for method’s VERB name are the -same as for Plugin API name except that the dot(.) character -is forbidden.</p> - +<p>However, the validity rules for method's VERB name are the same as for Plugin API name except that the dot(.) character is forbidden.</p> <p>Afb-daemon makes no case distinction when searching for an API by name.</p> - -<a name="Names.for.arguments"></a> -<h3>Names for arguments</h3> - -<p>Argument’s name are not restricted and can be everything you wish.</p> - -<blockquote><p>Warning arguments search is case sensitive and “index” and “Index” -are not two different arguments.</p></blockquote> - -<a name="Forging.names.widely.available"></a> -<h3>Forging names widely available</h3> - -<p>The key names of javascript object can be almost -anything using the arrayed notation:</p> - -<pre><code>object[key] = value -</code></pre> - +<h3 id="names-for-arguments">Names for arguments</h3> +<p>Argument's name are not restricted and can be everything you wish.</p> +<blockquote> +<p>Warning arguments search is case sensitive and "index" and "Index" are not two different arguments.</p> +</blockquote> +<h3 id="forging-names-widely-available">Forging names widely available</h3> +<p>The key names of javascript object can be almost anything using the arrayed notation:</p> +<pre><code>object[key] = value</code></pre> <p>Nevertheless this is not the case with javascript dot notation:</p> - -<pre><code>object.key = value -</code></pre> - -<p>Using the dot notation, the key must be a valid javascript -identifier and dash(-) as well as few other reserved characters cannot be used.</p> - +<pre><code>object.key = value</code></pre> +<p>Using the dot notation, the key must be a valid javascript identifier and dash(-) as well as few other reserved characters cannot be used.</p> <p>For this reason, we advise developper to chose name compatible with both javascript and HTML notation.</p> - -<p>It is a good practice, even for arguments not to rely on case sensitivity. -This may reduce headache strength at debug time, especially with interpreted language like -javascript that may not warn you that a variable was not defined.</p> - -<a name="Writing.a.synchronous.method.implementation"></a> -<h2>Writing a synchronous method implementation</h2> - -<p>The method <strong>tictactoe/board</strong> is a synchronous implementation. -Here is its listing:</p> - -<pre><code>/* - * get the board - */ -static void board(struct afb_req req) +<p>It is a good practice, even for arguments not to rely on case sensitivity. This may reduce headache strength at debug time, especially with interpreted language like javascript that may not warn you that a variable was not defined.</p> +<h2 id="writing-a-synchronous-method-implementation">Writing a synchronous method implementation</h2> +<p>The method <strong>tictactoe/board</strong> is a synchronous implementation. Here is its listing:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * get the board</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">void</span> board(<span class="kw">struct</span> afb_req req) { - struct board *board; - struct json_object *description; - - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'board' called for boardid %d", board->id); - - /* describe the board */ - description = describe(board); - - /* send the board's description */ - afb_req_success(req, description, NULL); -} -</code></pre> - -<p>This example shows many aspects of a synchronous -method implementation. Let summarise it:</p> - -<ol> -<li><p>The function <strong>board_of_req</strong> retrieves the context stored -for the plugin: the board.</p></li> -<li><p>The macro <strong>INFO</strong> sends a message of kind <em>INFO</em> -to the logging system. The global variable named <strong>afbitf</strong> -used represents the interface to afb-daemon.</p></li> -<li><p>The function <strong>describe</strong> creates a json_object representing -the board.</p></li> -<li><p>The function <strong>afb_req_success</strong> sends the reply, attaching to -it the object <em>description</em>.</p></li> + <span class="kw">struct</span> board *board; + <span class="kw">struct</span> json_object *description; + + <span class="co">/* retrieves the context for the session */</span> + board = board_of_req(req); + INFO(afbitf, <span class="st">"method 'board' called for boardid %d"</span>, board->id); + + <span class="co">/* describe the board */</span> + description = describe(board); + + <span class="co">/* send the board's description */</span> + afb_req_success(req, description, NULL); +}</code></pre> +<p>This example shows many aspects of a synchronous method implementation. Let summarise it:</p> +<ol type="1"> +<li><p>The function <strong>board_of_req</strong> retrieves the context stored for the plugin: the board.</p></li> +<li><p>The macro <strong>INFO</strong> sends a message of kind <em>INFO</em> to the logging system. The global variable named <strong>afbitf</strong> used represents the interface to afb-daemon.</p></li> +<li><p>The function <strong>describe</strong> creates a json_object representing the board.</p></li> +<li><p>The function <strong>afb_req_success</strong> sends the reply, attaching to it the object <em>description</em>.</p></li> </ol> - - -<a name="The.incoming.request"></a> -<h3>The incoming request</h3> - -<p>For any implementation, the request is received by a structure of type -<strong>struct afb_req</strong>.</p> - -<blockquote><p>Note that this is a PLAIN structure, not a pointer to a structure.</p></blockquote> - +<h3 id="the-incoming-request">The incoming request</h3> +<p>For any implementation, the request is received by a structure of type <strong>struct afb_req</strong>.</p> +<blockquote> +<p>Note that this is a PLAIN structure, not a pointer to a structure.</p> +</blockquote> <p>The definition of <strong>struct afb_req</strong> is:</p> - -<pre><code>/* - * Describes the request by plugins from afb-daemon - */ -struct afb_req { - const struct afb_req_itf *itf; /* the interfacing functions */ - void *closure; /* the closure for functions */ -}; -</code></pre> - -<p>It contains two pointers: first one <em>itf</em>, points to functions used -to handle internal request. Second one <em>closure</em> point onto function closure.</p> - -<blockquote><p>The structure must never be used directly. -Instead developer should use the intended functions provided -by afb-daemon as described here after.</p></blockquote> - -<p><em>req</em> is used to get arguments of the request, to send -answer, to store session data.</p> - -<p>This object and its interface is defined and documented -in the file names <em>afb/afb-req-itf.h</em></p> - +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Describes the request by plugins from afb-daemon</span> +<span class="co"> */</span> +<span class="kw">struct</span> afb_req { + <span class="dt">const</span> <span class="kw">struct</span> afb_req_itf *itf; <span class="co">/* the interfacing functions */</span> + <span class="dt">void</span> *closure; <span class="co">/* the closure for functions */</span> +};</code></pre> +<p>It contains two pointers: first one <em>itf</em>, points to functions used to handle internal request. Second one <em>closure</em> point onto function closure.</p> +<blockquote> +<p>The structure must never be used directly. Instead developer should use the intended functions provided by afb-daemon as described here after.</p> +</blockquote> +<p><em>req</em> is used to get arguments of the request, to send answer, to store session data.</p> +<p>This object and its interface is defined and documented in the file names <em>afb/afb-req-itf.h</em></p> <p>The above example uses twice <em>req</em> object request.</p> - <p>The first time, to retrieve the board attached to the session of the request.</p> - <p>The second time, to send the reply: an object that describes the current board.</p> - -<a name="Associating.a.client.context.to.a.session"></a> -<h3>Associating a client context to a session</h3> - -<p>When <em>tic-tac-toe</em> plugin receives a request, it musts get -the board describing the game associated to the session.</p> - -<p>For a plugin, having data associated to a session is common. -This data is called “plugin context” for the session. -Within <em>tic-tac-toe</em> plugin the context is the board.</p> - +<h3 id="associating-a-client-context-to-a-session">Associating a client context to a session</h3> +<p>When <em>tic-tac-toe</em> plugin receives a request, it musts get the board describing the game associated to the session.</p> +<p>For a plugin, having data associated to a session is common. This data is called "plugin context" for the session. Within <em>tic-tac-toe</em> plugin the context is the board.</p> <p>Requests <em>afb_req</em> offer four functions for storing and retrieving session associated context.</p> - <p>These functions are:</p> - <ul> -<li><p><strong>afb_req_context_get</strong>: -retrieves context data stored for current plugin.</p></li> -<li><p><strong>afb_req_context_set</strong>: -store context data of current plugin.</p></li> -<li><p><strong>afb_req_context</strong>: -if exist retrieves context data of current plugin. -if context does not yet exist, creates a new context and store it.</p></li> -<li><p><strong>afb_req_context_clear</strong>: -reset the stored context data.</p></li> +<li><p><strong>afb_req_context_get</strong>: retrieves context data stored for current plugin.</p></li> +<li><p><strong>afb_req_context_set</strong>: store context data of current plugin.</p></li> +<li><p><strong>afb_req_context</strong>: if exist retrieves context data of current plugin. if context does not yet exist, creates a new context and store it.</p></li> +<li><p><strong>afb_req_context_clear</strong>: reset the stored context data.</p></li> </ul> - - -<p>The plugin <em>tictactoe</em> use a convenient function to retrieve -its context: the board. This function is <em>board_of_req</em>:</p> - -<pre><code>/* - * retrieves the board of the request - */ -static inline struct board *board_of_req(struct afb_req req) +<p>The plugin <em>tictactoe</em> use a convenient function to retrieve its context: the board. This function is <em>board_of_req</em>:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * retrieves the board of the request</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="kw">inline</span> <span class="kw">struct</span> board *board_of_req(<span class="kw">struct</span> afb_req req) { - return afb_req_context(req, (void*)get_new_board, (void*)release_board); -} -</code></pre> - -<p>The function <strong>afb_req_context</strong> ensures an existing context -for the session of the request. -Its two last arguments are functions to allocate and free context. -Note function type casts to avoid compilation warnings.</p> - + <span class="kw">return</span> afb_req_context(req, (<span class="dt">void</span>*)get_new_board, (<span class="dt">void</span>*)release_board); +}</code></pre> +<p>The function <strong>afb_req_context</strong> ensures an existing context for the session of the request. Its two last arguments are functions to allocate and free context. Note function type casts to avoid compilation warnings.</p> <p>Here is the definition of the function <strong>afb_req_context</strong></p> - -<pre><code>/* - * Gets the pointer stored by the plugin for the session of 'req'. - * If the stored pointer is NULL, indicating that no pointer was - * already stored, afb_req_context creates a new context by calling - * the function 'create_context' and stores it with the freeing function - * 'free_context'. - */ -static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*)) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets the pointer stored by the plugin for the session of 'req'.</span> +<span class="co"> * If the stored pointer is NULL, indicating that no pointer was</span> +<span class="co"> * already stored, afb_req_context creates a new context by calling</span> +<span class="co"> * the function 'create_context' and stores it with the freeing function</span> +<span class="co"> * 'free_context'.</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="kw">inline</span> <span class="dt">void</span> *afb_req_context(<span class="kw">struct</span> afb_req req, <span class="dt">void</span> *(*create_context)(), <span class="dt">void</span> (*free_context)(<span class="dt">void</span>*)) { - void *result = afb_req_context_get(req); - if (result == NULL) { - result = create_context(); - afb_req_context_set(req, result, free_context); - } - return result; -} -</code></pre> - -<p>The second argument if the function that creates the context. -For plugin <em>tic-tac-toe</em> (function <strong>get_new_board</strong>). -The function <strong>get_new_board</strong> creates a new board and set usage its count to 1. -The boards are checking usage count to free resources when not used.</p> - -<p>The third argument is a function that frees context resources. -For plugin <em>tic-tac-toe</em> (function <strong>release_board</strong>). -The function <strong>release_board</strong> decrease usage count of the board passed in argument. -When usage count falls to zero, data board are freed.</p> - + <span class="dt">void</span> *result = afb_req_context_get(req); + <span class="kw">if</span> (result == NULL) { + result = create_context(); + afb_req_context_set(req, result, free_context); + } + <span class="kw">return</span> result; +}</code></pre> +<p>The second argument if the function that creates the context. For plugin <em>tic-tac-toe</em> (function <strong>get_new_board</strong>). The function <strong>get_new_board</strong> creates a new board and set usage its count to 1. The boards are checking usage count to free resources when not used.</p> +<p>The third argument is a function that frees context resources. For plugin <em>tic-tac-toe</em> (function <strong>release_board</strong>). The function <strong>release_board</strong> decrease usage count of the board passed in argument. When usage count falls to zero, data board are freed.</p> <p>Definition of other functions dealing with contexts:</p> - -<pre><code>/* - * Gets the pointer stored by the plugin for the session of 'req'. - * When the plugin has not yet recorded a pointer, NULL is returned. - */ -void *afb_req_context_get(struct afb_req req); - -/* - * Stores for the plugin the pointer 'context' to the session of 'req'. - * The function 'free_context' will be called when the session is closed - * or if plugin stores an other pointer. - */ -void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*)); - -/* - * Frees the pointer stored by the plugin for the session of 'req' - * and sets it to NULL. - * - * Shortcut for: afb_req_context_set(req, NULL, NULL) - */ -static inline void afb_req_context_clear(struct afb_req req) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets the pointer stored by the plugin for the session of 'req'.</span> +<span class="co"> * When the plugin has not yet recorded a pointer, NULL is returned.</span> +<span class="co"> */</span> +<span class="dt">void</span> *afb_req_context_get(<span class="kw">struct</span> afb_req req); + +<span class="co">/*</span> +<span class="co"> * Stores for the plugin the pointer 'context' to the session of 'req'.</span> +<span class="co"> * The function 'free_context' will be called when the session is closed</span> +<span class="co"> * or if plugin stores an other pointer.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_req_context_set(<span class="kw">struct</span> afb_req req, <span class="dt">void</span> *context, <span class="dt">void</span> (*free_context)(<span class="dt">void</span>*)); + +<span class="co">/*</span> +<span class="co"> * Frees the pointer stored by the plugin for the session of 'req'</span> +<span class="co"> * and sets it to NULL.</span> +<span class="co"> *</span> +<span class="co"> * Shortcut for: afb_req_context_set(req, NULL, NULL)</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="kw">inline</span> <span class="dt">void</span> afb_req_context_clear(<span class="kw">struct</span> afb_req req) { - afb_req_context_set(req, NULL, NULL); -} -</code></pre> - -<a name="Sending.reply.to.a.request"></a> -<h3>Sending reply to a request</h3> - + afb_req_context_set(req, NULL, NULL); +}</code></pre> +<h3 id="sending-reply-to-a-request">Sending reply to a request</h3> <p>Two kinds of replies: successful or failure.</p> - -<blockquote><p>Sending a reply to a request MUST be done once and only once.</p></blockquote> - -<p>It exists two functions for “success” replies: <strong>afb_req_success</strong> and <strong>afb_req_success_f</strong>.</p> - -<pre><code>/* - * Sends a reply of kind success to the request 'req'. - * The status of the reply is automatically set to "success". - * Its send the object 'obj' (can be NULL) with an - * informationnal comment 'info (can also be NULL). - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ -void afb_req_success(struct afb_req req, struct json_object *obj, const char *info); - -/* - * Same as 'afb_req_success' but the 'info' is a formatting - * string followed by arguments. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ -void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...); -</code></pre> - -<p>It exists two functions for “failure” replies: <strong>afb_req_fail</strong> and <strong>afb_req_fail_f</strong>.</p> - -<pre><code>/* - * Sends a reply of kind failure to the request 'req'. - * The status of the reply is set to 'status' and an - * informational comment 'info' (can also be NULL) can be added. - * - * Note that calling afb_req_fail("success", info) is equivalent - * to call afb_req_success(NULL, info). Thus even if possible it - * is strongly recommended to NEVER use "success" for status. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ -void afb_req_fail(struct afb_req req, const char *status, const char *info); - -/* - * Same as 'afb_req_fail' but the 'info' is a formatting - * string followed by arguments. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ -void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...); -</code></pre> - -<blockquote><p>For conveniency, these functions automatically call <strong>json_object_put</strong> to release <strong>obj</strong>. -Because <strong>obj</strong> usage count is null after being passed to a reply function, it SHOULD not be used anymore. -If exceptionally <strong>obj</strong> needs to remain usable after reply function then using <strong>json_object_get</strong> on <strong>obj</strong> -to increase usage count and cancels the effect the <strong>json_object_put</strong> is possible.</p></blockquote> - -<a name="Getting.argument.of.invocation"></a> -<h2>Getting argument of invocation</h2> - -<p>Many methods expect arguments. Afb-daemon’s plugins -retrieve arguments by name and not by position.</p> - -<p>Arguments are passed by requests through either HTTP -or WebSockets.</p> - -<p>For example, the method <strong>join</strong> of plugin <strong>tic-tac-toe</strong> -expects one argument: the <em>boardid</em> to join. Here is an extract:</p> - -<pre><code>/* - * Join a board - */ -static void join(struct afb_req req) +<blockquote> +<p>Sending a reply to a request MUST be done once and only once.</p> +</blockquote> +<p>It exists two functions for "success" replies: <strong>afb_req_success</strong> and <strong>afb_req_success_f</strong>.</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Sends a reply of kind success to the request 'req'.</span> +<span class="co"> * The status of the reply is automatically set to "success".</span> +<span class="co"> * Its send the object 'obj' (can be NULL) with an</span> +<span class="co"> * informationnal comment 'info (can also be NULL).</span> +<span class="co"> *</span> +<span class="co"> * For conveniency, the function calls 'json_object_put' for 'obj'.</span> +<span class="co"> * Thus, in the case where 'obj' should remain available after</span> +<span class="co"> * the function returns, the function 'json_object_get' shall be used.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_req_success(<span class="kw">struct</span> afb_req req, <span class="kw">struct</span> json_object *obj, <span class="dt">const</span> <span class="dt">char</span> *info); + +<span class="co">/*</span> +<span class="co"> * Same as 'afb_req_success' but the 'info' is a formatting</span> +<span class="co"> * string followed by arguments.</span> +<span class="co"> *</span> +<span class="co"> * For conveniency, the function calls 'json_object_put' for 'obj'.</span> +<span class="co"> * Thus, in the case where 'obj' should remain available after</span> +<span class="co"> * the function returns, the function 'json_object_get' shall be used.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_req_success_f(<span class="kw">struct</span> afb_req req, <span class="kw">struct</span> json_object *obj, <span class="dt">const</span> <span class="dt">char</span> *info, ...);</code></pre> +<p>It exists two functions for "failure" replies: <strong>afb_req_fail</strong> and <strong>afb_req_fail_f</strong>.</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Sends a reply of kind failure to the request 'req'.</span> +<span class="co"> * The status of the reply is set to 'status' and an</span> +<span class="co"> * informational comment 'info' (can also be NULL) can be added.</span> +<span class="co"> *</span> +<span class="co"> * Note that calling afb_req_fail("success", info) is equivalent</span> +<span class="co"> * to call afb_req_success(NULL, info). Thus even if possible it</span> +<span class="co"> * is strongly recommended to NEVER use "success" for status.</span> +<span class="co"> *</span> +<span class="co"> * For conveniency, the function calls 'json_object_put' for 'obj'.</span> +<span class="co"> * Thus, in the case where 'obj' should remain available after</span> +<span class="co"> * the function returns, the function 'json_object_get' shall be used.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_req_fail(<span class="kw">struct</span> afb_req req, <span class="dt">const</span> <span class="dt">char</span> *status, <span class="dt">const</span> <span class="dt">char</span> *info); + +<span class="co">/*</span> +<span class="co"> * Same as 'afb_req_fail' but the 'info' is a formatting</span> +<span class="co"> * string followed by arguments.</span> +<span class="co"> *</span> +<span class="co"> * For conveniency, the function calls 'json_object_put' for 'obj'.</span> +<span class="co"> * Thus, in the case where 'obj' should remain available after</span> +<span class="co"> * the function returns, the function 'json_object_get' shall be used.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_req_fail_f(<span class="kw">struct</span> afb_req req, <span class="dt">const</span> <span class="dt">char</span> *status, <span class="dt">const</span> <span class="dt">char</span> *info, ...);</code></pre> +<blockquote> +<p>For conveniency, these functions automatically call <strong>json_object_put</strong> to release <strong>obj</strong>. Because <strong>obj</strong> usage count is null after being passed to a reply function, it SHOULD not be used anymore. If exceptionally <strong>obj</strong> needs to remain usable after reply function then using <strong>json_object_get</strong> on <strong>obj</strong> to increase usage count and cancels the effect the <strong>json_object_put</strong> is possible.</p> +</blockquote> +<h2 id="getting-argument-of-invocation">Getting argument of invocation</h2> +<p>Many methods expect arguments. Afb-daemon's plugins retrieve arguments by name and not by position.</p> +<p>Arguments are passed by requests through either HTTP or WebSockets.</p> +<p>For example, the method <strong>join</strong> of plugin <strong>tic-tac-toe</strong> expects one argument: the <em>boardid</em> to join. Here is an extract:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Join a board</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">void</span> join(<span class="kw">struct</span> afb_req req) { - struct board *board, *new_board; - const char *id; - - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'join' called for boardid %d", board->id); - - /* retrieves the argument */ - id = afb_req_value(req, "boardid"); - if (id == NULL) - goto bad_request; - ... -</code></pre> - -<p>The function <strong>afb_req_value</strong> searches in the request <em>req</em> -for argument name passed in the second argument. When argument name -is not passed, <strong>afb_req_value</strong> returns NULL.</p> - -<blockquote><p>The search is case sensitive and <em>boardid</em> is not equivalent to <em>BoardId</em>. -Nevertheless having argument names that only differ by name case is not a good idea.</p></blockquote> - -<a name="Basic.functions.for.querying.arguments"></a> -<h3>Basic functions for querying arguments</h3> - + <span class="kw">struct</span> board *board, *new_board; + <span class="dt">const</span> <span class="dt">char</span> *id; + + <span class="co">/* retrieves the context for the session */</span> + board = board_of_req(req); + INFO(afbitf, <span class="st">"method 'join' called for boardid %d"</span>, board->id); + + <span class="co">/* retrieves the argument */</span> + id = afb_req_value(req, <span class="st">"boardid"</span>); + <span class="kw">if</span> (id == NULL) + <span class="kw">goto</span> bad_request; + ...</code></pre> +<p>The function <strong>afb_req_value</strong> searches in the request <em>req</em> for argument name passed in the second argument. When argument name is not passed, <strong>afb_req_value</strong> returns NULL.</p> +<blockquote> +<p>The search is case sensitive and <em>boardid</em> is not equivalent to <em>BoardId</em>. Nevertheless having argument names that only differ by name case is not a good idea.</p> +</blockquote> +<h3 id="basic-functions-for-querying-arguments">Basic functions for querying arguments</h3> <p>The function <strong>afb_req_value</strong> is defined here after:</p> - -<pre><code>/* - * Gets from the request 'req' the string value of the argument of 'name'. - * Returns NULL if when there is no argument of 'name'. - * Returns the value of the argument of 'name' otherwise. - * - * Shortcut for: afb_req_get(req, name).value - */ -static inline const char *afb_req_value(struct afb_req req, const char *name) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets from the request 'req' the string value of the argument of 'name'.</span> +<span class="co"> * Returns NULL if when there is no argument of 'name'.</span> +<span class="co"> * Returns the value of the argument of 'name' otherwise.</span> +<span class="co"> *</span> +<span class="co"> * Shortcut for: afb_req_get(req, name).value</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="kw">inline</span> <span class="dt">const</span> <span class="dt">char</span> *afb_req_value(<span class="kw">struct</span> afb_req req, <span class="dt">const</span> <span class="dt">char</span> *name) { - return afb_req_get(req, name).value; -} -</code></pre> - -<p>It is defined as a shortcut to call the function <strong>afb_req_get</strong>. -That function is defined here after:</p> - -<pre><code>/* - * Gets from the request 'req' the argument of 'name'. - * Returns a PLAIN structure of type 'struct afb_arg'. - * When the argument of 'name' is not found, all fields of result are set to NULL. - * When the argument of 'name' is found, the fields are filled, - * in particular, the field 'result.name' is set to 'name'. - * - * There is a special name value: the empty string. - * The argument of name "" is defined only if the request was made using - * an HTTP POST of Content-Type "application/json". In that case, the - * argument of name "" receives the value of the body of the HTTP request. - */ -struct afb_arg afb_req_get(struct afb_req req, const char *name); -</code></pre> - -<p>That function takes 2 parameters: the request and the name -of the argument to retrieve. It returns a PLAIN structure of -type <strong>struct afb_arg</strong>.</p> - -<p>There is a special name that is defined when the request is -of type HTTP/POST with a Content-Type being application/json. -This name is <strong>“”</strong> (the empty string). In that case, the value -of this argument of empty name is the string received as a body -of the post and is supposed to be a JSON string.</p> - + <span class="kw">return</span> afb_req_get(req, name).value; +}</code></pre> +<p>It is defined as a shortcut to call the function <strong>afb_req_get</strong>. That function is defined here after:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets from the request 'req' the argument of 'name'.</span> +<span class="co"> * Returns a PLAIN structure of type 'struct afb_arg'.</span> +<span class="co"> * When the argument of 'name' is not found, all fields of result are set to NULL.</span> +<span class="co"> * When the argument of 'name' is found, the fields are filled,</span> +<span class="co"> * in particular, the field 'result.name' is set to 'name'.</span> +<span class="co"> *</span> +<span class="co"> * There is a special name value: the empty string.</span> +<span class="co"> * The argument of name "" is defined only if the request was made using</span> +<span class="co"> * an HTTP POST of Content-Type "application/json". In that case, the</span> +<span class="co"> * argument of name "" receives the value of the body of the HTTP request.</span> +<span class="co"> */</span> +<span class="kw">struct</span> afb_arg afb_req_get(<span class="kw">struct</span> afb_req req, <span class="dt">const</span> <span class="dt">char</span> *name);</code></pre> +<p>That function takes 2 parameters: the request and the name of the argument to retrieve. It returns a PLAIN structure of type <strong>struct afb_arg</strong>.</p> +<p>There is a special name that is defined when the request is of type HTTP/POST with a Content-Type being application/json. This name is <strong>""</strong> (the empty string). In that case, the value of this argument of empty name is the string received as a body of the post and is supposed to be a JSON string.</p> <p>The definition of <strong>struct afb_arg</strong> is:</p> - -<pre><code>/* - * Describes an argument (or parameter) of a request - */ -struct afb_arg { - const char *name; /* name of the argument or NULL if invalid */ - const char *value; /* string representation of the value of the argument */ - /* original filename of the argument if path != NULL */ - const char *path; /* if not NULL, path of the received file for the argument */ - /* when the request is finalized this file is removed */ -}; -</code></pre> - -<p>The structure returns the data arguments that are known for the -request. This data include a field named <strong>path</strong>. This <strong>path</strong> -can be accessed using the function <strong>afb_req_path</strong> defined here after:</p> - -<pre><code>/* - * Gets from the request 'req' the path for file attached to the argument of 'name'. - * Returns NULL if when there is no argument of 'name' or when there is no file. - * Returns the path of the argument of 'name' otherwise. - * - * Shortcut for: afb_req_get(req, name).path - */ -static inline const char *afb_req_path(struct afb_req req, const char *name) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Describes an argument (or parameter) of a request</span> +<span class="co"> */</span> +<span class="kw">struct</span> afb_arg { + <span class="dt">const</span> <span class="dt">char</span> *name; <span class="co">/* name of the argument or NULL if invalid */</span> + <span class="dt">const</span> <span class="dt">char</span> *value; <span class="co">/* string representation of the value of the argument */</span> + <span class="co">/* original filename of the argument if path != NULL */</span> + <span class="dt">const</span> <span class="dt">char</span> *path; <span class="co">/* if not NULL, path of the received file for the argument */</span> + <span class="co">/* when the request is finalized this file is removed */</span> +};</code></pre> +<p>The structure returns the data arguments that are known for the request. This data include a field named <strong>path</strong>. This <strong>path</strong> can be accessed using the function <strong>afb_req_path</strong> defined here after:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets from the request 'req' the path for file attached to the argument of 'name'.</span> +<span class="co"> * Returns NULL if when there is no argument of 'name' or when there is no file.</span> +<span class="co"> * Returns the path of the argument of 'name' otherwise.</span> +<span class="co"> *</span> +<span class="co"> * Shortcut for: afb_req_get(req, name).path</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="kw">inline</span> <span class="dt">const</span> <span class="dt">char</span> *afb_req_path(<span class="kw">struct</span> afb_req req, <span class="dt">const</span> <span class="dt">char</span> *name) { - return afb_req_get(req, name).path; -} -</code></pre> - + <span class="kw">return</span> afb_req_get(req, name).path; +}</code></pre> <p>The path is only defined for HTTP/POST requests that send file.</p> - -<a name="Arguments.for.received.files"></a> -<h3>Arguments for received files</h3> - +<h3 id="arguments-for-received-files">Arguments for received files</h3> <p>As it is explained above, clients can send files using HTTP/POST requests.</p> - -<p>Received files are attached to “file” argument name. For example, the -following HTTP fragment (from test/sample-post.html) -will send an HTTP/POST request to the method -<strong>post/upload-image</strong> with 2 arguments named <em>file</em> and -<em>hidden</em>.</p> - -<pre><code><h2>Sample Post File</h2> -<form enctype="multipart/form-data"> - <input type="file" name="file" /> - <input type="hidden" name="hidden" value="bollobollo" /> - <br> - <button formmethod="POST" formaction="api/post/upload-image">Post File</button> -</form> -</code></pre> - +<p>Received files are attached to "file" argument name. For example, the following HTTP fragment (from test/sample-post.html) will send an HTTP/POST request to the method <strong>post/upload-image</strong> with 2 arguments named <em>file</em> and <em>hidden</em>.</p> +<pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><h2></span>Sample Post File<span class="kw"></h2></span> +<span class="kw"><form</span><span class="ot"> enctype=</span><span class="st">"multipart/form-data"</span><span class="kw">></span> + <span class="kw"><input</span><span class="ot"> type=</span><span class="st">"file"</span><span class="ot"> name=</span><span class="st">"file"</span> <span class="kw">/></span> + <span class="kw"><input</span><span class="ot"> type=</span><span class="st">"hidden"</span><span class="ot"> name=</span><span class="st">"hidden"</span><span class="ot"> value=</span><span class="st">"bollobollo"</span> <span class="kw">/></span> + <span class="kw"><br></span> + <span class="kw"><button</span><span class="ot"> formmethod=</span><span class="st">"POST"</span><span class="ot"> formaction=</span><span class="st">"api/post/upload-image"</span><span class="kw">></span>Post File<span class="kw"></button></span> +<span class="kw"></form></span></code></pre> <p>Argument named <strong>file</strong> should have both its value and path defined.</p> - -<p>The value is the name of the file as it was set by the HTTP client. -Generally it is the filename on client side.</p> - -<p>The path is the effective path of saved file on the temporary local storage -area of the application. This is a randomly generated and unique filename. -It is not linked with the original filename as used on client side.</p> - -<p>After success the plugin can use the uploaded file directly from local storage path with no restriction: -read, write, remove, copy, rename… -Nevertheless when request reply is set and query terminated, the uploaded temporary file at -path is destroyed.</p> - -<a name="Arguments.as.a.JSON.object"></a> -<h3>Arguments as a JSON object</h3> - -<p>Plugins may also request every arguments of a given call as one single object. -This feature is provided by the function <strong>afb_req_json</strong> defined here after:</p> - -<pre><code>/* - * Gets from the request 'req' the json object hashing the arguments. - * The returned object must not be released using 'json_object_put'. - */ -struct json_object *afb_req_json(struct afb_req req); -</code></pre> - +<p>The value is the name of the file as it was set by the HTTP client. Generally it is the filename on client side.</p> +<p>The path is the effective path of saved file on the temporary local storage area of the application. This is a randomly generated and unique filename. It is not linked with the original filename as used on client side.</p> +<p>After success the plugin can use the uploaded file directly from local storage path with no restriction: read, write, remove, copy, rename... Nevertheless when request reply is set and query terminated, the uploaded temporary file at path is destroyed.</p> +<h3 id="arguments-as-a-json-object">Arguments as a JSON object</h3> +<p>Plugins may also request every arguments of a given call as one single object. This feature is provided by the function <strong>afb_req_json</strong> defined here after:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Gets from the request 'req' the json object hashing the arguments.</span> +<span class="co"> * The returned object must not be released using 'json_object_put'.</span> +<span class="co"> */</span> +<span class="kw">struct</span> json_object *afb_req_json(<span class="kw">struct</span> afb_req req);</code></pre> <p>It returns a json object. This object depends on how the request was built:</p> - <ul> -<li><p>For HTTP requests, this json object uses key names mapped on argument name. -Values are either string for common arguments or object ie: { “file”: “…”, “path”: “…” }</p></li> +<li><p>For HTTP requests, this json object uses key names mapped on argument name. Values are either string for common arguments or object ie: { "file": "...", "path": "..." }</p></li> <li><p>For WebSockets requests, returned directly the object as provided by the client.</p></li> </ul> - - -<blockquote><p>In fact, for Websockets requests, the function <strong>afb_req_value</strong> -can be seen as a shortcut to -<strong><em>json_object_get_string(json_object_object_get(afb_req_json(req), name))</em></strong></p></blockquote> - -<a name="Initialisation.of.the.plugin.and.declaration.of.methods"></a> -<h2>Initialisation of the plugin and declaration of methods</h2> - -<p>To be active, plugin’s methods should be declared to -afb-daemon. Furthermore, the plugin itself must be recorded.</p> - -<p>The registration mechanism is very basic: when afb-need starts, -it loads all plugins listed in: command line or configuration file.</p> - +<blockquote> +<p>In fact, for Websockets requests, the function <strong>afb_req_value</strong> can be seen as a shortcut to <strong><em>json_object_get_string(json_object_object_get(afb_req_json(req), name))</em></strong></p> +</blockquote> +<h2 id="initialisation-of-the-plugin-and-declaration-of-methods">Initialisation of the plugin and declaration of methods</h2> +<p>To be active, plugin's methods should be declared to afb-daemon. Furthermore, the plugin itself must be recorded.</p> +<p>The registration mechanism is very basic: when afb-need starts, it loads all plugins listed in: command line or configuration file.</p> <p>Loading a plugin follows the following steps:</p> - -<ol> +<ol type="1"> <li><p>Afb-daemon loads the plugin with <em>dlopen</em>.</p></li> -<li><p>Afb-daemon searches for a symbol named <strong>pluginAfbV1Register</strong> using <em>dlsym</em>. -This symbol is assumed to be the exported initialisation function of the plugin.</p></li> +<li><p>Afb-daemon searches for a symbol named <strong>pluginAfbV1Register</strong> using <em>dlsym</em>. This symbol is assumed to be the exported initialisation function of the plugin.</p></li> <li><p>Afb-daemon builds an interface object for the plugin.</p></li> -<li><p>Afb-daemon calls the found function <strong>pluginAfbV1Register</strong> with interface pointer -as parameter.</p></li> +<li><p>Afb-daemon calls the found function <strong>pluginAfbV1Register</strong> with interface pointer as parameter.</p></li> <li><p>Function <strong>pluginAfbV1Register</strong> setups the plugin and initialises it.</p></li> -<li><p>Function <strong>pluginAfbV1Register</strong> returns the pointer to a structure -describing the plugin: version, name (prefix or API name), and list of methods.</p></li> -<li><p>Afb-daemon checks that the returned version and name can be managed. -If so, plugin and its methods are register to become usable as soon as -afb-daemon initialisation is finished.</p></li> +<li><p>Function <strong>pluginAfbV1Register</strong> returns the pointer to a structure describing the plugin: version, name (prefix or API name), and list of methods.</p></li> +<li><p>Afb-daemon checks that the returned version and name can be managed. If so, plugin and its methods are register to become usable as soon as afb-daemon initialisation is finished.</p></li> </ol> - - <p>Here after the code used for <strong>pluginAfbV1Register</strong> from plugin <em>tic-tac-toe</em>:</p> - -<pre><code>/* - * activation function for registering the plugin called by afb-daemon - */ -const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * activation function for registering the plugin called by afb-daemon</span> +<span class="co"> */</span> +<span class="dt">const</span> <span class="kw">struct</span> AFB_plugin *pluginAfbV1Register(<span class="dt">const</span> <span class="kw">struct</span> AFB_interface *itf) { - afbitf = itf; // records the interface for accessing afb-daemon - return &plugin_description; // returns the description of the plugin -} -</code></pre> - -<p>It is a very minimal initialisation function because <em>tic-tac-toe</em> plugin doesn’t -have any application related initialisation step. It merely record daemon’s interface -and returns its description.</p> - -<p>The variable <strong>afbitf</strong> is a plugin global variable. It keeps the -interface to afb-daemon that should be used for logging and pushing events. -Here is its declaration:</p> - -<pre><code>/* - * the interface to afb-daemon - */ -const struct AFB_interface *afbitf; -</code></pre> - + afbitf = itf; <span class="co">// records the interface for accessing afb-daemon</span> + <span class="kw">return</span> &plugin_description; <span class="co">// returns the description of the plugin</span> +}</code></pre> +<p>It is a very minimal initialisation function because <em>tic-tac-toe</em> plugin doesn't have any application related initialisation step. It merely record daemon's interface and returns its description.</p> +<p>The variable <strong>afbitf</strong> is a plugin global variable. It keeps the interface to afb-daemon that should be used for logging and pushing events. Here is its declaration:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * the interface to afb-daemon</span> +<span class="co"> */</span> +<span class="dt">const</span> <span class="kw">struct</span> AFB_interface *afbitf;</code></pre> <p>The description of the plugin is defined here after.</p> - -<pre><code>/* - * array of the methods exported to afb-daemon - */ -static const struct AFB_method_desc_v1 plugin_methods[] = { - /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ - { .name= "new", .session= AFB_SESSION_NONE, .callback= new, .info= "Starts a new game" }, - { .name= "play", .session= AFB_SESSION_NONE, .callback= play, .info= "Asks the server to play" }, - { .name= "move", .session= AFB_SESSION_NONE, .callback= move, .info= "Tells the client move" }, - { .name= "board", .session= AFB_SESSION_NONE, .callback= board, .info= "Get the current board" }, - { .name= "level", .session= AFB_SESSION_NONE, .callback= level, .info= "Set the server level" }, - { .name= "join", .session= AFB_SESSION_CHECK,.callback= join, .info= "Join a board" }, - { .name= "undo", .session= AFB_SESSION_NONE, .callback= undo, .info= "Undo the last move" }, - { .name= "wait", .session= AFB_SESSION_NONE, .callback= wait, .info= "Wait for a change" }, - { .name= NULL } /* marker for end of the array */ +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * array of the methods exported to afb-daemon</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">const</span> <span class="kw">struct</span> AFB_method_desc_v1 plugin_methods[] = { + <span class="co">/* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */</span> + { .name= <span class="st">"new"</span>, .session= AFB_SESSION_NONE, .callback= new, .info= <span class="st">"Starts a new game"</span> }, + { .name= <span class="st">"play"</span>, .session= AFB_SESSION_NONE, .callback= play, .info= <span class="st">"Asks the server to play"</span> }, + { .name= <span class="st">"move"</span>, .session= AFB_SESSION_NONE, .callback= move, .info= <span class="st">"Tells the client move"</span> }, + { .name= <span class="st">"board"</span>, .session= AFB_SESSION_NONE, .callback= board, .info= <span class="st">"Get the current board"</span> }, + { .name= <span class="st">"level"</span>, .session= AFB_SESSION_NONE, .callback= level, .info= <span class="st">"Set the server level"</span> }, + { .name= <span class="st">"join"</span>, .session= AFB_SESSION_CHECK,.callback= join, .info= <span class="st">"Join a board"</span> }, + { .name= <span class="st">"undo"</span>, .session= AFB_SESSION_NONE, .callback= undo, .info= <span class="st">"Undo the last move"</span> }, + { .name= <span class="st">"wait"</span>, .session= AFB_SESSION_NONE, .callback= wait, .info= <span class="st">"Wait for a change"</span> }, + { .name= NULL } <span class="co">/* marker for end of the array */</span> }; -/* - * description of the plugin for afb-daemon - */ -static const struct AFB_plugin plugin_description = +<span class="co">/*</span> +<span class="co"> * description of the plugin for afb-daemon</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">const</span> <span class="kw">struct</span> AFB_plugin plugin_description = { - /* description conforms to VERSION 1 */ + <span class="co">/* description conforms to VERSION 1 */</span> .type= AFB_PLUGIN_VERSION_1, - .v1= { /* fills the v1 field of the union when AFB_PLUGIN_VERSION_1 */ - .prefix= "tictactoe", /* the API name (or plugin name or prefix) */ - .info= "Sample tac-tac-toe game", /* short description of of the plugin */ - .methods = plugin_methods /* the array describing the methods of the API */ + .v1= { <span class="co">/* fills the v1 field of the union when AFB_PLUGIN_VERSION_1 */</span> + .prefix= <span class="st">"tictactoe"</span>, <span class="co">/* the API name (or plugin name or prefix) */</span> + .info= <span class="st">"Sample tac-tac-toe game"</span>, <span class="co">/* short description of of the plugin */</span> + .methods = plugin_methods <span class="co">/* the array describing the methods of the API */</span> } -}; -</code></pre> - -<p>The structure <strong>plugin_description</strong> describes the plugin. -It declares the type and version of the plugin, its name, a short description -and its methods list.</p> - +};</code></pre> +<p>The structure <strong>plugin_description</strong> describes the plugin. It declares the type and version of the plugin, its name, a short description and its methods list.</p> <p>The list of methods is an array of structures describing the methods and terminated by a NULL marker.</p> - <p>In version one of afb-damon plugin, a method description contains 4 fields:</p> - <ul> <li><p>the name of the method,</p></li> <li><p>the session management flags,</p></li> <li><p>the implementation function to be call for the method,</p></li> <li><p>a short description.</p></li> </ul> - - <p>The structure describing methods is defined as follows:</p> - -<pre><code>/* - * Description of one method of the API provided by the plugin - * This enumeration is valid for plugins of type 1 - */ -struct AFB_method_desc_v1 +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Description of one method of the API provided by the plugin</span> +<span class="co"> * This enumeration is valid for plugins of type 1</span> +<span class="co"> */</span> +<span class="kw">struct</span> AFB_method_desc_v1 { - const char *name; /* name of the method */ - enum AFB_session_v1 session; /* authorisation and session requirements of the method */ - void (*callback)(struct afb_req req); /* callback function implementing the method */ - const char *info; /* textual description of the method */ -}; -</code></pre> - -<p>For technical reasons, the enumeration <strong>enum AFB_session_v1</strong> is not exactly an -enumeration but the wrapper of constant definitions that can be mixed using bitwise or -(the C operator |).</p> - + <span class="dt">const</span> <span class="dt">char</span> *name; <span class="co">/* name of the method */</span> + <span class="kw">enum</span> AFB_session_v1 session; <span class="co">/* authorisation and session requirements of the method */</span> + <span class="dt">void</span> (*callback)(<span class="kw">struct</span> afb_req req); <span class="co">/* callback function implementing the method */</span> + <span class="dt">const</span> <span class="dt">char</span> *info; <span class="co">/* textual description of the method */</span> +};</code></pre> +<p>For technical reasons, the enumeration <strong>enum AFB_session_v1</strong> is not exactly an enumeration but the wrapper of constant definitions that can be mixed using bitwise or (the C operator |).</p> <p>The constants that can bit mixed are:</p> - <table> <thead> -<tr> -<th>Constant name </th> -<th> Meaning</th> +<tr class="header"> +<th style="text-align: left;">Constant name</th> +<th style="text-align: left;">Meaning</th> </tr> </thead> <tbody> -<tr> -<td><strong>AFB_SESSION_CREATE</strong> </td> -<td> Equals to AFB_SESSION_LOA_EQ_0|AFB_SESSION_RENEW</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_CREATE</strong></td> +<td style="text-align: left;">Equals to AFB_SESSION_LOA_EQ_0</td> </tr> -<tr> -<td><strong>AFB_SESSION_CLOSE</strong> </td> -<td> Closes the session after the reply and set the LOA to 0</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_CLOSE</strong></td> +<td style="text-align: left;">Closes the session after the reply and set the LOA to 0</td> </tr> -<tr> -<td><strong>AFB_SESSION_RENEW</strong> </td> -<td> Refreshes the token of authentification</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_RENEW</strong></td> +<td style="text-align: left;">Refreshes the token of authentification</td> </tr> -<tr> -<td><strong>AFB_SESSION_CHECK</strong> </td> -<td> Just requires the token authentification</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_CHECK</strong></td> +<td style="text-align: left;">Just requires the token authentification</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_LE_0</strong> </td> -<td> Requires the current LOA to be lesser then or equal to 0</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_LE_0</strong></td> +<td style="text-align: left;">Requires the current LOA to be lesser then or equal to 0</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_LE_1</strong> </td> -<td> Requires the current LOA to be lesser then or equal to 1</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_LE_1</strong></td> +<td style="text-align: left;">Requires the current LOA to be lesser then or equal to 1</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_LE_2</strong> </td> -<td> Requires the current LOA to be lesser then or equal to 2</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_LE_2</strong></td> +<td style="text-align: left;">Requires the current LOA to be lesser then or equal to 2</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_LE_3</strong> </td> -<td> Requires the current LOA to be lesser then or equal to 3</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_LE_3</strong></td> +<td style="text-align: left;">Requires the current LOA to be lesser then or equal to 3</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_GE_0</strong> </td> -<td> Requires the current LOA to be greater then or equal to 0</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_GE_0</strong></td> +<td style="text-align: left;">Requires the current LOA to be greater then or equal to 0</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_GE_1</strong> </td> -<td> Requires the current LOA to be greater then or equal to 1</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_GE_1</strong></td> +<td style="text-align: left;">Requires the current LOA to be greater then or equal to 1</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_GE_2</strong> </td> -<td> Requires the current LOA to be greater then or equal to 2</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_GE_2</strong></td> +<td style="text-align: left;">Requires the current LOA to be greater then or equal to 2</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_GE_3</strong> </td> -<td> Requires the current LOA to be greater then or equal to 3</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_GE_3</strong></td> +<td style="text-align: left;">Requires the current LOA to be greater then or equal to 3</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_EQ_0</strong> </td> -<td> Requires the current LOA to be equal to 0</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_EQ_0</strong></td> +<td style="text-align: left;">Requires the current LOA to be equal to 0</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_EQ_1</strong> </td> -<td> Requires the current LOA to be equal to 1</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_EQ_1</strong></td> +<td style="text-align: left;">Requires the current LOA to be equal to 1</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_EQ_2</strong> </td> -<td> Requires the current LOA to be equal to 2</td> +<tr class="odd"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_EQ_2</strong></td> +<td style="text-align: left;">Requires the current LOA to be equal to 2</td> </tr> -<tr> -<td><strong>AFB_SESSION_LOA_EQ_3</strong> </td> -<td> Requires the current LOA to be equal to 3</td> +<tr class="even"> +<td style="text-align: left;"><strong>AFB_SESSION_LOA_EQ_3</strong></td> +<td style="text-align: left;">Requires the current LOA to be equal to 3</td> </tr> </tbody> </table> - - -<p>If any of this flag is set, afb-daemon requires an authentication token -as if <strong>AFB_SESSION_CHECK</strong> flag was also set.</p> - +<p>If any of this flag is set, afb-daemon requires an authentication token as if <strong>AFB_SESSION_CHECK</strong> flag was also set.</p> <p>The special value <strong>AFB_SESSION_NONE</strong> is zero and can be used to bypass token check.</p> - -<blockquote><p>Note that <strong>AFB_SESSION_CREATE</strong> and <strong>AFB_SESSION_CLOSE</strong> might be removed in later versions.</p></blockquote> - -<a name="Sending.messages.to.the.log.system"></a> -<h2>Sending messages to the log system</h2> - +<blockquote> +<p>Note that <strong>AFB_SESSION_CREATE</strong> and <strong>AFB_SESSION_CLOSE</strong> might be removed in later versions.</p> +</blockquote> +<h2 id="sending-messages-to-the-log-system">Sending messages to the log system</h2> <p>Afb-daemon provides 4 levels of verbosity and 5 methods for logging messages.</p> - -<p>The verbosity is managed. Options allow the change the verbosity of afb-daemon -and the verbosity of the plugins can be set plugin by plugin.</p> - -<p>The methods for logging messages are defined as macros that test the -verbosity level and that call the real logging function only if the -message must be output. This avoid evaluation of arguments of the -formatting messages if the message must not be output.</p> - -<a name="Verbs.for.logging.messages"></a> -<h3>Verbs for logging messages</h3> - +<p>The verbosity is managed. Options allow the change the verbosity of afb-daemon and the verbosity of the plugins can be set plugin by plugin.</p> +<p>The methods for logging messages are defined as macros that test the verbosity level and that call the real logging function only if the message must be output. This avoid evaluation of arguments of the formatting messages if the message must not be output.</p> +<h3 id="verbs-for-logging-messages">Verbs for logging messages</h3> <p>The 5 logging methods are:</p> - <table> <thead> -<tr> -<th>Macro </th> -<th style="text-align:center;"> Verbosity </th> -<th> Meaning </th> -<th style="text-align:center;"> syslog level</th> +<tr class="header"> +<th style="text-align: left;">Macro</th> +<th style="text-align: center;">Verbosity</th> +<th style="text-align: left;">Meaning</th> +<th style="text-align: center;">syslog level</th> </tr> </thead> <tbody> -<tr> -<td>ERROR </td> -<td style="text-align:center;"> 0 </td> -<td> Error conditions </td> -<td style="text-align:center;"> 3</td> -</tr> -<tr> -<td>WARNING </td> -<td style="text-align:center;"> 1 </td> -<td> Warning conditions </td> -<td style="text-align:center;"> 4</td> -</tr> -<tr> -<td>NOTICE </td> -<td style="text-align:center;"> 1 </td> -<td> Normal but significant condition </td> -<td style="text-align:center;"> 5</td> -</tr> -<tr> -<td>INFO </td> -<td style="text-align:center;"> 2 </td> -<td> Informational </td> -<td style="text-align:center;"> 6</td> -</tr> -<tr> -<td>DEBUG </td> -<td style="text-align:center;"> 3 </td> -<td> Debug-level messages </td> -<td style="text-align:center;"> 7</td> +<tr class="odd"> +<td style="text-align: left;">ERROR</td> +<td style="text-align: center;">0</td> +<td style="text-align: left;">Error conditions</td> +<td style="text-align: center;">3</td> +</tr> +<tr class="even"> +<td style="text-align: left;">WARNING</td> +<td style="text-align: center;">1</td> +<td style="text-align: left;">Warning conditions</td> +<td style="text-align: center;">4</td> +</tr> +<tr class="odd"> +<td style="text-align: left;">NOTICE</td> +<td style="text-align: center;">1</td> +<td style="text-align: left;">Normal but significant condition</td> +<td style="text-align: center;">5</td> +</tr> +<tr class="even"> +<td style="text-align: left;">INFO</td> +<td style="text-align: center;">2</td> +<td style="text-align: left;">Informational</td> +<td style="text-align: center;">6</td> +</tr> +<tr class="odd"> +<td style="text-align: left;">DEBUG</td> +<td style="text-align: center;">3</td> +<td style="text-align: left;">Debug-level messages</td> +<td style="text-align: center;">7</td> </tr> </tbody> </table> - - -<p>You can note that the 2 methods <strong>WARNING</strong> and <strong>INFO</strong> have the same level -of verbosity. But they don’t have the same <em>syslog level</em>. It means that -they are output with a different level on the logging system.</p> - +<p>You can note that the 2 methods <strong>WARNING</strong> and <strong>INFO</strong> have the same level of verbosity. But they don't have the same <em>syslog level</em>. It means that they are output with a different level on the logging system.</p> <p>All of these methods have the same signature:</p> - -<pre><code>void ERROR(const struct AFB_interface *afbitf, const char *message, ...); -</code></pre> - -<p>The first argument <strong>afbitf</strong> is the interface to afb daemon that the -plugin received at initialisation time when <strong>pluginAfbV1Register</strong> is called.</p> - +<pre class="sourceCode c"><code class="sourceCode c"><span class="dt">void</span> ERROR(<span class="dt">const</span> <span class="kw">struct</span> AFB_interface *afbitf, <span class="dt">const</span> <span class="dt">char</span> *message, ...);</code></pre> +<p>The first argument <strong>afbitf</strong> is the interface to afb daemon that the plugin received at initialisation time when <strong>pluginAfbV1Register</strong> is called.</p> <p>The second argument <strong>message</strong> is a formatting string compatible with printf/sprintf.</p> - <p>The remaining arguments are arguments of the formating message like with printf.</p> - -<a name="Managing.verbosity"></a> -<h3>Managing verbosity</h3> - -<p>Depending on the level of verbosity, the messages are output or not. -The following table explains what messages will be output depending -ont the verbosity level.</p> - +<h3 id="managing-verbosity">Managing verbosity</h3> +<p>Depending on the level of verbosity, the messages are output or not. The following table explains what messages will be output depending ont the verbosity level.</p> <table> <thead> -<tr> -<th style="text-align:center;">Level of verbosity </th> -<th> Outputed macro</th> +<tr class="header"> +<th style="text-align: center;">Level of verbosity</th> +<th style="text-align: left;">Outputed macro</th> </tr> </thead> <tbody> -<tr> -<td style="text-align:center;">0 </td> -<td> ERROR</td> +<tr class="odd"> +<td style="text-align: center;">0</td> +<td style="text-align: left;">ERROR</td> </tr> -<tr> -<td style="text-align:center;">1 </td> -<td> ERROR + WARNING + NOTICE</td> +<tr class="even"> +<td style="text-align: center;">1</td> +<td style="text-align: left;">ERROR + WARNING + NOTICE</td> </tr> -<tr> -<td style="text-align:center;">2 </td> -<td> ERROR + WARNING + NOTICE + INFO</td> +<tr class="odd"> +<td style="text-align: center;">2</td> +<td style="text-align: left;">ERROR + WARNING + NOTICE + INFO</td> </tr> -<tr> -<td style="text-align:center;">3 </td> -<td> ERROR + WARNING + NOTICE + INFO + DEBUG</td> +<tr class="even"> +<td style="text-align: center;">3</td> +<td style="text-align: left;">ERROR + WARNING + NOTICE + INFO + DEBUG</td> </tr> </tbody> </table> - - -<a name="Output.format.and.destination"></a> -<h3>Output format and destination</h3> - -<p>The syslog level is used for forging a prefix to the message. -The prefixes are:</p> - +<h3 id="output-format-and-destination">Output format and destination</h3> +<p>The syslog level is used for forging a prefix to the message. The prefixes are:</p> <table> <thead> -<tr> -<th style="text-align:center;">syslog level </th> -<th> prefix</th> +<tr class="header"> +<th style="text-align: center;">syslog level</th> +<th style="text-align: left;">prefix</th> </tr> </thead> <tbody> -<tr> -<td style="text-align:center;">0 </td> -<td> <0> EMERGENCY</td> +<tr class="odd"> +<td style="text-align: center;">0</td> +<td style="text-align: left;"><0> EMERGENCY</td> </tr> -<tr> -<td style="text-align:center;">1 </td> -<td> <1> ALERT</td> +<tr class="even"> +<td style="text-align: center;">1</td> +<td style="text-align: left;"><1> ALERT</td> </tr> -<tr> -<td style="text-align:center;">2 </td> -<td> <2> CRITICAL</td> +<tr class="odd"> +<td style="text-align: center;">2</td> +<td style="text-align: left;"><2> CRITICAL</td> </tr> -<tr> -<td style="text-align:center;">3 </td> -<td> <3> ERROR</td> +<tr class="even"> +<td style="text-align: center;">3</td> +<td style="text-align: left;"><3> ERROR</td> </tr> -<tr> -<td style="text-align:center;">4 </td> -<td> <4> WARNING</td> +<tr class="odd"> +<td style="text-align: center;">4</td> +<td style="text-align: left;"><4> WARNING</td> </tr> -<tr> -<td style="text-align:center;">5 </td> -<td> <5> NOTICE</td> +<tr class="even"> +<td style="text-align: center;">5</td> +<td style="text-align: left;"><5> NOTICE</td> </tr> -<tr> -<td style="text-align:center;">6 </td> -<td> <6> INFO</td> +<tr class="odd"> +<td style="text-align: center;">6</td> +<td style="text-align: left;"><6> INFO</td> </tr> -<tr> -<td style="text-align:center;">7 </td> -<td> <7> DEBUG</td> +<tr class="even"> +<td style="text-align: center;">7</td> +<td style="text-align: left;"><7> DEBUG</td> </tr> </tbody> </table> - - -<p>The message is pushed to standard error. -The final destination of the message depends on how systemd service -was configured through its variable <strong>StandardError</strong>. It can be -journal, syslog or kmsg. (See man sd-daemon).</p> - -<a name="Sending.events"></a> -<h2>Sending events</h2> - -<p>Since version 0.5, plugins can broadcast events to any potential listener. -As today only unattended even are supported. Targeted events are expected for next -coming version.</p> - -<p>The plugin <em>tic-tac-toe</em> broadcasts events when the board changes. -This is done in the function <strong>changed</strong>:</p> - -<pre><code>/* - * signals a change of the board - */ -static void changed(struct board *board, const char *reason) +<p>The message is pushed to standard error. The final destination of the message depends on how systemd service was configured through its variable <strong>StandardError</strong>. It can be journal, syslog or kmsg. (See man sd-daemon).</p> +<h2 id="sending-events">Sending events</h2> +<p>Since version 0.5, plugins can broadcast events to any potential listener. As today only unattended even are supported. Targeted events are expected for next coming version.</p> +<p>The plugin <em>tic-tac-toe</em> broadcasts events when the board changes. This is done in the function <strong>changed</strong>:</p> +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * signals a change of the board</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">void</span> changed(<span class="kw">struct</span> board *board, <span class="dt">const</span> <span class="dt">char</span> *reason) { - ... - struct json_object *description; + ... + <span class="kw">struct</span> json_object *description; - /* get the description */ - description = describe(board); + <span class="co">/* get the description */</span> + description = describe(board); - ... - - afb_daemon_broadcast_event(afbitf->daemon, reason, description); -} -</code></pre> + ... + afb_daemon_broadcast_event(afbitf->daemon, reason, description); +}</code></pre> <p>The description of the changed board is pushed via the daemon interface.</p> - -<p>Within plugin <em>tic-tac-toe</em>, <em>reason</em> indicates the origin of -the change. In function <strong>afb_daemon_broadcast_event</strong> the second -parameter is the name of broadcasted event. The third argument is the -object that is transmitted with the event.</p> - +<p>Within plugin <em>tic-tac-toe</em>, <em>reason</em> indicates the origin of the change. In function <strong>afb_daemon_broadcast_event</strong> the second parameter is the name of broadcasted event. The third argument is the object that is transmitted with the event.</p> <p>Function <strong>afb_daemon_broadcast_event</strong> is defined here after:</p> - -<pre><code>/* - * Broadcasts widely the event of 'name' with the data 'object'. - * 'object' can be NULL. - * 'daemon' MUST be the daemon given in interface when activating the plugin. - * - * For conveniency, the function calls 'json_object_put' for 'object'. - * Thus, in the case where 'object' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ -void afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object); -</code></pre> - -<blockquote><p>Be aware, as with reply functions <strong>object</strong> is automatically released using -<strong>json_object_put</strong> when using this function. Call <strong>json_object_get</strong> before -calling <strong>afb_daemon_broadcast_event</strong> to keep <strong>object</strong> available -after function returns.</p></blockquote> - -<p>Event name received by listeners is prefixed with plugin name. -So when a change occurs after a move, the reason is <strong>move</strong> and every clients -receive an event <strong>tictactoe/move</strong>.</p> - -<blockquote><p>Note that nothing is said about case sensitivity of event names. -However, the event is always prefixed with the name that the plugin -declared, with the same case, followed with a slash /. -Thus it is safe to compare event using a case sensitive comparison.</p></blockquote> - -<a name="Writing.an.asynchronous.method.implementation"></a> -<h2>Writing an asynchronous method implementation</h2> - -<p>The <em>tic-tac-toe</em> example allows two clients or more to share the same board. -This is implemented by the method <strong>join</strong> that illustrated partly how to -retrieve arguments.</p> - -<p>When two or more clients are sharing a same board, one of them can wait -until the state of the board changes, but this could also be implemented using -events because an even is generated each time the board changes.</p> - -<p>In this case, the reply to the wait is sent only when the board changes. -See the diagram below:</p> - +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * Broadcasts widely the event of 'name' with the data 'object'.</span> +<span class="co"> * 'object' can be NULL.</span> +<span class="co"> * 'daemon' MUST be the daemon given in interface when activating the plugin.</span> +<span class="co"> *</span> +<span class="co"> * For conveniency, the function calls 'json_object_put' for 'object'.</span> +<span class="co"> * Thus, in the case where 'object' should remain available after</span> +<span class="co"> * the function returns, the function 'json_object_get' shall be used.</span> +<span class="co"> */</span> +<span class="dt">void</span> afb_daemon_broadcast_event(<span class="kw">struct</span> afb_daemon daemon, <span class="dt">const</span> <span class="dt">char</span> *name, <span class="kw">struct</span> json_object *object);</code></pre> +<blockquote> +<p>Be aware, as with reply functions <strong>object</strong> is automatically released using <strong>json_object_put</strong> when using this function. Call <strong>json_object_get</strong> before calling <strong>afb_daemon_broadcast_event</strong> to keep <strong>object</strong> available after function returns.</p> +</blockquote> +<p>Event name received by listeners is prefixed with plugin name. So when a change occurs after a move, the reason is <strong>move</strong> and every clients receive an event <strong>tictactoe/move</strong>.</p> +<blockquote> +<p>Note that nothing is said about case sensitivity of event names. However, the event is always prefixed with the name that the plugin declared, with the same case, followed with a slash /. Thus it is safe to compare event using a case sensitive comparison.</p> +</blockquote> +<h2 id="writing-an-asynchronous-method-implementation">Writing an asynchronous method implementation</h2> +<p>The <em>tic-tac-toe</em> example allows two clients or more to share the same board. This is implemented by the method <strong>join</strong> that illustrated partly how to retrieve arguments.</p> +<p>When two or more clients are sharing a same board, one of them can wait until the state of the board changes, but this could also be implemented using events because an even is generated each time the board changes.</p> +<p>In this case, the reply to the wait is sent only when the board changes. See the diagram below:</p> <pre><code>CLIENT A CLIENT B TIC-TAC-TOE | | | +--------------|----------------->| wait . . . . . . . . @@ -1261,227 +811,141 @@ See the diagram below:</p> | | | V . | |<-----------------+ success of move . | | | . - |<-------------|------------------+ success of wait < -</code></pre> - -<p>Here, this is an invocation of the plugin by an other client that -unblock the suspended <em>wait</em> call. -Nevertheless in most case this should be a timer, a hardware event, a sync with -a concurrent process or thread, …</p> - + |<-------------|------------------+ success of wait <</code></pre> +<p>Here, this is an invocation of the plugin by an other client that unblock the suspended <em>wait</em> call. Nevertheless in most case this should be a timer, a hardware event, a sync with a concurrent process or thread, ...</p> <p>Common case of an asynchronous implementation.</p> - <p>Here is the listing of the function <strong>wait</strong>:</p> - -<pre><code>static void wait(struct afb_req req) +<pre class="sourceCode c"><code class="sourceCode c"><span class="dt">static</span> <span class="dt">void</span> wait(<span class="kw">struct</span> afb_req req) { - struct board *board; - struct waiter *waiter; - - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'wait' called for boardid %d", board->id); - - /* creates the waiter and enqueues it */ - waiter = calloc(1, sizeof *waiter); - waiter->req = req; - waiter->next = board->waiters; - afb_req_addref(req); - board->waiters = waiter; -} -</code></pre> - -<p>After retrieving the board, the function adds a new waiter to -waiters list and returns without setting a reply.</p> - -<p>Before returning, it increases <strong>req</strong> request’s reference count using <strong>afb_req_addref</strong> function.</p> - -<blockquote><p>When a method returns without setting a reply, -it <strong>MUST</strong> increment request’s reference count -using <strong>afb_req_addref</strong>. If unpredictable behaviour may pop up.</p></blockquote> - -<p>Later, when a board changes, it calls <em>tic-tac-toe</em> <strong>changed</strong> function -with reason of change in parameter.</p> - + <span class="kw">struct</span> board *board; + <span class="kw">struct</span> waiter *waiter; + + <span class="co">/* retrieves the context for the session */</span> + board = board_of_req(req); + INFO(afbitf, <span class="st">"method 'wait' called for boardid %d"</span>, board->id); + + <span class="co">/* creates the waiter and enqueues it */</span> + waiter = calloc(<span class="dv">1</span>, <span class="kw">sizeof</span> *waiter); + waiter->req = req; + waiter->next = board->waiters; + afb_req_addref(req); + board->waiters = waiter; +}</code></pre> +<p>After retrieving the board, the function adds a new waiter to waiters list and returns without setting a reply.</p> +<p>Before returning, it increases <strong>req</strong> request's reference count using <strong>afb_req_addref</strong> function.</p> +<blockquote> +<p>When a method returns without setting a reply, it <strong>MUST</strong> increment request's reference count using <strong>afb_req_addref</strong>. If unpredictable behaviour may pop up.</p> +</blockquote> +<p>Later, when a board changes, it calls <em>tic-tac-toe</em> <strong>changed</strong> function with reason of change in parameter.</p> <p>Here is the full listing of the function <strong>changed</strong>:</p> - -<pre><code>/* - * signals a change of the board - */ -static void changed(struct board *board, const char *reason) +<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*</span> +<span class="co"> * signals a change of the board</span> +<span class="co"> */</span> +<span class="dt">static</span> <span class="dt">void</span> changed(<span class="kw">struct</span> board *board, <span class="dt">const</span> <span class="dt">char</span> *reason) { - struct waiter *waiter, *next; - struct json_object *description; - - /* get the description */ - description = describe(board); - - waiter = board->waiters; - board->waiters = NULL; - while (waiter != NULL) { - next = waiter->next; - afb_req_success(waiter->req, json_object_get(description), reason); - afb_req_unref(waiter->req); - free(waiter); - waiter = next; - } - - afb_event_sender_push(afb_daemon_get_event_sender(afbitf->daemon), reason, description); -} -</code></pre> - -<p>The list of waiters is walked and a reply is sent to each waiter. -After sending the reply, the reference count of the request -is decremented using <strong>afb_req_unref</strong> to allow resources to be freed.</p> - -<blockquote><p>The reference count <strong>MUST</strong> be decremented using <strong>afb_req_unref</strong> to free -resources and avoid memory leaks. -This usage count decrement should happen <strong>AFTER</strong> setting reply or -bad things may happen.</p></blockquote> - -<a name="How.to.build.a.plugin"></a> -<h2>How to build a plugin</h2> - -<p>Afb-daemon provides a <em>pkg-config</em> configuration file that can be -queried by providing <strong>afb-daemon</strong> in command line arguments. -This configuration file provides data that should be used -for plugins compilation. Examples:</p> - -<pre><code>$ pkg-config --cflags afb-daemon -$ pkg-config --libs afb-daemon -</code></pre> - -<a name="Example.for.cmake.meta.build.system"></a> -<h3>Example for cmake meta build system</h3> - + <span class="kw">struct</span> waiter *waiter, *next; + <span class="kw">struct</span> json_object *description; + + <span class="co">/* get the description */</span> + description = describe(board); + + waiter = board->waiters; + board->waiters = NULL; + <span class="kw">while</span> (waiter != NULL) { + next = waiter->next; + afb_req_success(waiter->req, json_object_get(description), reason); + afb_req_unref(waiter->req); + free(waiter); + waiter = next; + } + + afb_event_sender_push(afb_daemon_get_event_sender(afbitf->daemon), reason, description); +}</code></pre> +<p>The list of waiters is walked and a reply is sent to each waiter. After sending the reply, the reference count of the request is decremented using <strong>afb_req_unref</strong> to allow resources to be freed.</p> +<blockquote> +<p>The reference count <strong>MUST</strong> be decremented using <strong>afb_req_unref</strong> to free resources and avoid memory leaks. This usage count decrement should happen <strong>AFTER</strong> setting reply or bad things may happen.</p> +</blockquote> +<h2 id="how-to-build-a-plugin">How to build a plugin</h2> +<p>Afb-daemon provides a <em>pkg-config</em> configuration file that can be queried by providing <strong>afb-daemon</strong> in command line arguments. This configuration file provides data that should be used for plugins compilation. Examples:</p> +<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pkg-config</span> --cflags afb-daemon +$ <span class="kw">pkg-config</span> --libs afb-daemon</code></pre> +<h3 id="example-for-cmake-meta-build-system">Example for cmake meta build system</h3> <p>This example is the extract for building the plugin <em>afm-main</em> using <em>CMAKE</em>.</p> - -<pre><code>pkg_check_modules(afb afb-daemon) -if(afb_FOUND) - message(STATUS "Creation afm-main-plugin for AFB-DAEMON") - add_library(afm-main-plugin MODULE afm-main-plugin.c) - target_compile_options(afm-main-plugin PRIVATE ${afb_CFLAGS}) - target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) - target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) - set_target_properties(afm-main-plugin PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" - ) - install(TARGETS afm-main-plugin LIBRARY DESTINATION ${plugin_dir}) -else() - message(STATUS "Not creating the plugin for AFB-DAEMON") -endif() -</code></pre> - +<pre class="sourceCode cmake"><code class="sourceCode cmake"><span class="fu">pkg_check_modules</span>(afb afb-daemon) +<span class="kw">if</span>(afb_FOUND) + <span class="kw">message</span>(<span class="ot">STATUS</span> <span class="st">"Creation afm-main-plugin for AFB-DAEMON"</span>) + <span class="kw">add_library</span>(afm-main-plugin <span class="ot">MODULE</span> afm-main-plugin.c) + <span class="kw">target_compile_options</span>(afm-main-plugin <span class="ot">PRIVATE</span> <span class="dv">${afb_CFLAGS}</span>) + <span class="kw">target_include_directories</span>(afm-main-plugin <span class="ot">PRIVATE</span> <span class="dv">${afb_INCLUDE_DIRS}</span>) + <span class="kw">target_link_libraries</span>(afm-main-plugin utils <span class="dv">${afb_LIBRARIES}</span>) + <span class="kw">set_target_properties</span>(afm-main-plugin <span class="ot">PROPERTIES</span> + <span class="ot">PREFIX</span> <span class="st">""</span> + <span class="ot">LINK_FLAGS</span> <span class="st">"-Wl,--version-script=</span><span class="dv">${CMAKE_CURRENT_SOURCE_DIR}</span><span class="st">/afm-main-plugin.export-map"</span> + ) + <span class="kw">install</span>(<span class="ot">TARGETS</span> afm-main-plugin <span class="ot">LIBRARY</span> <span class="ot">DESTINATION</span> <span class="dv">${plugin_dir}</span>) +<span class="kw">else</span>() + <span class="kw">message</span>(<span class="ot">STATUS</span> <span class="st">"Not creating the plugin for AFB-DAEMON"</span>) +<span class="kw">endif</span>()</code></pre> <p>Let now describe some of these lines.</p> - -<pre><code>pkg_check_modules(afb afb-daemon) -</code></pre> - -<p>This first lines searches to the <em>pkg-config</em> configuration file for -<strong>afb-daemon</strong>. Resulting data are stored in the following variables:</p> - +<pre class="sourceCode cmake"><code class="sourceCode cmake"><span class="fu">pkg_check_modules</span>(afb afb-daemon)</code></pre> +<p>This first lines searches to the <em>pkg-config</em> configuration file for <strong>afb-daemon</strong>. Resulting data are stored in the following variables:</p> <table> <thead> -<tr> -<th>Variable </th> -<th> Meaning</th> +<tr class="header"> +<th style="text-align: left;">Variable</th> +<th style="text-align: left;">Meaning</th> </tr> </thead> <tbody> -<tr> -<td>afb_FOUND </td> -<td> Set to 1 if afb-daemon plugin development files exist</td> +<tr class="odd"> +<td style="text-align: left;">afb_FOUND</td> +<td style="text-align: left;">Set to 1 if afb-daemon plugin development files exist</td> </tr> -<tr> -<td>afb_LIBRARIES </td> -<td> Only the libraries (w/o the ‘-l’) for compiling afb-daemon plugins</td> +<tr class="even"> +<td style="text-align: left;">afb_LIBRARIES</td> +<td style="text-align: left;">Only the libraries (w/o the '-l') for compiling afb-daemon plugins</td> </tr> -<tr> -<td>afb_LIBRARY_DIRS </td> -<td> The paths of the libraries (w/o the ‘-L’) for compiling afb-daemon plugins</td> +<tr class="odd"> +<td style="text-align: left;">afb_LIBRARY_DIRS</td> +<td style="text-align: left;">The paths of the libraries (w/o the '-L') for compiling afb-daemon plugins</td> </tr> -<tr> -<td>afb_LDFLAGS </td> -<td> All required linker flags for compiling afb-daemon plugins</td> +<tr class="even"> +<td style="text-align: left;">afb_LDFLAGS</td> +<td style="text-align: left;">All required linker flags for compiling afb-daemon plugins</td> </tr> -<tr> -<td>afb_INCLUDE_DIRS </td> -<td> The ‘-I’ preprocessor flags (w/o the ‘-I’) for compiling afb-daemon plugins</td> +<tr class="odd"> +<td style="text-align: left;">afb_INCLUDE_DIRS</td> +<td style="text-align: left;">The '-I' preprocessor flags (w/o the '-I') for compiling afb-daemon plugins</td> </tr> -<tr> -<td>afb_CFLAGS </td> -<td> All required cflags for compiling afb-daemon plugins</td> +<tr class="even"> +<td style="text-align: left;">afb_CFLAGS</td> +<td style="text-align: left;">All required cflags for compiling afb-daemon plugins</td> </tr> </tbody> </table> - - -<p>If development files are found, the plugin can be added to the set of -target to build.</p> - -<pre><code>add_library(afm-main-plugin MODULE afm-main-plugin.c) -</code></pre> - -<p>This line asks to create a shared library having a single -source file named afm-main-plugin.c to be compiled. -The default name of the created shared object is -<strong>libafm-main-plugin.so</strong>.</p> - -<pre><code>set_target_properties(afm-main-plugin PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" -) -</code></pre> - +<p>If development files are found, the plugin can be added to the set of target to build.</p> +<pre class="sourceCode cmake"><code class="sourceCode cmake"><span class="kw">add_library</span>(afm-main-plugin <span class="ot">MODULE</span> afm-main-plugin.c)</code></pre> +<p>This line asks to create a shared library having a single source file named afm-main-plugin.c to be compiled. The default name of the created shared object is <strong>libafm-main-plugin.so</strong>.</p> +<pre class="sourceCode cmake"><code class="sourceCode cmake"><span class="kw">set_target_properties</span>(afm-main-plugin <span class="ot">PROPERTIES</span> + <span class="ot">PREFIX</span> <span class="st">""</span> + <span class="ot">LINK_FLAGS</span> <span class="st">"-Wl,--version-script=</span><span class="dv">${CMAKE_CURRENT_SOURCE_DIR}</span><span class="st">/afm-main-plugin.export-map"</span> +)</code></pre> <p>This lines are doing two things:</p> - -<ol> -<li><p>It renames the built library from <strong>libafm-main-plugin.so</strong> to <strong>afm-main-plugin.so</strong> -by removing the implicitly added prefix <em>lib</em>. This step is not mandatory -because afb-daemon doesn’t check names of files at load time. -The only filename convention used by afb-daemon relates to <strong>.so</strong> termination. -*.so pattern is used when afb-daemon automatically discovers plugin from a directory hierarchy.</p></li> -<li><p>It applies a version script at link time to only export the reserved name -<strong>pluginAfbV1Register</strong> for registration entry point. By default, when building -a shared library linker exports all the public symbols (C functions that are not <strong>static</strong>).</p></li> +<ol type="1"> +<li><p>It renames the built library from <strong>libafm-main-plugin.so</strong> to <strong>afm-main-plugin.so</strong> by removing the implicitly added prefix <em>lib</em>. This step is not mandatory because afb-daemon doesn't check names of files at load time. The only filename convention used by afb-daemon relates to <strong>.so</strong> termination. *.so pattern is used when afb-daemon automatically discovers plugin from a directory hierarchy.</p></li> +<li><p>It applies a version script at link time to only export the reserved name <strong>pluginAfbV1Register</strong> for registration entry point. By default, when building a shared library linker exports all the public symbols (C functions that are not <strong>static</strong>).</p></li> </ol> - - <p>Next line are:</p> - -<pre><code>target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) -target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) -</code></pre> - -<p>As you can see it uses the variables computed by <strong><em>pkg_check_modules(afb afb-daemon)</em></strong> -to configure the compiler and the linker.</p> - -<a name="Exporting.the.function.pluginAfbV1Register"></a> -<h3>Exporting the function pluginAfbV1Register</h3> - -<p>The function <strong>pluginAfbV1Register</strong> MUST be exported. This can be achieved -using a version script at link time. Here after is a version script used for -<em>tic-tac-toe</em> (plugins/samples/export.map).</p> - -<pre><code>{ global: pluginAfbV1Register; local: *; }; -</code></pre> - -<p>This sample <a href="https://sourceware.org/binutils/docs-2.26/ld/VERSION.html#VERSION">version script</a> -exports as global the symbol <em>pluginAfbV1Register</em> and hides any -other symbols.</p> - -<p>This version script is added to the link options using the -option <strong>–version-script=export.map</strong> is given directly to the -linker or using the option <strong>-Wl,–version-script=export.map</strong> -when the option is given to the C compiler.</p> - -<a name="Building.within.yocto"></a> -<h3>Building within yocto</h3> - +<pre class="sourceCode cmake"><code class="sourceCode cmake"><span class="kw">target_include_directories</span>(afm-main-plugin <span class="ot">PRIVATE</span> <span class="dv">${afb_INCLUDE_DIRS}</span>) +<span class="kw">target_link_libraries</span>(afm-main-plugin utils <span class="dv">${afb_LIBRARIES}</span>)</code></pre> +<p>As you can see it uses the variables computed by <strong><em>pkg_check_modules(afb afb-daemon)</em></strong> to configure the compiler and the linker.</p> +<h3 id="exporting-the-function-pluginafbv1register">Exporting the function pluginAfbV1Register</h3> +<p>The function <strong>pluginAfbV1Register</strong> MUST be exported. This can be achieved using a version script at link time. Here after is a version script used for <em>tic-tac-toe</em> (plugins/samples/export.map).</p> +<pre><code>{ global: pluginAfbV1Register; local: *; };</code></pre> +<p>This sample <a href="https://sourceware.org/binutils/docs-2.26/ld/VERSION.html#VERSION">version script</a> exports as global the symbol <em>pluginAfbV1Register</em> and hides any other symbols.</p> +<p>This version script is added to the link options using the option <strong>--version-script=export.map</strong> is given directly to the linker or using the option <strong>-Wl,--version-script=export.map</strong> when the option is given to the C compiler.</p> +<h3 id="building-within-yocto">Building within yocto</h3> <p>Adding a dependency to afb-daemon is enough. See below:</p> - -<pre><code>DEPENDS += " afb-daemon " -</code></pre> +<pre><code>DEPENDS += " afb-daemon "</code></pre> </body> </html> diff --git a/doc/afb-plugin-writing.md b/doc/afb-plugin-writing.md index f5966092..bc43e4e4 100644 --- a/doc/afb-plugin-writing.md +++ b/doc/afb-plugin-writing.md @@ -189,11 +189,13 @@ Header files to include Plugin *tictactoe* has following includes: - #define _GNU_SOURCE - #include <stdio.h> - #include <string.h> - #include <json-c/json.h> - #include <afb/afb-plugin.h> +```C +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <json-c/json.h> +#include <afb/afb-plugin.h> +``` Header *afb/afb-plugin.h* is the only hard dependency, it includes all features that a plugin MUST HAVE. Outside of includes used to support application logic, @@ -280,24 +282,26 @@ Writing a synchronous method implementation The method **tictactoe/board** is a synchronous implementation. Here is its listing: - /* - * get the board - */ - static void board(struct afb_req req) - { - struct board *board; - struct json_object *description; +```C +/* + * get the board + */ +static void board(struct afb_req req) +{ + struct board *board; + struct json_object *description; - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'board' called for boardid %d", board->id); + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'board' called for boardid %d", board->id); - /* describe the board */ - description = describe(board); + /* describe the board */ + description = describe(board); - /* send the board's description */ - afb_req_success(req, description, NULL); - } + /* send the board's description */ + afb_req_success(req, description, NULL); +} +``` This example shows many aspects of a synchronous method implementation. Let summarise it: @@ -324,13 +328,15 @@ For any implementation, the request is received by a structure of type The definition of **struct afb_req** is: - /* - * Describes the request by plugins from afb-daemon - */ - struct afb_req { - const struct afb_req_itf *itf; /* the interfacing functions */ - void *closure; /* the closure for functions */ - }; +```C +/* + * Describes the request by plugins from afb-daemon + */ +struct afb_req { + const struct afb_req_itf *itf; /* the interfacing functions */ + void *closure; /* the closure for functions */ +}; +``` It contains two pointers: first one *itf*, points to functions used to handle internal request. Second one *closure* point onto function closure. @@ -380,13 +386,15 @@ These functions are: The plugin *tictactoe* use a convenient function to retrieve its context: the board. This function is *board_of_req*: - /* - * retrieves the board of the request - */ - static inline struct board *board_of_req(struct afb_req req) - { - return afb_req_context(req, (void*)get_new_board, (void*)release_board); - } +```C +/* + * retrieves the board of the request + */ +static inline struct board *board_of_req(struct afb_req req) +{ + return afb_req_context(req, (void*)get_new_board, (void*)release_board); +} +``` The function **afb_req_context** ensures an existing context for the session of the request. @@ -395,22 +403,24 @@ Note function type casts to avoid compilation warnings. Here is the definition of the function **afb_req_context** - /* - * Gets the pointer stored by the plugin for the session of 'req'. - * If the stored pointer is NULL, indicating that no pointer was - * already stored, afb_req_context creates a new context by calling - * the function 'create_context' and stores it with the freeing function - * 'free_context'. - */ - static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*)) - { - void *result = afb_req_context_get(req); - if (result == NULL) { - result = create_context(); - afb_req_context_set(req, result, free_context); - } - return result; +```C +/* + * Gets the pointer stored by the plugin for the session of 'req'. + * If the stored pointer is NULL, indicating that no pointer was + * already stored, afb_req_context creates a new context by calling + * the function 'create_context' and stores it with the freeing function + * 'free_context'. + */ +static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*)) +{ + void *result = afb_req_context_get(req); + if (result == NULL) { + result = create_context(); + afb_req_context_set(req, result, free_context); } + return result; +} +``` The second argument if the function that creates the context. For plugin *tic-tac-toe* (function **get_new_board**). @@ -424,29 +434,31 @@ When usage count falls to zero, data board are freed. Definition of other functions dealing with contexts: - /* - * Gets the pointer stored by the plugin for the session of 'req'. - * When the plugin has not yet recorded a pointer, NULL is returned. - */ - void *afb_req_context_get(struct afb_req req); - - /* - * Stores for the plugin the pointer 'context' to the session of 'req'. - * The function 'free_context' will be called when the session is closed - * or if plugin stores an other pointer. - */ - void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*)); - - /* - * Frees the pointer stored by the plugin for the session of 'req' - * and sets it to NULL. - * - * Shortcut for: afb_req_context_set(req, NULL, NULL) - */ - static inline void afb_req_context_clear(struct afb_req req) - { - afb_req_context_set(req, NULL, NULL); - } +```C +/* + * Gets the pointer stored by the plugin for the session of 'req'. + * When the plugin has not yet recorded a pointer, NULL is returned. + */ +void *afb_req_context_get(struct afb_req req); + +/* + * Stores for the plugin the pointer 'context' to the session of 'req'. + * The function 'free_context' will be called when the session is closed + * or if plugin stores an other pointer. + */ +void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*)); + +/* + * Frees the pointer stored by the plugin for the session of 'req' + * and sets it to NULL. + * + * Shortcut for: afb_req_context_set(req, NULL, NULL) + */ +static inline void afb_req_context_clear(struct afb_req req) +{ + afb_req_context_set(req, NULL, NULL); +} +``` ### Sending reply to a request @@ -456,54 +468,58 @@ Two kinds of replies: successful or failure. It exists two functions for "success" replies: **afb_req_success** and **afb_req_success_f**. - /* - * Sends a reply of kind success to the request 'req'. - * The status of the reply is automatically set to "success". - * Its send the object 'obj' (can be NULL) with an - * informationnal comment 'info (can also be NULL). - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ - void afb_req_success(struct afb_req req, struct json_object *obj, const char *info); - - /* - * Same as 'afb_req_success' but the 'info' is a formatting - * string followed by arguments. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ - void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...); +```C +/* + * Sends a reply of kind success to the request 'req'. + * The status of the reply is automatically set to "success". + * Its send the object 'obj' (can be NULL) with an + * informationnal comment 'info (can also be NULL). + * + * For conveniency, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +void afb_req_success(struct afb_req req, struct json_object *obj, const char *info); + +/* + * Same as 'afb_req_success' but the 'info' is a formatting + * string followed by arguments. + * + * For conveniency, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...); +``` It exists two functions for "failure" replies: **afb_req_fail** and **afb_req_fail_f**. - /* - * Sends a reply of kind failure to the request 'req'. - * The status of the reply is set to 'status' and an - * informational comment 'info' (can also be NULL) can be added. - * - * Note that calling afb_req_fail("success", info) is equivalent - * to call afb_req_success(NULL, info). Thus even if possible it - * is strongly recommended to NEVER use "success" for status. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ - void afb_req_fail(struct afb_req req, const char *status, const char *info); - - /* - * Same as 'afb_req_fail' but the 'info' is a formatting - * string followed by arguments. - * - * For conveniency, the function calls 'json_object_put' for 'obj'. - * Thus, in the case where 'obj' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ - void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...); +```C +/* + * Sends a reply of kind failure to the request 'req'. + * The status of the reply is set to 'status' and an + * informational comment 'info' (can also be NULL) can be added. + * + * Note that calling afb_req_fail("success", info) is equivalent + * to call afb_req_success(NULL, info). Thus even if possible it + * is strongly recommended to NEVER use "success" for status. + * + * For conveniency, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +void afb_req_fail(struct afb_req req, const char *status, const char *info); + +/* + * Same as 'afb_req_fail' but the 'info' is a formatting + * string followed by arguments. + * + * For conveniency, the function calls 'json_object_put' for 'obj'. + * Thus, in the case where 'obj' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...); +``` > For conveniency, these functions automatically call **json_object_put** to release **obj**. > Because **obj** usage count is null after being passed to a reply function, it SHOULD not be used anymore. @@ -522,23 +538,25 @@ or WebSockets. For example, the method **join** of plugin **tic-tac-toe** expects one argument: the *boardid* to join. Here is an extract: - /* - * Join a board - */ - static void join(struct afb_req req) - { - struct board *board, *new_board; - const char *id; - - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'join' called for boardid %d", board->id); - - /* retrieves the argument */ - id = afb_req_value(req, "boardid"); - if (id == NULL) - goto bad_request; - ... +```C +/* + * Join a board + */ +static void join(struct afb_req req) +{ + struct board *board, *new_board; + const char *id; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'join' called for boardid %d", board->id); + + /* retrieves the argument */ + id = afb_req_value(req, "boardid"); + if (id == NULL) + goto bad_request; + ... +``` The function **afb_req_value** searches in the request *req* for argument name passed in the second argument. When argument name @@ -551,34 +569,38 @@ is not passed, **afb_req_value** returns NULL. The function **afb_req_value** is defined here after: - /* - * Gets from the request 'req' the string value of the argument of 'name'. - * Returns NULL if when there is no argument of 'name'. - * Returns the value of the argument of 'name' otherwise. - * - * Shortcut for: afb_req_get(req, name).value - */ - static inline const char *afb_req_value(struct afb_req req, const char *name) - { - return afb_req_get(req, name).value; - } +```C +/* + * Gets from the request 'req' the string value of the argument of 'name'. + * Returns NULL if when there is no argument of 'name'. + * Returns the value of the argument of 'name' otherwise. + * + * Shortcut for: afb_req_get(req, name).value + */ +static inline const char *afb_req_value(struct afb_req req, const char *name) +{ + return afb_req_get(req, name).value; +} +``` It is defined as a shortcut to call the function **afb_req_get**. That function is defined here after: - /* - * Gets from the request 'req' the argument of 'name'. - * Returns a PLAIN structure of type 'struct afb_arg'. - * When the argument of 'name' is not found, all fields of result are set to NULL. - * When the argument of 'name' is found, the fields are filled, - * in particular, the field 'result.name' is set to 'name'. - * - * There is a special name value: the empty string. - * The argument of name "" is defined only if the request was made using - * an HTTP POST of Content-Type "application/json". In that case, the - * argument of name "" receives the value of the body of the HTTP request. - */ - struct afb_arg afb_req_get(struct afb_req req, const char *name); +```C +/* + * Gets from the request 'req' the argument of 'name'. + * Returns a PLAIN structure of type 'struct afb_arg'. + * When the argument of 'name' is not found, all fields of result are set to NULL. + * When the argument of 'name' is found, the fields are filled, + * in particular, the field 'result.name' is set to 'name'. + * + * There is a special name value: the empty string. + * The argument of name "" is defined only if the request was made using + * an HTTP POST of Content-Type "application/json". In that case, the + * argument of name "" receives the value of the body of the HTTP request. + */ +struct afb_arg afb_req_get(struct afb_req req, const char *name); +``` That function takes 2 parameters: the request and the name of the argument to retrieve. It returns a PLAIN structure of @@ -592,32 +614,36 @@ of the post and is supposed to be a JSON string. The definition of **struct afb_arg** is: - /* - * Describes an argument (or parameter) of a request - */ - struct afb_arg { - const char *name; /* name of the argument or NULL if invalid */ - const char *value; /* string representation of the value of the argument */ - /* original filename of the argument if path != NULL */ - const char *path; /* if not NULL, path of the received file for the argument */ - /* when the request is finalized this file is removed */ - }; +```C +/* + * Describes an argument (or parameter) of a request + */ +struct afb_arg { + const char *name; /* name of the argument or NULL if invalid */ + const char *value; /* string representation of the value of the argument */ + /* original filename of the argument if path != NULL */ + const char *path; /* if not NULL, path of the received file for the argument */ + /* when the request is finalized this file is removed */ +}; +``` The structure returns the data arguments that are known for the request. This data include a field named **path**. This **path** can be accessed using the function **afb_req_path** defined here after: - /* - * Gets from the request 'req' the path for file attached to the argument of 'name'. - * Returns NULL if when there is no argument of 'name' or when there is no file. - * Returns the path of the argument of 'name' otherwise. - * - * Shortcut for: afb_req_get(req, name).path - */ - static inline const char *afb_req_path(struct afb_req req, const char *name) - { - return afb_req_get(req, name).path; - } +```C +/* + * Gets from the request 'req' the path for file attached to the argument of 'name'. + * Returns NULL if when there is no argument of 'name' or when there is no file. + * Returns the path of the argument of 'name' otherwise. + * + * Shortcut for: afb_req_get(req, name).path + */ +static inline const char *afb_req_path(struct afb_req req, const char *name) +{ + return afb_req_get(req, name).path; +} +``` The path is only defined for HTTP/POST requests that send file. @@ -631,13 +657,15 @@ will send an HTTP/POST request to the method **post/upload-image** with 2 arguments named *file* and *hidden*. - <h2>Sample Post File</h2> - <form enctype="multipart/form-data"> - <input type="file" name="file" /> - <input type="hidden" name="hidden" value="bollobollo" /> - <br> - <button formmethod="POST" formaction="api/post/upload-image">Post File</button> - </form> +```html +<h2>Sample Post File</h2> +<form enctype="multipart/form-data"> + <input type="file" name="file" /> + <input type="hidden" name="hidden" value="bollobollo" /> + <br> + <button formmethod="POST" formaction="api/post/upload-image">Post File</button> +</form> +``` Argument named **file** should have both its value and path defined. @@ -658,11 +686,13 @@ path is destroyed. Plugins may also request every arguments of a given call as one single object. This feature is provided by the function **afb_req_json** defined here after: - /* - * Gets from the request 'req' the json object hashing the arguments. - * The returned object must not be released using 'json_object_put'. - */ - struct json_object *afb_req_json(struct afb_req req); +```C +/* + * Gets from the request 'req' the json object hashing the arguments. + * The returned object must not be released using 'json_object_put'. + */ +struct json_object *afb_req_json(struct afb_req req); +``` It returns a json object. This object depends on how the request was built: @@ -707,14 +737,16 @@ afb-daemon initialisation is finished. Here after the code used for **pluginAfbV1Register** from plugin *tic-tac-toe*: - /* - * activation function for registering the plugin called by afb-daemon - */ - const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf) - { - afbitf = itf; // records the interface for accessing afb-daemon - return &plugin_description; // returns the description of the plugin - } +```C +/* + * activation function for registering the plugin called by afb-daemon + */ +const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf) +{ + afbitf = itf; // records the interface for accessing afb-daemon + return &plugin_description; // returns the description of the plugin +} +``` It is a very minimal initialisation function because *tic-tac-toe* plugin doesn't have any application related initialisation step. It merely record daemon's interface @@ -724,42 +756,46 @@ The variable **afbitf** is a plugin global variable. It keeps the interface to afb-daemon that should be used for logging and pushing events. Here is its declaration: - /* - * the interface to afb-daemon - */ - const struct AFB_interface *afbitf; +```C +/* + * the interface to afb-daemon + */ +const struct AFB_interface *afbitf; +``` The description of the plugin is defined here after. - /* - * array of the methods exported to afb-daemon - */ - static const struct AFB_method_desc_v1 plugin_methods[] = { - /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ - { .name= "new", .session= AFB_SESSION_NONE, .callback= new, .info= "Starts a new game" }, - { .name= "play", .session= AFB_SESSION_NONE, .callback= play, .info= "Asks the server to play" }, - { .name= "move", .session= AFB_SESSION_NONE, .callback= move, .info= "Tells the client move" }, - { .name= "board", .session= AFB_SESSION_NONE, .callback= board, .info= "Get the current board" }, - { .name= "level", .session= AFB_SESSION_NONE, .callback= level, .info= "Set the server level" }, - { .name= "join", .session= AFB_SESSION_CHECK,.callback= join, .info= "Join a board" }, - { .name= "undo", .session= AFB_SESSION_NONE, .callback= undo, .info= "Undo the last move" }, - { .name= "wait", .session= AFB_SESSION_NONE, .callback= wait, .info= "Wait for a change" }, - { .name= NULL } /* marker for end of the array */ - }; - - /* - * description of the plugin for afb-daemon - */ - static const struct AFB_plugin plugin_description = - { - /* description conforms to VERSION 1 */ - .type= AFB_PLUGIN_VERSION_1, - .v1= { /* fills the v1 field of the union when AFB_PLUGIN_VERSION_1 */ - .prefix= "tictactoe", /* the API name (or plugin name or prefix) */ - .info= "Sample tac-tac-toe game", /* short description of of the plugin */ - .methods = plugin_methods /* the array describing the methods of the API */ - } - }; +```C +/* + * array of the methods exported to afb-daemon + */ +static const struct AFB_method_desc_v1 plugin_methods[] = { + /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */ + { .name= "new", .session= AFB_SESSION_NONE, .callback= new, .info= "Starts a new game" }, + { .name= "play", .session= AFB_SESSION_NONE, .callback= play, .info= "Asks the server to play" }, + { .name= "move", .session= AFB_SESSION_NONE, .callback= move, .info= "Tells the client move" }, + { .name= "board", .session= AFB_SESSION_NONE, .callback= board, .info= "Get the current board" }, + { .name= "level", .session= AFB_SESSION_NONE, .callback= level, .info= "Set the server level" }, + { .name= "join", .session= AFB_SESSION_CHECK,.callback= join, .info= "Join a board" }, + { .name= "undo", .session= AFB_SESSION_NONE, .callback= undo, .info= "Undo the last move" }, + { .name= "wait", .session= AFB_SESSION_NONE, .callback= wait, .info= "Wait for a change" }, + { .name= NULL } /* marker for end of the array */ +}; + +/* + * description of the plugin for afb-daemon + */ +static const struct AFB_plugin plugin_description = +{ + /* description conforms to VERSION 1 */ + .type= AFB_PLUGIN_VERSION_1, + .v1= { /* fills the v1 field of the union when AFB_PLUGIN_VERSION_1 */ + .prefix= "tictactoe", /* the API name (or plugin name or prefix) */ + .info= "Sample tac-tac-toe game", /* short description of of the plugin */ + .methods = plugin_methods /* the array describing the methods of the API */ + } +}; +``` The structure **plugin_description** describes the plugin. It declares the type and version of the plugin, its name, a short description @@ -779,17 +815,19 @@ In version one of afb-damon plugin, a method description contains 4 fields: The structure describing methods is defined as follows: - /* - * Description of one method of the API provided by the plugin - * This enumeration is valid for plugins of type 1 - */ - struct AFB_method_desc_v1 - { - const char *name; /* name of the method */ - enum AFB_session_v1 session; /* authorisation and session requirements of the method */ - void (*callback)(struct afb_req req); /* callback function implementing the method */ - const char *info; /* textual description of the method */ - }; +```C +/* + * Description of one method of the API provided by the plugin + * This enumeration is valid for plugins of type 1 + */ +struct AFB_method_desc_v1 +{ + const char *name; /* name of the method */ + enum AFB_session_v1 session; /* authorisation and session requirements of the method */ + void (*callback)(struct afb_req req); /* callback function implementing the method */ + const char *info; /* textual description of the method */ +}; +``` For technical reasons, the enumeration **enum AFB_session_v1** is not exactly an enumeration but the wrapper of constant definitions that can be mixed using bitwise or @@ -854,7 +892,9 @@ they are output with a different level on the logging system. All of these methods have the same signature: - void ERROR(const struct AFB_interface *afbitf, const char *message, ...); +```C +void ERROR(const struct AFB_interface *afbitf, const char *message, ...); +``` The first argument **afbitf** is the interface to afb daemon that the plugin received at initialisation time when **pluginAfbV1Register** is called. @@ -871,10 +911,10 @@ ont the verbosity level. Level of verbosity | Outputed macro :-----------------:|-------------------------- - 0 | ERROR - 1 | ERROR + WARNING + NOTICE - 2 | ERROR + WARNING + NOTICE + INFO - 3 | ERROR + WARNING + NOTICE + INFO + DEBUG +0 | ERROR +1 | ERROR + WARNING + NOTICE +2 | ERROR + WARNING + NOTICE + INFO +3 | ERROR + WARNING + NOTICE + INFO + DEBUG ### Output format and destination @@ -883,14 +923,14 @@ The prefixes are: syslog level | prefix :-----------:|--------------- - 0 | <0> EMERGENCY - 1 | <1> ALERT - 2 | <2> CRITICAL - 3 | <3> ERROR - 4 | <4> WARNING - 5 | <5> NOTICE - 6 | <6> INFO - 7 | <7> DEBUG +0 | <0> EMERGENCY +1 | <1> ALERT +2 | <2> CRITICAL +3 | <3> ERROR +4 | <4> WARNING +5 | <5> NOTICE +6 | <6> INFO +7 | <7> DEBUG The message is pushed to standard error. @@ -908,21 +948,23 @@ coming version. The plugin *tic-tac-toe* broadcasts events when the board changes. This is done in the function **changed**: - /* - * signals a change of the board - */ - static void changed(struct board *board, const char *reason) - { - ... - struct json_object *description; +```C +/* + * signals a change of the board + */ +static void changed(struct board *board, const char *reason) +{ + ... + struct json_object *description; - /* get the description */ - description = describe(board); + /* get the description */ + description = describe(board); - ... + ... - afb_daemon_broadcast_event(afbitf->daemon, reason, description); - } + afb_daemon_broadcast_event(afbitf->daemon, reason, description); +} +``` The description of the changed board is pushed via the daemon interface. @@ -933,16 +975,18 @@ object that is transmitted with the event. Function **afb_daemon_broadcast_event** is defined here after: - /* - * Broadcasts widely the event of 'name' with the data 'object'. - * 'object' can be NULL. - * 'daemon' MUST be the daemon given in interface when activating the plugin. - * - * For conveniency, the function calls 'json_object_put' for 'object'. - * Thus, in the case where 'object' should remain available after - * the function returns, the function 'json_object_get' shall be used. - */ - void afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object); +```C +/* + * Broadcasts widely the event of 'name' with the data 'object'. + * 'object' can be NULL. + * 'daemon' MUST be the daemon given in interface when activating the plugin. + * + * For conveniency, the function calls 'json_object_put' for 'object'. + * Thus, in the case where 'object' should remain available after + * the function returns, the function 'json_object_get' shall be used. + */ +void afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object); +``` > Be aware, as with reply functions **object** is automatically released using > **json_object_put** when using this function. Call **json_object_get** before @@ -996,22 +1040,24 @@ Common case of an asynchronous implementation. Here is the listing of the function **wait**: - static void wait(struct afb_req req) - { - struct board *board; - struct waiter *waiter; - - /* retrieves the context for the session */ - board = board_of_req(req); - INFO(afbitf, "method 'wait' called for boardid %d", board->id); - - /* creates the waiter and enqueues it */ - waiter = calloc(1, sizeof *waiter); - waiter->req = req; - waiter->next = board->waiters; - afb_req_addref(req); - board->waiters = waiter; - } +```C +static void wait(struct afb_req req) +{ + struct board *board; + struct waiter *waiter; + + /* retrieves the context for the session */ + board = board_of_req(req); + INFO(afbitf, "method 'wait' called for boardid %d", board->id); + + /* creates the waiter and enqueues it */ + waiter = calloc(1, sizeof *waiter); + waiter->req = req; + waiter->next = board->waiters; + afb_req_addref(req); + board->waiters = waiter; +} +``` After retrieving the board, the function adds a new waiter to waiters list and returns without setting a reply. @@ -1027,30 +1073,32 @@ with reason of change in parameter. Here is the full listing of the function **changed**: - /* - * signals a change of the board - */ - static void changed(struct board *board, const char *reason) - { - struct waiter *waiter, *next; - struct json_object *description; - - /* get the description */ - description = describe(board); - - waiter = board->waiters; - board->waiters = NULL; - while (waiter != NULL) { - next = waiter->next; - afb_req_success(waiter->req, json_object_get(description), reason); - afb_req_unref(waiter->req); - free(waiter); - waiter = next; - } - - afb_event_sender_push(afb_daemon_get_event_sender(afbitf->daemon), reason, description); +```C +/* + * signals a change of the board + */ +static void changed(struct board *board, const char *reason) +{ + struct waiter *waiter, *next; + struct json_object *description; + + /* get the description */ + description = describe(board); + + waiter = board->waiters; + board->waiters = NULL; + while (waiter != NULL) { + next = waiter->next; + afb_req_success(waiter->req, json_object_get(description), reason); + afb_req_unref(waiter->req); + free(waiter); + waiter = next; } + afb_event_sender_push(afb_daemon_get_event_sender(afbitf->daemon), reason, description); +} +``` + The list of waiters is walked and a reply is sent to each waiter. After sending the reply, the reference count of the request is decremented using **afb_req_unref** to allow resources to be freed. @@ -1068,32 +1116,38 @@ queried by providing **afb-daemon** in command line arguments. This configuration file provides data that should be used for plugins compilation. Examples: - $ pkg-config --cflags afb-daemon - $ pkg-config --libs afb-daemon +```bash +$ pkg-config --cflags afb-daemon +$ pkg-config --libs afb-daemon +``` ### Example for cmake meta build system This example is the extract for building the plugin *afm-main* using *CMAKE*. - pkg_check_modules(afb afb-daemon) - if(afb_FOUND) - message(STATUS "Creation afm-main-plugin for AFB-DAEMON") - add_library(afm-main-plugin MODULE afm-main-plugin.c) - target_compile_options(afm-main-plugin PRIVATE ${afb_CFLAGS}) - target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) - target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) - set_target_properties(afm-main-plugin PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" - ) - install(TARGETS afm-main-plugin LIBRARY DESTINATION ${plugin_dir}) - else() - message(STATUS "Not creating the plugin for AFB-DAEMON") - endif() +```cmake +pkg_check_modules(afb afb-daemon) +if(afb_FOUND) + message(STATUS "Creation afm-main-plugin for AFB-DAEMON") + add_library(afm-main-plugin MODULE afm-main-plugin.c) + target_compile_options(afm-main-plugin PRIVATE ${afb_CFLAGS}) + target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) + target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) + set_target_properties(afm-main-plugin PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" + ) + install(TARGETS afm-main-plugin LIBRARY DESTINATION ${plugin_dir}) +else() + message(STATUS "Not creating the plugin for AFB-DAEMON") +endif() +``` Let now describe some of these lines. - pkg_check_modules(afb afb-daemon) +```cmake +pkg_check_modules(afb afb-daemon) +``` This first lines searches to the *pkg-config* configuration file for **afb-daemon**. Resulting data are stored in the following variables: @@ -1110,17 +1164,21 @@ afb_CFLAGS | All required cflags for compiling afb-daemon plugins If development files are found, the plugin can be added to the set of target to build. - add_library(afm-main-plugin MODULE afm-main-plugin.c) +```cmake +add_library(afm-main-plugin MODULE afm-main-plugin.c) +``` This line asks to create a shared library having a single source file named afm-main-plugin.c to be compiled. The default name of the created shared object is **libafm-main-plugin.so**. - set_target_properties(afm-main-plugin PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" - ) +```cmake +set_target_properties(afm-main-plugin PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map" +) +``` This lines are doing two things: @@ -1136,8 +1194,10 @@ a shared library linker exports all the public symbols (C functions that are not Next line are: - target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) - target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) +```cmake +target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS}) +target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES}) +``` As you can see it uses the variables computed by ***pkg_check_modules(afb afb-daemon)*** to configure the compiler and the linker. diff --git a/doc/afb-plugins-overview.html b/doc/afb-plugins-overview.html index 4fdb72f4..6c1506ad 100644 --- a/doc/afb-plugins-overview.html +++ b/doc/afb-plugins-overview.html @@ -1,22 +1,38 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <title></title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Overview.of.plugins.shipped.with.AFB-Daemon"></a> -<h1>Overview of plugins shipped with AFB-Daemon</h1> - +<nav id="TOC"> +<ul> +<li><a href="#overview-of-plugins-shipped-with-afb-daemon">Overview of plugins shipped with AFB-Daemon</a><ul> +<li><a href="#list-of-plugins">List of plugins</a></li> +<li><a href="#detail-of-plugins">Detail of plugins</a><ul> +<li><a href="#hello-world">Hello World</a></li> +<li><a href="#authentication">Authentication</a></li> +<li><a href="#tic-tac-toe">Tic Tac Toe</a></li> +<li><a href="#audio">Audio</a></li> +<li><a href="#radio">Radio</a></li> +<li><a href="#media">Media</a></li> +</ul></li> +</ul></li> +</ul> +</nav> +<h1 id="overview-of-plugins-shipped-with-afb-daemon">Overview of plugins shipped with AFB-Daemon</h1> <pre><code>Version: 1 Date: 09 juin 2016 -Author: Manuel Bachmann -</code></pre> - -<a name="List.of.plugins"></a> -<h2>List of plugins</h2> - +Author: Manuel Bachmann</code></pre> +<h2 id="list-of-plugins">List of plugins</h2> <p>Here are the plugins shipped in the source tree:</p> - <ul> <li>Hello World</li> <li>Authentication</li> @@ -25,22 +41,12 @@ Author: Manuel Bachmann <li>Radio <em>(1 backend: RTLSDR RTL2832U)</em></li> <li>Media <em>(1 backend: Rygel UPnP)</em></li> </ul> - - <p>All plugins may not be built, depending on the development libraries present on the system at build time.</p> - -<a name="Detail.of.plugins"></a> -<h2>Detail of plugins</h2> - -<a name="Hello.World"></a> -<h3>Hello World</h3> - +<h2 id="detail-of-plugins">Detail of plugins</h2> +<h3 id="hello-world">Hello World</h3> <p>A sample Hello World plugin for demonstration and learning purposes.</p> - -<p>This plugin provides a few unauthenticated requests, all beginning with “ping”, to demonstrate basic binder capabilities.</p> - +<p>This plugin provides a few unauthenticated requests, all beginning with "ping", to demonstrate basic binder capabilities.</p> <p><strong>Verbs</strong>:</p> - <ul> <li><em>ping:</em> returns a success response</li> <li><em>pingfail:</em> returns a failure response</li> @@ -49,21 +55,12 @@ Author: Manuel Bachmann <li><em>pingJson:</em> returns a success response, with a complex JSON response field</li> <li><em>pingevent:</em> broadcasts a global event</li> </ul> - - <p><br /></p> - -<a name="Authentication"></a> -<h3>Authentication</h3> - +<h3 id="authentication">Authentication</h3> <p>An sample Authentication plugin for demonstration purposes.</p> - -<p>This plugin provides a few requests to demonstrate the binder’s token-based security mechanism.</p> - -<p>Calling “<em>connect</em>” with a security token will initiate a session, calling “<em>refresh</em>” will issue a new token and invalidate the previous one, calling “<em>logout</em>” will invalidate all tokens and close the session.</p> - +<p>This plugin provides a few requests to demonstrate the binder's token-based security mechanism.</p> +<p>Calling "<em>connect</em>" with a security token will initiate a session, calling "<em>refresh</em>" will issue a new token and invalidate the previous one, calling "<em>logout</em>" will invalidate all tokens and close the session.</p> <p><strong>Verbs</strong>:</p> - <ul> <li><em>ping:</em> returns a success response</li> <li><em>connect:</em> creates a session and returns a new token</li> @@ -71,19 +68,11 @@ Author: Manuel Bachmann <li><em>check:</em> verifies the passed token is valid</li> <li><em>logout:</em> closes the session</li> </ul> - - <p><br /></p> - -<a name="Tic.Tac.Toe"></a> -<h3>Tic Tac Toe</h3> - +<h3 id="tic-tac-toe">Tic Tac Toe</h3> <p>A sample Tic Tac Toe game plugin.</p> - <p>This plugin provides an interactive Tic Tac Toe game where the binder returns the grid as a JSON response.</p> - <p><strong>Verbs</strong>:</p> - <ul> <li><em>new:</em> starts a new game</li> <li><em>play:</em> asks the server to play</li> @@ -94,57 +83,34 @@ Author: Manuel Bachmann <li><em>undo</em>: undo the last move</li> <li><em>wait</em>: wait for a move</li> </ul> - - <p><br /></p> - -<a name="Audio"></a> -<h3>Audio</h3> - +<h3 id="audio">Audio</h3> <p>A sample Audio plugin with 2 backends:</p> - <ul> <li>ALSA (mandatory)</li> <li>PulseAudio (optional)</li> </ul> - - -<p>This plugin is able to initialize a specific soundcard, define volume levels, channels (mono/stereo…), mute sound, and play a 22,050 Hz PCM stream.</p> - +<p>This plugin is able to initialize a specific soundcard, define volume levels, channels (mono/stereo...), mute sound, and play a 22,050 Hz PCM stream.</p> <p><strong>Verbs</strong>:</p> - <ul> <li><em>ping:</em> returns a success response</li> -<li><em>init:</em> initializes backend, on the “default” sound card</li> +<li><em>init:</em> initializes backend, on the "default" sound card</li> <li><em>volume:</em> gets or sets volume, in % (0-100)</li> <li><em>channels:</em> gets or sets channels count (1-8)</li> <li><em>mute:</em> gets or sets the mute status (on-off)</li> <li><em>play</em>: gets or sets the playing status (on-off)</li> </ul> - - <p><em>(if PulseAudio development libraries are not found at build time, only ALSA will be available)</em></p> - <p><em>(if a PulseAudio server is not found at runtime, the plugin will dynamically fall back to ALSA)</em></p> - <p><em>(a specifc backend can be forced by using this syntax before running afb-daemon : <strong>$ export AFB_AUDIO_OUTPUT=Alsa</strong>)</em></p> - <p><br /></p> - -<a name="Radio"></a> -<h3>Radio</h3> - +<h3 id="radio">Radio</h3> <p>A sample AM/FM Radio plugin with 1 backend:</p> - <ul> <li>RTLSDR - Realtek RTL2832U dongles (mandatory)</li> </ul> - - <p>This plugin is able to initialize specific RTL2832U dongles, switch between AM/FM modes, define frequency, mute sound, and play sound (if combining with the <strong>audio</strong> plugin).</p> - <p><strong>Verbs</strong>:</p> - <ul> <li><em>ping:</em> returns a success response</li> <li><em>init:</em> initializes backend, looking for plugged-in devices</li> @@ -154,54 +120,32 @@ Author: Manuel Bachmann <li><em>mute</em>: sets device mute status (on-off)</li> <li><em>play</em>: sets device playing status (on-off)</li> </ul> - - <p><em>(if rtlsdr development libraries are not found at build time, this plugin will not be built)</em></p> - <p><br /></p> - -<a name="Media"></a> -<h3>Media</h3> - +<h3 id="media">Media</h3> <p>A sample Media Server plugin with 1 backend:</p> - <ul> <li>Rygel</li> </ul> - - <p>This plugin is able to detect a local Rygel UPnP media server, list audio files, select an audio file for playback, play/pause/seek in this file, upload an audio file to the server.</p> - <p><strong>Verbs</strong>:</p> - <ul> <li><em>ping:</em> returns a success response</li> <li><em>init:</em> initializes backend, looking for an active local UPnP server</li> <li><em>list:</em> returns list of audio files, as a JSON structure</li> -<li><em>select:</em> select an audio files, by index number (001-…)</li> +<li><em>select:</em> select an audio files, by index number (001-...)</li> <li><em>play:</em> plays the currently selected audio file</li> <li><em>stop:</em> stops the currently selected audio file</li> <li><em>pause:</em> pauses the currently selected audio file</li> <li><em>seek:</em> seeks in the currently selected audio file, in seconds</li> <li><em>upload:</em> uploads an audio file, with a POST request</li> </ul> - - <p><em>(if GUPnP/GSSDP development libraries are not fund at build time, this plugin will not be built)</em></p> - <p><br /></p> - <hr /> - <p><br /></p> - <p>Sample command-line applications: <em>afb-client-demo</em> (built by default)</p> - -<p>Sample HTML5 applications: -<strong>test/*.html</strong>, -<strong><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree">afb-client</a></strong>, -<strong><a href="https://github.com/iotbzh/afb-radio">afb-radio</a></strong></p> - +<p>Sample HTML5 applications: **test/*.html<strong>, </strong><a href="https://gerrit.automotivelinux.org/gerrit/gitweb?p=src/app-framework-demo.git;a=tree">afb-client</a><strong>, </strong><a href="https://github.com/iotbzh/afb-radio">afb-radio</a>**</p> <p>Sample Qt/QML applications: <em>test/token-websock.qml</em></p> </body> </html> diff --git a/doc/afb-tests-overview.html b/doc/afb-tests-overview.html index ce1cba44..f663a75f 100644 --- a/doc/afb-tests-overview.html +++ b/doc/afb-tests-overview.html @@ -1,106 +1,77 @@ +<!DOCTYPE html> <html> <head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> + <meta charset="utf-8"> + <meta name="generator" content="pandoc"> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> + <meta name="author" content="Manuel Bachmann"> + <title>Overview of tests shipped with AFB-Daemon</title> + <style type="text/css">code{white-space: pre;}</style> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link rel="stylesheet" href="doc.css"> </head> <body> -<a name="Overview.of.tests.shipped.with.AFB-Daemon"></a> -<h1>Overview of tests shipped with AFB-Daemon</h1> - +<header> +<h1 class="title">Overview of tests shipped with AFB-Daemon</h1> +<h2 class="author">Manuel Bachmann</h2> +<h3 class="date">30 mai 2016</h3> +</header> +<nav id="TOC"> +<ul> +<li><a href="#overview-of-tests-shipped-with-afb-daemon">Overview of tests shipped with AFB-Daemon</a><ul> +<li><a href="#list-of-tests">List of tests</a></li> +<li><a href="#detail-of-tests">Detail of tests</a><ul> +<li><a href="#afb-client-demo-command-line-websockets">afb-client-demo (command-line WebSockets)</a></li> +<li><a href="#token-websock.qml-qtqml-websockets">token-websock.qml (Qt/QML WebSockets)</a></li> +<li><a href="#html-html5js-http-rest-websockets">*.html (HTML5/JS HTTP-REST & WebSockets)</a></li> +</ul></li> +</ul></li> +</ul> +</nav> +<h1 id="overview-of-tests-shipped-with-afb-daemon">Overview of tests shipped with AFB-Daemon</h1> <pre><code>Version: 1 Date: 30 mai 2016 -Author: Manuel Bachmann -</code></pre> - -<a name="List.of.tests"></a> -<h2>List of tests</h2> - +Author: Manuel Bachmann</code></pre> +<h2 id="list-of-tests">List of tests</h2> <p>Here are the tests shipped in the source tree:</p> - <ul> <li><p><strong>afb-client-demo</strong> (command-line WebSockets)</p></li> <li><p><strong>token-websock.qml</strong> (Qt/QML WebSockets)</p></li> -<li><p><strong>*.html</strong> (HTML5/JS HTTP-REST & WebSockets)</p></li> +<li><p>*<strong>.html</strong> (HTML5/JS HTTP-REST & WebSockets)</p></li> </ul> - - -<a name="Detail.of.tests"></a> -<h2>Detail of tests</h2> - -<a name="afb-client-demo..command-line.WebSockets."></a> -<h3>afb-client-demo (command-line WebSockets)</h3> - -<p>This clients interactively calls plugins APIs from the command line, using the binder -<a href="https://en.wikipedia.org/wiki/WebSocket">WebSockets</a> facility.</p> - +<h2 id="detail-of-tests">Detail of tests</h2> +<h3 id="afb-client-demo-command-line-websockets">afb-client-demo (command-line WebSockets)</h3> +<p>This clients interactively calls plugins APIs from the command line, using the binder <a href="https://en.wikipedia.org/wiki/WebSocket">WebSockets</a> facility.</p> <p>If <em>afb-daemon</em> has been launched with the following parameters:</p> - -<pre><code>$ afb-daemon --port=1234 --token=123456 [...] -</code></pre> - +<pre><code>$ afb-daemon --port=1234 --token=123456 [...]</code></pre> <p>Then run the client with :</p> - -<pre><code>afb-client-demo ws://localhost:1234/api?token=123456 [<api> <verb> [<json-data>]] -</code></pre> - +<pre><code>afb-client-demo ws://localhost:1234/api?token=123456 [<api> <verb> [<json-data>]]</code></pre> <p>For instance, to initialize the Audio plugin from the command line :</p> - -<pre><code>afb-client-demo ws://localhost:1234/api?token=123456 -</code></pre> - -<p>The command doesn’t return. You should type requests of type <api> <verb> [<json-data>]. -So, try:</p> - +<pre><code>afb-client-demo ws://localhost:1234/api?token=123456</code></pre> +<p>The command doesn't return. You should type requests of type <api> <verb> [<json-data>]. So, try:</p> <pre><code>auth connect -hello pingjson true -</code></pre> - +hello pingjson true</code></pre> <p><br /></p> - -<a name="token-websock.qml..Qt.QML.WebSockets."></a> -<h3>token-websock.qml (Qt/QML WebSockets)</h3> - +<h3 id="token-websock.qml-qtqml-websockets">token-websock.qml (Qt/QML WebSockets)</h3> <p>If <em>afb-daemon</em> has been launched with the following parameters:</p> - -<pre><code>$ afb-daemon --port=1234 --token=123456 [...] -</code></pre> - +<pre><code>$ afb-daemon --port=1234 --token=123456 [...]</code></pre> <p>and Qt5 is installed.</p> - <p>For installing Qt5 on <strong>Ubuntu 16.04</strong>:</p> - -<pre><code>$ apt-get install qmlscene qml-module-qtwebsockets qml-module-qtquick-controls -</code></pre> - +<pre><code>$ apt-get install qmlscene qml-module-qtwebsockets qml-module-qtquick-controls</code></pre> <p>For installing Qt5 on <strong>Fedora 23</strong> :</p> - -<pre><code>$ dnf install qt5-qtdeclarative-devel qt5-qtwebsockets-devel qt5-qtquickcontrols -</code></pre> - +<pre><code>$ dnf install qt5-qtdeclarative-devel qt5-qtwebsockets-devel qt5-qtquickcontrols</code></pre> <p>Then run the client with :</p> - -<pre><code>qmlscene test/token-websock.qml -</code></pre> - -<p>and interactively press the buttons, “Connect”, “Refresh”, “Logout”.</p> - +<pre><code>qmlscene test/token-websock.qml</code></pre> +<p>and interactively press the buttons, "Connect", "Refresh", "Logout".</p> <p><br /></p> - -<a name="L..html..HTML5.JS.HTTP-REST..amp..WebSockets."></a> -<h3>*.html (HTML5/JS HTTP-REST & WebSockets)</h3> - +<h3 id="html-html5js-http-rest-websockets">*.html (HTML5/JS HTTP-REST & WebSockets)</h3> <p>If <em>afb-daemon</em> has been launched with the following parameters:</p> - -<pre><code>$ afb-daemon --port=1234 --rootdir=$PWD/test [...] -</code></pre> - -<p><em>(“$PWD/test</em>” being the “test” subdirectory of the source tree)_</p> - +<pre><code>$ afb-daemon --port=1234 --rootdir=$PWD/test [...]</code></pre> +<p><em>("$PWD/test</em>" being the "test" subdirectory of the source tree)_</p> <p>Then open your preferred Web browser, connect to the following URL:</p> - -<pre><code>http://localhost:1234 -</code></pre> - +<pre><code>http://localhost:1234</code></pre> <p>and interactively run the various tests.</p> </body> </html> diff --git a/doc/afb-tests-overview.md b/doc/afb-tests-overview.md index 98d971fc..272d98af 100644 --- a/doc/afb-tests-overview.md +++ b/doc/afb-tests-overview.md @@ -1,10 +1,12 @@ -# Overview of tests shipped with AFB-Daemon +Overview of tests shipped with AFB-Daemon +========================================= Version: 1 Date: 30 mai 2016 Author: Manuel Bachmann -## List of tests +List of tests +------------- Here are the tests shipped in the source tree: @@ -15,7 +17,8 @@ Here are the tests shipped in the source tree: * ***.html** (HTML5/JS HTTP-REST & WebSockets) -## Detail of tests +Detail of tests +--------------- ### afb-client-demo (command-line WebSockets) diff --git a/doc/updt.sh b/doc/updt.sh index 1c9d023e..e6b721d6 100755 --- a/doc/updt.sh +++ b/doc/updt.sh @@ -1,19 +1,15 @@ #!/bin/bash -# the HTML template -main='<html> -<head> - <link rel="stylesheet" type="text/css" href="doc.css"> - <meta charset="UTF-8"> -</head> -<body> -GENERATED-MARKDOWN-HERE -</body> -</html>' - -# substitute the pattern $1 by the content of the file $2 -subst() { - awk -v pat="$1" -v rep="$(sed 's:\\:\\\\:g' $2)" '{gsub(pat,rep);gsub(pat,"\\&");print}' +meta() { + awk ' + NR == 1 { t = $0; next } + NR == 2 && $1 ~ "======" { next } + NR == 2 { exit } + $1 == "Date:" { d = $2; for(i = 3 ; i <= NF ; i++) d = d " " $i; next } + $1 == "Author:" { a = $2; for(i = 3 ; i <= NF ; i++) a = a " " $i; next } + $1 == "version" || $1 == "Version" {next} + /^[ \t]*$/ { printf "%% %s\n%% %s\n%% %s\n", t, a, d; exit } + ' "$1" } # update the date field of file $1 @@ -29,14 +25,8 @@ updadate() { mkhtml() { local x=$1 local h=${x%%.md}.html - expand -i $x | sed 's:^ : :' > $h.pre - markdown -f toc,autolink $h.pre > $h.toc.no - markdown -Tf toc,autolink $h.pre > $h.toc.yes - head --bytes=-$(stat -c %s $h.toc.no) $h.toc.yes > $h.toc - echo "$main" | - subst GENERATED-MARKDOWN-HERE $h.toc.no | - subst TABLE-OF-CONTENT-HERE $h.toc > $h - rm $h.* + { meta "$x"; sed 's/TABLE-OF-CONTENT-HERE//' "$x"; } | + pandoc --css doc.css -f markdown -t html5 --toc > "$h" } # apply |