Utilities/SingleApplication.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the single application server and client.
8 """
9
10 from PyQt4.QtCore import SIGNAL
11 from PyQt4.QtNetwork import QLocalServer, QLocalSocket
12
13 import Utilities
14
15 class SingleApplicationServer(QLocalServer):
16 """
17 Class implementing the single application server base class.
18 """
19 def __init__(self, name):
20 """
21 Constructor
22
23 @param name name this server is listening to (string)
24 """
25 QLocalServer.__init__(self)
26
27 res = self.listen(name)
28 if not res:
29 # maybe it crashed last time
30 self.removeServer(name)
31 self.listen(name)
32
33 self.connect(self, SIGNAL("newConnection()"), self.__newConnection)
34
35 self.qsock = None
36
37 def __newConnection(self):
38 """
39 Private slot to handle a new connection.
40 """
41 sock = self.nextPendingConnection()
42
43 # If we already have a connection, refuse this one. It will be closed
44 # automatically.
45 if self.qsock is not None:
46 return
47
48 self.qsock = sock
49
50 self.connect(self.qsock, SIGNAL('readyRead()'), self.__parseLine)
51 self.connect(self.qsock, SIGNAL('disconnected()'), self.__disconnected)
52
53 def __parseLine(self):
54 """
55 Private method to handle data from the client.
56 """
57 while self.qsock and self.qsock.canReadLine():
58 line = unicode(self.qsock.readLine())
59
60 ## print line ##debug
61
62 eoc = line.find('<') + 1
63
64 boc = line.find('>')
65 if boc >= 0 and eoc > boc:
66 # handle the command sent by the client.
67 cmd = line[boc:eoc]
68 params = line[eoc:-1]
69
70 self.handleCommand(cmd, params)
71
72 def __disconnected(self):
73 """
74 Private method to handle the closure of the socket.
75 """
76 self.qsock = None
77
78 def shutdown(self):
79 """
80 Public method used to shut down the server.
81 """
82 if self.qsock is not None:
83 self.disconnect(self.qsock, SIGNAL('readyRead()'), self.__parseLine)
84 self.disconnect(self.qsock, SIGNAL('disconnected()'), self.__disconnected)
85
86 self.qsock = None
87
88 self.close()
89
90 def handleCommand(self, cmd, params):
91 """
92 Public slot to handle the command sent by the client.
93
94 <b>Note</b>: This method must be overridden by subclasses.
95
96 @param cmd commandstring (string)
97 @param params parameterstring (string)
98 """
99 raise RuntimeError("'handleCommand' must be overridden")
100
101 class SingleApplicationClient(object):
102 """
103 Class implementing the single application client base class.
104 """
105 def __init__(self, name):
106 """
107 Constructor
108
109 @param name name of the local server to connect to (string)
110 """
111 self.name = name
112 self.connected = False
113
114 def connect(self):
115 """
116 Public method to connect the single application client to its server.
117
118 @return value indicating success or an error number. Value is one of:
119 <table>
120 <tr><td>0</td><td>No application is running</td></tr>
121 <tr><td>1</td><td>Application is already running</td></tr>
122 </table>
123 """
124 self.sock = QLocalSocket()
125 self.sock.connectToServer(self.name)
126 if self.sock.waitForConnected(10000):
127 self.connected = True
128 return 1
129 else:
130 err = self.sock.error()
131 if err == QLocalSocket.ServerNotFoundError:
132 return 0
133 else:
134 return -err
135
136 def disconnect(self):
137 """
138 Public method to disconnect from the Single Appliocation server.
139 """
140 self.sock.disconnectFromServer()
141 self.connected = False
142
143 def processArgs(self, args):
144 """
145 Public method to process the command line args passed to the UI.
146
147 <b>Note</b>: This method must be overridden by subclasses.
148
149 @param args command line args (list of strings)
150 """
151 raise RuntimeError("'processArgs' must be overridden")
152
153 def sendCommand(self, cmd):
154 """
155 Public method to send the command to the application server.
156
157 @param cmd command to be sent (string)
158 """
159 if self.connected:
160 self.sock.write(cmd)
161 self.sock.flush()
162
163 def errstr(self):
164 """
165 Public method to return a meaningful error string for the last error.
166
167 @return error string for the last error (string)
168 """
169 return self.sock.errorString()

eric ide

mercurial