1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
# Author: Thomas Conway
30
class Terminate(Exception):
31
"""Exception thrown when server is to be shut down. It will attempt to sned
32
the final_response to the client and then exits"""
33
def __init__(self, final_response=None):
34
self.final_response = final_response
37
return repr(self.final_response)
40
def start_server(port, magic, daemon_mode, handler, initializer = None):
41
# Attempt to open the socket.
42
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46
# Excellent! It worked. Let's turn ourself into a daemon,
47
# then get on with the job of being a python interpreter.
49
if os.fork(): # launch child and...
50
os._exit(0) # kill off parent
52
if os.fork(): # launch child and...
53
os._exit(0) # kill off parent again.
57
MAXFD = os.sysconf("SC_OPEN_MAX")
61
# Close all file descriptors, except the socket.
62
for i in xrange(MAXFD):
70
si = os.open(os.devnull, os.O_RDONLY)
71
os.dup2(si, sys.stdin.fileno())
73
so = os.open(os.devnull, os.O_WRONLY)
74
os.dup2(so, sys.stdout.fileno())
76
se = os.open(os.devnull, os.O_WRONLY)
77
os.dup2(se, sys.stderr.fileno())
83
(conn, addr) = s.accept()
86
buf = cStringIO.StringIO()
91
blk = conn.recv(1024, socket.MSG_DONTWAIT)
93
# Exception thrown if it WOULD block (but we
94
# told it not to wait) - ie. we are done
97
env = cjson.decode(inp)
99
# Check that the message is
100
digest = hashlib.md5(env['content'] + magic).hexdigest()
101
if env['digest'] != digest:
105
content = cjson.decode(env['content'])
107
response = handler(content)
109
conn.sendall(cjson.encode(response))
114
# Try and send final response and then terminate
116
conn.sendall(cjson.encode(t.final_response))
120
# Make a JSON object full of exceptional goodness
121
tb_dump = cStringIO.StringIO()
122
e_type, e_val, e_tb = sys.exc_info()
123
traceback.print_tb(e_tb, file=tb_dump)
125
"type": e_type.__name__,
127
"traceback": tb_dump.getvalue()
129
conn.sendall(cjson.encode(json_exc))
133
def chat(host, port, msg, magic, decode = True):
134
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
135
sok.connect((host, port))
136
content = cjson.encode(msg)
137
digest = hashlib.md5(content + magic).hexdigest()
138
env = {'digest':digest,'content':content}
139
sok.send(cjson.encode(env))
141
buf = cStringIO.StringIO()
146
blk = sok.recv(1024, socket.MSG_DONTWAIT)
147
except socket.error, e:
150
# Exception thrown if it WOULD block (but we
151
# told it not to wait) - ie. we are done
157
return cjson.decode(inp)