feat : delete

This commit is contained in:
inrev 2019-04-30 17:18:41 +02:00
parent 678cd5ac0f
commit cf16dbc788
4 changed files with 49 additions and 17 deletions

View File

@ -10,18 +10,19 @@ from random import randint
import copy import copy
import queue import queue
import zmq
lock = threading.Lock() lock = threading.Lock()
try: try:
from .libs import umsgpack from .libs import umsgpack
from .libs import zmq # from .libs import zmq
from .libs import dump_anything from .libs import dump_anything
from . import helpers from . import helpers
from . import message from . import message
except: except:
# Server import # Server import
from libs import umsgpack from libs import umsgpack
from libs import zmq # from libs import zmq
from libs import dump_anything from libs import dump_anything
import helpers import helpers
import message import message
@ -88,6 +89,13 @@ class RCFClient(object):
self.pipe.send_multipart( self.pipe.send_multipart(
[b"INIT"]) [b"INIT"])
def disconnect(self):
"""
Disconnect
"""
self.pipe.send_multipart(
[b"DISCONNECT"])
def set(self, key, value=None, override=False): def set(self, key, value=None, override=False):
"""Set new value in distributed hash table """Set new value in distributed hash table
Sends [SET][key][value] to the agent Sends [SET][key][value] to the agent
@ -118,6 +126,11 @@ class RCFClient(object):
def exit(self): def exit(self):
if self.agent.is_alive(): if self.agent.is_alive():
self.disconnect()
# Disconnect time
time.sleep(0.2)
global stop global stop
stop = True stop = True
@ -223,6 +236,17 @@ class RCFClientAgent(object):
else: else:
logger.error("E: too many servers (max. %i)", SERVER_MAX) logger.error("E: too many servers (max. %i)", SERVER_MAX)
elif command == b"DISCONNECT":
if not self.admin:
uid = self.id.decode()
for k,v in self.property_map.items():
if v.body["id"] == uid:
delete_msg = message.RCFMessage(
key=k, id=self.id, body=None)
# delete_msg.store(self.property_map)
delete_msg.send(self.publisher)
elif command == b"SET": elif command == b"SET":
key = umsgpack.unpackb(msg[0]) key = umsgpack.unpackb(msg[0])
value = umsgpack.unpackb(msg[1]) value = umsgpack.unpackb(msg[1])
@ -293,8 +317,10 @@ class RCFClientAgent(object):
if 'Client' in k: if 'Client' in k:
dump_list.append([k,v.id.decode()]) dump_list.append([k,v.id.decode()])
else: else:
dump_list.append([k,v.body['id']]) try:
dump_list.append([k,v.body['id']])
except:
pass
self.pipe.send(umsgpack.packb(dump_list) self.pipe.send(umsgpack.packb(dump_list)
if dump_list else umsgpack.packb('')) if dump_list else umsgpack.packb(''))
@ -353,7 +379,7 @@ def rcf_client_agent(ctx, pipe, queue):
client_store = message.RCFMessage( client_store = message.RCFMessage(
key=client_key, id=agent.id, body=client_dict) key=client_key, id=agent.id, body=client_dict)
logger.info(client_store) logger.info(client_store)
client_store.store(agent.property_map, True) client_store.store(agent.property_map)
client_store.send(agent.publisher) client_store.send(agent.publisher)
logger.info("snapshot complete") logger.info("snapshot complete")
agent.state = State.ACTIVE agent.state = State.ACTIVE

View File

@ -40,6 +40,8 @@ def load(key, value):
target = resolve_bpy_path(key) target = resolve_bpy_path(key)
target_type = key.split('/')[0] target_type = key.split('/')[0]
if value == "None":
return
if target_type == 'Object': if target_type == 'Object':
load_object(target=target, data=value, load_object(target=target, data=value,

View File

@ -1,3 +1,4 @@
import logging
try: try:
from .libs import umsgpack from .libs import umsgpack
from .libs import zmq from .libs import zmq
@ -6,6 +7,8 @@ except:
from libs import umsgpack from libs import umsgpack
from libs import zmq from libs import zmq
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
class RCFMessage(object): class RCFMessage(object):
""" """
@ -28,28 +31,29 @@ class RCFMessage(object):
def apply(self): def apply(self):
pass pass
def store(self, dikt, override=False): def store(self, dikt):
"""Store me in a dict if I have anything to store""" """Store me in a dict if I have anything to store"""
# this currently erasing old value # this currently erasing old value
if self in dikt: if self.key is not None:
if override: if self in dikt and self.body == 'None':
dikt[self.key] = self logger.info("erasing key {}".format(self.key))
else: del dikt[self.key]
if self.key is not None: else:
dikt[self.key] = self dikt[self.key] = self
# elif self.key in dikt: # elif self.key in dikt:
# del dikt[self.key] # del dikt[self.key]
def send(self, socket): def send(self, socket):
"""Send key-value message to socket; any empty frames are sent as such.""" """Send key-value message to socket; any empty frames are sent as such."""
key = ''.encode() if self.key is None else self.key.encode() key = ''.encode() if self.key is None else self.key.encode()
body = ''.encode() if self.body is None else umsgpack.packb(self.body) body = umsgpack.packb('None') if self.body is None else umsgpack.packb(self.body)
id = ''.encode() if self.id is None else self.id id = ''.encode() if self.id is None else self.id
try: try:
socket.send_multipart([key, id, body]) socket.send_multipart([key, id, body])
except: except:
print("Fail to send {} {}".format(key, id)) print("Fail to send {} {} {}".format(key, id, body))
@classmethod @classmethod
def recv(cls, socket): def recv(cls, socket):

View File

@ -170,14 +170,14 @@ def init_datablocks():
def default_tick(): def default_tick():
# bpy.ops.session.refresh() bpy.ops.session.refresh()
# global client_instance # global client_instance
# if not client_instance.queue.empty(): # if not client_instance.queue.empty():
# update = client_instance.queue.get() # update = client_instance.queue.get()
# helpers.load(update[0],update[1]) # helpers.load(update[0],update[1])
return 10 return 1
def draw_tick(): def draw_tick():
@ -186,7 +186,7 @@ def draw_tick():
drawer.draw() drawer.draw()
# refresh_window() refresh_window()
# Upload # Upload
upload_client_instance_position() upload_client_instance_position()
@ -219,7 +219,7 @@ def unregister_ticks():
# REGISTER Updaters # REGISTER Updaters
global drawer global drawer
drawer.unregister_handlers() drawer.unregister_handlers()
bpy.app.timers.unregister(draw_tick) # bpy.app.timers.unregister(draw_tick)
bpy.app.timers.unregister(default_tick) bpy.app.timers.unregister(default_tick)