Documentation Communication

class motey.communication.apiserver.APIServer(logger, host=‘127.0.0.1’, port=5023)[source]

Starts a Flask webserver which acts as an REST API to control the Motey service. The webserver runs in a separate thread and will not block the main thread.

check_heartbeat()[source]

Method to get the heartbeat. Checks if all the requirements for a healthy webserver are fulfilled. :return: True if the service is in a healthy state, otherwise False.

configure_url()[source]

Adds all the configured api endpoints.

is_running()[source]

Checks if the webserver is still running. :return: True if the server is running, otherwise False.

run_server()[source]

Starts the server and add an info to the logs, that the webserver is started.

start()[source]

Starts the execution thread.

stop()[source]

Stops the webserver and add an info the logs, that the webserver is stopped.

class motey.communication.communication_manager.CommunicationManager(api_server, mqtt_server, zeromq_server)[source]

This class acts as an facade for the communication endpoints like the MQTT server, the API server and the ZeroMQ server. It covers all method calls and can start and stop the mentioned components.

after_connect_callback()[source]

Will be called after the MQTTServer has established a connection to the broker. Send out a request to fetch the ip from all existing nodes.

deploy_image(image)[source]

Facades the ZeroMQServer.deploy_image() method. Will deploy an image to the node stored in the Image.node attribute.

Parameters:image (motey.models.image.Image) – Image to be deployed.
Returns:the id of the deployed image or None if something went wrong.
request_capabilities(ip)[source]

Facades the ZeroMQServer.request_capabilities() method. Will fetch the capabilities of a specific node and will return them.

Parameters:ip (str) – The ip of the node to be requested.
Returns:the capabilities of a specific node
request_image_status(image)[source]

Facades the ZeroMQServer.request_image_status() method. Request the status of an specific image instance or None if something went wrong.

Parameters:image (motey.models.image.Image) – Image to be used to get the status.
Returns:the status of the image or None if something went wrong
start()[source]

Start all the connected communication components.

stop()[source]

Stop all the connected communication components. Will send out a mqtt message to remove the current node.

terminate_image(image)[source]

Facades the ZeroMQServer.terminate_image() method. Will terminate an image instance.

Parameters:image (motey.models.image.Image) – the image instance to be terminated
class motey.communication.mqttserver.MQTTServer(logger, nodes_repository, host=‘127.0.0.1’, port=1883, username=None, password=None, keepalive=60)[source]

MQTT server to register and unregister adjacent fog nodes. The webserver runs in a separate thread and will not block the main thread.

handle_nodes_request(client, userdata, message)[source]

Define the node request callback implementation. Will execute the callback of the request to fetch the ip from all existing nodes.

Parameters:
  • client – the client instance for this callback
  • userdata – the private user data as set in Client() or userdata_set()
  • message – the data which was send
handle_on_connect(client, userdata, flags, resultcode)[source]

Define the connect callback implementation. If the client is connected to the MQTT broker, the nodes will be registered and the _after_connect method will be executed.

flags is a dict that contains response flags from the broker:
flags[‘session present’] - this flag is useful for clients that are
using clean session set to 0 only. If a client with clean session=0, that reconnects to a broker that it has previously connected to, this flag indicates whether the broker still has the session information for the client. If 1, the session still exists.
The value of rc indicates success or not:
0: Connection successful 1: Connection refused - incorrect protocol version 2: Connection refused - invalid client identifier 3: Connection refused - server unavailable 4: Connection refused - bad username or password 5: Connection refused - not authorised 6-255: Currently unused.
Parameters:
  • client – the client instance for this callback
  • userdata – the private user data as set in Client() or userdata_set()
  • flags – response flags sent by the broker
  • resultcode – the connection result
handle_on_disconnect(client, userdata, resultcode)[source]

Define the disconnect callback implementation.

Parameters:
  • client – the client instance for this callback
  • userdata – the private user data as set in Client() or userdata_set()
  • resultcode – the connection result
handle_register_node(client, userdata, message)[source]

Define the register new node callback implementation. Adds the new node to the NodesRepository.

Parameters:
  • client – the client instance for this callback
  • userdata – the private user data as set in Client() or userdata_set()
  • message – the data which was send
publish_new_node(ip=None)[source]

Publish the info that a new node is available to the all subscribers. If the ip is none, nothing will be send.

Parameters:ip – The IP address of the new node. Default is None.
publish_node_request(ip=None)[source]

Publish the request to fetch the ip from all existing nodes.

Parameters:ip – the own ip to let the other nodes know where the request cames from.
register_routes()[source]

Adds all the configured MQTT endpoints.

remove_node(ip=None)[source]

Remove a specific node and publish it to all subscribers. If the ip is none, nothing will be send.

Parameters:ip – The IP address of the new node. Default is None.
run_server()[source]

Starts the server and add an info to the logs, that the MQTT server is started. Also adds an error message to the logs if the broker is not available.

start()[source]

Starts the execution thread.

stop()[source]

Stops the MQTT server and add an info the logs, that the server is stopped.

class motey.communication.zeromq_server.ZeroMQServer(logger, valmanager, capability_repository)[source]

ZeroMQ server to communicate with adjacent fog nodes and to reply to requests. The different listeners will be executed in a separate thread and will not block the main thread.

deploy_image(image)[source]

Will deploy an image to the node stored in the Image.node attribute.

Parameters:image (motey.models.image.Image) – Image to be deployed.
Returns:the id of the deployed image or None if something went wrong.
request_capabilities(ip)[source]

Method to request all capabilities from another node. Will request via the ZeroMQ.REQ pattern. After the request is send, the method will wait for the response.

Parameters:ip – the IP address of the node to request the capabilities
Returns:the capabilities as a JSON object
request_image_status(image)[source]

Request the status of an specific ImageState instance or ImageState.ERROR if something went wrong.

Parameters:image (motey.models.image.Image) – Image to be used to get the status.
Returns:the ImageState or ImageState.ERROR if something went wrong
start()[source]

Starts the listening on a given port. This method will be executed on a separate thread.

stop()[source]

Should be executed to clean up the capability engine

terminate_image(image)[source]

Will terminate an image instance.

Parameters:image (motey.models.image.Image) – the image instance to be terminated