Skip to content

Commit

Permalink
added support DISCOVERY packets for getting available commands and args
Browse files Browse the repository at this point in the history
  • Loading branch information
zNitche committed Sep 2, 2024
1 parent 1f5add9 commit 03ff201
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
import os
from iscep import Client

Expand All @@ -12,11 +11,12 @@
auth_token=auth_token,
ssl_cert_file="../cert.pem") as client:

discovery = client.get_commands()
response = client.send_command("example_task", args={"message": "hello world"})
# time.sleep(5)
response2 = client.send_command("example_task")
response3 = client.send_command("example_task", use_auth=False)

print(f"available commands: {discovery}")
print(f"r1: {response}")
print(f"r2: {response2}")
print(f"r3: {response3}")
17 changes: 15 additions & 2 deletions iscep/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __send_close_connection_packet(self):

self.__send_packet(packet)

def send_echo(self, message: str) -> Packet | None:
def send_echo(self, message: str) -> Packet:
self.__logger.debug(f"sending echo...")

content = PacketContent(auth_token=self.auth_token, response={"echo": message})
Expand All @@ -90,7 +90,20 @@ def send_command(self,

return CommandResponse(type=response.type, response=response.content.response, error=response.content.error)

def __send_packet(self, packet: Packet) -> Packet | None:
def get_commands(self, use_auth: bool = True) -> dict[str, dict[str, object]] | None:
self.__logger.debug(f"sending commands discover...")

auth_token = self.auth_token if use_auth else None
packet = Packet(type=PacketType.DISCOVER, content=PacketContent(auth_token=auth_token))

response = self.__send_packet(packet)

if response is None or response.content is None:
return None

return response.content.response

def __send_packet(self, packet: Packet) -> Packet:
response = None

try:
Expand Down
11 changes: 6 additions & 5 deletions iscep/core/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

class PacketType(Enum):
ECHO = 0
SEND_CMD = 1
CMD_RESPONSE = 2
CLOSE_CONNECTION = 3
UNAUTHORIZED = 4
ERROR = 5
DISCOVER = 1
SEND_CMD = 2
CMD_RESPONSE = 3
CLOSE_CONNECTION = 4
UNAUTHORIZED = 5
ERROR = 6


class Packet:
Expand Down
14 changes: 14 additions & 0 deletions iscep/core/requests_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from iscep.utils import communication, auth
from iscep.core.packet import Packet, PacketType
from iscep.type_classes.packet_content import PacketContent
from iscep.utils.logger import Logger
from iscep.core.task import Task

Expand Down Expand Up @@ -118,6 +119,8 @@ def __process_packet(self, packet: Packet) -> Packet | None:
if not self.__check_auth(packet):
return Packet(type=PacketType.UNAUTHORIZED)

self.__commands_logger.info(f"processing packet: '{packet.type.name}' by '{self.__token_owner}'")

match packet.type:
case PacketType.CLOSE_CONNECTION:
self.requested_shutdown = True
Expand All @@ -126,6 +129,9 @@ def __process_packet(self, packet: Packet) -> Packet | None:
case PacketType.ECHO:
return packet

case PacketType.DISCOVER:
return self.__commands_discovery()

case PacketType.SEND_CMD:
return self.__process_cmd(packet)

Expand All @@ -147,3 +153,11 @@ def __process_cmd(self, packet: Packet) -> Packet:
return Packet.get_cmd_response_packet(response)

return Packet.get_error_packet(f"command not found")

def __commands_discovery(self) -> Packet:
struct = {}

for task_key in self.tasks:
struct[task_key] = self.tasks[task_key].get_module_args()

return Packet(type=PacketType.DISCOVER, content=PacketContent(response=struct))
12 changes: 12 additions & 0 deletions iscep/core/task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import inspect


class Task:
def __init__(self, name: str):
self.name = name

def module(self, *args, **kwargs):
raise NotImplemented()

def get_module_args(self):
args = {}
inspect_data = inspect.signature(self.module).parameters

for key, val in inspect_data.items():
args[key] = val.annotation.__name__

return args

def run(self, args: dict[str, any] | None = None) -> any:
res = self.module(**args if args is not None else {})
return res

0 comments on commit 03ff201

Please sign in to comment.