This chapter will explain the basics of how this can be done using the facilities provided by the server-side Javascript and dunamic HTML framework in the clarens-server-web< package.
This chapter describes how to write services that use the Javascript Object Notation (JSON), which is conveniently handled natively by most modern browsers.
These files are also available using standard HTTP GET requests under the server URL, typically of the form http://machine.name/clarens/. This is summarized in table 4.1.
| Context | Path |
| Filesystem | $opkg_root/share/apache2/clarens/.clarens_file_root |
| File service | / |
| Browser URL | http://machine.name/clarens/ |
| Path | Description |
| / | Index file to display 'shell' of different services |
| /web | Files that make up the navigation panel and system-provided content |
| /web/registry | Registry files used to generate navigation panel |
| /web/javascript | System-provided Javascript files |
| /web/images | System-provided image files |
| /web/images | System-provided stylesheet files |
| /web/service_name | Service directories |
These files are in the form of JSONRPC response methods as if generated by web service calls. They are text files that can be created with any text editor, or automatically using a scripting language like Python.
Each registry file may describe one or more service interfaces, with an associated category, name, description and a link to the interface HTML page. In the registry file these properties are as follows:
| Element | Description |
| name | Short name to be displayed in the menu |
| desc | Longer description to be displyed as a 'tooltip' |
| cat | A category to place the item in |
| file | A relative link to the interface HTML file |
The file containing these definitions must have the .json extension to be used in the navigation menu.
As an example, let's create a registry file for a test service, with a user interface in /web/test:
| JSON | |
|
For anyone familiar with Javascript or Python, the above should be immediately obvious: the file consists of a mapping with elements id, error, and result. The id tag is ignored, while the error must always be null.
The result element contains a list of mappings, in this case only one, each with the information needed to describe one menu item.
| HTML | |
|
This page does a few things:
| Javascript | |
|
First the value of the word form element is obtained, followed by a call to the jsonrpc method provided by the Clarens framework.
The jsonrpc method takes the following arguments:
| Type | Name | Description |
| string | method | The name of the remote method |
| list | arguments | A list of arguments |
| function | success callback | Function to be called upon success |
| function | fault callback | Function to be called when a fault is returned |
The arguments to the remote method is usually passed as a list, although in our example we passed a single string as an argument. The success callback, named echo_callback in the example, takes a result object as its only argument. The type of the result object is determined by the remote method.
The fault callback is called with the full JSONRPC result object as was demonstrated in section 4.1.3 for the registry files. In the case of a fault, or exception, returned by the server, the error element of the JSONRPC return object will contain information about the fault, while the result element will be set to null.