Skip to content

Commit

Permalink
updated client API to resemble wot operations
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshVSV committed Aug 8, 2024
1 parent 7be429a commit 504cde2
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions hololinked/client/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ObjectProxy:
when True, remote object is located and its resources are loaded. Otherwise, only the client is initialised.
protocol: str
ZMQ protocol used to connect to server. Unlike the server, only one can be specified.
**kwargs:
**kwargs:
async_mixin: bool, default False
whether to use both synchronous and asynchronous clients.
serializer: BaseSerializer
Expand Down Expand Up @@ -71,12 +71,12 @@ def __init__(self, instance_name : str, protocol : str = ZMQ_PROTOCOLS.IPC, invo
# compose ZMQ client in Proxy client so that all sending and receiving is
# done by the ZMQ client and not by the Proxy client directly. Proxy client only
# bothers mainly about __setattr__ and _getattr__
self._async_zmq_client = None
self._zmq_client = SyncZMQClient(instance_name, self.identity, client_type=PROXY, protocol=protocol,
self.async_zmq_client = None
self.zmq_client = SyncZMQClient(instance_name, self.identity, client_type=PROXY, protocol=protocol,
zmq_serializer=kwargs.get('serializer', None), handshake=load_thing,
logger=self.logger, **kwargs)
if kwargs.get("async_mixin", False):
self._async_zmq_client = AsyncZMQClient(instance_name, self.identity + '|async', client_type=PROXY, protocol=protocol,
self.async_zmq_client = AsyncZMQClient(instance_name, self.identity + '|async', client_type=PROXY, protocol=protocol,
zmq_serializer=kwargs.get('serializer', None), handshake=load_thing,
logger=self.logger, **kwargs)
if load_thing:
Expand Down Expand Up @@ -108,11 +108,11 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
del self
pass

def __bool__(self) -> bool:
try:
self._zmq_client.handshake(num_of_tries=10)
self.zmq_client.handshake(num_of_tries=10)
return True
except RuntimeError:
return False
Expand All @@ -121,12 +121,12 @@ def __eq__(self, other) -> bool:
if other is self:
return True
return (isinstance(other, ObjectProxy) and other.instance_name == self.instance_name and
other._zmq_client.protocol == self._zmq_client.protocol)
other.zmq_client.protocol == self.zmq_client.protocol)

def __ne__(self, other) -> bool:
if other and isinstance(other, ObjectProxy):
return (other.instance_name != self.instance_name or
other._zmq_client.protocol != self._zmq_client.protocol)
other.zmq_client.protocol != self.zmq_client.protocol)
return True

def __hash__(self) -> int:
Expand Down Expand Up @@ -164,7 +164,7 @@ def set_execution_timeout(self, value : typing.Union[float, int]) -> None:
)


def invoke(self, method : str, oneway : bool = False, noblock : bool = False,
def invoke_action(self, method : str, oneway : bool = False, noblock : bool = False,
*args, **kwargs) -> typing.Any:
"""
call a method specified by name on the server with positional/keyword arguments
Expand Down Expand Up @@ -207,7 +207,7 @@ def invoke(self, method : str, oneway : bool = False, noblock : bool = False,
return method(*args, **kwargs)


async def async_invoke(self, method : str, *args, **kwargs) -> typing.Any:
async def async_invoke_action(self, method : str, *args, **kwargs) -> typing.Any:
"""
async(io) call a method specified by name on the server with positional/keyword
arguments. noblock and oneway not supported for async calls.
Expand Down Expand Up @@ -241,7 +241,7 @@ async def async_invoke(self, method : str, *args, **kwargs) -> typing.Any:
return await method.async_call(*args, **kwargs)


def get_property(self, name : str, noblock : bool = False) -> typing.Any:
def read_property(self, name : str, noblock : bool = False) -> typing.Any:
"""
get property specified by name on server.
Expand Down Expand Up @@ -270,7 +270,7 @@ def get_property(self, name : str, noblock : bool = False) -> typing.Any:
return prop.get()


def set_property(self, name : str, value : typing.Any, oneway : bool = False,
def write_property(self, name : str, value : typing.Any, oneway : bool = False,
noblock : bool = False) -> None:
"""
set property specified by name on server with specified value.
Expand Down Expand Up @@ -307,7 +307,7 @@ def set_property(self, name : str, value : typing.Any, oneway : bool = False,
prop.set(value)


async def async_get_property(self, name : str) -> None:
async def async_read_property(self, name : str) -> None:
"""
async(io) get property specified by name on server.
Expand All @@ -329,7 +329,7 @@ async def async_get_property(self, name : str) -> None:
return await prop.async_get()


async def async_set_property(self, name : str, value : typing.Any) -> None:
async def async_write_property(self, name : str, value : typing.Any) -> None:
"""
async(io) set property specified by name on server with specified value.
noblock and oneway not supported for async calls.
Expand All @@ -354,7 +354,7 @@ async def async_set_property(self, name : str, value : typing.Any) -> None:
await prop.async_set(value)


def get_properties(self, names : typing.List[str], noblock : bool = False) -> typing.Any:
def read_multiple_properties(self, names : typing.List[str], noblock : bool = False) -> typing.Any:
"""
get properties specified by list of names.
Expand All @@ -381,7 +381,7 @@ def get_properties(self, names : typing.List[str], noblock : bool = False) -> ty
return method(names=names)


def set_properties(self, oneway : bool = False, noblock : bool = False,
def write_multiple_properties(self, oneway : bool = False, noblock : bool = False,
**properties : typing.Dict[str, typing.Any]) -> None:
"""
set properties whose name is specified by keys of a dictionary
Expand Down Expand Up @@ -418,7 +418,7 @@ def set_properties(self, oneway : bool = False, noblock : bool = False,
return method(**properties)


async def async_get_properties(self, names) -> None:
async def async_read_multiple_properties(self, names) -> None:
"""
async(io) get properties specified by list of names. no block gets are not supported for asyncio.
Expand All @@ -438,7 +438,7 @@ async def async_get_properties(self, names) -> None:
return await method.async_call(names=names)


async def async_set_properties(self, **properties) -> None:
async def async_write_multiple_properties(self, **properties) -> None:
"""
async(io) set properties whose name is specified by keys of a dictionary
Expand Down Expand Up @@ -523,9 +523,9 @@ def read_reply(self, message_id : bytes, timeout : typing.Optional[float] = 5000
obj = self._noblock_messages.get(message_id, None)
if not obj:
raise ValueError('given message id not a one way call or invalid.')
reply = self._zmq_client._reply_cache.get(message_id, None)
reply = self.zmq_client._reply_cache.get(message_id, None)
if not reply:
reply = self._zmq_client.recv_reply(message_id=message_id, timeout=timeout,
reply = self.zmq_client.recv_reply(message_id=message_id, timeout=timeout,
raise_client_side_exception=True)
if not reply:
raise ReplyNotArrivedError(f"could not fetch reply within timeout for message id '{message_id}'")
Expand All @@ -541,7 +541,7 @@ def load_thing(self):
"""
Get exposed resources from server (methods, properties, events) and remember them as attributes of the proxy.
"""
fetch = _RemoteMethod(self._zmq_client, CommonRPC.zmq_resource_read(instance_name=self.instance_name),
fetch = _RemoteMethod(self.zmq_client, CommonRPC.zmq_resource_read(instance_name=self.instance_name),
invokation_timeout=self._invokation_timeout) # type: _RemoteMethod
reply = fetch() # type: typing.Dict[str, typing.Dict[str, typing.Any]]

Expand All @@ -559,15 +559,15 @@ def load_thing(self):
elif not isinstance(data, (ZMQResource, ServerSentEvent)):
raise RuntimeError("Logic error - deserialized info about server not instance of hololinked.server.data_classes.ZMQResource")
if data.what == ResourceTypes.ACTION:
_add_method(self, _RemoteMethod(self._zmq_client, data.instruction, self.invokation_timeout,
self.execution_timeout, data.argument_schema, self._async_zmq_client, self._schema_validator), data)
_add_method(self, _RemoteMethod(self.zmq_client, data.instruction, self.invokation_timeout,
self.execution_timeout, data.argument_schema, self.async_zmq_client, self._schema_validator), data)
elif data.what == ResourceTypes.PROPERTY:
_add_property(self, _Property(self._zmq_client, data.instruction, self.invokation_timeout,
self.execution_timeout, self._async_zmq_client), data)
_add_property(self, _Property(self.zmq_client, data.instruction, self.invokation_timeout,
self.execution_timeout, self.async_zmq_client), data)
elif data.what == ResourceTypes.EVENT:
assert isinstance(data, ServerSentEvent)
event = _Event(self._zmq_client, data.name, data.obj_name, data.unique_identifier, data.socket_address,
serializer=self._zmq_client.zmq_serializer, logger=self.logger)
event = _Event(self.zmq_client, data.name, data.obj_name, data.unique_identifier, data.socket_address,
serializer=self.zmq_client.zmq_serializer, logger=self.logger)
_add_event(self, event, data)
self.__dict__[data.name] = event

Expand Down

0 comments on commit 504cde2

Please sign in to comment.