-
Notifications
You must be signed in to change notification settings - Fork 0
/
KOBServer.py
225 lines (198 loc) · 7.74 KB
/
KOBServer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""KOBServer.py
MorseKOB server.
Command line parameters:
webroot - path for web root directory
port - port for server to listen on (default: 7890)
Whenever a station connects or disconnects, the program updates an activity
web page, index.html, in the root directory. It also maintains a log file,
log.html, in the <webroot>/logs subdirectory.
Usage examples:
python KOBServer.py /var/www
python KOBServer.py C:/www 7891
History:
6.1.0 added private wire feature
"""
from __future__ import print_function # for Python 2.7 compatibility
import sys
import socket
import struct
import threading
import time
import os
from pins import pins # dictionary mapping wire nos. to PIN codes
# constants
VERSION = os.environ.get('VERSION', '6.1.0')
PORT = int(os.environ.get('PORT', '7890'))
TIMEOUT = float(os.environ.get('TIMEOUT', '90.0')) # to accommodate NewsBot's 30 second heartbeat timer
NEWSBOTIP = os.environ.get('NEWSBOTIP', '70.167.219.231')
# command codes
DIS = 2 # Disconnect
DAT = 3 # Code or ID
CON = 4 # Connect
ACK = 5 # Ack
# packet formats
longRecord = struct.Struct('<H2x 128s 20x 204x I 128s 8x')
shortRecord = struct.Struct('<HH')
ackRecord = struct.Struct('<H')
# get parameters
nargs = len(sys.argv)
webroot = sys.argv[1]
port = int(sys.argv[2]) if nargs > 2 else 7890
# station class
class Station:
def __init__(self, stnAddr):
self.addr = stnAddr # remote IP address + port no.
self.wire = 0 # current wire no.
self.id = '' # station ID
self.pin = '' # PIN code
self.version = '' # version of client software
self.time = time.time() # time last heard from
def updateWireNo(self, wireNo):
global statusChanged
if self.wire != wireNo:
self.wire = wireNo
if self.version:
log('Chan\t' + stationString(self))
statusChanged = True
def updateIDPINandVersion(self, stnID, stnPIN, stnVersion):
global statusChanged
if self.id != stnID:
self.id = stnID
if self.version:
log('Idnt\t' + stationString(self))
statusChanged = True
self.pin = stnPIN
if stnVersion and self.version != stnVersion:
self.version = stnVersion
log('Conn\t' + stationString(self))
statusChanged = True
# look up a station in the station list
def findStation(stnAddr):
if stnAddr not in stations:
stations[stnAddr] = Station(stnAddr)
return stations[stnAddr]
# periodically purge stations that have timed out and post changes
def updateStationList():
global statusChanged
while True:
for stn in list(stations.values()):
if time.time() - stn.time > TIMEOUT:
del stations[stn.addr]
log('Time\t' + stationString(stn))
statusChanged = True
break
updateWebPage()
time.sleep(1.0) # update again after one second
# format station data as a string
def stationString(stn):
ip, port = stn.addr
return '{}\t{}:{}\t{}\t{}'.format(stn.wire, ip, port, stn.version, stn.id)
# post a new web page if station status has changed
HTMLHEAD = '''\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="refresh" content="30">
<title>KOBServer</title>
</head>
<body style="background-color:white; color:black; text-align:center; font:80% sans-serif">
<p style="font-family:times new roman, serif; font-size:200%; font-weight:bold">K O B S<span style="font-size:80%"> E R V E R</span></p>
<table cellspacing="1" border="2" cellpadding="4" align="center">
<tr style="font-weight:bold"><td>Wire</td><td>ID</td></tr>
'''
HTMLEND = '''\
</table>
<p style="">[ <a href="info.html">Server Info</a> ]</p>
</body>
</html>
'''
def updateWebPage():
global statusChanged
if statusChanged:
statusChanged = False
activityPage = open(webroot + '/index.html', mode='w')
print(HTMLHEAD, end='', file=activityPage)
for stn in sorted(stations.values(), key=lambda s: s.wire):
print('<tr><td>', stn.wire, '</td><td style="text-align:left">',
stn.id, '</td></tr>', sep='', file=activityPage)
print(HTMLEND, end='', file=activityPage)
activityPage.close()
# post message to log file
def log(msg):
t = time.strftime('%Y-%m-%d %H:%M:%S\t%z')
# %Z deprecated, %z not always supported
# print('{}\t{}<br>'.format(t, msg), file=logPage, flush=True) # Python3 only
print('{}\t{}<br>'.format(t, msg), file=logPage)
logPage.flush()
# main program
print('')
print(time.asctime())
print('KOBServer ' + VERSION)
sys.stdout.flush()
logPage = open(webroot + '/logs/log.html', mode='a')
log('Started KOBServer ' + VERSION)
stations = {} # dictionary mapping IP+port to station instance
statusChanged = True
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', PORT))
updateThread = threading.Thread(target=updateStationList)
updateThread.daemon = True
updateThread.start()
while True:
try:
buf, stnAddr = sock.recvfrom(500)
stnIP, stnPort = stnAddr
nBytes = len(buf)
station = findStation(stnAddr)
station.time = time.time()
if nBytes == 4:
command, wireNo = shortRecord.unpack(buf)
if command == CON: # connect
sock.sendto(ackRecord.pack(ACK), stnAddr)
station.updateWireNo(wireNo)
elif command == DIS and stnAddr in stations: # disconnect
del stations[stnAddr]
statusChanged = True
log('Disc\t{}'.format(stationString
(station)))
else:
log('Unrecognized command code: {}'.format(command))
elif nBytes == 496:
command, stnID, nCodeElements, stnVersion = longRecord.unpack(buf)
stnID, sep, fill = stnID.decode(encoding='ascii',
errors='ignore').partition('\x00')
if stnID[-5:-4] == '#':
stnPIN = stnID[-4:]
stnID = stnID[:-5]
pinLoc = 4 + len(stnID)
buf = buf[:pinLoc] + 5 * b'\x00' + buf[pinLoc + 5:]
else:
stnPIN = ''
stnVersion, sep, fill = stnVersion.decode(encoding='ascii',
errors='ignore').partition('\x00')
if stnIP == NEWSBOTIP:
stnVersion = 'NewsBot'
if nCodeElements > 0:
stnVersion = ''
if command == DAT:
station.updateIDPINandVersion(stnID, stnPIN, stnVersion)
sw = station.wire
if sw and (not sw in pins or stnPIN == pins[sw]) and \
station.id and station.version:
for addr in stations:
stn = stations[addr]
if stn.wire == sw and \
(not sw in pins or stn.pin == pins[sw]) \
and stn.id and stn.version and stn != station:
sock.sendto(buf, addr)
else:
log('Unrecognized command code: {}'.format(command))
else:
log('Invalid record length: {}'.format(nBytes))
except Exception as e:
sys.stderr.write('{0} KOBServer error: {1}\n'
.format(time.asctime(), e))
sys.stderr.flush()