Convert to Python logging module for greater flexibility in where / how things are displayed.
This commit is contained in:
parent
fec136d20e
commit
1253c7f405
146
ipsc.py
146
ipsc.py
|
@ -6,6 +6,59 @@ import argparse
|
||||||
import binascii
|
import binascii
|
||||||
import hmac
|
import hmac
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import logging
|
||||||
|
from logging.config import dictConfig
|
||||||
|
|
||||||
|
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'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'handlers': {
|
||||||
|
'console': {
|
||||||
|
'level': 'DEBUG',
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'simple'
|
||||||
|
},
|
||||||
|
'file': {
|
||||||
|
'level': 'DEBUG',
|
||||||
|
'class': 'logging.FileHandler',
|
||||||
|
'formatter': 'simple',
|
||||||
|
'filename': '/tmp/ipsc.log',
|
||||||
|
},
|
||||||
|
'console-timed': {
|
||||||
|
'level': 'DEBUG',
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'timed'
|
||||||
|
},
|
||||||
|
'file-timed': {
|
||||||
|
'level': 'DEBUG',
|
||||||
|
'class': 'logging.FileHandler',
|
||||||
|
'formatter': 'timed',
|
||||||
|
'filename': '/tmp/ipsc.log',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'loggers': {
|
||||||
|
'ipsc': {
|
||||||
|
# 'handlers': ['file-timed', 'console-timed'],
|
||||||
|
'handlers': ['file', 'console'],
|
||||||
|
'level': 'DEBUG',
|
||||||
|
'propagate': True,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
logger = logging.getLogger('ipsc')
|
||||||
|
|
||||||
|
|
||||||
# Data structure for holding IPSC information
|
# Data structure for holding IPSC information
|
||||||
|
@ -115,9 +168,9 @@ IPSC_VER = IPSC_OP_VER + IPSC_OLD_VER
|
||||||
def hashed_packet(key, data):
|
def hashed_packet(key, data):
|
||||||
hash = binascii.unhexlify((hmac.new(key,data,hashlib.sha1)).hexdigest()[:20])
|
hash = binascii.unhexlify((hmac.new(key,data,hashlib.sha1)).hexdigest()[:20])
|
||||||
return (data + hash)
|
return (data + hash)
|
||||||
|
|
||||||
def print_peer_list(_ipsc_network):
|
def print_peer_list(_ipsc_network):
|
||||||
print('\t', _ipsc_network['LOCAL']['DESCRIPTION'])
|
logger.info('\t%s', _ipsc_network['LOCAL']['DESCRIPTION'])
|
||||||
for dictionary in _ipsc_network['PEERS']:
|
for dictionary in _ipsc_network['PEERS']:
|
||||||
hex_address = dictionary['IP']
|
hex_address = dictionary['IP']
|
||||||
hex_port = dictionary['PORT']
|
hex_port = dictionary['PORT']
|
||||||
|
@ -128,10 +181,10 @@ def print_peer_list(_ipsc_network):
|
||||||
port = int(hex_port, 16)
|
port = int(hex_port, 16)
|
||||||
radio_id = int(hex_radio_id, 16)
|
radio_id = int(hex_radio_id, 16)
|
||||||
|
|
||||||
print ('\t', address[0],address[1],address[2],address[3], sep='.', end='\t')
|
logger.info('\t%s.%s.%s.%s\t', address[0], address[1], address[2], address[3])
|
||||||
print (port, radio_id, sep=':', end=' ')
|
logger.info('%s:%s ', port, radio_id)
|
||||||
print ("IPSC Mode:", hex_mode)
|
logger.info("IPSC Mode: %s", hex_mode)
|
||||||
print()
|
logger.info("")
|
||||||
|
|
||||||
class IPSC(DatagramProtocol):
|
class IPSC(DatagramProtocol):
|
||||||
|
|
||||||
|
@ -147,21 +200,20 @@ class IPSC(DatagramProtocol):
|
||||||
self.PEER_ALIVE_REQ_PKT = (PEER_ALIVE_REQ + self._config['LOCAL']['RADIO_ID'] + self.TS_FLAGS)
|
self.PEER_ALIVE_REQ_PKT = (PEER_ALIVE_REQ + self._config['LOCAL']['RADIO_ID'] + self.TS_FLAGS)
|
||||||
self.PEER_ALIVE_REPLY_PKT = (PEER_ALIVE_REPLY + self._config['LOCAL']['RADIO_ID'] + self.TS_FLAGS)
|
self.PEER_ALIVE_REPLY_PKT = (PEER_ALIVE_REPLY + self._config['LOCAL']['RADIO_ID'] + self.TS_FLAGS)
|
||||||
else:
|
else:
|
||||||
print("Unexpected arguments found.")
|
logger.error("Unexpected arguments found.")
|
||||||
|
|
||||||
def masterKeepalive(self):
|
def masterKeepalive(self):
|
||||||
master_alive_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_ALIVE_PKT)
|
master_alive_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_ALIVE_PKT)
|
||||||
self.transport.write(master_alive_packet, (self._config['MASTER']['IP'], self._config['MASTER']['PORT']))
|
self.transport.write(master_alive_packet, (self._config['MASTER']['IP'], self._config['MASTER']['PORT']))
|
||||||
print("->> Master Keep Alive Sent To:\t", self._config['MASTER']['IP'],":", self._config['MASTER']['PORT'], "\n")
|
logger.info("->> Master Keep Alive Sent To:\t%s:%s\n", self._config['MASTER']['IP'], self._config['MASTER']['PORT'])
|
||||||
|
|
||||||
def startProtocol(self):
|
def startProtocol(self):
|
||||||
print ("*** config: %s" % self._config)
|
logger.debug("*** config: %s", self._config)
|
||||||
print ()
|
logger.info("")
|
||||||
print ("*** Starting up IPSC Client and Registering to the Master ***")
|
logger.info("*** Starting up IPSC Client and Registering to the Master ***")
|
||||||
reg_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_REG_REQ_PKT)
|
reg_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_REG_REQ_PKT)
|
||||||
self.transport.write(reg_packet, (self._config['MASTER']['IP'], self._config['MASTER']['PORT']))
|
self.transport.write(reg_packet, (self._config['MASTER']['IP'], self._config['MASTER']['PORT']))
|
||||||
print ("->> Sending Registration to Master:\t", self._config['MASTER']['IP'],":", self._config['MASTER']['PORT'],
|
logger.info("->> Sending Registration to Master:\t%s:%s\tFrom:%s\n", self._config['MASTER']['IP'], self._config['MASTER']['PORT'], binascii.b2a_hex(self._config['LOCAL']['RADIO_ID']))
|
||||||
"\tFrom:", binascii.b2a_hex(self._config['LOCAL']['RADIO_ID']), "\n")
|
|
||||||
#
|
#
|
||||||
self._call = task.LoopingCall(self.masterKeepalive)
|
self._call = task.LoopingCall(self.masterKeepalive)
|
||||||
self._loop = self._call.start(6)
|
self._loop = self._call.start(6)
|
||||||
|
@ -169,55 +221,55 @@ class IPSC(DatagramProtocol):
|
||||||
def datagramReceived(self, data, (host, port)):
|
def datagramReceived(self, data, (host, port)):
|
||||||
dest_ip = self._config['MASTER']['IP']
|
dest_ip = self._config['MASTER']['IP']
|
||||||
dest_port = self._config['MASTER']['PORT']
|
dest_port = self._config['MASTER']['PORT']
|
||||||
#print "received %r from %s:%d" % (binascii.b2a_hex(data), host, port)
|
#logger.info("received %r from %s:%d", binascii.b2a_hex(data), host, port)
|
||||||
|
|
||||||
_packettype = (data[0:1])
|
_packettype = (data[0:1])
|
||||||
|
|
||||||
if (_packettype == MASTER_REG_REQ):
|
if (_packettype == MASTER_REG_REQ):
|
||||||
print("<<- Registration Packet Recieved\n")
|
logger.info("<<- Registration Packet Recieved\n")
|
||||||
|
|
||||||
elif (_packettype == MASTER_REG_REPLY):
|
elif (_packettype == MASTER_REG_REPLY):
|
||||||
print("<<- Master Registration Reply From:\t", host,":",port)
|
logger.info("<<- Master Registration Reply From:\t%s:%s", host, port)
|
||||||
master_alive_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_ALIVE_PKT)
|
master_alive_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.MASTER_ALIVE_PKT)
|
||||||
self.transport.write(master_alive_packet, (host, port))
|
self.transport.write(master_alive_packet, (host, port))
|
||||||
print("->> Master Keep Alive Sent To:\t", host,":",port, "\n")
|
logger.info("->> Master Keep Alive Sent To:\t%s:%s\n", host, port)
|
||||||
# the only time we need to ask for the peer list is after we've registered to the master
|
# the only time we need to ask for the peer list is after we've registered to the master
|
||||||
peer_list_req_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_LIST_REQ_PKT)
|
peer_list_req_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_LIST_REQ_PKT)
|
||||||
self.transport.write(peer_list_req_packet, (host, port))
|
self.transport.write(peer_list_req_packet, (host, port))
|
||||||
print("->> Peer List Reqested from Master:\t", host,":",port, "\n")
|
logger.info("->> Peer List Reqested from Master:\t%s:%s\n", host, port)
|
||||||
#print binascii.b2a_hex(peer_list_req_packet)
|
#logger.info(binascii.b2a_hex(peer_list_req_packet))
|
||||||
|
|
||||||
elif (_packettype == PEER_REG_REQUEST):
|
elif (_packettype == PEER_REG_REQUEST):
|
||||||
print("<<- Peer Registration Request From:\t", host,":",port)
|
logger.info("<<- Peer Registration Request From:\t%s:%s", host, port)
|
||||||
peer_reg_reply_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_REG_REPLY_PKT)
|
peer_reg_reply_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_REG_REPLY_PKT)
|
||||||
self.transport.write(peer_reg_reply_packet, (host, port))
|
self.transport.write(peer_reg_reply_packet, (host, port))
|
||||||
print("->> Peer Registration Reply Sent To:\t", host,":",port,"\n")
|
logger.info("->> Peer Registration Reply Sent To:\t%s:%s\n", host, port)
|
||||||
#print host, port
|
#logger.info("%s:%s", host, port)
|
||||||
#print binascii.b2a_hex(peer_reg_reply_packet)
|
#logger.info(binascii.b2a_hex(peer_reg_reply_packet))
|
||||||
|
|
||||||
elif (_packettype == PEER_ALIVE_REQ):
|
elif (_packettype == PEER_ALIVE_REQ):
|
||||||
print("<<- Received Peer Keep Alive From:\t", host,":",port)
|
logger.info("<<- Received Peer Keep Alive From:\t%s:%s", host, port)
|
||||||
peer_alive_req_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_ALIVE_REQ_PKT)
|
peer_alive_req_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_ALIVE_REQ_PKT)
|
||||||
peer_alive_reply_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_ALIVE_REPLY_PKT)
|
peer_alive_reply_packet = hashed_packet(self._config['LOCAL']['AUTH_KEY'], self.PEER_ALIVE_REPLY_PKT)
|
||||||
self.transport.write(peer_alive_reply_packet, (host, port))
|
self.transport.write(peer_alive_reply_packet, (host, port))
|
||||||
print("->> Sent Peer Keep Alive Reply To:\t\t", host,":",port,)
|
logger.info("->> Sent Peer Keep Alive Reply To:\t\t%s:%s", host, port)
|
||||||
self.transport.write(peer_alive_req_packet, (host, port))
|
self.transport.write(peer_alive_req_packet, (host, port))
|
||||||
print("->> Sent Peer Keep Alive Request To:\t\t", host,":",port, "\n")
|
logger.info("->> Sent Peer Keep Alive Request To:\t\t%s:%s\n", host, port)
|
||||||
#print binascii.b2a_hex(peer_alive_req_packet)
|
#logger.info(binascii.b2a_hex(peer_alive_req_packet))
|
||||||
|
|
||||||
elif (_packettype == MASTER_ALIVE_REPLY):
|
elif (_packettype == MASTER_ALIVE_REPLY):
|
||||||
print("<<- Keep Alive Recieved from Master:\t", host,":",port, "\n")
|
logger.info("<<- Keep Alive Received from Master:\t%s:%s\n", host, port)
|
||||||
|
|
||||||
elif (_packettype == PEER_ALIVE_REPLY):
|
elif (_packettype == PEER_ALIVE_REPLY):
|
||||||
print("<<- Keep Alive Recieved from Peer:\t", host,":",port, "\n")
|
logger.info("<<- Keep Alive Received from Peer:\t%s:%s\n", host, port)
|
||||||
|
|
||||||
elif (_packettype == RDAC_CTL):
|
elif (_packettype == RDAC_CTL):
|
||||||
print("<<- RDAC and/or Control Packet From:\t", host,":",port, "\n")
|
logger.info("<<- RDAC and/or Control Packet From:\t%s:%s\n", host, port)
|
||||||
|
|
||||||
elif (_packettype == PEER_LIST_REPLY):
|
elif (_packettype == PEER_LIST_REPLY):
|
||||||
print("<<- The Peer List has been Received from Master:\t", host,":",port)
|
logger.info("<<- The Peer List has been Received from Master:\t%s:%s", host, port)
|
||||||
_num_peers = int(str(int(binascii.b2a_hex(data[5:7]), 16))[1:])
|
_num_peers = int(str(int(binascii.b2a_hex(data[5:7]), 16))[1:])
|
||||||
print(' There are', _num_peers, 'peers in this IPSC Network')
|
logger.info(' There are %s peers in this IPSC Network', _num_peers)
|
||||||
for i in range(7, (_num_peers*11)+7, 11):
|
for i in range(7, (_num_peers*11)+7, 11):
|
||||||
self._config['PEERS'].append({
|
self._config['PEERS'].append({
|
||||||
'RADIO_ID': binascii.b2a_hex(data[i:i+4]),
|
'RADIO_ID': binascii.b2a_hex(data[i:i+4]),
|
||||||
|
@ -226,11 +278,11 @@ class IPSC(DatagramProtocol):
|
||||||
'MODE': binascii.b2a_hex(data[i+10:i+11])
|
'MODE': binascii.b2a_hex(data[i+10:i+11])
|
||||||
})
|
})
|
||||||
print_peer_list(self._config)
|
print_peer_list(self._config)
|
||||||
print()
|
logger.info("")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
packet_type = binascii.b2a_hex(_packettype)
|
packet_type = binascii.b2a_hex(_packettype)
|
||||||
print("<<- Recieved Unprocessed Type", packet_type, "From:\t", host,":",port,"\n")
|
logger.error("<<- Received Unprocessed Type %s From:\t%s:%s\n", packet_type, host, port)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -240,14 +292,14 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
if args.network is not None:
|
if args.network is not None:
|
||||||
if args.network in NETWORK:
|
if args.network in NETWORK:
|
||||||
print("Connecting to %s" % args.network)
|
logger.info("Connecting to %s", args.network)
|
||||||
reactor.listenUDP(NETWORK[args.network]['LOCAL']['PORT'], IPSC(NETWORK[args.network]))
|
reactor.listenUDP(NETWORK[args.network]['LOCAL']['PORT'], IPSC(NETWORK[args.network]))
|
||||||
else:
|
else:
|
||||||
print("%s is not a configured ISPC network." % args.network)
|
logger.info("%s is not a configured ISPC network.", args.network)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
else: # connect to all
|
else: # connect to all
|
||||||
print("No network supplied, connecting to all networks.")
|
logger.info("No network supplied, connecting to all networks.")
|
||||||
for ipsc_network in NETWORK:
|
for ipsc_network in NETWORK:
|
||||||
reactor.listenUDP(NETWORK[ipsc_network]['LOCAL']['PORT'], IPSC(NETWORK[ipsc_network]))
|
reactor.listenUDP(NETWORK[ipsc_network]['LOCAL']['PORT'], IPSC(NETWORK[ipsc_network]))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue