src/eric7/Cooperation/CooperationClient.py

branch
eric7
changeset 10423
299802979277
parent 9653
e67609152c5e
child 10439
21c28b0f9e41
equal deleted inserted replaced
10422:e28b89693f37 10423:299802979277
43 43
44 def __init__(self, parent=None): 44 def __init__(self, parent=None):
45 """ 45 """
46 Constructor 46 Constructor
47 47
48 @param parent reference to the parent object (QObject) 48 @param parent reference to the parent object
49 @type QObject
49 """ 50 """
50 super().__init__(parent) 51 super().__init__(parent)
51 52
52 self.__chatWidget = parent 53 self.__chatWidget = parent
53 54
89 90
90 def chatWidget(self): 91 def chatWidget(self):
91 """ 92 """
92 Public method to get a reference to the chat widget. 93 Public method to get a reference to the chat widget.
93 94
94 @return reference to the chat widget (ChatWidget) 95 @return reference to the chat widget
96 @rtype ChatWidget
95 """ 97 """
96 return self.__chatWidget 98 return self.__chatWidget
97 99
98 def sendMessage(self, message): 100 def sendMessage(self, message):
99 """ 101 """
100 Public method to send a message. 102 Public method to send a message.
101 103
102 @param message message to be sent (string) 104 @param message message to be sent
105 @type str
103 """ 106 """
104 if message == "": 107 if message == "":
105 return 108 return
106 109
107 for connectionList in self.__peers.values(): 110 for connectionList in self.__peers.values():
110 113
111 def nickName(self): 114 def nickName(self):
112 """ 115 """
113 Public method to get the nick name. 116 Public method to get the nick name.
114 117
115 @return nick name (string) 118 @return nick name
119 @rtype str
116 """ 120 """
117 return "{0}@{1}@{2}".format( 121 return "{0}@{1}@{2}".format(
118 self.__username, QHostInfo.localHostName(), self.__servers[0].serverPort() 122 self.__username, QHostInfo.localHostName(), self.__servers[0].serverPort()
119 ) 123 )
120 124
121 def hasConnection(self, senderIp, senderPort=-1): 125 def hasConnection(self, senderIp, senderPort=-1):
122 """ 126 """
123 Public method to check for an existing connection. 127 Public method to check for an existing connection.
124 128
125 @param senderIp address of the sender (QHostAddress) 129 @param senderIp address of the sender
126 @param senderPort port of the sender (integer) 130 @type QHostAddress
127 @return flag indicating an existing connection (boolean) 131 @param senderPort port of the sender
132 @type int
133 @return flag indicating an existing connection
134 @rtype bool
128 """ 135 """
129 if senderPort == -1: 136 if senderPort == -1:
130 return senderIp in self.__peers 137 return senderIp in self.__peers
131 138
132 if senderIp not in self.__peers: 139 if senderIp not in self.__peers:
138 145
139 def hasConnections(self): 146 def hasConnections(self):
140 """ 147 """
141 Public method to check, if there are any connections established. 148 Public method to check, if there are any connections established.
142 149
143 @return flag indicating the presence of connections (boolean) 150 @return flag indicating the presence of connections
151 @rtype bool
144 """ 152 """
145 return any(bool(connectionList) for connectionList in self.__peers.values()) 153 return any(bool(connectionList) for connectionList in self.__peers.values())
146 154
147 def removeConnection(self, connection): 155 def removeConnection(self, connection):
148 """ 156 """
149 Public method to remove a connection. 157 Public method to remove a connection.
150 158
151 @param connection reference to the connection to be removed 159 @param connection reference to the connection to be removed
152 (Connection) 160 @type Connection
153 """ 161 """
154 if ( 162 if (
155 connection.peerAddress() in self.__peers 163 connection.peerAddress() in self.__peers
156 and connection in self.__peers[connection.peerAddress()] 164 and connection in self.__peers[connection.peerAddress()]
157 ): 165 ):
173 181
174 def __newConnection(self, connection): 182 def __newConnection(self, connection):
175 """ 183 """
176 Private slot to handle a new connection. 184 Private slot to handle a new connection.
177 185
178 @param connection reference to the new connection (Connection) 186 @param connection reference to the new connection
187 @type Connection
179 """ 188 """
180 connection.setParent(self) 189 connection.setParent(self)
181 connection.setClient(self) 190 connection.setClient(self)
182 connection.setGreetingMessage(self.__username, self.__servers[0].serverPort()) 191 connection.setGreetingMessage(self.__username, self.__servers[0].serverPort())
183 192
188 197
189 def __connectionRejected(self, msg): 198 def __connectionRejected(self, msg):
190 """ 199 """
191 Private slot to handle the rejection of a connection. 200 Private slot to handle the rejection of a connection.
192 201
193 @param msg error message (string) 202 @param msg error message
203 @type str
194 """ 204 """
195 self.connectionError.emit(msg) 205 self.connectionError.emit(msg)
196 206
197 def __connectionError(self, socketError, connection): 207 def __connectionError(self, socketError, connection):
198 """ 208 """
251 261
252 def connectToHost(self, host, port): 262 def connectToHost(self, host, port):
253 """ 263 """
254 Public method to connect to a host. 264 Public method to connect to a host.
255 265
256 @param host host to connect to (string) 266 @param host host to connect to
257 @param port port to connect to (integer) 267 @type str
268 @param port port to connect to
269 @type int
258 """ 270 """
259 self.__initialConnection = Connection(self) 271 self.__initialConnection = Connection(self)
260 self.__newConnection(self.__initialConnection) 272 self.__newConnection(self.__initialConnection)
261 self.__initialConnection.participants.connect(self.__processParticipants) 273 self.__initialConnection.participants.connect(self.__processParticipants)
262 self.__initialConnection.connectToHost(host, port) 274 self.__initialConnection.connectToHost(host, port)
282 294
283 def __processParticipants(self, participants): 295 def __processParticipants(self, participants):
284 """ 296 """
285 Private slot to handle the receipt of a list of participants. 297 Private slot to handle the receipt of a list of participants.
286 298
287 @param participants list of participants (list of strings of 299 @param participants list of participants (list of "host:port" strings)
288 "host:port") 300 @type list of str
289 """ 301 """
290 for participant in participants: 302 for participant in participants:
291 host, port = participant.split("@") 303 host, port = participant.split("@")
292 port = int(port) 304 port = int(port)
293 305
302 314
303 def sendEditorCommand(self, projectHash, filename, message): 315 def sendEditorCommand(self, projectHash, filename, message):
304 """ 316 """
305 Public method to send an editor command. 317 Public method to send an editor command.
306 318
307 @param projectHash hash of the project (string) 319 @param projectHash hash of the project
320 @type str
308 @param filename project relative universal file name of 321 @param filename project relative universal file name of
309 the sending editor (string) 322 the sending editor
310 @param message editor command to be sent (string) 323 @type str
324 @param message editor command to be sent
325 @type str
311 """ 326 """
312 for connectionList in self.__peers.values(): 327 for connectionList in self.__peers.values():
313 for connection in connectionList: 328 for connection in connectionList:
314 connection.sendEditorCommand(projectHash, filename, message) 329 connection.sendEditorCommand(projectHash, filename, message)
315 330
316 def __findConnections(self, nick): 331 def __findConnections(self, nick):
317 """ 332 """
318 Private method to get a list of connection given a nick name. 333 Private method to get a list of connection given a nick name.
319 334
320 @param nick nick name in the format of self.nickName() (string) 335 @param nick nick name in the format of self.nickName()
321 @return list of references to the connection objects (list of 336 @type str
322 Connection) 337 @return list of references to the connection objects
338 @rtype list of Connection
323 """ 339 """
324 if "@" not in nick: 340 if "@" not in nick:
325 # nick given in wrong format 341 # nick given in wrong format
326 return [] 342 return []
327 343
335 351
336 def kickUser(self, nick): 352 def kickUser(self, nick):
337 """ 353 """
338 Public method to kick a user by its nick name. 354 Public method to kick a user by its nick name.
339 355
340 @param nick nick name in the format of self.nickName() (string) 356 @param nick nick name in the format of self.nickName()
357 @type str
341 """ 358 """
342 for connection in self.__findConnections(nick): 359 for connection in self.__findConnections(nick):
343 connection.abort() 360 connection.abort()
344 361
345 def banUser(self, nick): 362 def banUser(self, nick):
346 """ 363 """
347 Public method to ban a user by its nick name. 364 Public method to ban a user by its nick name.
348 365
349 @param nick nick name in the format of self.nickName() (string) 366 @param nick nick name in the format of self.nickName()
367 @type str
350 """ 368 """
351 Preferences.syncPreferences() 369 Preferences.syncPreferences()
352 user = nick.rsplit("@")[0] 370 user = nick.rsplit("@")[0]
353 bannedUsers = Preferences.getCooperation("BannedUsers")[:] 371 bannedUsers = Preferences.getCooperation("BannedUsers")[:]
354 if user not in bannedUsers: 372 if user not in bannedUsers:
357 375
358 def banKickUser(self, nick): 376 def banKickUser(self, nick):
359 """ 377 """
360 Public method to ban and kick a user by its nick name. 378 Public method to ban and kick a user by its nick name.
361 379
362 @param nick nick name in the format of self.nickName() (string) 380 @param nick nick name in the format of self.nickName()
381 @type str
363 """ 382 """
364 self.banUser(nick) 383 self.banUser(nick)
365 self.kickUser(nick) 384 self.kickUser(nick)
366 385
367 def startListening(self, port=-1): 386 def startListening(self, port=-1):
368 """ 387 """
369 Public method to start listening for new connections. 388 Public method to start listening for new connections.
370 389
371 @param port port to listen on (integer) 390 @param port port to listen on
372 @return tuple giving a flag indicating success (boolean) and 391 @type int
373 the port the server listens on 392 @return tuple giving a flag indicating success and the port the
393 server listens on
394 @rtype tuple of (bool, int)
374 """ 395 """
375 if self.__servers: 396 if self.__servers:
376 # do first server and determine free port 397 # do first server and determine free port
377 res, port = self.__servers[0].startListening(port, True) 398 res, port = self.__servers[0].startListening(port, True)
378 if res and len(self.__servers) > 1: 399 if res and len(self.__servers) > 1:
393 414
394 def isListening(self): 415 def isListening(self):
395 """ 416 """
396 Public method to check, if the client is listening for connections. 417 Public method to check, if the client is listening for connections.
397 418
398 @return flag indicating the listening state (boolean) 419 @return flag indicating the listening state
420 @rtype bool
399 """ 421 """
400 return self.__listening 422 return self.__listening
401 423
402 def close(self): 424 def close(self):
403 """ 425 """
411 """ 433 """
412 Public method to get a human readable error message about the last 434 Public method to get a human readable error message about the last
413 server error. 435 server error.
414 436
415 @return human readable error message about the last server error 437 @return human readable error message about the last server error
416 (string) 438 @rtype str
417 """ 439 """
418 return self.__serversErrorString 440 return self.__serversErrorString

eric ide

mercurial