-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
134 lines (107 loc) · 5.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -*- coding: UTF-8 -*-
OK = 0
ERROR = 1
## Return :: (HookSpec, State)
##
## HookSpec :: [(HookName, CallbackModule, CallbackFunction, Opts)]
## State :: Any
##
## HookName :: "client_connect" | "client_connack" | "client_connected" | ...
## CallbackModule :: ...
## CallbackFunctiin :: ...
## Opts :: [(Key, Value)]
def init():
## Maybe a connection object?
state = ()
hookspec = [("client_connect", "main", "on_client_connect", []),
("client_connack", "main", "on_client_connack", []),
("client_connected", "main", "on_client_connected", []),
("client_disconnected", "main", "on_client_disconnected", []),
("client_authenticate", "main", "on_client_authenticate", []),
("client_check_acl", "main", "on_client_check_acl", []),
("client_subscribe", "main", "on_client_subscribe", []),
("client_unsubscribe", "main", "on_client_unsubscribe", []),
("session_created", "main", "on_session_created", []),
("session_subscribed", "main", "on_session_subscribed", []),
("session_unsubscribed","main", "on_session_unsubscribed", []),
("session_resumed", "main", "on_session_resumed", []),
("session_discarded", "main", "on_session_discarded", []),
("session_takeovered", "main", "on_session_takeovered", []),
("session_terminated", "main", "on_session_terminated", []),
("message_publish", "main", "on_message_publish", [("topics", ["t/#"])]),
("message_delivered", "main", "on_message_delivered", [("topics", ["t/#"])]),
("message_acked", "main", "on_message_acked", [("topics", ["t/#"])]),
("message_dropped", "main", "on_message_dropped", [("topics", ["t/#"])])
]
return (OK, (hookspec, state))
def deinit():
return
##--------------------------------------------------------------------
## Callback functions
##--------------------------------------------------------------------
##--------------------------------------------------------------------
## Clients
def on_client_connect(conninfo, props, state):
print("on_client_connect: conninfo: {0}, props: {1}, state: {2}".format(conninfo, props, state))
return
def on_client_connack(conninfo, rc, props, state):
print("on_client_connack: conninfo: {0}, rc{1}, props: {2}, state: {3}".format(conninfo, rc, props, state))
return
def on_client_connected(clientinfo, state):
print("on_client_connected: clientinfo: {0}, state: {1}".format(clientinfo, state))
return
def on_client_disconnected(clientinfo, reason, state):
print("on_client_disconnected: clientinfo: {0}, reason: {1}, state: {2}".format(clientinfo, reason, state))
return
def on_client_authenticate(clientinfo, authresult, state):
print("on_client_authenticate: clientinfo: {0}, authresult: {1}, state: {2}".format(clientinfo, authresult, state))
## True / False
return (OK, True)
def on_client_check_acl(clientinfo, pubsub, topic, result, state):
print("on_client_check_acl: clientinfo: {0}, pubsub: {1}, topic: {2}, result: {3}, state: {4}".format(clientinfo, pubsub, topic, result, state))
## True / False
return (OK, True)
def on_client_subscribe(clientinfo, props, topics, state):
print("on_client_subscribe: clientinfo: {0}, props: {1}, topics: {2}, state: {3}".format(clientinfo, props, topics, state))
return
def on_client_unsubscribe(clientinfo, props, topics, state):
print("on_client_unsubscribe: clientinfo: {0}, props: {1}, topics: {2}, state: {3}".format(clientinfo, props, topics, state))
return
##--------------------------------------------------------------------
## Sessions
def on_session_created(clientinfo, state):
print("on_session_created: clientinfo: {0}, state: {1}".format(clientinfo, state))
return
def on_session_subscribed(clientinfo, topic, opts, state):
print("on_session_subscribed: clientinfo: {0}, topic: {1}, opts: {2}, state: {3}".format(clientinfo, topic, opts, state))
return
def on_session_unsubscribed(clientinfo, topic, state):
print("on_session_unsubscribed: clientinfo: {0}, topic: {1}, state: {2}".format(clientinfo, topic, state))
return
def on_session_resumed(clientinfo, state):
print("on_session_resumed: clientinfo: {0}, state: {1}".format(clientinfo, state))
return
def on_session_discarded(clientinfo, state):
print("on_session_discared: clientinfo: {0}, state: {1}".format(clientinfo, state))
return
def on_session_takeovered(clientinfo, state):
print("on_session_takeovered: clientinfo: {0}, state: {1}".format(clientinfo, state))
return
def on_session_terminated(clientinfo, reason, state):
print("on_session_terminated: clientinfo: {0}, reason: {1}, state: {2}".format(clientinfo, reason, state))
return
##--------------------------------------------------------------------
## Messages
def on_message_publish(message, state):
print("on_message_publish: message: {0}, state: {1}".format(message, state))
return message
def on_message_dropped(message, reason, state):
print("on_message_dropped: message: {0}, reason: {1}, state: {2}".format(message, reason, state))
return
def on_message_delivered(clientinfo, message, state):
print("on_message_delivered: clientinfo: {0}, message: {1}, state: {2}".format(clientinfo, message, state))
return
def on_message_acked(clientinfo, message, state):
print("on_message_acked: clientinfo: {0}, message: {1}, state: {2}".format(clientinfo, message, state))
return