This plugin example demonstrates how you can create a service on the server side. This example consists of one python script /demoServiceServer.py and manifest file /manifest.json.
from DAP.Core import logIt
from CellFrame.Network import Net, Service, ServiceUID
def requested(srv, usage_id, client_remote, data):
logIt.info("[server] func requested")
return 0
def response_success(srv, usage_id, client_remote, data):
logIt.notice("[server] func response success")
return 0
def response_error(srv, usage_id, client_remote, data):
logIt.warning("[server] func response error")
def next_success(srv, usage_id, client_remote, data):
logIt.notice("[server] func next success")
return 0
def custom_data(srv, usage_id, client_remote, data):
logIt.notice("[server] Input data: " + data.decode("utf-8"))
return data
def init():
logIt.notice("Init demoServer")
ch_uid = ServiceUID(123)
srv_object = Service(
ch_uid,
"py_service",
requested,
response_success,
response_error,
next_success,
custom_data
)
return 0
{
"name": "demoServiceServer",
"version": "1.0",
"author": "DEMLABS (C) 2022",
"dependencies": [],
"description": "This plugin is an example of a service server plugin."
}
This /demoServiceServer.py script contains following functions: requested
, response_success
, response_error
, next_success
, custom_data
and init
.
The execution of the plugin starts with the init
. Other functions are handlers.
1. The plugin is initialized by the init
function:
def init():
2. The logIt.notice
function logs a message:
logIt.notice("Init demoServer")
3. Creating an instance of a ServiceUID
object using a constractor:
ch_uid = ServiceUID(123)
The 123 is a service UID (arbitrary).
4. Creating an instance of a Service
object using a constractor:
srv_object = Service(
ch_uid,
"py_service",
requested,
response_success,
response_error,
next_success,
custom_data
)
The "py_service"
is a section of a configuration file. Other arguments are handler function names. These functions must have the following signature: f(srv: CellFrame.Network.Service, usageID: int, clientRemote: CellFrame.Network.ServiceClientRemote, data: bytes) -> int
.
5. Returning 0. This means that the plugin has been successfully initialized:
return 0