| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- from pandac.PandaModules import *
- from direct.task import Task
- from direct.directnotify import DirectNotifyGlobal
- from direct.showbase import DirectObject
- from PyDatagram import PyDatagram
- from PyDatagramIterator import PyDatagramIterator
- import types
- import imp
- class ConnectionRepository(DirectObject.DirectObject, CConnectionRepository):
- """
- This is a base class for things that know how to establish a
- connection (and exchange datagrams) with a gameserver. This
- includes ClientRepository and AIRepository.
- """
-
- notify = DirectNotifyGlobal.directNotify.newCategory("ConnectionRepository")
- taskPriority = -30
- def __init__(self, config):
- DirectObject.DirectObject.__init__(self)
- CConnectionRepository.__init__(self)
- self.setPythonRepository(self)
- self.config = config
-
- # Set this to 'http' to establish a connection to the server
- # using the HTTPClient interface, which ultimately uses the
- # OpenSSL socket library (even though SSL is not involved).
- # This is not as robust a socket library as NSPR's, but the
- # HTTPClient interface does a good job of negotiating the
- # connection over an HTTP proxy if one is in use.
- # Set it to 'nspr' to use Panda's net interface
- # (e.g. QueuedConnectionManager, etc.) to establish the
- # connection, which ultimately uses the NSPR socket library.
- # This is a much better socket library, but it may be more
- # than you need for most applications; and there is no support
- # for proxies.
- # Set it to 'default' to use the HTTPClient interface if a
- # proxy is in place, but the NSPR interface if we don't have a
- # proxy.
- self.connectMethod = self.config.GetString('connect-method', 'default')
- self.connectHttp = None
- self.http = None
- # This DatagramIterator is constructed once, and then re-used
- # each time we read a datagram.
- self.__di = PyDatagramIterator()
- self.recorder = None
- # This is the string that is appended to symbols read from the
- # DC file. The AIRepository will redefine this to 'AI'.
- self.dcSuffix = ''
- def readDCFile(self, dcFileNames = None):
- """ Reads in the dc files listed in dcFileNames, or if
- dcFileNames is None, reads in all of the dc files listed in
- the Configrc file. """
-
- dcFile = self.getDcFile()
- dcFile.clear()
- self.dclassesByName = {}
- self.dclassesByNumber = {}
- self.hashVal = 0
-
- dcImports = {}
- if dcFileNames == None:
- readResult = dcFile.readAll()
- if not readResult:
- self.notify.error("Could not read dc file.")
- else:
- for dcFileName in dcFileNames:
- readResult = dcFile.read(Filename(dcFileName))
- if not readResult:
- self.notify.error("Could not read dc file: %s" % (dcFileName))
- self.hashVal = dcFile.getHash()
- # Now import all of the modules required by the DC file.
- for n in range(dcFile.getNumImportModules()):
- moduleName = dcFile.getImportModule(n)
- # Maybe the module name is represented as "moduleName/AI".
- suffix = moduleName.split('/')
- moduleName = suffix[0]
- if self.dcSuffix and self.dcSuffix in suffix[1:]:
- moduleName += self.dcSuffix
- importSymbols = []
- for i in range(dcFile.getNumImportSymbols(n)):
- symbolName = dcFile.getImportSymbol(n, i)
- # Maybe the symbol name is represented as "symbolName/AI".
- suffix = symbolName.split('/')
- symbolName = suffix[0]
- if self.dcSuffix and self.dcSuffix in suffix[1:]:
- symbolName += self.dcSuffix
- importSymbols.append(symbolName)
-
- self.importModule(dcImports, moduleName, importSymbols)
- # Now get the class definition for the classes named in the DC
- # file.
- for i in range(dcFile.getNumClasses()):
- dclass = dcFile.getClass(i)
- number = dclass.getNumber()
- className = dclass.getName() + self.dcSuffix
- # Does the class have a definition defined in the newly
- # imported namespace?
- classDef = dcImports.get(className)
- if classDef == None:
- self.notify.info("No class definition for %s." % (className))
- else:
- if type(classDef) == types.ModuleType:
- if not hasattr(classDef, className):
- self.notify.error("Module %s does not define class %s." % (className, className))
- classDef = getattr(classDef, className)
-
- if type(classDef) != types.ClassType:
- self.notify.error("Symbol %s is not a class name." % (className))
- else:
- dclass.setClassDef(classDef)
- self.dclassesByName[className] = dclass
- self.dclassesByNumber[number] = dclass
- def importModule(self, dcImports, moduleName, importSymbols):
- """ Imports the indicated moduleName and all of its symbols
- into the current namespace. This more-or-less reimplements
- the Python import command. """
- module = __import__(moduleName, globals(), locals(), importSymbols)
- if importSymbols:
- # "from moduleName import symbolName, symbolName, ..."
- # Copy just the named symbols into the dictionary.
- if importSymbols == ['*']:
- # "from moduleName import *"
- if hasattr(module, "__all__"):
- importSymbols = module.__all__
- else:
- importSymbols = module.__dict__.keys()
-
- for symbolName in importSymbols:
- if hasattr(module, symbolName):
- dcImports[symbolName] = getattr(module, symbolName)
- else:
- raise StandardError, 'Symbol %s not defined in module %s.' % (symbolName, moduleName)
- else:
- # "import moduleName"
- # Copy the root module name into the dictionary.
- # Follow the dotted chain down to the actual module.
- components = moduleName.split('.')
- dcImports[components[0]] = module
- def connect(self, serverList,
- successCallback = None, successArgs = [],
- failureCallback = None, failureArgs = []):
- """
- Attempts to establish a connection to the server. May return
- before the connection is established. The two callbacks
- represent the two functions to call (and their arguments) on
- success or failure, respectively. The failure callback also
- gets one additional parameter, which will be passed in first:
- the return status code giving reason for failure, if it is
- known.
- """
- ## if self.recorder and self.recorder.isPlaying():
- ## # If we have a recorder and it's already in playback mode,
- ## # don't actually attempt to connect to a gameserver since
- ## # we don't need to. Just let it play back the data.
- ## self.notify.info("Not connecting to gameserver; using playback data instead.")
- ## self.connectHttp = 1
- ## self.tcpConn = SocketStreamRecorder()
- ## self.recorder.addRecorder('gameserver', self.tcpConn)
-
- ## self.startReaderPollTask()
- ## if successCallback:
- ## successCallback(*successArgs)
- ## return
- hasProxy = 0
- if self.checkHttp():
- proxies = self.http.getProxiesForUrl(serverList[0])
- hasProxy = (proxies != 'DIRECT')
- if hasProxy:
- self.notify.info("Connecting to gameserver via proxy list: %s" % (proxies))
- else:
- self.notify.info("Connecting to gameserver directly (no proxy).");
- if self.connectMethod == 'http':
- self.connectHttp = 1
- elif self.connectMethod == 'nspr':
- self.connectHttp = 0
- else:
- self.connectHttp = (hasProxy or serverList[0].isSsl())
- self.bootedIndex = None
- self.bootedText = None
- if self.connectHttp:
- # In the HTTP case, we can't just iterate through the list
- # of servers, because each server attempt requires
- # spawning a request and then coming back later to check
- # the success or failure. Instead, we start the ball
- # rolling by calling the connect callback, which will call
- # itself repeatedly until we establish a connection (or
- # run out of servers).
- ch = self.http.makeChannel(0)
- self.httpConnectCallback(ch, serverList, 0,
- successCallback, successArgs,
- failureCallback, failureArgs)
- else:
- # Try each of the servers in turn.
- for url in serverList:
- self.notify.info("Connecting to %s via NSPR interface." % (url.cStr()))
- if self.tryConnectNspr(url):
- self.startReaderPollTask()
- if successCallback:
- successCallback(*successArgs)
- return
- # Failed to connect.
- if failureCallback:
- failureCallback(0, '', *failureArgs)
- def disconnect(self):
- """Closes the previously-established connection.
- """
- self.notify.info("Closing connection to server.")
- CConnectionRepository.disconnect(self)
- self.stopReaderPollTask()
-
- def httpConnectCallback(self, ch, serverList, serverIndex,
- successCallback, successArgs,
- failureCallback, failureArgs):
- if ch.isConnectionReady():
- self.setConnectionHttp(ch)
- ## if self.recorder:
- ## # If we have a recorder, we wrap the connect inside a
- ## # SocketStreamRecorder, which will trap incoming data
- ## # when the recorder is set to record mode. (It will
- ## # also play back data when the recorder is in playback
- ## # mode, but in that case we never get this far in the
- ## # code, since we just create an empty
- ## # SocketStreamRecorder without actually connecting to
- ## # the gameserver.)
- ## stream = SocketStreamRecorder(self.tcpConn, 1)
- ## self.recorder.addRecorder('gameserver', stream)
- ## # In this case, we pass ownership of the original
- ## # connection to the SocketStreamRecorder object.
- ## self.tcpConn.userManagesMemory = 0
- ## self.tcpConn = stream
-
- self.startReaderPollTask()
- if successCallback:
- successCallback(*successArgs)
- elif serverIndex < len(serverList):
- # No connection yet, but keep trying.
-
- url = serverList[serverIndex]
- self.notify.info("Connecting to %s via HTTP interface." % (url.cStr()))
- ch.preserveStatus()
-
- ch.beginConnectTo(DocumentSpec(url))
- ch.spawnTask(name = 'connect-to-server',
- callback = self.httpConnectCallback,
- extraArgs = [ch, serverList, serverIndex + 1,
- successCallback, successArgs,
- failureCallback, failureArgs])
- else:
- # No more servers to try; we have to give up now.
- if failureCallback:
- failureCallback(ch.getStatusCode(), ch.getStatusString(),
- *failureArgs)
- def checkHttp(self):
- # Creates an HTTPClient, if possible, if we don't have one
- # already. This might fail if the OpenSSL library isn't
- # available. Returns the HTTPClient (also self.http), or None
- # if not set.
-
- if self.http == None:
- try:
- self.http = HTTPClient()
- except:
- pass
- return self.http
- def startReaderPollTask(self):
- # Stop any tasks we are running now
- self.stopReaderPollTask()
- taskMgr.add(self.readerPollUntilEmpty, "readerPollTask",
- priority = self.taskPriority)
- def stopReaderPollTask(self):
- taskMgr.remove("readerPollTask")
- def readerPollUntilEmpty(self, task):
- while self.readerPollOnce():
- pass
- return Task.cont
- def readerPollOnce(self):
- if self.checkDatagram():
- self.getDatagramIterator(self.__di)
- self.handleDatagram(self.__di)
- return 1
- # Unable to receive a datagram: did we lose the connection?
- if not self.isConnected():
- self.stopReaderPollTask()
- self.lostConnection()
- return 0
- def lostConnection(self):
- # This should be overrided by a derived class to handle an
- # unexpectedly lost connection to the gameserver.
- self.notify.warning("Lost connection to gameserver.")
- def handleDatagram(self, di):
- # This class is meant to be pure virtual, and any classes that
- # inherit from it need to make their own handleDatagram method
- pass
- def send(self, datagram):
- self.sendDatagram(datagram)
- # debugging funcs for simulating a network-plug-pull
- def pullNetworkPlug(self):
- self.notify.warning('*** SIMULATING A NETWORK-PLUG-PULL ***')
- self.setSimulatedDisconnect(1)
- def networkPlugPulled(self):
- return self.getSimulatedDisconnect()
- def restoreNetworkPlug(self):
- if self.networkPlugPulled():
- self.notify.info('*** RESTORING SIMULATED PULLED-NETWORK-PLUG ***')
- self.setSimulatedDisconnect(0)
- def doFind(self, str):
- """ returns list of distributed objects with matching str in value """
- for value in self.doId2do.values():
- if `value`.find(str) >= 0:
- return value
- def doFindAll(self, str):
- """ returns list of distributed objects with matching str in value """
- matches = []
- for value in self.doId2do.values():
- if `value`.find(str) >= 0:
- matches.append(value)
- return matches
|