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

« back to all changes in this revision

Viewing changes to ivle/chat.py

  • Committer: William Grant
  • Date: 2010-02-24 07:22:43 UTC
  • Revision ID: grantw@unimelb.edu.au-20100224072243-xq5w2we8iuoteen1
Reword and reformat the tour a bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# Author: Thomas Conway
20
20
# Date:   5/2/2008
21
21
 
22
 
try:
23
 
    import json
24
 
except ImportError:
25
 
    import simplejson as json
26
 
 
 
22
import cjson
27
23
import cStringIO
28
24
import hashlib
29
25
import sys
93
89
        (conn, addr) = s.accept()
94
90
        conn.settimeout(SOCKETTIMEOUT)
95
91
        try:
96
 
            # Grab the input and try to decode
 
92
            # Grab the input
97
93
            inp = recv_netstring(conn)
98
 
            try:
99
 
                content = decode(inp, magic)
100
 
            except ProtocolError:
 
94
            env = cjson.decode(inp)
 
95
 
 
96
            # Check that the message is 
 
97
            digest = hashlib.md5(env['content'] + magic).hexdigest()
 
98
            if env['digest'] != digest:
101
99
                conn.close()
102
100
                continue
103
101
 
 
102
            content = cjson.decode(env['content'])
 
103
 
104
104
            response = handler(content)
105
105
 
106
 
            send_netstring(conn, json.dumps(response))
 
106
            send_netstring(conn, cjson.encode(response))
107
107
 
108
108
            conn.close()
109
109
 
110
110
        except Terminate, t:
111
111
            # Try and send final response and then terminate
112
112
            if t.final_response:
113
 
                send_netstring(conn, json.dumps(t.final_response))
 
113
                send_netstring(conn, cjson.encode(t.final_response))
114
114
            conn.close()
115
115
            sys.exit(0)
116
116
        except Exception:
123
123
                "value": str(e_val),
124
124
                "traceback": tb_dump.getvalue()
125
125
            }
126
 
            send_netstring(conn, json.dumps(json_exc))
 
126
            send_netstring(conn, cjson.encode(json_exc))
127
127
            conn.close()
128
128
 
129
129
 
131
131
    sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
132
132
    sok.connect((host, port))
133
133
    sok.settimeout(SOCKETTIMEOUT)
134
 
 
135
 
    out = encode(msg, magic)
136
 
 
137
 
    send_netstring(sok, out)
 
134
    content = cjson.encode(msg)
 
135
    digest = hashlib.md5(content + magic).hexdigest()
 
136
    env = {'digest':digest,'content':content}
 
137
    json = cjson.encode(env)
 
138
 
 
139
    send_netstring(sok, json)
138
140
    inp = recv_netstring(sok)
139
141
 
140
142
    sok.close()
141
143
 
142
144
    if decode:
143
 
        return json.loads(inp)
 
145
        return cjson.decode(inp)
144
146
    else:
145
147
        return inp
146
148
 
147
 
def encode(message, magic):
148
 
    """Converts a message into a JSON serialisation and uses a magic
149
 
    string to attach a HMAC digest.
150
 
    """
151
 
    # XXX: Any reason that we double encode?
152
 
    content = json.dumps(message)
153
 
 
154
 
    digest = hashlib.md5(content + magic).hexdigest()
155
 
    env = {'digest':digest,'content':content}
156
 
    return json.dumps(env)
157
 
 
158
 
 
159
 
def decode(message, magic):
160
 
    """Takes a message with an attached HMAC digest and validates the message.
161
 
    """
162
 
    msg = json.loads(message)
163
 
 
164
 
    # Check that the message is valid
165
 
    digest = hashlib.md5(msg['content'] + magic).hexdigest()
166
 
    if msg['digest'] != digest:
167
 
        raise ProtocolError("HMAC digest is invalid")
168
 
    content = json.loads(msg['content'])
169
 
 
170
 
    return content
171
 
 
172
149
 
173
150
def send_netstring(sok, data):
174
 
    """ Sends a netstring to a socket
175
 
    """
176
151
    netstring = "%d:%s,"%(len(data),data)
177
152
    sok.sendall(netstring)
178
153
 
179
154
 
180
155
def recv_netstring(sok):
181
 
    """ Attempts to recieve a Netstring from a socket.
182
 
    Throws a ProtocolError if the received data violates the Netstring 
183
 
    protocol.
184
 
    """
185
156
    # Decode netstring
186
157
    size_buffer = []
187
158
    c = sok.recv(1)