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