~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/consoleservice/__init__.py

  • Committer: drtomc
  • Date: 2008-02-06 03:21:36 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:432
usrmgt: more work on this. Still some work to go.
console: refactored to use the ivle-chat protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
import cjson
34
34
 
35
 
from common import (util, studpath)
 
35
from common import (util, studpath, chat)
36
36
import conf
37
37
 
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)])
101
101
 
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
105
105
 
106
106
        if res == 0:
107
107
            # success
112
112
    if tries == 5:
113
113
        raise Exception, "unable to find a free port!"
114
114
 
115
 
    # Return port, magic
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")))
117
118
 
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
 
123
    # console server.
 
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()
126
129
    try:
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"))
 
131
        host = key['host']
 
132
        port = key['port']
 
133
        magic = key['magic']
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:
137
141
        text = ""
138
142
 
139
 
    msg = {'cmd':kind, 'text':text, 'digest':digest}
140
 
 
141
 
    sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
142
 
    sok.connect((host, port))
143
 
    sok.send(cjson.encode(msg))
144
 
 
145
 
    buf = cStringIO.StringIO()
146
 
    blk = sok.recv(1024)
147
 
    while blk:
148
 
        buf.write(blk)
149
 
        try:
150
 
            blk = conn.recv(1024, socket.MSG_DONTWAIT)
151
 
        except:
152
 
            # Exception thrown if it WOULD block (but we
153
 
            # told it not to wait) - ie. we are done
154
 
            blk = None
155
 
    inp = buf.getvalue()
156
 
    
157
 
    sok.close()
158
 
 
 
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"
160
 
    req.write(inp)
 
147
    req.write(response)
161
148