mirror of https://github.com/dirtbags/moth.git
pointsd now using tcp connections .. not tested
This commit is contained in:
parent
e8df83c83b
commit
09d5b1385c
|
@ -9,48 +9,72 @@ from . import config
|
||||||
|
|
||||||
house = config.get('global', 'house_team')
|
house = config.get('global', 'house_team')
|
||||||
|
|
||||||
class MyHandler(asyncore.dispatcher):
|
class PointsServer(asyncore.dispatcher):
|
||||||
def __init__(self, port=6667):
|
''' Receive connections from client and passes them off to handler. '''
|
||||||
asyncore.dispatcher.__init__(self)
|
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
self.bind(('', port))
|
|
||||||
self.store = points.Storage(fix=True)
|
|
||||||
self.acked = set()
|
|
||||||
self.outq = []
|
|
||||||
|
|
||||||
def writable(self):
|
def __init__(self, port=6667):
|
||||||
return bool(self.outq)
|
asyncore.dispatcher.__init__(self)
|
||||||
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.bind(('', port))
|
||||||
|
self.listen(5)
|
||||||
|
self.acked = set()
|
||||||
|
self.outq = []
|
||||||
|
|
||||||
def handle_write(self):
|
def handle_accept(self):
|
||||||
dgram, peer = self.outq.pop(0)
|
''' Accept a connection from a client and pass it to the handler. '''
|
||||||
self.socket.sendto(dgram, peer)
|
sock, addr = self.accept()
|
||||||
|
clientip = addr[0]
|
||||||
|
ClientHandler(sock, clientip)
|
||||||
|
|
||||||
def handle_read(self):
|
class ClientHandler(asyncore.dispatcher):
|
||||||
now = int(time.time())
|
''' Handles talking to clients. '''
|
||||||
dgram, peer = self.socket.recvfrom(4096)
|
|
||||||
try:
|
|
||||||
id, when, cat, team, score = points.decode_request(dgram)
|
|
||||||
except ValueError as e:
|
|
||||||
return self.respond(peer, now, str(e))
|
|
||||||
team = team or house
|
|
||||||
|
|
||||||
# Replays can happen legitimately.
|
def __init__(self, sock, clientip):
|
||||||
if not ((peer, id) in self.acked):
|
asyncore.dispatcher.__init__(self, sock=sock)
|
||||||
if not (now - 2 < when <= now):
|
self.clientip = clientip
|
||||||
return self.respond(peer, id, 'Your clock is off')
|
self.store = points.Storage(fix=True)
|
||||||
self.store.add((when, cat, team, score))
|
self.acked = set()
|
||||||
self.acked.add((peer, id))
|
self.outq = []
|
||||||
|
|
||||||
self.respond(peer, id, 'OK')
|
def writable(self):
|
||||||
|
''' If there is data in the queue, the socket is made writable. '''
|
||||||
|
return bool(self.outq)
|
||||||
|
|
||||||
def respond(self, peer, id, txt):
|
def handle_write(self):
|
||||||
resp = points.encode_response(id, txt)
|
''' Pop data from the queue and send it to the client. '''
|
||||||
self.outq.append((resp, peer))
|
resp = self.outq.pop(0)
|
||||||
|
self.send(resp)
|
||||||
|
|
||||||
|
# conversation over
|
||||||
|
self.close()
|
||||||
|
|
||||||
def start():
|
def handle_read(self):
|
||||||
return MyHandler()
|
''' Receive data from the client. '''
|
||||||
|
now = int(time.time())
|
||||||
|
data = self.recv(4096)
|
||||||
|
|
||||||
|
# decode their message
|
||||||
|
try:
|
||||||
|
id, when, cat, team, score = points.decode_request(data)
|
||||||
|
except ValueError as e:
|
||||||
|
return self.respond(now, str(e))
|
||||||
|
team = team or house
|
||||||
|
|
||||||
|
# do points and send ACK
|
||||||
|
if not ((self.clientip, id) in self.acked):
|
||||||
|
if not (now - 2 < when <= now):
|
||||||
|
return self.respond(id, 'Your clock is off')
|
||||||
|
self.store.add((when, cat, team, score))
|
||||||
|
self.acked.add((self.clientip, id))
|
||||||
|
|
||||||
|
self.respond(id, 'OK')
|
||||||
|
|
||||||
|
def respond(self, id, txt):
|
||||||
|
''' Queue responses to the client. '''
|
||||||
|
resp = points.encode_response(id, txt)
|
||||||
|
self.outq.append(resp)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
server = PointsServer()
|
||||||
|
asyncore.loop()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
h = start()
|
|
||||||
asyncore.loop()
|
|
||||||
|
|
Loading…
Reference in New Issue