Skip to content

Commit 4083bb5

Browse files
committed
bf and cleanup
1 parent 0c2f714 commit 4083bb5

3 files changed

Lines changed: 41 additions & 26 deletions

File tree

Server/__init__.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from urllib.parse import urlparse
1616
from urllib.parse import parse_qs
1717
import collections
18+
import queue
1819

1920
import Server.Controller.Api
2021
import __main__
@@ -111,6 +112,17 @@ def __init__(self, handler):
111112
def setHead(self, h):
112113
self.head = h
113114

115+
class NutQueue:
116+
def __init__(self):
117+
self.q = queue.Queue(maxsize=10)
118+
self.lock = threading.Lock()
119+
120+
def push(self, obj):
121+
self.q.put(obj)
122+
123+
def shift(self):
124+
return self.q.get(timeout=1)
125+
114126
class NutResponse:
115127
def __init__(self, handler):
116128
self.handler = handler
@@ -119,18 +131,26 @@ def __init__(self, handler):
119131
self.head = False
120132
self.headersSent = False
121133
self.headers = {'Content-type': 'text/html'}
122-
self.q = collections.deque(maxlen=10)
134+
self.q = NutQueue()
123135
self.thread = None
124136
self.running = False
125137

126138
def worker(self):
127-
while self.running:
139+
while True:
128140
try:
129-
item = self.q.popleft()
141+
item = self.q.shift()
130142
self._write(item)
131-
except:
132-
pass
133-
143+
except queue.Empty:
144+
if not self.running:
145+
return
146+
except IndexError:
147+
if not self.running:
148+
return
149+
except BaseException:
150+
self.running = False
151+
return
152+
153+
134154
def __enter__(self):
135155
if not self.running:
136156
self.running = True
@@ -184,10 +204,7 @@ def write(self, data):
184204
if self.running == False:
185205
raise IOError('no writer thread')
186206

187-
while len(self.q) == self.q.maxlen:
188-
time.sleep(0.5)
189-
190-
self.q.append(data)
207+
self.q.push(data)
191208

192209
def _write(self, data):
193210
if self.bytesSent == 0 and not self.headersSent:
@@ -219,16 +236,15 @@ def Response401(request, response):
219236

220237
def route(request, response, verb = 'get'):
221238
try:
222-
print('routing')
223239
if len(request.bits) > 0 and request.bits[0] in mappings:
224240
i = request.bits[1]
225241
methodName = verb + i[0].capitalize() + i[1:]
226-
print('routing to ' + methodName)
242+
Print.info('routing to ' + methodName)
227243
method = getattr(mappings[request.bits[0]], methodName, Response404)
228244
method(request, response, **request.query)
229245
return True
230246
except BaseException as e:
231-
print(str(e))
247+
Print.error('route exception: ' + str(e))
232248
return None
233249
return False
234250

nut/Usb.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def sendHeader(self):
7777
pass
7878

7979
def _write(self, data):
80-
print('usbresponse write')
80+
Print.info('usbresponse write')
8181
if self.bytesSent == 0 and not self.headersSent:
8282
self.sendHeader()
8383

@@ -99,10 +99,9 @@ def __init__(self, url):
9999
self.head = False
100100
self.url = urlparse(self.path)
101101

102-
print('url ' + self.path);
102+
Print.info('url ' + self.path);
103103

104104
self.bits = [x for x in self.url.path.split('/') if x]
105-
print(self.bits)
106105
self.query = parse_qs(self.url.query)
107106

108107
try:
@@ -126,9 +125,9 @@ def __init__(self, i, o):
126125
self.o = o
127126

128127
def recv(self, timeout = 60000):
129-
print('begin recv')
128+
Print.info('begin recv')
130129
header = bytes(self.i.read(32, timeout=timeout))
131-
print('read complete')
130+
Print.info('read complete')
132131
magic = header[:4]
133132
self.command = int.from_bytes(header[4:8], byteorder='little')
134133
self.size = int.from_bytes(header[8:16], byteorder='little')
@@ -138,15 +137,15 @@ def recv(self, timeout = 60000):
138137
self.timestamp = int.from_bytes(header[24:32], byteorder='little')
139138

140139
if magic != b'\x12\x12\x12\x12':
141-
print('invalid magic! ' + str(magic));
140+
Print.error('invalid magic! ' + str(magic));
142141
return False
143142

144-
print('receiving %d bytes' % self.size)
143+
Print.info('receiving %d bytes' % self.size)
145144
self.payload = bytes(self.i.read(self.size, timeout=0))
146145
return True
147146

148147
def send(self, timeout = 60000):
149-
print('sending %d bytes' % len(self.payload))
148+
Print.info('sending %d bytes' % len(self.payload))
150149
self.o.write(b'\x12\x12\x12\x12', timeout=timeout)
151150
self.o.write(struct.pack('<I', self.command), timeout=timeout)
152151
self.o.write(struct.pack('<Q', len(self.payload)), timeout=timeout) # size
@@ -161,14 +160,14 @@ def poll_commands(in_ep, out_ep):
161160
while True:
162161
if p.recv(0):
163162
if p.command == 1:
164-
print('Recv command! %d' % p.command)
163+
Print.debug('Recv command! %d' % p.command)
165164
req = UsbRequest(p.payload.decode('utf-8'))
166165
with UsbResponse(p) as resp:
167166
Server.route(req, resp)
168167
else:
169-
print('Unknown command! %d' % p.command)
168+
Print.error('Unknown command! %d' % p.command)
170169
else:
171-
print('failed to read!')
170+
Print.error('failed to read!')
172171

173172
def daemon():
174173
global status
@@ -199,5 +198,5 @@ def daemon():
199198

200199
poll_commands(in_ep, out_ep)
201200
except BaseException as e:
202-
print('usb exception: ' + str(e))
201+
Print.error('usb exception: ' + str(e))
203202
time.sleep(1)

server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __init__(self):
144144
super().__init__()
145145
self.setWindowIcon(QIcon('public_html/images/logo.jpg'))
146146
screen = QDesktopWidget().screenGeometry()
147-
self.title = 'NUT USB / Web Server v2.3'
147+
self.title = 'NUT USB / Web Server v2.4'
148148
self.left = screen.width() / 4
149149
self.top = screen.height() / 4
150150
self.width = screen.width() / 2

0 commit comments

Comments
 (0)