Cooperation/CooperationClient.py

changeset 6115
ac3a98f3ebc2
parent 6048
82ad8ec9548c
child 6645
ad476851d7e0
equal deleted inserted replaced
6113:0cf5c9683a51 6115:ac3a98f3ebc2
189 connection.setParent(self) 189 connection.setParent(self)
190 connection.setClient(self) 190 connection.setClient(self)
191 connection.setGreetingMessage(self.__username, 191 connection.setGreetingMessage(self.__username,
192 self.__servers[0].serverPort()) 192 self.__servers[0].serverPort())
193 193
194 connection.error.connect(self.__connectionError) 194 connection.error.connect(
195 connection.disconnected.connect(self.__disconnected) 195 lambda err: self.__connectionError(err, connection))
196 connection.readyForUse.connect(self.__readyForUse) 196 connection.disconnected.connect(
197 lambda: self.__disconnected(connection))
198 connection.readyForUse.connect(
199 lambda: self.__readyForUse(connection))
197 connection.rejected.connect(self.__connectionRejected) 200 connection.rejected.connect(self.__connectionRejected)
198 201
199 def __connectionRejected(self, msg): 202 def __connectionRejected(self, msg):
200 """ 203 """
201 Private slot to handle the rejection of a connection. 204 Private slot to handle the rejection of a connection.
202 205
203 @param msg error message (string) 206 @param msg error message (string)
204 """ 207 """
205 self.connectionError.emit(msg) 208 self.connectionError.emit(msg)
206 209
207 def __connectionError(self, socketError): 210 def __connectionError(self, socketError, connection):
208 """ 211 """
209 Private slot to handle a connection error. 212 Private slot to handle a connection error.
210 213
211 @param socketError reference to the error object 214 @param socketError reference to the error object
212 (QAbstractSocket.SocketError) 215 @type QAbstractSocket.SocketError
213 """ 216 @param connection connection that caused the error
214 connection = self.sender() 217 @type Connection
218 """
215 if socketError != QAbstractSocket.RemoteHostClosedError: 219 if socketError != QAbstractSocket.RemoteHostClosedError:
216 if connection.peerPort() != 0: 220 if connection.peerPort() != 0:
217 msg = "* {0}:{1}\n{2}\n".format( 221 msg = "* {0}:{1}\n{2}\n".format(
218 connection.peerAddress().toString(), 222 connection.peerAddress().toString(),
219 connection.peerPort(), 223 connection.peerPort(),
224 self.connectionError.emit(msg) 228 self.connectionError.emit(msg)
225 if connection == self.__initialConnection: 229 if connection == self.__initialConnection:
226 self.cannotConnect.emit() 230 self.cannotConnect.emit()
227 self.removeConnection(connection) 231 self.removeConnection(connection)
228 232
229 def __disconnected(self): 233 def __disconnected(self, connection):
230 """ 234 """
231 Private slot to handle the disconnection of a chat client. 235 Private slot to handle the disconnection of a chat client.
232 """ 236
233 connection = self.sender() 237 @param connection connection that was disconnected
238 @type Connection
239 """
234 self.removeConnection(connection) 240 self.removeConnection(connection)
235 241
236 def __readyForUse(self): 242 def __readyForUse(self, connection):
237 """ 243 """
238 Private slot to handle a connection getting ready for use. 244 Private slot to handle a connection getting ready for use.
239 """ 245
240 connection = self.sender() 246 @param connection connection that got ready for use
247 @type Connection
248 """
241 if self.hasConnection(connection.peerAddress(), connection.peerPort()): 249 if self.hasConnection(connection.peerAddress(), connection.peerPort()):
242 return 250 return
243 251
244 connection.newMessage.connect(self.newMessage) 252 connection.newMessage.connect(self.newMessage)
245 connection.getParticipants.connect(self.__getParticipants) 253 connection.getParticipants.connect(
254 lambda: self.__getParticipants(connection))
246 connection.editorCommand.connect(self.editorCommand) 255 connection.editorCommand.connect(self.editorCommand)
247 256
248 self.__peers[connection.peerAddress()].append(connection) 257 self.__peers[connection.peerAddress()].append(connection)
249 nick = connection.name() 258 nick = connection.name()
250 if nick != "": 259 if nick != "":
265 self.__newConnection(self.__initialConnection) 274 self.__newConnection(self.__initialConnection)
266 self.__initialConnection.participants.connect( 275 self.__initialConnection.participants.connect(
267 self.__processParticipants) 276 self.__processParticipants)
268 self.__initialConnection.connectToHost(host, port) 277 self.__initialConnection.connectToHost(host, port)
269 278
270 def __getParticipants(self): 279 def __getParticipants(self, reqConnection):
271 """ 280 """
272 Private slot to handle the request for a list of participants. 281 Private slot to handle the request for a list of participants.
273 """ 282
274 reqConnection = self.sender() 283 @param reqConnection reference to the connection to get
284 participants for
285 @type Connection
286 """
275 participants = [] 287 participants = []
276 for connectionList in self.__peers.values(): 288 for connectionList in self.__peers.values():
277 for connection in connectionList: 289 for connection in connectionList:
278 if connection != reqConnection: 290 if connection != reqConnection:
279 participants.append("{0}@{1}".format( 291 participants.append("{0}@{1}".format(

eric ide

mercurial