Cooperation/CooperationServer.py

changeset 149
a134031209be
child 791
9ec2ac20e54e
equal deleted inserted replaced
148:727a907b8305 149:a134031209be
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the cooperation server.
8 """
9
10 from PyQt4.QtCore import pyqtSignal
11 from PyQt4.QtNetwork import QTcpServer, QHostAddress
12
13 from .Connection import Connection
14
15 import Preferences
16
17 class CooperationServer(QTcpServer):
18 """
19 Class implementing the cooperation server.
20
21 @signal newConnection(connection) emitted after a new connection was received
22 (Connection)
23 """
24 newConnection = pyqtSignal(Connection)
25
26 def __init__(self, parent = None):
27 """
28 Constructor
29
30 @param parent reference to the parent object (QObject)
31 """
32 QTcpServer.__init__(self, parent)
33
34 def incomingConnection(self, socketDescriptor):
35 """
36 Protected method handling an incoming connection.
37
38 @param socketDescriptor native socket descriptor (integer)
39 """
40 connection = Connection(self)
41 connection.setSocketDescriptor(socketDescriptor)
42 self.newConnection.emit(connection)
43
44 def startListening(self, port = -1):
45 """
46 Public method to start listening for new connections.
47
48 @param port port to listen on (integer)
49 @return tuple giving a flag indicating success (boolean) and
50 the port the server listens on
51 """
52 res = self.listen(QHostAddress.Any, port)
53 if Preferences.getCooperation("TryOtherPorts"):
54 endPort = port + Preferences.getCooperation("MaxPortsToTry")
55 while not res and port < endPort:
56 port += 1
57 res = self.listen(QHostAddress.Any, port)
58 return res, port

eric ide

mercurial