diff --git a/requirements.txt b/requirements.txt index d65785c..2ed1eda 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ transitions==0.2.5 -blinker websockets -passlib \ No newline at end of file +passlib +docopt +pyyaml \ No newline at end of file diff --git a/scripts/default_broker.yaml b/scripts/default_broker.yaml new file mode 100644 index 0000000..bc0e532 --- /dev/null +++ b/scripts/default_broker.yaml @@ -0,0 +1,10 @@ +listeners: + default: + type: tcp + bind: 0.0.0.0:1883 +sys_interval: 20 +auth: + allow-anonymous: true +plugins: + - auth_file + - auth_anonymous \ No newline at end of file diff --git a/scripts/hbmqtt.py b/scripts/hbmqtt.py index e69de29..1f2ec0b 100644 --- a/scripts/hbmqtt.py +++ b/scripts/hbmqtt.py @@ -0,0 +1,81 @@ +# 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 ] [-d] + +Options: + -h --help Show this screen. + --version Show version. + -c Broker configuration file (YAML format) + -d Enable debug messages +""" + +import sys +import logging +import asyncio +import os +from hbmqtt.broker import Broker +from hbmqtt.version import get_version +from docopt import docopt +from .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' + ] + } +} + +logger = logging.getLogger(__name__) + + +def main(*args, **kwargs): + if sys.version_info[:2] < (3, 5): + logger.fatal("Error: Python 3.5 is required") + sys.exit(-1) + + arguments = docopt(__doc__, version=get_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.warning("Using default configuration") + loop = asyncio.get_event_loop() + broker = Broker(config) + try: + loop.run_until_complete(broker.start()) + loop.run_forever() + except KeyboardInterrupt: + asyncio.get_event_loop().run_until_complete(broker.shutdown()) + finally: + loop.close() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/utils.py b/scripts/utils.py new file mode 100644 index 0000000..70fa14e --- /dev/null +++ b/scripts/utils.py @@ -0,0 +1,17 @@ +# Copyright (c) 2015 Nicolas JOUANIN +# +# See the file license.txt for copying permission. +import yaml +import logging + +logger = logging.getLogger(__name__) + + +def read_yaml_config(config_file): + config = None + try: + with open(config_file, 'r') as stream: + config = yaml.load(stream) + except yaml.YAMLError as exc: + logger.error("Invalid config_file %s: %s" % (config_file, exc)) + return config diff --git a/setup.py b/setup.py index 00895cf..88891a0 100644 --- a/setup.py +++ b/setup.py @@ -17,9 +17,10 @@ setup( platforms='all', install_requires=[ 'transitions==0.2.5', - 'blinker', 'websockets', - 'passlib' + 'passlib', + 'docopt', + 'pyyaml' ], classifiers=[ 'Development Status :: 3 - Alpha', @@ -47,6 +48,11 @@ setup( ], 'hbmqtt.client.plugins': [ 'packet_logger_plugin = hbmqtt.plugins.logging:PacketLoggerPlugin', + ], + 'console_scripts': [ + 'hbmqtt = scripts.hbmqtt:main', + 'hbmqtt_pub = scripts.hbmqtt_pub:main', + 'hbmqtt_sub = scripts.hbmqtt_sub:main', ] } ) \ No newline at end of file