@@ -65,16 +65,27 @@ def rewrite_atoms_dict(bert_dict):
6565 # rewriter
6666 return {str (k ): bert_val (v ) for (k , v ) in bert_dict .items ()}
6767
68+
6869class BinaryStorageConnection (object ):
70+ '''
71+ A connection slot which is instantiated by BinaryStorageManager.
72+
73+ The states of a slot are: 'closed', 'opening', 'busy', 'idle'. Additionally a slot can either
74+ be general purpose or associated with a Vault (i.e. it is logged in to that Vault and can
75+ execute commands related to it).
76+ '''
77+
6978 def __init__ (self , manager ):
7079 self .manager = manager
80+ self .writer = None
81+ self .reader = None
82+
83+ # State
7184 self .vault = None
7285 self .available = asyncio .Event ()
7386 self .available .clear ()
7487 self .connected = False
7588 self .connecting = False
76- self .writer = None
77- self .reader = None
7889
7990 @property
8091 def state (self ):
@@ -96,29 +107,35 @@ def __repr__(self):
96107 def read_term (self , assert_ok = True ):
97108 '''reads a BERT tuple, asserts that first item is "ok"'''
98109 pl_read = (yield from self .reader .read (4 ))
99- #logger.debug('Read: %s (%d bytes)', pl_read, len(pl_read))
110+
100111 if len (pl_read ) != 4 :
101112 raise ConnectionResetException ()
113+
102114 pl_tuple = struct .unpack ('!I' , pl_read )
103115 packet_length = pl_tuple [0 ]
104116 assert packet_length > 0
105- #logger.debug('Will read %d bytes', packet_length)
117+
106118 packet = b''
107119 while len (packet ) < packet_length :
108120 buf = yield from self .reader .read (packet_length - len (packet ))
109121 if len (buf ) == 0 :
110122 raise ConnectionResetException ()
111123 packet += buf
124+
112125 if BINARY_DEBUG :
113126 logger .debug ('[READ] Serialized: %s' , packet )
127+
114128 decoded = bert .decode (packet )
129+
115130 if BINARY_DEBUG :
116131 logger .debug ('[READ] Unserialized: %s' , decoded )
132+
117133 if assert_ok and decoded [0 ] != Atom ('ok' ):
118134 if decoded [0 ] == Atom ('error' ):
119135 raise ServerError (decoded [1 :])
120136 else :
121137 raise UnsuccessfulResponse (decoded )
138+
122139 return decoded
123140
124141 @asyncio .coroutine
@@ -282,6 +299,8 @@ def disconnect(self):
282299 self ._clear_connection ()
283300
284301 def __enter__ (self ):
302+ # This enables the connection to be used as a context manager. When the context is closed,
303+ # the connection is automatically set to "idle" (available).
285304 return self
286305
287306 def __exit__ (self , ex_type , ex_value , ex_st ):
@@ -698,7 +717,15 @@ def myco(*args, **kwargs):
698717 def version (self ):
699718 return self .server_version
700719
720+
701721class BinaryStorageManager (object ):
722+ '''
723+ The BinaryStorageManager will manage n connection slots of type BinaryStorageConnection.
724+
725+ It will automatically open new connections or find idle connections when a new connection
726+ is requested through the "acquire_connection" function. It will also close connection that
727+ have been idle for some time.
728+ '''
702729
703730 def __init__ (self ):
704731 self .host = None
@@ -825,6 +852,10 @@ def get_manager_instance():
825852
826853
827854class BinaryStorageBackend (StorageBackend ):
855+ '''
856+ Implements the actual backend for the vault. Each Vault will have its own BinaryStorageBackend
857+ object associated with it, but all will use the same manager.
858+ '''
828859
829860 def __init__ (self , vault = None , auth = None , host = None , port = None ,
830861 concurrency = None , username = None , password = None , ssl = True ,
0 commit comments