feat: unregister key
feat: _repr_
This commit is contained in:
parent
3d04765543
commit
78f051a231
@ -31,6 +31,8 @@ class ReplicatedDataFactory(object):
|
|||||||
|
|
||||||
# Default registered types
|
# Default registered types
|
||||||
self.register_type(str,RepCommand)
|
self.register_type(str,RepCommand)
|
||||||
|
self.register_type(RepDeleteCommand, RepDeleteCommand)
|
||||||
|
|
||||||
|
|
||||||
def register_type(self, dtype, implementation):
|
def register_type(self, dtype, implementation):
|
||||||
"""
|
"""
|
||||||
@ -180,7 +182,12 @@ class ReplicatedDatablock(object):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "{uuid} - Owner: {owner} - ETA: {state} ".format(
|
||||||
|
uuid=self.uuid,
|
||||||
|
owner=self.owner,
|
||||||
|
state=self.state
|
||||||
|
)
|
||||||
|
|
||||||
class RepCommand(ReplicatedDatablock):
|
class RepCommand(ReplicatedDatablock):
|
||||||
def serialize(self,data):
|
def serialize(self,data):
|
||||||
@ -192,6 +199,21 @@ class RepCommand(ReplicatedDatablock):
|
|||||||
def load(self,target):
|
def load(self,target):
|
||||||
target = self.pointer
|
target = self.pointer
|
||||||
|
|
||||||
|
class RepDeleteCommand(ReplicatedDatablock):
|
||||||
|
def serialize(self,data):
|
||||||
|
return pickle.dumps(data)
|
||||||
|
|
||||||
|
def deserialize(self,data):
|
||||||
|
return pickle.loads(data)
|
||||||
|
|
||||||
|
def store(self,rep_store):
|
||||||
|
assert(self.buffer)
|
||||||
|
|
||||||
|
if rep_store and self.buffer in rep_store.keys():
|
||||||
|
del rep_store[self.buffer]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# class RepObject(ReplicatedDatablock):
|
# class RepObject(ReplicatedDatablock):
|
||||||
# def deserialize(self):
|
# def deserialize(self):
|
||||||
# try:
|
# try:
|
||||||
|
@ -2,7 +2,7 @@ import threading
|
|||||||
import logging
|
import logging
|
||||||
import zmq
|
import zmq
|
||||||
import time
|
import time
|
||||||
from replication import ReplicatedDatablock, RepCommand
|
from replication import ReplicatedDatablock, RepCommand,RepDeleteCommand
|
||||||
from replication_graph import ReplicationGraph
|
from replication_graph import ReplicationGraph
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
@ -75,12 +75,21 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def unregister(self,object_uuid):
|
def unregister(self,object_uuid,clean=False):
|
||||||
"""
|
"""
|
||||||
Unregister for replication the given
|
Unregister for replication the given
|
||||||
object
|
object.
|
||||||
|
The clean option purpose is to remove
|
||||||
|
the pointer data's
|
||||||
"""
|
"""
|
||||||
pass
|
|
||||||
|
if object_uuid in self._rep_store.keys():
|
||||||
|
delete_command = RepDeleteCommand(owner='client', buffer=object_uuid)
|
||||||
|
delete_command.store(self._rep_store)
|
||||||
|
delete_command.push(self._net_client.publish)
|
||||||
|
else:
|
||||||
|
raise KeyError("Cannot unregister key")
|
||||||
|
|
||||||
|
|
||||||
class ClientNetService(threading.Thread):
|
class ClientNetService(threading.Thread):
|
||||||
def __init__(self,store_reference=None, factory=None,id="default"):
|
def __init__(self,store_reference=None, factory=None,id="default"):
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import collections
|
import collections
|
||||||
|
from replication import ReplicatedDatablock
|
||||||
|
|
||||||
class ReplicationGraph(collections.MutableMapping):
|
class ReplicationGraph(collections.MutableMapping):
|
||||||
"""
|
"""
|
||||||
@ -26,3 +27,4 @@ class ReplicationGraph(collections.MutableMapping):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -35,6 +35,9 @@ class RepSampleData(ReplicatedDatablock):
|
|||||||
|
|
||||||
def load(self,target=None):
|
def load(self,target=None):
|
||||||
import json
|
import json
|
||||||
|
if target is None:
|
||||||
|
target = SampleData()
|
||||||
|
|
||||||
target.map = json.loads(self.buffer['map'])
|
target.map = json.loads(self.buffer['map'])
|
||||||
|
|
||||||
|
|
||||||
@ -159,6 +162,45 @@ class TestClient(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(test_map_result.map["toto"], test_map["toto"])
|
self.assertEqual(test_map_result.map["toto"], test_map["toto"])
|
||||||
|
|
||||||
|
def test_client_unregister_key(self):
|
||||||
|
# Setup environment
|
||||||
|
factory = ReplicatedDataFactory()
|
||||||
|
factory.register_type(SampleData, RepSampleData)
|
||||||
|
|
||||||
|
server = Server(factory=factory)
|
||||||
|
server.serve(port=5560)
|
||||||
|
|
||||||
|
client = Client(factory=factory, id="cli_test_client_data_intergity")
|
||||||
|
client.connect(port=5560)
|
||||||
|
|
||||||
|
client2 = Client(factory=factory, id="cli2_test_client_data_intergity")
|
||||||
|
client2.connect(port=5560)
|
||||||
|
|
||||||
|
test_map = {"toto":"test"}
|
||||||
|
# Test the key registering
|
||||||
|
data_sample_key = client.register(SampleData(map=test_map))
|
||||||
|
|
||||||
|
test_map_result = SampleData()
|
||||||
|
|
||||||
|
#Waiting for server to receive the datas
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
client2._rep_store[data_sample_key].load(target=test_map_result)
|
||||||
|
|
||||||
|
client.unregister(data_sample_key)
|
||||||
|
|
||||||
|
|
||||||
|
client.disconnect()
|
||||||
|
client2.disconnect()
|
||||||
|
server.stop()
|
||||||
|
|
||||||
|
self.assertFalse(data_sample_key in client._rep_store)
|
||||||
|
|
||||||
|
def test_client_disconnect(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_client_change_rights(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
@ -175,6 +217,6 @@ def suite():
|
|||||||
return suite
|
return suite
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
runner = unittest.TextTestRunner(failfast=True,verbosity=2)
|
runner = unittest.TextTestRunner(verbosity=2)
|
||||||
runner.run(suite())
|
runner.run(suite())
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user