Cooperation/CooperationClient.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2525
8b507a9a2d40
parent 2986
cd4e2cab7eb2
child 3145
a9de05d4a22f
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 11
12 import collections 12 import collections
13 13
14 from PyQt4.QtCore import QObject, pyqtSignal, QProcess, QRegExp 14 from PyQt4.QtCore import QObject, pyqtSignal, QProcess, QRegExp
15 from PyQt4.QtNetwork import QHostInfo, QHostAddress, QAbstractSocket, QNetworkInterface 15 from PyQt4.QtNetwork import QHostInfo, QHostAddress, QAbstractSocket, \
16 QNetworkInterface
16 17
17 from .CooperationServer import CooperationServer 18 from .CooperationServer import CooperationServer
18 from .Connection import Connection 19 from .Connection import Connection
19 20
20 import Preferences 21 import Preferences
23 class CooperationClient(QObject): 24 class CooperationClient(QObject):
24 """ 25 """
25 Class implementing the client of the cooperation package. 26 Class implementing the client of the cooperation package.
26 27
27 @signal newMessage(user, message) emitted after a new message has 28 @signal newMessage(user, message) emitted after a new message has
28 arrived (string, string) 29 arrived (string, string)
29 @signal newParticipant(nickname) emitted after a new participant joined (string) 30 @signal newParticipant(nickname) emitted after a new participant joined
31 (string)
30 @signal participantLeft(nickname) emitted after a participant left (string) 32 @signal participantLeft(nickname) emitted after a participant left (string)
31 @signal connectionError(message) emitted when a connection error occurs (string) 33 @signal connectionError(message) emitted when a connection error occurs
34 (string)
32 @signal cannotConnect() emitted, if the initial connection fails 35 @signal cannotConnect() emitted, if the initial connection fails
33 @signal editorCommand(hash, filename, message) emitted when an editor command 36 @signal editorCommand(hash, filename, message) emitted when an editor
34 has been received (string, string, string) 37 command has been received (string, string, string)
35 """ 38 """
36 newMessage = pyqtSignal(str, str) 39 newMessage = pyqtSignal(str, str)
37 newParticipant = pyqtSignal(str) 40 newParticipant = pyqtSignal(str)
38 participantLeft = pyqtSignal(str) 41 participantLeft = pyqtSignal(str)
39 connectionError = pyqtSignal(str) 42 connectionError = pyqtSignal(str)
154 157
155 def removeConnection(self, connection): 158 def removeConnection(self, connection):
156 """ 159 """
157 Public method to remove a connection. 160 Public method to remove a connection.
158 161
159 @param connection reference to the connection to be removed (Connection) 162 @param connection reference to the connection to be removed
163 (Connection)
160 """ 164 """
161 if connection.peerAddress() in self.__peers and \ 165 if connection.peerAddress() in self.__peers and \
162 connection in self.__peers[connection.peerAddress()]: 166 connection in self.__peers[connection.peerAddress()]:
163 self.__peers[connection.peerAddress()].remove(connection) 167 self.__peers[connection.peerAddress()].remove(connection)
164 nick = connection.name() 168 nick = connection.name()
202 206
203 def __connectionError(self, socketError): 207 def __connectionError(self, socketError):
204 """ 208 """
205 Private slot to handle a connection error. 209 Private slot to handle a connection error.
206 210
207 @param socketError reference to the error object (QAbstractSocket.SocketError) 211 @param socketError reference to the error object
212 (QAbstractSocket.SocketError)
208 """ 213 """
209 connection = self.sender() 214 connection = self.sender()
210 if socketError != QAbstractSocket.RemoteHostClosedError: 215 if socketError != QAbstractSocket.RemoteHostClosedError:
211 if connection.peerPort() != 0: 216 if connection.peerPort() != 0:
212 msg = "* {0}:{1}\n{2}\n".format( 217 msg = "* {0}:{1}\n{2}\n".format(
256 @param host host to connect to (string) 261 @param host host to connect to (string)
257 @param port port to connect to (integer) 262 @param port port to connect to (integer)
258 """ 263 """
259 self.__initialConnection = Connection(self) 264 self.__initialConnection = Connection(self)
260 self.__newConnection(self.__initialConnection) 265 self.__newConnection(self.__initialConnection)
261 self.__initialConnection.participants.connect(self.__processParticipants) 266 self.__initialConnection.participants.connect(
267 self.__processParticipants)
262 self.__initialConnection.connectToHost(host, port) 268 self.__initialConnection.connectToHost(host, port)
263 269
264 def __getParticipants(self): 270 def __getParticipants(self):
265 """ 271 """
266 Private slot to handle the request for a list of participants. 272 Private slot to handle the request for a list of participants.
269 participants = [] 275 participants = []
270 for connectionList in self.__peers.values(): 276 for connectionList in self.__peers.values():
271 for connection in connectionList: 277 for connection in connectionList:
272 if connection != reqConnection: 278 if connection != reqConnection:
273 participants.append("{0}@{1}".format( 279 participants.append("{0}@{1}".format(
274 connection.peerAddress().toString(), connection.serverPort())) 280 connection.peerAddress().toString(),
281 connection.serverPort()))
275 reqConnection.sendParticipants(participants) 282 reqConnection.sendParticipants(participants)
276 283
277 def __processParticipants(self, participants): 284 def __processParticipants(self, participants):
278 """ 285 """
279 Private slot to handle the receipt of a list of participants. 286 Private slot to handle the receipt of a list of participants.
280 287
281 @param participants list of participants (list of strings of "host:port") 288 @param participants list of participants (list of strings of
289 "host:port")
282 """ 290 """
283 for participant in participants: 291 for participant in participants:
284 host, port = participant.split("@") 292 host, port = participant.split("@")
285 port = int(port) 293 port = int(port)
286 294
287 if port == 0: 295 if port == 0:
288 msg = self.trUtf8("Illegal address: {0}@{1}\n").format(host, port) 296 msg = self.trUtf8("Illegal address: {0}@{1}\n").format(
297 host, port)
289 self.connectionError.emit(msg) 298 self.connectionError.emit(msg)
290 else: 299 else:
291 if not self.hasConnection(QHostAddress(host), port): 300 if not self.hasConnection(QHostAddress(host), port):
292 connection = Connection(self) 301 connection = Connection(self)
293 self.__newConnection(connection) 302 self.__newConnection(connection)
309 def __findConnections(self, nick): 318 def __findConnections(self, nick):
310 """ 319 """
311 Public method to get a list of connection given a nick name. 320 Public method to get a list of connection given a nick name.
312 321
313 @param nick nick name in the format of self.nickName() (string) 322 @param nick nick name in the format of self.nickName() (string)
314 @return list of references to the connection objects (list of Connection) 323 @return list of references to the connection objects (list of
324 Connection)
315 """ 325 """
316 if "@" not in nick: 326 if "@" not in nick:
317 # nick given in wrong format 327 # nick given in wrong format
318 return [] 328 return []
319 329
399 server.close() 409 server.close()
400 self.__listening = False 410 self.__listening = False
401 411
402 def errorString(self): 412 def errorString(self):
403 """ 413 """
404 Public method to get a human readable error message about the last server error. 414 Public method to get a human readable error message about the last
405 415 server error.
406 @return human readable error message about the last server error (string) 416
417 @return human readable error message about the last server error
418 (string)
407 """ 419 """
408 return self.__serversErrorString 420 return self.__serversErrorString

eric ide

mercurial