35
from common import (util, studpath)
35
from common import (util, studpath, chat)
38
38
trampoline_path = os.path.join(conf.ivle_install_dir, "bin/trampoline")
99
99
console_dir, python_path, console_path,
100
100
str(port), str(magic)])
102
print >> sys.stderr, cmd
102
# print >> sys.stderr, cmd
103
103
res = os.system(cmd)
104
print >> sys.stderr, res
104
# print >> sys.stderr, res
113
113
raise Exception, "unable to find a free port!"
116
req.write(cjson.encode({"host": host, "port": port, "magic": magic}))
115
# Assemble the key and return it.
116
key = cjson.encode({"host": host, "port": port, "magic": magic})
117
req.write(cjson.encode(key.encode("hex")))
118
119
def handle_chat(req, kind = "chat"):
119
120
# The request *should* have the following four fields:
120
# host, port: Host and port where the console server apparently lives
121
# digest, text: Fields to pass along to the console server
121
# host, port, magic: Host and port where the console server lives,
122
# and the secret to use to digitally sign the communication with the
124
# text: Fields to pass along to the console server
122
125
# It simply acts as a proxy to the console server
123
126
if req.method != "POST":
124
127
req.throw_error(req.HTTP_BAD_REQUEST)
125
128
fields = req.get_fieldstorage()
127
host = fields.getfirst("host").value
128
port = int(fields.getfirst("port").value)
129
digest = fields.getfirst("digest").value
130
key = cjson.decode(fields.getfirst("key").value.decode("hex"))
130
134
except AttributeError:
131
135
# Any of the getfirsts returned None
132
136
req.throw_error(req.HTTP_BAD_REQUEST)
136
140
except AttributeError:
139
msg = {'cmd':kind, 'text':text, 'digest':digest}
141
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
142
sok.connect((host, port))
143
sok.send(cjson.encode(msg))
145
buf = cStringIO.StringIO()
150
blk = conn.recv(1024, socket.MSG_DONTWAIT)
152
# Exception thrown if it WOULD block (but we
153
# told it not to wait) - ie. we are done
143
msg = {'cmd':kind, 'text':text}
144
response = chat.chat(host, port, msg, magic, decode = False)
145
print >> open("/tmp/wibble","w"), repr(msg), repr(response)
159
146
req.content_type = "text/plain"