From 4297e6a374c5992fd354c71ef77ab28f0434f41e Mon Sep 17 00:00:00 2001 From: Cort Buffington Date: Wed, 20 Jul 2016 19:14:42 -0500 Subject: [PATCH] Added multi-master, moved logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seemed like multiple masters could be a thing, so I set it up to create an arbitrary number of them. Moved logging out of the main script file so there’s less junk to scroll through. --- hblink.cfg | 14 +++++- hblink.py | 131 ++++++++++++++++------------------------------------- log.py | 60 ++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 93 deletions(-) create mode 100644 log.py diff --git a/hblink.cfg b/hblink.cfg index 53bdaae..0e07cd2 100644 --- a/hblink.cfg +++ b/hblink.cfg @@ -7,13 +7,22 @@ LOG_HANDLERS: console-timed LOG_LEVEL: INFO LOG_NAME: HBlink -[MASTER] +[MASTER-1] +MODE: MASTER ENABLED: True IP: PORT: 54000 -PASSPHRASE: +PASSPHRASE: s3cr37w0rd + +[MASTER-2] +MODE: MASTER +ENABLED: True +IP: +PORT: 55000 +PASSPHRASE: 13370p3r470r [REPEATER-1] +MODE: CLIENT ENABLED: True IP: PORT: 54001 @@ -36,6 +45,7 @@ SOFTWARE_ID: HBlink v1.0 PACKAGE_ID: HBlink v1.0 [REPEATER-2] +MODE: CLIENT ENABLED: True IP: PORT: 54002 diff --git a/hblink.py b/hblink.py index f5ead7e..66cf9d4 100755 --- a/hblink.py +++ b/hblink.py @@ -12,9 +12,8 @@ import ConfigParser import argparse import sys import os -import logging +import log -from logging.config import dictConfig from binascii import b2a_hex as h from socket import gethostbyname @@ -42,6 +41,7 @@ cli_args = parser.parse_args() #************************************************ CLIENTS = {} +MASTERS = {} config = ConfigParser.ConfigParser() if not cli_args.CFG_FILE: @@ -66,40 +66,41 @@ try: 'LOG_LEVEL': config.get(section, 'LOG_LEVEL'), 'LOG_NAME': config.get(section, 'LOG_NAME') } - - elif section == 'MASTER': - # HomeBrew Master Configuration - MASTER = { - 'ENABLED': config.getboolean(section, 'ENABLED'), - 'IP': gethostbyname(config.get(section, 'IP')), - 'PORT': config.getint(section, 'PORT'), - 'PASSPHRASE': config.get(section, 'PASSPHRASE') - } elif config.getboolean(section, 'ENABLED'): # HomeBrew Client (Repeater) Configuration(s) - CLIENTS.update({section: { - 'ENABLED': config.getboolean(section, 'ENABLED'), - 'IP': gethostbyname(config.get(section, 'IP')), - 'PORT': config.getint(section, 'PORT'), - 'MASTER_IP': gethostbyname(config.get(section, 'MASTER_IP')), - 'MASTER_PORT': config.getint(section, 'MASTER_PORT'), - 'PASSPHRASE': config.get(section, 'PASSPHRASE'), - 'CALLSIGN': config.get(section, 'CALLSIGN'), - 'RADIO_ID': hex(int(config.get(section, 'RADIO_ID')))[2:].rjust(8,'0').decode('hex'), - 'RX_FREQ': config.get(section, 'RX_FREQ'), - 'TX_FREQ': config.get(section, 'TX_FREQ'), - 'TX_POWER': config.get(section, 'TX_POWER'), - 'COLORCODE': config.get(section, 'COLORCODE'), - 'LATITUDE': config.get(section, 'LATITUDE'), - 'LONGITUDE': config.get(section, 'LONGITUDE'), - 'HEIGHT': config.get(section, 'HEIGHT'), - 'LOCATION': config.get(section, 'LOCATION'), - 'DESCRIPTION': config.get(section, 'DESCRIPTION'), - 'URL': config.get(section, 'URL'), - 'SOFTWARE_ID': config.get(section, 'SOFTWARE_ID'), - 'PACKAGE_ID': config.get(section, 'PACKAGE_ID') - }}) + if config.get(section, 'MODE') == 'CLIENT': + CLIENTS.update({section: { + 'ENABLED': config.getboolean(section, 'ENABLED'), + 'IP': gethostbyname(config.get(section, 'IP')), + 'PORT': config.getint(section, 'PORT'), + 'MASTER_IP': gethostbyname(config.get(section, 'MASTER_IP')), + 'MASTER_PORT': config.getint(section, 'MASTER_PORT'), + 'PASSPHRASE': config.get(section, 'PASSPHRASE'), + 'CALLSIGN': config.get(section, 'CALLSIGN'), + 'RADIO_ID': hex(int(config.get(section, 'RADIO_ID')))[2:].rjust(8,'0').decode('hex'), + 'RX_FREQ': config.get(section, 'RX_FREQ'), + 'TX_FREQ': config.get(section, 'TX_FREQ'), + 'TX_POWER': config.get(section, 'TX_POWER'), + 'COLORCODE': config.get(section, 'COLORCODE'), + 'LATITUDE': config.get(section, 'LATITUDE'), + 'LONGITUDE': config.get(section, 'LONGITUDE'), + 'HEIGHT': config.get(section, 'HEIGHT'), + 'LOCATION': config.get(section, 'LOCATION'), + 'DESCRIPTION': config.get(section, 'DESCRIPTION'), + 'URL': config.get(section, 'URL'), + 'SOFTWARE_ID': config.get(section, 'SOFTWARE_ID'), + 'PACKAGE_ID': config.get(section, 'PACKAGE_ID') + }}) + + elif config.get(section, 'MODE') == 'MASTER': + # HomeBrew Master Configuration + MASTERS.update({section: { + 'ENABLED': config.getboolean(section, 'ENABLED'), + 'IP': gethostbyname(config.get(section, 'IP')), + 'PORT': config.getint(section, 'PORT'), + 'PASSPHRASE': config.get(section, 'PASSPHRASE') + }}) except: sys.exit('Could not parse configuration file, exiting...') @@ -109,61 +110,7 @@ except: # CONFIGURE THE SYSTEM LOGGER #************************************************ -dictConfig({ - 'version': 1, - 'disable_existing_loggers': False, - 'filters': { - }, - 'formatters': { - 'verbose': { - 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' - }, - 'timed': { - 'format': '%(levelname)s %(asctime)s %(message)s' - }, - 'simple': { - 'format': '%(levelname)s %(message)s' - }, - 'syslog': { - 'format': '%(name)s (%(process)d): %(levelname)s %(message)s' - } - }, - 'handlers': { - 'null': { - 'class': 'logging.NullHandler' - }, - 'console': { - 'class': 'logging.StreamHandler', - 'formatter': 'simple' - }, - 'console-timed': { - 'class': 'logging.StreamHandler', - 'formatter': 'timed' - }, - 'file': { - 'class': 'logging.FileHandler', - 'formatter': 'simple', - 'filename': LOGGER['LOG_FILE'], - }, - 'file-timed': { - 'class': 'logging.FileHandler', - 'formatter': 'timed', - 'filename': LOGGER['LOG_FILE'], - }, - 'syslog': { - 'class': 'logging.handlers.SysLogHandler', - 'formatter': 'syslog', - } - }, - 'loggers': { - LOGGER['LOG_NAME']: { - 'handlers': LOGGER['LOG_HANDLERS'].split(','), - 'level': LOGGER['LOG_LEVEL'], - 'propagate': True, - } - } -}) -logger = logging.getLogger(LOGGER['LOG_NAME']) +logger = log.config_logging(LOGGER) #************************************************ # HERE ARE THE IMPORTANT PARTS @@ -186,9 +133,11 @@ if __name__ == '__main__': logger.info('HBlink \'HBlink.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') # HBlink Master - if MASTER: - hbmaster = HBMASTER() - reactor.listenUDP(MASTER['PORT'], hbmaster, interface=MASTER['IP']) + masters = {} + for master in MASTERS: + if MASTERS[master]['ENABLED']: + masters[master] = HBMASTER(master) + reactor.listenUDP(MASTERS[master]['PORT'], masters[master], interface=MASTERS[master]['IP']) clients = {} for client in CLIENTS: diff --git a/log.py b/log.py new file mode 100644 index 0000000..9bd2c6b --- /dev/null +++ b/log.py @@ -0,0 +1,60 @@ +import logging +from logging.config import dictConfig + +def config_logging(_logger): + dictConfig({ + 'version': 1, + 'disable_existing_loggers': False, + 'filters': { + }, + 'formatters': { + 'verbose': { + 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' + }, + 'timed': { + 'format': '%(levelname)s %(asctime)s %(message)s' + }, + 'simple': { + 'format': '%(levelname)s %(message)s' + }, + 'syslog': { + 'format': '%(name)s (%(process)d): %(levelname)s %(message)s' + } + }, + 'handlers': { + 'null': { + 'class': 'logging.NullHandler' + }, + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'simple' + }, + 'console-timed': { + 'class': 'logging.StreamHandler', + 'formatter': 'timed' + }, + 'file': { + 'class': 'logging.FileHandler', + 'formatter': 'simple', + 'filename': _logger['LOG_FILE'], + }, + 'file-timed': { + 'class': 'logging.FileHandler', + 'formatter': 'timed', + 'filename': _logger['LOG_FILE'], + }, + 'syslog': { + 'class': 'logging.handlers.SysLogHandler', + 'formatter': 'syslog', + } + }, + 'loggers': { + _logger['LOG_NAME']: { + 'handlers': _logger['LOG_HANDLERS'].split(','), + 'level': _logger['LOG_LEVEL'], + 'propagate': True, + } + } + }) + + return logging.getLogger(_logger['LOG_NAME']) \ No newline at end of file