-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmlserver.py
60 lines (58 loc) · 1.91 KB
/
wmlserver.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
import gzip_connection
import simplewml
import time
import traceback
class WMLClient(object):
def __init__(self, sock):
self.sock = sock
self.wml = simplewml.SimpleWML()
def poll(self):
if self.sock.poll():
frag = self.sock.nextfragment()
if frag == None:
raise StopIteration
data = self.wml.parse(frag)
self.process(data)
return True
return self.sock.process_buffers()
def process(self, data):
raise NotImplementedError
def write_wml(self, wml):
self.sock.sendfragment(str(wml))
class WMLServer(object):
def __init__(self, clientclass, **kwargs):
self.clientclass = clientclass
self.sock = gzip_connection.GzipServer(clientclass=gzip_connection.GzipSocketNonBlocking, **kwargs)
self.clients = []
def poll(self):
acted = False
if self.sock.poll():
try:
self.accept(self.sock.accept())
except Exception as e:
print "Failed to accept a connection:"
traceback.print_exc()
acted = True
for client in self.clients:
try:
if client.poll():
acted = True
except StopIteration:
self.clients.remove(client)
except Exception as e:
try:
error = simplewml.Tag("error")
error.keys["message"] = "Internal error: " + str(e)
client.write_wml(error)
except:
pass
print "A client died:"
traceback.print_exc()
self.clients.remove(client)
return acted
def accept(self, sock):
self.clients.append(self.clientclass(sock))
def loop(self):
while True:
if not self.poll():
time.sleep(.02)