Changeset - e6d2c5c6027d
[Not reviewed]
0 0 1
HanzZ - 12 years ago 2013-01-26 12:47:09
hanzz.k@gmail.com
protocol
1 file changed with 52 insertions and 0 deletions:
0 comments (0 inline, 0 general)
developer/backends/protocol.md
Show inline comments
 
new file 100644
 
---
 
layout: page
 
title: Spectrum 2
 
---
 

	
 
## Spectrum backend protocol
 

	
 
Spectrum 2 communicates with backend using protocol based on [Google Protocol Buffers](http://code.google.com/p/protobuf/). This protocol has libraries for many languages, so backend can be written practically in any language.
 

	
 
This document describes how to implement new backend in different language than C++.
 

	
 
### Backend execution
 

	
 
When XMPP user wants to login to legacy network, Spectrum 2 executes particular backend according to configuration. Typically it executes backend with following arguments:
 

	
 
	/usr/bin/backend --host localhost --port 31640 --service.backend_id=1 "/etc/spectrum2/transports/sample_client.cfg"
 

	
 
As the first thing after start, backed should connect Spectrum 2 main instance according to `--host` and `--port` argument. `--service.backend_id` is ID of backend and the last argument is always path to config file. Note that Spectrum 2 can pass more
 
arguments to backend and backend should ignore them.
 

	
 
### The protocol
 

	
 
When connection betwen backend and Spectrum 2 is establish, Spectrum 2 starts communicating with the backend.
 

	
 
The protocol is defined in in [include/transport/protocol.proto](https://github.com/hanzz/libtransport/blob/master/include/transport/protocol.proto) file. Usually there is tool for your language to compile this .proto file into source module which can be later used to serialize/deserialize structures in your language and make packets from them.
 

	
 
Once you serialize particular Protocol Buffer structure, you can send it in following format:
 

	
 
	| size | serialized Protocol Buffer structure |
 

	
 
* size - 4 bytes long size of "serialized Protocol Buffer structure" in network encoding (as returned by htonl() function - see "man htonl")
 

	
 
### Receiving PING message
 

	
 
First message sent by Spectrum 2 is always PING. Backend has to response this message with PONG.
 

	
 
Your backend will receive 4 bytes in network encoding. You should parse those 4 bytes to number representation and store it into variable N. Then you should read N bytes from the socket to receive serialized Protobuf message called WrapperMessage. Use the
 
module generated from protocol.proto to parse it.
 

	
 
WrapperMessage has two fields, type and payload:
 

	
 
* type - Type of the real message stored in the payload field.
 
* payload - Real message wrapped in this WrapperMessage. This is again serialized Protobuf message and can be parsed using the module generated from protocol.proto
 

	
 
In our case, the type is TYPE_PING and the payload is not defined. You should now answer the PING message
 

	
 
### Answering PING message
 

	
 
PING message has to be answered with PONG. To Answer this message, just generate WrapperMessage using the module generated from protocol.proto and set the type to TYPE_PONG. Now serialie the message, prepend it with its size as defined above and send it to Spectrum 2.
 

	
 

	
 

	
0 comments (0 inline, 0 general)