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

« back to all changes in this revision

Viewing changes to lib/common/chat.py

  • Committer: agdimech
  • Date: 2008-02-29 00:14:16 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:616
/console/console.js: Added dynamic scrolling for the console.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2008 The University of Melbourne
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
# Module: Chat
 
19
# Author: Thomas Conway
 
20
# Date:   5/2/2008
 
21
 
 
22
import cjson
 
23
import cStringIO
 
24
import md5
 
25
import sys
 
26
import os
 
27
import socket
 
28
import traceback
 
29
 
 
30
def start_server(port, magic, daemon_mode, handler, initializer = None):
 
31
    # Attempt to open the socket.
 
32
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
33
    s.bind(('', port))
 
34
    s.listen(1)
 
35
 
 
36
    # Excellent! It worked. Let's turn ourself into a daemon,
 
37
    # then get on with the job of being a python interpreter.
 
38
    if daemon_mode:
 
39
        if os.fork():   # launch child and...
 
40
            os._exit(0) # kill off parent
 
41
        os.setsid()
 
42
        if os.fork():   # launch child and...
 
43
            os._exit(0) # kill off parent again.
 
44
        os.umask(077)
 
45
 
 
46
    if initializer:
 
47
        initializer()
 
48
 
 
49
    while True:
 
50
        (conn, addr) = s.accept()
 
51
        try:
 
52
            # Grab the input
 
53
            buf = cStringIO.StringIO()
 
54
            blk = conn.recv(1024)
 
55
            while blk:
 
56
                buf.write(blk)
 
57
                try:
 
58
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
 
59
                except:
 
60
                    # Exception thrown if it WOULD block (but we
 
61
                    # told it not to wait) - ie. we are done
 
62
                    blk = None
 
63
            inp = buf.getvalue()
 
64
            env = cjson.decode(inp)
 
65
            
 
66
            # Check that the message is 
 
67
            digest = md5.new(env['content'] + magic).digest().encode('hex')
 
68
            if env['digest'] != digest:
 
69
                conn.close()
 
70
                continue
 
71
 
 
72
            content = cjson.decode(env['content'])
 
73
 
 
74
            response = handler(content)
 
75
 
 
76
            conn.sendall(cjson.encode(response))
 
77
 
 
78
            conn.close()
 
79
        except Exception:
 
80
            # Make a JSON object full of exceptional goodness
 
81
            tb_dump = cStringIO.StringIO()
 
82
            e_type, e_val, e_tb = sys.exc_info()
 
83
            traceback.print_tb(e_tb, file=tb_dump)
 
84
            json_exc = {
 
85
                "type": e_type.__name__,
 
86
                "value": str(e_val),
 
87
                "traceback": tb_dump.getvalue()
 
88
            }
 
89
            conn.sendall(cjson.encode(json_exc))
 
90
            conn.close()
 
91
 
 
92
 
 
93
def chat(host, port, msg, magic, decode = True):
 
94
    sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
95
    sok.connect((host, port))
 
96
    content = cjson.encode(msg)
 
97
    digest = md5.new(content + magic).digest().encode("hex")
 
98
    env = {'digest':digest,'content':content}
 
99
    sok.send(cjson.encode(env))
 
100
 
 
101
    buf = cStringIO.StringIO()
 
102
    blk = sok.recv(1024)
 
103
    while blk:
 
104
        buf.write(blk)
 
105
        try:
 
106
            blk = conn.recv(1024, socket.MSG_DONTWAIT)
 
107
        except:
 
108
            # Exception thrown if it WOULD block (but we
 
109
            # told it not to wait) - ie. we are done
 
110
            blk = None
 
111
    inp = buf.getvalue()
 
112
    sok.close()
 
113
 
 
114
    if decode:
 
115
        return cjson.decode(inp)
 
116
    else:
 
117
        return inp
 
118