Changeset - ccbe369ce439
[Not reviewed]
default
0 6 1
Laman - 7 years ago 2018-01-19 01:17:36

checking version compatibility
7 files changed with 28 insertions and 10 deletions:
0 comments (0 inline, 0 general)
protocol.md
Show inline comments
 
# Protocol #
 

	
 
[Communication protocol proposal. Not yet fully implemented and expected to change.]
 
[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:
 

	
 
@@ -19,7 +19,7 @@ The client sends a request (a message) a
 
Initiates communication between the client and the server. Checks that the nodes can meaningfully talk to each other.
 

	
 
#### Params ####
 
- (str) version: program version. Both the client and the server are to check for compatibility with their partner.
 
- [(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
 
@@ -74,7 +74,7 @@ Cease talking and close the connection.
 
See [the init command](#init). If the client doesn't like server's protocol version, it is free to [end](#end) the connection.
 

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

	
 

	
 
### <a name="deny"></a>deny ###
 
@@ -82,6 +82,7 @@ The server refuses connection. It might 
 

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

	
 

	
src/client.py
Show inline comments
 
@@ -5,10 +5,11 @@ import logging as log
 
from datetime import datetime
 

	
 
import config as conf
 
import status
 
import stats
 
from util import Progress
 
from hashtree import HashTree,hashBlock
 
from netnode import BaseConnection,NetNode,FailedConnection,LockedException
 
from netnode import BaseConnection,NetNode,FailedConnection,LockedException,IncompatibleException
 

	
 

	
 
class DeniedConnection(Exception): pass
 
@@ -50,8 +51,12 @@ class Client(NetNode):
 
		self._outcoming.writeMsg(jsonData)
 
		jsonData,binData=self._incoming.readMsg()
 
		if jsonData["command"]=="deny":
 
			if jsonData["status"]==status.incompatible.version:
 
				raise DeniedConnection("Incompatible client version. Consider upgrading it.")
 
			raise DeniedConnection()
 
		assert jsonData["command"]=="init"
 
		if jsonData["version"]<conf.lowestCompatible:
 
			raise IncompatibleException("Incompatible server version. Consider upgrading it.")
 

	
 
	## Asks server for node hashes to determine which are to be transferred.
 
	#
src/config.py
Show inline comments
 
@@ -21,7 +21,8 @@ conf=dict()
 
if os.path.isfile(configFile):
 
	with open(configFile) as f: conf=json.load(f)
 

	
 
version=(0,0,0)
 
version=[0,0,1]
 
lowestCompatible=[0,0,0] # tuple is more fitting but json conversion transforms it into a list anyway
 

	
 
hosts=conf.get("hosts",["127.0.0.1"])
 
port=conf.get("port",9901)
src/morevna.py
Show inline comments
 
@@ -50,8 +50,9 @@ def push(args):
 
			print(stats.report())
 
			print()
 
		except FailedConnection: pass
 
		except DeniedConnection:
 
		except DeniedConnection as e:
 
			print("Server {0}:{1} denied connection.".format(*host))
 
			print(e)
 

	
 
def pull(args):
 
	_checkFile(args.datafile)
 
@@ -71,8 +72,9 @@ def pull(args):
 
		print()
 
		print(stats.report())
 
	except FailedConnection: pass
 
	except DeniedConnection:
 
	except DeniedConnection as e:
 
		print("Server {0}:{1} denied connection.".format(*host))
 
		print(e)
 

	
 
def serve(args):
 
	_checkFile(args.datafile)
src/netnode.py
Show inline comments
 
@@ -12,6 +12,7 @@ lockFile=os.path.join(conf.directory,"di
 

	
 
class FailedConnection(Exception): pass
 
class LockedException(Exception): pass
 
class IncompatibleException(Exception): pass
 

	
 

	
 
class BaseConnection: # abstract
src/server.py
Show inline comments
 
@@ -6,6 +6,7 @@ import logging as log
 
from hashtree import hashBlock
 
from netnode import BaseConnection,NetNode
 
import config as conf
 
import status
 

	
 

	
 
class Connection(BaseConnection):
 
@@ -40,7 +41,7 @@ class Miniserver:
 
				if p and p.is_alive():
 
					with connection as c:
 
						c[0].readMsg()
 
						c[1].writeMsg({"command":"deny"})
 
						c[1].writeMsg({"command":"deny","status":status.locked})
 
					continue
 
				p=multiprocessing.Process(target=Server.run,args=(connection,self._filename,self._treeFile))
 
				p.start()
 
@@ -79,9 +80,11 @@ class Server(NetNode):
 

	
 
		if jsonData["command"]=="init":
 
			if jsonData["blockSize"]!=self.BLOCK_SIZE or jsonData["blockCount"]!=self._tree.leafCount:
 
				self._outcoming.writeMsg({"command":"deny"})
 
				self._outcoming.writeMsg({"command":"deny","status":status.incompatible.parameters})
 
			if jsonData["version"]<conf.lowestCompatible:
 
				self._outcoming.writeMsg({"command":"deny","status":status.incompatible.version})
 
			if jsonData["action"]=="pull" and self.isLocked():
 
				self._outcoming.writeMsg({"command":"deny"})
 
				self._outcoming.writeMsg({"command":"deny","status":status.locked})
 
			if jsonData["action"]=="push" and not self.isLocked():
 
				self._lock()
 

	
src/status.py
Show inline comments
 
new file 100644
 
class incompatible:
 
	version=100
 
	parameters=101
 

	
 
locked=102
0 comments (0 inline, 0 general)