Server Protocol

From BaseX Documentation
Revision as of 14:39, 18 September 2011 by CG (talk | contribs)
Jump to navigation Jump to search

This page presents the code structure of the BaseX clients and the client/server protocol of BaseX that is needed to write clients in other programming languages.

Description

First of all, the BaseX database server must be running to work with the clients.

Each client provides a session class or script with methods to connect to and communicate with the database server.

A socket connection will be established by the constructor, which expects a host, port, user name and password as arguments.

For the execution of commands you need to call the execute() method with the database command as argument. The method returns the result or throws an exception with the received error message.

To speedup execution, an output stream can be specified by some clients; this way, all results will be directed to that output stream.

The query() method creates a query instance. Variables can be bound to that object, and the result can either be requested by execute(), or in an iterative manner with the more() and next() functions. If an error occurs, an exception will be thrown.

The create(), add(), replace() and store() method can be used to pass on input streams to the corresponding database commands.

Most clients are accompanied by some example files, which demonstrate how database commands can be executed or how queries can be evaluated.

Code Structure

Session

  • Creates and returns session with host, port, user name and password:
    Session(String host, int port, String name, String password)
  • Executes a command and returns the result:
    String execute(String command)
  • Returns a query object for the specified query:
    Query query(String query)
  • Creates a database from an input stream:
    void create(String name, InputStream in)
  • Adds a document to the current database from an input stream:
    void add(String name, String target, InputStream in)
  • Replaces a document with the specified input stream:
    void replace(String path, InputStream in)
  • Stores raw data at the specified path:
    void store(String path, InputStream in)
  • Watches the specified event:
    void watch(String name, Event notifier)
  • Unwatches the specified event:
    void unwatch(String name)
  • Returns process information:
    String info()
  • Closes the session:
    void close()

Query

  • Creates query object with session and query:
    constructor(Session s, String query)
  • Binds an external variable:
    void bind(String name, String value)
  • Executes the query:
    String execute()
  • Iterator: checks if a query returns more items:
    boolean more()
  • Returns next item:
    String next()
  • Returns query information:
    String info()
  • Returns serialization options:
    String options()
  • Closes the iterator and query:
    close()

Transfer Protocol

Syntax

  • \x: single byte.
  • {...}: strings, which are transfered in the UTF8 encoding.
  • [...]: binary data. All 00 and FF bytes are prefixed with FF.

Authentication

  1. Client connects to server socket
  2. Server sends timestamp: {timestamp} \0
  3. Client sends username and hash: {username} \0 {md5(md5(password) + timestamp)} \0
  4. Server sends \0 (success) or \1 (error)

Client Request

- command:       -> {command} \0
- create:        -> \8 {name} \0 [input] \0
- add:           -> \9 {name} \0 {path} \0 [input] \0
- watch:         -> \10 {name} \0
- unwatch:       -> \11 {name} \0
- replace:       -> \12 {path} \0 [input] \0
- store:         -> \13 {path} \0 [input] \0
- query: start   -> \0 {query} \0
         bind    -> \3 {id} \0 {variable} \0 {value} \0 {type} \0
         next    -> \1 {id} \0
         execute -> \5 {id} \0
         info    -> \6 {id} \0
         options -> \7 {id} \0
         close   -> \2 {id} \0

Server Response

- command:       -> {result} \0 {info} \0 \0
- create:        -> {info} \0 \0
- add:           -> {info} \0 \0
         error   -> \0 {error} \0 \1
- query: start   -> {id} \0 \0
         bind    -> \0 \0
         next    -> {result} \0 \0
         execute -> {result} \0 \0
         info    -> {result} \0 \0
         options -> {result} \0 \0
         close   -> \0 \0
         error   -> \0 \1 {error} \0