feat: refactoring progress
This commit is contained in:
parent
df5310e1fb
commit
4336ff9836
@ -62,7 +62,7 @@ class ReplicatedDatablock(object):
|
|||||||
|
|
||||||
key = self.uuid.encode()
|
key = self.uuid.encode()
|
||||||
|
|
||||||
socket.send_multipart([])
|
socket.send_multipart([key,data])
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def pull(cls, socket):
|
def pull(cls, socket):
|
||||||
@ -71,7 +71,7 @@ class ReplicatedDatablock(object):
|
|||||||
- read data from the socket
|
- read data from the socket
|
||||||
- reconstruct an instance
|
- reconstruct an instance
|
||||||
"""
|
"""
|
||||||
pass
|
uuid, data = socket.recv_multipart(zmq.NOBLOCK)
|
||||||
|
|
||||||
def store(self, dict, persistent=False):
|
def store(self, dict, persistent=False):
|
||||||
"""
|
"""
|
||||||
|
@ -2,11 +2,16 @@ import threading
|
|||||||
import logging
|
import logging
|
||||||
import zmq
|
import zmq
|
||||||
import time
|
import time
|
||||||
import replication
|
from replication import ReplicatedDatablock
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
STATE_INITIAL = 0
|
||||||
|
STATE_SYNCING = 1
|
||||||
|
STATE_ACTIVE = 2
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
def __init__(self,factory=None, config=None):
|
def __init__(self,factory=None, config=None):
|
||||||
self._rep_store = {}
|
self._rep_store = {}
|
||||||
@ -20,20 +25,22 @@ class Client(object):
|
|||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self._net.stop()
|
self._net.stop()
|
||||||
|
|
||||||
def register(self, object):
|
def add(self, object):
|
||||||
"""
|
"""
|
||||||
Register a new item for replication
|
Register a new item for replication
|
||||||
"""
|
"""
|
||||||
assert(object)
|
assert(object)
|
||||||
|
|
||||||
new_item = self._factory.construct(object)(owner="client")
|
new_item = self._factory.construct(object)(owner="client")
|
||||||
new_item.store(self._rep_store)
|
new_item.store(self._rep_store)
|
||||||
|
log.info("Registering {} on {}".format(object,new_item.uuid))
|
||||||
|
|
||||||
|
new_item.push(self._net.publish)
|
||||||
return new_item.uuid
|
return new_item.uuid
|
||||||
|
|
||||||
def get(self,object=None):
|
def pull(self,object=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def unregister(self,object):
|
def unregister(self,object):
|
||||||
pass
|
pass
|
||||||
@ -62,7 +69,7 @@ class ClientNetService(threading.Thread):
|
|||||||
self.publish = self.context.socket(zmq.PUSH)
|
self.publish = self.context.socket(zmq.PUSH)
|
||||||
self.publish.connect("tcp://127.0.0.1:5562")
|
self.publish.connect("tcp://127.0.0.1:5562")
|
||||||
|
|
||||||
self.state = 0
|
self.state = STATE_INITIAL
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -82,9 +89,6 @@ class ClientNetService(threading.Thread):
|
|||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
def send(data):
|
|
||||||
assert(issubclass(data, ReplicatedDatablock))
|
|
||||||
data.push(self.publish)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.exit_event.set()
|
self.exit_event.set()
|
||||||
@ -159,16 +163,52 @@ class ServerNetService(threading.Thread):
|
|||||||
poller.register(self.snapshot, zmq.POLLIN)
|
poller.register(self.snapshot, zmq.POLLIN)
|
||||||
poller.register(self.pull, zmq.POLLIN)
|
poller.register(self.pull, zmq.POLLIN)
|
||||||
|
|
||||||
self.state = 1
|
self.state = STATE_ACTIVE
|
||||||
|
|
||||||
while not self.exit_event.is_set():
|
while not self.exit_event.is_set():
|
||||||
items = dict(poller.poll(10))
|
# Non blocking poller
|
||||||
|
socks = dict(poller.poll(1000))
|
||||||
|
|
||||||
if not items:
|
# Snapshot system for late join (Server - Client)
|
||||||
pass
|
# if self.snapshot in socks:
|
||||||
|
# msg = self.snapshot.recv_multipart(zmq.DONTWAIT)
|
||||||
|
|
||||||
time.sleep(.1)
|
# identity = msg[0]
|
||||||
|
# request = msg[1]
|
||||||
|
|
||||||
|
# if request == b"SNAPSHOT_REQUEST":
|
||||||
|
# pass
|
||||||
|
# else:
|
||||||
|
# logger.info("Bad snapshot request")
|
||||||
|
# break
|
||||||
|
|
||||||
|
# ordered_props = [(SUPPORTED_TYPES.index(k.split('/')[0]),k,v) for k, v in self.property_map.items()]
|
||||||
|
# ordered_props.sort(key=itemgetter(0))
|
||||||
|
|
||||||
|
# for i, k, v in ordered_props:
|
||||||
|
# logger.info(
|
||||||
|
# "Sending {} snapshot to {}".format(k, identity))
|
||||||
|
# self.request_sock.send(identity, zmq.SNDMORE)
|
||||||
|
# v.send(self.request_sock)
|
||||||
|
|
||||||
|
# msg_end_snapshot = message.Message(key="SNAPSHOT_END", id=identity)
|
||||||
|
# self.request_sock.send(identity, zmq.SNDMORE)
|
||||||
|
# msg_end_snapshot.send(self.request_sock)
|
||||||
|
# logger.info("done")
|
||||||
|
|
||||||
|
# Regular update routing (Clients / Client)
|
||||||
|
if self.pull in socks:
|
||||||
|
log.info("Receiving changes from client")
|
||||||
|
msg = ReplicatedDatablock.pull(self.pull)
|
||||||
|
|
||||||
|
msg.store(self.store)
|
||||||
|
# msg = message.Message.recv(self.collector_sock)
|
||||||
|
# # logger.info("received object")
|
||||||
|
# # Update all clients
|
||||||
|
# msg.store(self.store)
|
||||||
|
# msg.send(self.pub_sock)
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.exit_event.set()
|
self.exit_event.set()
|
||||||
|
|
||||||
|
@ -65,6 +65,7 @@ class TestDataReplication(unittest.TestCase):
|
|||||||
factory.register_type(SampleData, RepSampleData)
|
factory.register_type(SampleData, RepSampleData)
|
||||||
data_sample = SampleData()
|
data_sample = SampleData()
|
||||||
rep_sample = factory.construct(data_sample)(owner="toto")
|
rep_sample = factory.construct(data_sample)(owner="toto")
|
||||||
|
|
||||||
self.assertEqual(isinstance(rep_sample,RepSampleData), True)
|
self.assertEqual(isinstance(rep_sample,RepSampleData), True)
|
||||||
|
|
||||||
def test_replicate_client_data(self):
|
def test_replicate_client_data(self):
|
||||||
@ -77,25 +78,11 @@ class TestDataReplication(unittest.TestCase):
|
|||||||
client_api.connect()
|
client_api.connect()
|
||||||
|
|
||||||
data_sample = SampleData()
|
data_sample = SampleData()
|
||||||
data_sample_key = client_api.register(data_sample)
|
data_sample_key = client_api.add(data_sample)
|
||||||
|
|
||||||
|
|
||||||
|
self.assertNotEqual(client_api._rep_store[data_sample_key],None)
|
||||||
|
|
||||||
self.assertEqual(data_sample_key)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# def test_client_connect(self):
|
|
||||||
# log.info("test_client_connect")
|
|
||||||
# self.client_api.connect()
|
|
||||||
# time.sleep(1)
|
|
||||||
# self.assertEqual(self.client_api.state(),1)
|
|
||||||
|
|
||||||
# def test_client_stop(self):
|
|
||||||
# self.client_api.stop()
|
|
||||||
# time.sleep(1)
|
|
||||||
# self.assertEqual(self.client_api.state(),0)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user