Difference between revisions of "Server Protocol"

From BaseX Documentation
Jump to navigation Jump to search
Line 1: Line 1:
This page presents the code structure of the BaseX clients and
+
This page presents the code structure of the BaseX clients,
the client/server protocol of BaseX that is needed to
+
and the client/server protocol needed to write [[Clients]]
write [[Clients]] in other programming languages.
+
in other programming languages.
  
 
==Description==
 
==Description==
  
First of all, the BaseX database server must be running to
+
* First of all, the BaseX database server must be running in order to use the clients.
work with the clients.
 
  
Each client provides a session class or script with methods
+
* 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.
to connect to and communicate with the database server.
 
  
A socket connection will be established by the constructor, which
+
* For the execution of commands you need to call the <code>execute()</code> method with the database command as argument. The method returns the result or throws an exception with the received error message.
expects a host, port, user name and password as arguments.
 
  
For the execution of commands you need to call the <code>execute()</code>
+
* The <code>query()</code> method creates a query instance. Variables can be bound to that object, and the result can either be requested by <code>execute()</code>, or in an iterative manner with the <code>more()</code> and <code>next()</code> functions. If an error occurs, an exception will be thrown.
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;
+
* The <code>create()</code>, <code>add()</code>, <code>replace()</code> and <code>store()</code> method can be used to pass on input streams to the corresponding database commands.
this way, all results will be directed to that output stream.
 
  
The <code>query()</code> method creates a query instance. Variables can be
+
* To speed up execution, an output stream can be specified by some clients; this way, all results will be directed to that output stream.
bound to that object, and the result can either be requested by
 
<code>execute()</code>, or in an iterative manner with the
 
<code>more()</code> and <code>next()</code> functions.
 
If an error occurs, an exception will be thrown.
 
  
The <code>create()</code>, <code>add()</code>, <code>replace()</code>
+
* Most clients are accompanied by some example files, which demonstrate how database commands can be executed or how queries can be evaluated.
and <code>store()</code> 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==
 
==Code Structure==

Revision as of 14:54, 19 September 2011

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

Description

  • First of all, the BaseX database server must be running in order to use 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.
  • 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.
  • To speed up execution, an output stream can be specified by some clients; this way, all results will be directed to that output stream.
  • 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