Skip to content

Commit

Permalink
Implemented pong support
Browse files Browse the repository at this point in the history
  • Loading branch information
naman108 committed Dec 22, 2023
1 parent 30c543f commit a201fe7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[actionmapping]
hello_world.HelloService?Hello?client_message = components.hello_world.hello_world_facade.HelloWorld.client_message
?Hello?ClientMessage = components.hello_world.hello_world_facade.HelloWorld.client_message

[HelloWorld]
__class = components.hello_world.hello_world.HelloWorld
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ repo = https://pypi.org/project/DigtalPyHelloWorld/
license = EPL

[HelloService]
default_status = Running
service_id = hello_world.HelloService
name = HelloService
description = "Hello World Service"
network = TCPNetwork
protocol = hello_protocol
protocol = XML
port = 8080
host = 127.0.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TYPE_CHECKING

from digitalpy.core.main.controller import Controller
from digitalpy.core.network.domain.network_client import NetworkClient
from digitalpy.core.domain.domain.network_client import NetworkClient
if TYPE_CHECKING:
from digitalpy.core.digipy_configuration.configuration import Configuration
from digitalpy.core.zmanager.action_mapper import ActionMapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

if TYPE_CHECKING:
from digitalpy.core.digipy_configuration.configuration import Configuration
from digitalpy.core.zmanager.action_mapper import ActionMapper
from digitalpy.core.zmanager.impl.default_action_mapper import DefaultActionMapper
from digitalpy.core.zmanager.request import Request
from digitalpy.core.zmanager.response import Response
from digitalpy.core.domain.domain.network_client import NetworkClient


class HelloController(Controller):
Expand All @@ -18,7 +19,7 @@ class HelloController(Controller):

def __init__(self, request: 'Request',
response: 'Response',
sync_action_mapper: 'ActionMapper',
sync_action_mapper: 'DefaultActionMapper',
configuration: 'Configuration'):
super().__init__(request, response, sync_action_mapper, configuration)
self.hello_builder = HelloMessageBuilder(
Expand All @@ -30,11 +31,15 @@ def initialize(self, request: 'Request', response: 'Response'):
self.hello_builder.initialize(request, response)
return super().initialize(request, response)

def client_message(self, data: str, *args, **kwargs): # pylint: disable=unused-argument
def client_message(self, client: 'NetworkClient', data: bytes, config_loader, *args, **kwargs): # pylint: disable=unused-argument
"""This function is used to handle inbound messages from a service and send a
response back to the service."""

self.hello_builder.build_empty_object(self.configuration)
self.hello_builder.add_object_data(data)
self.hello_builder.build_empty_object(config_loader)
self.hello_builder.add_object_data(str(data))
msg = self.hello_builder.get_result()

# list type as only lists are supported for this parameter
self.response.set_value("message", [msg])
self.response.set_value("recipients", [str(client.get_oid())])
self.response.set_action("publish")
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, *args, **kwargs):

def build_empty_object(self, config_loader, *args, **kwargs): # pylint: disable=unused-argument
"""Builds a HelloMessage object"""
self.request.set_value("object_class_name", "MissionChangeRecord")
self.request.set_value("object_class_name", "HelloMessage")

configuration = config_loader.find_configuration(SIMPLE_HELLO_MESSAGE)

Expand All @@ -22,7 +22,7 @@ def build_empty_object(self, config_loader, *args, **kwargs): # pylint: disable

def add_object_data(self, mapped_object: str):
"""adds the data from the mapped object to the HelloMessage object """
self.result.message = mapped_object
self.result.text = mapped_object

def get_result(self):
"""gets the result of the builder"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class HelloClient(Node): # pylint: disable=abstract-method
"""HelloClient is a DigitalPyNode that is used to manipulate Hello Clients.
"""

def __init__(self, node_type="hello_client", oid=None) -> None:
super().__init__(node_type, oid=oid) # type: ignore
def __init__(self, model_configuration, model, oid=None, node_type="hello_client") -> None:
super().__init__(node_type, model_configuration=model_configuration, model=model, oid=oid) # type: ignore
self._name: str = ""

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
class HelloMessage(Node): # pylint: disable=abstract-method
"""HelloMessage is a DigitalPyNode that is used to manipulate Hello Messages."""

def __init__(self, node_type="hello_message", oid=None) -> None:
super().__init__(node_type, oid=oid) # type: ignore
self._message: str = ""
def __init__(self, model_configuration, model, oid=None, node_type="hello_message") -> None:
super().__init__(node_type, model_configuration=model_configuration, model=model, oid=oid) # type: ignore
self._text: str = ""

@property
def message(self) -> str:
"""get the message
def text(self) -> str:
"""get the text
Returns:
str: the message
str: the text
"""
return self._message
return self._text

@message.setter
def message(self, message: str):
"""set the message
@text.setter
def text(self, text: str):
"""set the text
Args:
message (str): the message
text (str): the text
Raises:
TypeError: if message is not a string
TypeError: if text is not a string
"""
if not isinstance(message, str):
raise TypeError("'message' must be a string")
if not isinstance(text, str):
raise TypeError("'text' must be a string")

self._message = message
self._text = text

def __str__(self) -> str:
return f"HelloMessage(message={self.message})"
return f"HelloMessage(text={self.text})"
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self, sync_action_mapper, request: Request,

def initialize(self, request, response):
self.connection_controller.initialize(request, response)
self.hello_controller.initialize(request, response)
return super().initialize(request, response)

def execute(self, method=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from digitalpy.core.service_management.domain.service_status import ServiceStatus
from digitalpy.core.network.network_interface import NetworkInterface
from digitalpy.core.zmanager.request import Request
from digitalpy.core.zmanager.response import Response
from digitalpy.core.main.impl.default_factory import DefaultFactory
from digitalpy.core.telemetry.tracing_provider import TracingProvider

Expand Down Expand Up @@ -43,15 +44,24 @@ def handle_inbound_message(self, message: Request):
"""This function is used to handle inbound messages from other services.
It is intiated by the event loop.
"""
if message.get_value("data"):

# TODO: discuss this with giu and see if we should move the to the action mapping system?
if message.get_value("action") == "connection":
self.handle_connection(message.get_value("client"), message)

elif message.get_value("action") == "disconnection":
self.handle_disconnection(message.get_value("client"), message)

elif message.get_value("data"):
self.handle_simple_hello(message)

def handle_simple_hello(self, message: Request):
"""This function handles a simple hello message.
It is intiated by the inbound message handler.
"""
message.set_context("hello")
message.set_action("client_message")
message.set_context("Hello")
message.set_action("ClientMessage")
message.set_format("pickled")
self.subject_send_request(message, self.protocol)

def handle_exception(self, exception: Exception):
Expand Down

0 comments on commit a201fe7

Please sign in to comment.