Commits

Anonymous committed 6494e55

do not kill the local server when it is still used

the client starts a local server if needed. Until now, when it did,
it would also kill it when ending. But meanwhile a different client
might use the same server. New: The client never kills the server.
But when the last client disconnects from a local server, it ends
itself.

Comments (0)

Files changed (3)

kajongg/src/deferredutil.py

         assert not self.completed
         if result is None:
             # the player has already logged out
-            msg = m18nE('The game server lost connection to player %1')
-            self.table.abort(msg, request.player.name)
+            if request in self.requests:
+                msg = m18nE('The game server lost connection to player %1')
+                self.table.abort(msg, request.player.name)
             return
         failures = [x[1] for x in result if not x[0]]
         if failures:

kajongg/src/humanclient.py

     # disable warning about too many public methods
     # pylint: disable=R0902
     # we have 11 instance attributes, more than pylint likes
-    serverProcess = None
-    socketServerProcess = None
 
     def __init__(self, tableList, callback):
         Client1.__init__(self)
                     common.PREF.serverPort = port
                 else:
                     port = None
-                HumanClient.startLocalServer(self.useSocket, port)
+                self.startLocalServer(port)
                 # give the server up to 5 seconds time to start
                 for loop in range(50):
                     if self.serverListening():
             else:
                 return True
 
-    @staticmethod
-    def startLocalServer(useSocket, port=None):
+    def startLocalServer(self, port):
         """start a local server"""
         try:
             if os.path.exists('kajonggserver.py'):
                 args.append('--showtraffic')
             if InternalParameters.showSql:
                 args.append('--showsql')
-            if useSocket or os.name == 'nt':
+            if self.useSocket or os.name == 'nt':
                 args.append('--local')
             if port:
                 args.append('--port=%d' % port)
-            if useSocket:
+            if self.useSocket:
                 args.append('--db=%slocal.db' % appdataDir())
             process = subprocess.Popen(args, shell=os.name=='nt')
             logInfo(m18n('started the local kajongg server: pid=<numid>%1</numid> %2',
                 process.pid, ' '.join(args)))
-            if useSocket:
-                HumanClient.socketServerProcess = process
-            else:
-                HumanClient.serverProcess = process
         except OSError, exc:
             logException(exc)
 
-    @staticmethod
-    def stopLocalServers():
-        """stop the local servers we started"""
-        for process in [HumanClient.serverProcess, HumanClient.socketServerProcess]:
-            if process:
-                logInfo(m18n('stopped the local kajongg server: pid=<numid>%1</numid>',
-                    process.pid))
-                process.terminate()
-        HumanClient.serverProcess = None
-        HumanClient.socketServerProcess = None
-
-    def __del__(self):
-        """if we go away and we started a local server, stop it again"""
-        HumanClient.stopLocalServers()
-
     def remote_tablesChanged(self, tables):
         """update table list"""
         Client1.remote_tablesChanged(self, tables)
         if isinstance(result, Failure):
             logException(result)
         InternalParameters.reactor.stop()
-        HumanClient.stopLocalServers()
         # we may be in a Deferred callback generated in abortGame which would
         # catch sys.exit as an exception
         # and the qt4reactor does not quit the app when being stopped

kajongg/src/server.py

 from twisted.internet.address import UNIXAddress
 from zope.interface import implements
 from twisted.cred import checkers, portal, credentials, error as credError
+from twisted.internet import reactor
 
 from game import RemoteGame
 from player import Players
                         else:
                             self.leaveTable(user, table.tableid)
                 self.users.remove(user)
+        if InternalParameters.socket and not self.users and reactor.running:
+            logInfo('local server terminates. Reason: last client disconnected')
+            reactor.stop()
 
     def loadSuspendedTables(self, user):
         """loads all yet unloaded suspended tables where this
 
 def kajonggServer():
     """start the server"""
-    from twisted.internet import reactor
     from optparse import OptionParser
     parser = OptionParser()
     parser.add_option('', '--port', dest='port', help=m18n('the server will listen on PORT'),
     try:
         if InternalParameters.socket:
             if os.name == 'nt':
-                logInfo('kajonggserver listening on 127.0.0.1 port %d' % port)
+                logInfo('local server listening on 127.0.0.1 port %d' % port)
                 reactor.listenTCP(port, pb.PBServerFactory(kajonggPortal), interface='127.0.0.1')
             else:
-                logInfo('kajonggserver listening on UNIX socket %s' % InternalParameters.socket)
+                logInfo('local server listening on UNIX socket %s' % InternalParameters.socket)
                 reactor.listenUNIX(InternalParameters.socket, pb.PBServerFactory(kajonggPortal))
         else:
+            logInfo('server listening on port %d' % port)
             reactor.listenTCP(port, pb.PBServerFactory(kajonggPortal))
     except error.CannotListenError, errObj:
         logWarning(errObj)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.