This plugin example demonstrates how you can create a service on the client side.. This example consists of one python script /demoServiceClient.py and manifest file /manifest.json.
from DAP.Core import logIt
from DAP.Crypto import Cert, HashFast
from CellFrame.Common import TxReceipt
from CellFrame.Network import Net, ServiceUID, ServiceClient
def callback_connected(serviceClient, arg):
logIt.notice("Python client connected")
ch_uid = ServiceUID(123)
net = Net.byName("private")
condHash = HashFast.fromString("0xAF14CB70DB383A4FB840F8BD531A7B58C300B65AD3B3F418DC0713B0F6648643");
serviceClient.request(net, ch_uid, condHash)
def callback_disconnected(serviceClient, arg):
logIt.notice("Python client disconnected")
def callback_deleted(serviceClient, arg):
logIt.notice("Python client deleted")
def callback_check(serviceClient, arg):
logIt.notice("Python client successfully checked the service")
def callback_sign(serviceClient, txCondRec, arg):
logIt.notice("Siging receipt by python client")
signCert = Cert.load("svc_client")
return txCondRec.sign(signCert)
def callback_success(serviceClient, txCondHash, arg):
logIt.notice("Python client successfully requested the service")
def callback_error(serviceClient, errorNum, arg):
logIt.warning(f"Python client got error {errorNum:#x}")
def callback_data(serviceClient, data, arg):
logIt.notice(f"Python client custom data read back \'{data.decode('utf-8')}\'")
def init():
logIt.notice("Init demoClient")
net = Net.byName("private")
client = ServiceClient(net, "127.0.0.1", 8089, callback_connected,
callback_disconnected,
callback_deleted,
callback_check,
callback_sign,
callback_success,
callback_error,
callback_data,
0)
return 0
{
"name": "demoServiceClient",
"version": "1.0",
"author": "DEMLABS (C) 2022",
"dependencies": [],
"description": "This plugin have demo service client."
}
This /demoServiceClient.py script contains following functions: callback_connected
, callback_disconnected
, callback_deleted
, callback_check
, callback_sign
, callback_success
, callback_error
, callback_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. Getting a net by name using the byName
method of the Net
class:
net = Net.byName("private")
The "private"
is a net name. The net
is a instance of Net
.
3. Creating an instance of a ServiceClient
object using a constructor:
client = ServiceClient(net, "127.0.0.1", 8089, callback_connected,
callback_disconnected,
callback_deleted,
callback_check,
callback_sign,
callback_success,
callback_error,
callback_data,
0)
The "127.0.0.1"
and the 8089
are a IP address and port of the server. Other arguments are handler function names.
1. This function must have following signature:
def callback_connected(serviceClient, arg):
An ServiceClient
object is passed as the first argument. The arg
is some object.
2. Logging a message:
logIt.notice("Python client connected")
3. Creating an instance of the ServiceUID
object using a constructor:
ch_uid = ServiceUID(123)
4. Getting a net by name using the byName
method of the Net
class:
net = Net.byName("private")
The "private"
is a net name. The net
is a instance of Net
object.
5. Getting a hash using the fromString
method of the HashFast
class:
condHash = HashFast.fromString("0xAF14CB70DB383A4FB840F8BD531A7B58C300B65AD3B3F418DC0713B0F6648643");
6. Sending a request using the request
of the ServiceClient
object:
serviceClient.request(net, ch_uid, condHash)
1. This function must have following signature:
def callback_disconnected(serviceClient, arg):
2. Logging a message:
logIt.notice("Python client disconnected")
1. This function must have following signature:
def callback_deleted(serviceClient, arg):
2. Logging a message:
logIt.notice("Python client deleted")
1. This function must have following signature:
def callback_check(serviceClient, arg):
2. Logging a message:
logIt.notice("Python client successfully checked the service")
1. This function must have following signature:
def callback_sign(serviceClient, txCondRec, arg):
Parametr txCondRec
accept an object of type TxReceipt
.
2. Logging a message:
logIt.notice("Siging receipt by python client")
3. Loading a certificate using the load
method of the Cert
class:
signCert = Cert.load("svc_client")
4. Adding a sign to the transaction using the sign
method of the TxReceipt
object:
return txCondRec.sign(signCert)
1. This function must have following signature:
def callback_success(serviceClient, txCondHash, arg):
2. Logging a message:
logIt.notice("Python client successfully requested the service")
1. This function must have following signature:
def callback_error(serviceClient, errorNum, arg):
2. Logging a message:
logIt.warning(f"Python client got error {errorNum:#x}")
1. This function must have following signature:
def callback_data(serviceClient, data, arg):
2. Logging a message:
logIt.notice(f"Python client custom data read back \'{data.decode('utf-8')}\'")