Changeset - 13603ac261f1
[Not reviewed]
default
0 3 0
Laman - 7 years ago 2018-02-19 22:46:08

minor enhancements
3 files changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
protocol.md
Show inline comments
 
# Protocol #
 
# Protocol v0.1 #
 

	
 
[Communication protocol proposal. Not yet fully implemented and still expected to change.]
 

	
 
Nodes communicate by exchanging messages through network sockets. Basic message format is simple:
 

	
 
		json-length: ABC
 
		bin-length: DEF
 
		{... JSON payload ...}
 
		... binary payload ...
 

	
 
`ABC` is the JSON payload length (decimal integer), `DEF` is the binary payload length. JSON payload always contains a "command" field.
 

	
 
The client sends a request (a message) and the server answers with a response (another message).
 

	
 

	
 
## Commands ##
 
### <a name="init"></a>init ###
 
Initiates communication between the client and the server. Checks that the nodes can meaningfully talk to each other.
 

	
 
#### Params ####
 
- [(int) major, (int) minor, (int) tiny] version version: program version. Both the client and the server are to check for compatibility with their partner.
 
- {push, pull} action: what the client desires to do
 
- (str) filename: name of the file to be synchronized
 
- (int) blockSize
 
- (int) blockCount
 

	
 
#### Responses ####
 
- [init](#r-init)
 
- [deny](#deny)
 

	
 

	
 
### <a name="req"></a>req ###
 
Requests hashes or a data chunk from the server.
 

	
 
#### Params ####
 
- {hash, data} dataType
 

	
 
hash
 
- [(int) i0, ...] index: list of requested node keys from the HashTree
 

	
 
data
 
- (int) index: requested data block number in the data file. Returns blockSize bytes starting at blockSize * index.
 
- [(int) i0, ...] index: list of requested data blocks from the data file. Returns blockSize bytes starting at blockSize * i.
 

	
 
#### Responses ####
 
- [send](#send)
 
- [err](#err)
 

	
 

	
 
### <a name="send"></a>send ####
 
Sends data. Can be a client action or a response to a request.
 

	
 
#### Params ####
 
- {hash, data} dataType
 
- index: see [req](#req)
 

	
 
#### Responses ####
 
- [ack](#ack)
 
- [err](#err)
 

	
 

	
 
### <a name="end"></a>end ###
 
Cease talking and close the connection.
 

	
 
#### Params ####
 
- {push} (optional) action: unlocks the server's dirty lock
 

	
 
#### Responses ####
 
- end
 

	
 

	
 
## Responses ##
 
### <a name="r-init"></a>init ###
 
See [the init command](#init). If the client doesn't like server's protocol version, it is free to [end](#end) the connection.
 

	
 
#### Params ####
 
- [(int) major, (int) minor, (int) tiny] version
 

	
 

	
 
### <a name="deny"></a>deny ###
 
The server refuses connection. It might be busy, corrupted or incompatible.
 

	
 
#### Params ####
 
- (int) retry: suggested delay in seconds before another attempt at connection
 
- (int) status
 
- (str) msg
 

	
 

	
 
### <a name="ack"></a>ack ###
 
Everything is alright, but the server has nothing better to say.
 

	
src/morevna.sh
Show inline comments
 
#!/bin/bash
 

	
 
# setup encrypted container
 
#sudo losetup -f ext2.img
 
#sudo cryptsetup open --type=luks /dev/loop0 ext2luks
 
#sudo mkfs.ext2 /dev/mapper/ext2luks
 

	
 
# generate certificate
 
# openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=name"
 

	
 
set -e
 
DIRNAME=`dirname $0`
 

	
 
sudo losetup -f ~/ext2.img
 
sudo cryptsetup open --type=luks /dev/loop0 ext2luks
 
sudo mount /dev/mapper/ext2luks ~/temp
 

	
 
sudo rdiff-backup -v 5 ~/Dokumenty ~/temp/Dokumenty
 
sudo rdiff-backup -v 5 --exclude '**/__pycache__/' ~/Projekty ~/temp/Projekty
 
sudo rdiff-backup -v 5 --exclude-regexp '/__pycache__/' ~/Projekty ~/temp/Projekty
 
sudo rdiff-backup -v 5 ~/Obrázky ~/temp/Obrázky
 

	
 
sudo umount /dev/mapper/ext2luks
 
sudo cryptsetup close ext2luks
 
sudo losetup -d /dev/loop0
 

	
 
echo
 

	
 
python $DIRNAME/morevna.py build ~/ext2.bin ~/ext2.img
 
python $DIRNAME/morevna.py push --tree ~/ext2.bin ~/ext2.img
src/networkers.py
Show inline comments
 
import json
 

	
 
import stats
 

	
 

	
 
class NetworkReader:
 
	def __init__(self,stream):
 
		self._stream=stream
 

	
 
	def readMsg(self):
 
		data=self._stream.readline()
 
		assert data
 
		stats.logReceived(data)
 
		jsonLength=int(data.split(b":")[1].strip()) # "json-length: length" -> length
 

	
 
		data=self._stream.readline()
 
		assert data
 
		stats.logReceived(data)
 
		binLength=int(data.split(b":")[1].strip()) # "bin-length: length" -> length
 

	
 
		jsonData=self._stream.read(jsonLength)
 
		assert len(jsonData)==jsonLength
 
		stats.logReceived(jsonData)
 
		jsonData=json.loads(str(jsonData,encoding="utf-8"))
 

	
 
		binData=self._stream.read(binLength)
 
		assert len(binData)==binLength
 
		stats.logReceived(binData)
 
		
 
		return (jsonData,binData)
 
		
 

	
 
class NetworkWriter:
 
	def __init__(self,stream):
 
		self._stream=stream
 

	
 
	def writeMsg(self,*args):
 
		msg=self.prepMsg(*args)
 
		self._stream.write(msg)
 
		self._stream.flush()
 
		stats.logSent(msg)
 

	
 
	def prepMsg(self,jsonData,binData=b""):
 
		jsonData=bytes(json.dumps(jsonData)+"\n",encoding="utf-8")
 
		jsonData=bytes(json.dumps(jsonData,separators=(',',':'))+"\n",encoding="utf-8")
 
		jsonLength=bytes("json-length: "+str(len(jsonData))+"\n",encoding="utf-8")
 
		binLength=bytes("bin-length: "+str(len(binData))+"\n",encoding="utf-8")
 

	
 
		return b"".join((jsonLength,binLength,jsonData,binData))
0 comments (0 inline, 0 general)