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

« back to all changes in this revision

Viewing changes to lib/common/chat.py

  • Committer: mattgiuca
  • Date: 2008-02-14 00:49:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:450
Set svn:ignore on a a few more things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import cjson
23
23
import cStringIO
24
24
import md5
25
 
import sys
26
25
import os
27
26
import socket
28
 
import traceback
29
 
 
30
 
class Terminate(Exception):
31
 
    """Exception thrown when server is to be shut down. It will attempt to sned 
32
 
    the final_response to the client and then exits"""
33
 
    def __init__(self, final_response=None):
34
 
        self.final_response = final_response
35
 
 
36
 
    def __str__(self):
37
 
        return repr(self.final_response)
38
 
 
39
27
 
40
28
def start_server(port, magic, daemon_mode, handler, initializer = None):
41
29
    # Attempt to open the socket.
86
74
            conn.sendall(cjson.encode(response))
87
75
 
88
76
            conn.close()
89
 
 
90
 
        except Terminate, t:
91
 
            # Try and send final response and then terminate
92
 
            if t.final_response:
93
 
                conn.sendall(cjson.encode(t.final_response))
94
 
            conn.close()
95
 
            sys.exit(0)
96
 
        except Exception:
97
 
            # Make a JSON object full of exceptional goodness
98
 
            tb_dump = cStringIO.StringIO()
99
 
            e_type, e_val, e_tb = sys.exc_info()
100
 
            traceback.print_tb(e_tb, file=tb_dump)
101
 
            json_exc = {
102
 
                "type": e_type.__name__,
103
 
                "value": str(e_val),
104
 
                "traceback": tb_dump.getvalue()
105
 
            }
106
 
            conn.sendall(cjson.encode(json_exc))
 
77
        except Exception, e:
 
78
            conn.sendall(cjson.encode(repr(e)))
107
79
            conn.close()
108
80
 
109
81
 
120
92
    while blk:
121
93
        buf.write(blk)
122
94
        try:
123
 
            blk = sok.recv(1024, socket.MSG_DONTWAIT)
124
 
        except socket.error, e:
125
 
            if e[0] != 11:
126
 
                raise
 
95
            blk = conn.recv(1024, socket.MSG_DONTWAIT)
 
96
        except:
127
97
            # Exception thrown if it WOULD block (but we
128
98
            # told it not to wait) - ie. we are done
129
99
            blk = None