/
broker_script.py
85 lines (71 loc) · 2 KB
/
broker_script.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
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.
"""
HBMQTT - MQTT 3.1.1 broker
Usage:
hbmqtt --version
hbmqtt (-h | --help)
hbmqtt [-c <config_file> ] [-d]
Options:
-h --help Show this screen.
--version Show version.
-c <config_file> Broker configuration file (YAML format)
-d Enable debug messages
"""
import sys
import logging
import asyncio
import os
import hbmqtt
from hbmqtt.broker import Broker
from docopt import docopt
from hbmqtt.utils import read_yaml_config
default_config = {
'listeners': {
'default': {
'type': 'tcp',
'bind': '0.0.0.0:1883',
},
},
'sys_interval': 10,
'auth': {
'allow-anonymous': True,
'password-file': os.path.join(os.path.dirname(os.path.realpath(__file__)), "passwd"),
'plugins': [
'auth_file', 'auth_anonymous'
]
},
'topic-check': {
'enabled': False
}
}
logger = logging.getLogger(__name__)
def main(*args, **kwargs):
if sys.version_info[:2] < (3, 4):
logger.fatal("Error: Python 3.4+ is required")
sys.exit(-1)
arguments = docopt(__doc__, version=hbmqtt.__version__)
formatter = "[%(asctime)s] :: %(levelname)s - %(message)s"
if arguments['-d']:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(level=level, format=formatter)
config = None
if arguments['-c']:
config = read_yaml_config(arguments['-c'])
else:
config = read_yaml_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default_broker.yaml'))
logger.debug("Using default configuration")
loop = asyncio.get_event_loop()
broker = Broker(config)
try:
loop.run_until_complete(broker.start())
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(broker.shutdown())
finally:
loop.close()
if __name__ == "__main__":
main()