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

« back to all changes in this revision

Viewing changes to lib/common/chat.py

  • Committer: mattgiuca
  • Date: 2008-01-29 23:52:19 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:329
Converted Console from an "app" into a "plugin". It can now be plugged in to
any app.
Added "plugins" directory in www. Added "console" plugin. This contains all of
the functionality of what was previously the console app, but modularized so
it can be imported by another app.

apps/console: Removed most of the logic (moved to plugins/console). Replaced
with a simple import of the console plugin. Should behave exactly the same.
apps/tutorial: As proof of concept, imported the console plugin. It now
appears at the bottom of the page (yet to make it have "pop up" behaviour).

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 os
26
 
import socket
27
 
 
28
 
def start_server(port, magic, daemon_mode, handler, initializer = None):
29
 
    # Attempt to open the socket.
30
 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31
 
    s.bind(('', port))
32
 
    s.listen(1)
33
 
 
34
 
    # Excellent! It worked. Let's turn ourself into a daemon,
35
 
    # then get on with the job of being a python interpreter.
36
 
    if daemon_mode:
37
 
        if os.fork():   # launch child and...
38
 
            os._exit(0) # kill off parent
39
 
        os.setsid()
40
 
        if os.fork():   # launch child and...
41
 
            os._exit(0) # kill off parent again.
42
 
        os.umask(077)
43
 
 
44
 
    if initializer:
45
 
        initializer()
46
 
 
47
 
    while True:
48
 
        (conn, addr) = s.accept()
49
 
        try:
50
 
            # Grab the input
51
 
            buf = cStringIO.StringIO()
52
 
            blk = conn.recv(1024)
53
 
            while blk:
54
 
                buf.write(blk)
55
 
                try:
56
 
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
57
 
                except:
58
 
                    # Exception thrown if it WOULD block (but we
59
 
                    # told it not to wait) - ie. we are done
60
 
                    blk = None
61
 
            inp = buf.getvalue()
62
 
            env = cjson.decode(inp)
63
 
            
64
 
            # Check that the message is 
65
 
            digest = md5.new(env['content'] + magic).digest().encode('hex')
66
 
            if env['digest'] != digest:
67
 
                conn.close()
68
 
                continue
69
 
 
70
 
            content = cjson.decode(env['content'])
71
 
 
72
 
            response = handler(content)
73
 
 
74
 
            conn.sendall(cjson.encode(response))
75
 
 
76
 
            conn.close()
77
 
        except Exception, e:
78
 
            conn.sendall(cjson.encode(repr(e)))
79
 
            conn.close()
80
 
 
81
 
 
82
 
def chat(host, port, msg, magic, decode = True):
83
 
    sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
84
 
    sok.connect((host, port))
85
 
    content = cjson.encode(msg)
86
 
    digest = md5.new(content + magic).digest().encode("hex")
87
 
    env = {'digest':digest,'content':content}
88
 
    sok.send(cjson.encode(env))
89
 
 
90
 
    buf = cStringIO.StringIO()
91
 
    blk = sok.recv(1024)
92
 
    while blk:
93
 
        buf.write(blk)
94
 
        try:
95
 
            blk = conn.recv(1024, socket.MSG_DONTWAIT)
96
 
        except:
97
 
            # Exception thrown if it WOULD block (but we
98
 
            # told it not to wait) - ie. we are done
99
 
            blk = None
100
 
    inp = buf.getvalue()
101
 
    sok.close()
102
 
 
103
 
    if decode:
104
 
        return cjson.decode(inp)
105
 
    else:
106
 
        return inp
107