The notification example demonstrates how you can create a notification handler. This example consists of one python script /exampleCreateTx.py and manifest file /manifest.json.
from DAP.Core import logIt
from CellFrame.Network import Net
def notify_net_db(op_code, group, key, value, arg):
logIt.notice("Notificator")
logIt.notice(" op_code: "+op_code)
logIt.notice(" group: "+group)
logIt.notice(" key:"+key)
logIt.notice(" value:"+str(value))
logIt.notice(" Arg: "+str(arg))
def init():
logIt.notice("Start plugin notifyDB")
net = Net.byName("kelvin-testnet")
net.addNotify(notify_net_db, "This is notificator work with net db in network "+str(net))
return 0
{
"name": "notifyDB",
"version": "1.0",
"author": "DEMLABS (C) 2021",
"dependencies": [],
"description": "This plugin demonstrates a work with notification."
}
This /exampleCreateTx.py script contains two function: init
and notify_net_db
.
The execution of the plugin starts with the init
. The notify_net_db
function is passed to the addNotify
method as an argument. The notify_net_db
function will be called when records are added or deleted in GDB.
1. The plugin is initialized by the init
function:
def init():
2. Getting a net by name using a byName
static method of the Net
class:
net = Net.byName("kelvin-testnet")
The "kelvin-testnet"
is a net name. The net
is a instance of the Net
object.
3. Adding a handler for the net notifier using the addNotify
method:
net.addNotify(notify_net_db, "This is notificator work with net db in network "+str(net))
The notify_net_db
is a function that will handle GDB change notifications. The second argument can be object of any type, in this example str
4. Returning 0. This means that the plugin has been successfully initialized:
return 0
1. The function must take four arguments: an operation code, a group, a key and a value:
def notify_net_db(op_code, group, key, value, arg):
The op_code
, group
, key
are strings and value
is bytes.
2. We logging the information received from the notifier:
logIt.notice("Notificator")
logIt.notice(" op_code: "+op_code)
logIt.notice(" group: "+group)
logIt.notice(" key:"+key)
logIt.notice(" value:"+str(value))
logIt.notice(" Arg: "+str(arg))