forked from dcramer/mangodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
43 lines (37 loc) · 1.18 KB
/
server.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
from gevent.server import StreamServer
import os
import threading
MANGODB_DURABLE = os.environ.get('MANGODB_DURABLE', False)
MANGODB_EVENTUAL = os.environ.get('MANGODB_EVENTUAL', False)
def mangodb(socket, address):
socket.sendall('HELLO\r\n')
client = socket.makefile()
output = open(os.devnull, 'w')
lock = threading.Lock()
wait = threading.Condition(lock)
while 1:
line = client.readline()
if not line:
break
cmd_bits = line.split(' ', 1)
cmd = cmd_bits[0]
if cmd == 'BYE':
break
if cmd == 'WAIT':
wait.wait()
continue
if len(cmd_bits) > 1:
lock.acquire(True)
output.write(cmd_bits[1])
if MANGODB_DURABLE:
output.flush()
os.fsync(output.fileno())
data = '42' if MANGODB_EVENTUAL else \
os.urandom(1024).encode('string-escape')
lock.release()
client.write('OK' + data + '\r\n')
client.flush()
if __name__ == '__main__':
server = StreamServer(('0.0.0.0', 27017), mangodb)
print ('Starting MangoDB on port 27017')
server.serve_forever()