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

« back to all changes in this revision

Viewing changes to lib/common/chat.py

  • Committer: wagrant
  • Date: 2008-09-18 09:23:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1050
setup: Don't give possible options in the top-level usage string.
setup.build: Bail out if there is no jail and we've not been told to
             build one.
setup.setuputil: Don't import pysvn except where it is needed, so we
                 can at least display the usage without extra deps.

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
25
26
import os
26
27
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
 
27
39
 
28
40
def start_server(port, magic, daemon_mode, handler, initializer = None):
29
41
    # Attempt to open the socket.
74
86
            conn.sendall(cjson.encode(response))
75
87
 
76
88
            conn.close()
77
 
        except Exception, e:
78
 
            conn.sendall(cjson.encode(repr(e)))
 
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))
79
107
            conn.close()
80
108
 
81
109
 
92
120
    while blk:
93
121
        buf.write(blk)
94
122
        try:
95
 
            blk = conn.recv(1024, socket.MSG_DONTWAIT)
96
 
        except:
 
123
            blk = sok.recv(1024, socket.MSG_DONTWAIT)
 
124
        except socket.error, e:
 
125
            if e[0] != 11:
 
126
                raise
97
127
            # Exception thrown if it WOULD block (but we
98
128
            # told it not to wait) - ie. we are done
99
129
            blk = None