112 lines
4.6 KiB
Python
112 lines
4.6 KiB
Python
|
#!/usr/bin/env python
|
||
|
#
|
||
|
# This work is licensed under the Creative Attribution-NonCommercial-ShareAlike
|
||
|
# 3.0 Unported License.To view a copy of this license, visit
|
||
|
# http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
|
||
|
# Creative Commons, 444 Castro Street, Suite 900, Mountain View,
|
||
|
# California, 94041, USA.
|
||
|
|
||
|
from __future__ import print_function
|
||
|
|
||
|
# Python modules we need
|
||
|
import sys
|
||
|
|
||
|
# Debugging functions
|
||
|
from pprint import pprint
|
||
|
|
||
|
# Twisted is pretty important, so I keep it separate
|
||
|
from twisted.internet.protocol import DatagramProtocol
|
||
|
from twisted.internet import reactor
|
||
|
from twisted.internet import task
|
||
|
|
||
|
# Things we import from the main hblink module
|
||
|
from hblink import CONFIG, HBMASTER, HBCLIENT, logger, masters, clients, hex_str_3
|
||
|
|
||
|
# Import Bridging rules
|
||
|
# Note: A stanza *must* exist for any MASTER or CLIENT configured in the main
|
||
|
# configuration file and listed as "active". It can be empty,
|
||
|
# but it has to exist.
|
||
|
try:
|
||
|
from hb_routing_rules import RULES as RULES_FILE
|
||
|
logger.info('Routing rules file found and rules imported')
|
||
|
except ImportError:
|
||
|
sys.exit('Routing rules file not found or invalid')
|
||
|
|
||
|
# Convert integer GROUP ID numbers from the config into hex strings
|
||
|
# we need to send in the actual data packets.
|
||
|
for _system in RULES_FILE['MASTERS']:
|
||
|
for _rule in RULES_FILE['MASTERS'][_system]['GROUP_VOICE']:
|
||
|
_rule['SRC_GROUP'] = hex_str_3(_rule['SRC_GROUP'])
|
||
|
_rule['DST_GROUP'] = hex_str_3(_rule['DST_GROUP'])
|
||
|
_rule['SRC_TS'] = _rule['SRC_TS']
|
||
|
_rule['DST_TS'] = _rule['DST_TS']
|
||
|
for i, e in enumerate(_rule['ON']):
|
||
|
_rule['ON'][i] = hex_str_3(_rule['ON'][i])
|
||
|
for i, e in enumerate(_rule['OFF']):
|
||
|
_rule['OFF'][i] = hex_str_3(_rule['OFF'][i])
|
||
|
if _system not in CONFIG['MASTERS']:
|
||
|
sys.exit('ERROR: Routing rules found for MASTER system not configured in main configuration')
|
||
|
for _system in CONFIG['MASTERS']:
|
||
|
if _system not in RULES_FILE['MASTERS']:
|
||
|
print(_system)
|
||
|
sys.exit('ERROR: Routing rules not found for all MASTER systems configured')
|
||
|
|
||
|
for _system in RULES_FILE['CLIENTS']:
|
||
|
for _rule in RULES_FILE['CLIENTS'][_system]['GROUP_VOICE']:
|
||
|
_rule['SRC_GROUP'] = hex_str_3(_rule['SRC_GROUP'])
|
||
|
_rule['DST_GROUP'] = hex_str_3(_rule['DST_GROUP'])
|
||
|
_rule['SRC_TS'] = _rule['SRC_TS']
|
||
|
_rule['DST_TS'] = _rule['DST_TS']
|
||
|
for i, e in enumerate(_rule['ON']):
|
||
|
_rule['ON'][i] = hex_str_3(_rule['ON'][i])
|
||
|
for i, e in enumerate(_rule['OFF']):
|
||
|
_rule['OFF'][i] = hex_str_3(_rule['OFF'][i])
|
||
|
if _system not in CONFIG['CLIENTS']:
|
||
|
print(_system)
|
||
|
sys.exit('ERROR: Routing rules found for CLIENT system not configured in main configuration')
|
||
|
for _system in CONFIG['CLIENTS']:
|
||
|
if _system not in RULES_FILE['CLIENTS']:
|
||
|
sys.exit('ERROR: Routing rules not found for all CLIENT systems configured')
|
||
|
|
||
|
RULES = RULES_FILE
|
||
|
|
||
|
# TEMPORARY DEBUGGING LINE -- TO BE REMOVED LATER
|
||
|
pprint(RULES)
|
||
|
|
||
|
# Does anybody read this stuff? There's a PEP somewhere that says I should do this.
|
||
|
__author__ = 'Cortney T. Buffington, N0MJS'
|
||
|
__copyright__ = 'Copyright (c) 2016 Cortney T. Buffington, N0MJS and the K0USY Group'
|
||
|
__credits__ = 'Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman, N4IRR; Jonathan Naylor, G4KLX; Hans Barthen, DL5DI; Torsten Shultze, DG1HT'
|
||
|
__license__ = 'Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported'
|
||
|
__maintainer__ = 'Cort Buffington, N0MJS'
|
||
|
__email__ = 'n0mjs@me.com'
|
||
|
__status__ = 'pre-alpha'
|
||
|
|
||
|
|
||
|
class routerMASTER(HBMASTER):
|
||
|
pass
|
||
|
|
||
|
class routerCLIENT(HBCLIENT):
|
||
|
pass
|
||
|
|
||
|
#************************************************
|
||
|
# MAIN PROGRAM LOOP STARTS HERE
|
||
|
#************************************************
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
logger.info('HBlink \'hb_router.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...')
|
||
|
|
||
|
# HBlink Master
|
||
|
for master in CONFIG['MASTERS']:
|
||
|
if CONFIG['MASTERS'][master]['ENABLED']:
|
||
|
masters[master] = HBMASTER(master)
|
||
|
reactor.listenUDP(CONFIG['MASTERS'][master]['PORT'], masters[master], interface=CONFIG['MASTERS'][master]['IP'])
|
||
|
logger.debug('MASTER instance created: %s, %s', master, masters[master])
|
||
|
|
||
|
for client in CONFIG['CLIENTS']:
|
||
|
if CONFIG['CLIENTS'][client]['ENABLED']:
|
||
|
clients[client] = HBCLIENT(client)
|
||
|
reactor.listenUDP(CONFIG['CLIENTS'][client]['PORT'], clients[client], interface=CONFIG['CLIENTS'][client]['IP'])
|
||
|
logger.debug('CLIENT instance created: %s, %s', client, clients[client])
|
||
|
|
||
|
reactor.run()
|