*** empty log message ***

This commit is contained in:
Mike Goslin 2001-02-16 22:54:17 +00:00
parent afe5c76ebd
commit d20f9783cf

View File

@ -1,281 +1,281 @@
"""ClientRepository module: contains the ClientRepository class""" """ClientRepository module: contains the ClientRepository class"""
from PandaModules import * from PandaModules import *
from TaskManagerGlobal import * from TaskManagerGlobal import *
from MsgTypes import * from MsgTypes import *
from ShowBaseGlobal import * from ShowBaseGlobal import *
import Task import Task
import DirectNotifyGlobal import DirectNotifyGlobal
import ClientDistClass import ClientDistClass
import CRCache import CRCache
import Datagram import Datagram
# The repository must import all known types of Distributed Objects # The repository must import all known types of Distributed Objects
#import DistributedObject #import DistributedObject
#import DistributedToon #import DistributedToon
import DirectObject import DirectObject
class ClientRepository(DirectObject.DirectObject): class ClientRepository(DirectObject.DirectObject):
notify = DirectNotifyGlobal.directNotify.newCategory("ClientRepository") notify = DirectNotifyGlobal.directNotify.newCategory("ClientRepository")
def __init__(self, dcFileName): def __init__(self, dcFileName):
self.number2cdc={} self.number2cdc={}
self.name2cdc={} self.name2cdc={}
self.doId2do={} self.doId2do={}
self.doId2cdc={} self.doId2cdc={}
self.parseDcFile(dcFileName) self.parseDcFile(dcFileName)
self.cache=CRCache.CRCache() self.cache=CRCache.CRCache()
return None return None
def parseDcFile(self, dcFileName): def parseDcFile(self, dcFileName):
self.dcFile = DCFile() self.dcFile = DCFile()
self.dcFile.read(dcFileName) self.dcFile.read(dcFileName)
return self.parseDcClasses(self.dcFile) return self.parseDcClasses(self.dcFile)
def parseDcClasses(self, dcFile): def parseDcClasses(self, dcFile):
numClasses = dcFile.getNumClasses() numClasses = dcFile.getNumClasses()
for i in range(0, numClasses): for i in range(0, numClasses):
# Create a clientDistClass from the dcClass # Create a clientDistClass from the dcClass
dcClass = dcFile.getClass(i) dcClass = dcFile.getClass(i)
clientDistClass = ClientDistClass.ClientDistClass(dcClass) clientDistClass = ClientDistClass.ClientDistClass(dcClass)
# List the cdc in the number and name dictionaries # List the cdc in the number and name dictionaries
self.number2cdc[dcClass.getNumber()]=clientDistClass self.number2cdc[dcClass.getNumber()]=clientDistClass
self.name2cdc[dcClass.getName()]=clientDistClass self.name2cdc[dcClass.getName()]=clientDistClass
return None return None
def connect(self, serverName, serverPort): def connect(self, serverName, serverPort):
self.qcm=QueuedConnectionManager() self.qcm=QueuedConnectionManager()
gameServerTimeoutMs = base.config.GetInt("game-server-timeout-ms", gameServerTimeoutMs = base.config.GetInt("game-server-timeout-ms",
20000) 20000)
# A big old 20 second timeout. # A big old 20 second timeout.
self.tcpConn = self.qcm.openTCPClientConnection( self.tcpConn = self.qcm.openTCPClientConnection(
serverName, serverPort, gameServerTimeoutMs) serverName, serverPort, gameServerTimeoutMs)
# Test for bad connection # Test for bad connection
if self.tcpConn == None: if self.tcpConn == None:
return None return None
else: else:
self.qcr=QueuedConnectionReader(self.qcm, 0) self.qcr=QueuedConnectionReader(self.qcm, 0)
self.qcr.addConnection(self.tcpConn) self.qcr.addConnection(self.tcpConn)
self.cw=ConnectionWriter(self.qcm, 0) self.cw=ConnectionWriter(self.qcm, 0)
self.startReaderPollTask() self.startReaderPollTask()
return self.tcpConn return self.tcpConn
def startReaderPollTask(self): def startReaderPollTask(self):
task = Task.Task(self.readerPollUntilEmpty) task = Task.Task(self.readerPollUntilEmpty)
taskMgr.spawnTaskNamed(task, "readerPollTask") taskMgr.spawnTaskNamed(task, "readerPollTask")
return None return None
def readerPollUntilEmpty(self, task): def readerPollUntilEmpty(self, task):
while self.readerPollOnce(): while self.readerPollOnce():
pass pass
return Task.cont return Task.cont
def readerPollOnce(self): def readerPollOnce(self):
availGetVal = self.qcr.dataAvailable() availGetVal = self.qcr.dataAvailable()
if availGetVal: if availGetVal:
#print "Client: Incoming message!" #print "Client: Incoming message!"
datagram = NetDatagram() datagram = NetDatagram()
readRetVal = self.qcr.getData(datagram) readRetVal = self.qcr.getData(datagram)
if readRetVal: if readRetVal:
self.handleDatagram(datagram) self.handleDatagram(datagram)
else: else:
ClientRepository.notify.warning("getData returned false") ClientRepository.notify.warning("getData returned false")
return availGetVal return availGetVal
def handleDatagram(self, datagram): def handleDatagram(self, datagram):
# This class is meant to be pure virtual, and any classes that # This class is meant to be pure virtual, and any classes that
# inherit from it need to make their own handleDatagram method # inherit from it need to make their own handleDatagram method
pass pass
def handleGenerateWithRequired(self, di): def handleGenerateWithRequired(self, di):
# Get the class Id # Get the class Id
classId = di.getArg(STUint16); classId = di.getArg(STUint16);
# Get the DO Id # Get the DO Id
doId = di.getArg(STUint32) doId = di.getArg(STUint32)
# Look up the cdc # Look up the cdc
cdc = self.number2cdc[classId] cdc = self.number2cdc[classId]
# Create a new distributed object, and put it in the dictionary # Create a new distributed object, and put it in the dictionary
distObj = self.generateWithRequiredFields(cdc, doId, di) distObj = self.generateWithRequiredFields(cdc, doId, di)
# Call "generate" for the dist obj # Call "generate" for the dist obj
distObj.generate() distObj.generate()
return None return None
def handleGenerateWithRequiredOther(self, di): def handleGenerateWithRequiredOther(self, di):
# Get the class Id # Get the class Id
classId = di.getArg(STUint16); classId = di.getArg(STUint16);
# Get the DO Id # Get the DO Id
doId = di.getArg(STUint32) doId = di.getArg(STUint32)
# Look up the cdc # Look up the cdc
cdc = self.number2cdc[classId] cdc = self.number2cdc[classId]
# Create a new distributed object, and put it in the dictionary # Create a new distributed object, and put it in the dictionary
distObj = self.generateWithRequiredOtherFields(cdc, doId, di) distObj = self.generateWithRequiredOtherFields(cdc, doId, di)
# Call "generate" for the distObj # Call "generate" for the distObj
distObj.generate() distObj.generate()
return None return None
def generateWithRequiredFields(self, cdc, doId, di): def generateWithRequiredFields(self, cdc, doId, di):
# Is it in our dictionary? # Is it in our dictionary?
if self.doId2do.has_key(doId): if self.doId2do.has_key(doId):
# If so, just update it. # If so, just update it.
distObj = self.doId2do[doId] distObj = self.doId2do[doId]
distObj.updateRequiredFields(cdc, di) distObj.updateRequiredFields(cdc, di)
# Is it in the cache? If so, pull it out, put it in the dictionaries, # Is it in the cache? If so, pull it out, put it in the dictionaries,
# and update it. # and update it.
elif self.cache.contains(doId): elif self.cache.contains(doId):
# If so, pull it out of the cache... # If so, pull it out of the cache...
distObj = self.cache.retrieve(doId) distObj = self.cache.retrieve(doId)
# put it in both dictionaries... # put it in both dictionaries...
self.doId2do[doId] = distObj self.doId2do[doId] = distObj
self.doId2cdc[doId] = cdc self.doId2cdc[doId] = cdc
# and update it. # and update it.
distObj.updateRequiredFields(cdc, di) distObj.updateRequiredFields(cdc, di)
# If it is not in the dictionary or the cache, then... # If it is not in the dictionary or the cache, then...
else: else:
# Construct a new one # Construct a new one
distObj = cdc.constructor(self) distObj = cdc.constructor(self)
# Assign it an Id # Assign it an Id
distObj.doId = doId distObj.doId = doId
# Update the required fields # Update the required fields
distObj.updateRequiredFields(cdc, di) distObj.updateRequiredFields(cdc, di)
# Put the new do in both dictionaries # Put the new do in both dictionaries
self.doId2do[doId] = distObj self.doId2do[doId] = distObj
self.doId2cdc[doId] = cdc self.doId2cdc[doId] = cdc
return distObj return distObj
def generateWithRequiredOtherFields(self, cdc, doId, di): def generateWithRequiredOtherFields(self, cdc, doId, di):
# Is it in our dictionary? # Is it in our dictionary?
if self.doId2do.has_key(doId): if self.doId2do.has_key(doId):
# If so, just update it. # If so, just update it.
distObj = self.doId2do[doId] distObj = self.doId2do[doId]
distObj.updateRequiredOtherFields(cdc, di) distObj.updateRequiredOtherFields(cdc, di)
# Is it in the cache? If so, pull it out, put it in the dictionaries, # Is it in the cache? If so, pull it out, put it in the dictionaries,
# and update it. # and update it.
elif self.cache.contains(doId): elif self.cache.contains(doId):
# If so, pull it out of the cache... # If so, pull it out of the cache...
distObj = self.cache.retrieve(doId) distObj = self.cache.retrieve(doId)
# put it in both dictionaries... # put it in both dictionaries...
self.doId2do[doId] = distObj self.doId2do[doId] = distObj
self.doId2cdc[doId] = cdc self.doId2cdc[doId] = cdc
# and update it. # and update it.
distObj.updateRequiredOtherFields(cdc, di) distObj.updateRequiredOtherFields(cdc, di)
# If it is not in the dictionary or the cache, then... # If it is not in the dictionary or the cache, then...
else: else:
# Construct a new one # Construct a new one
distObj = cdc.constructor(self) distObj = cdc.constructor(self)
# Assign it an Id # Assign it an Id
distObj.doId = doId distObj.doId = doId
# Update the required fields # Update the required fields
distObj.updateRequiredOtherFields(cdc, di) distObj.updateRequiredOtherFields(cdc, di)
# Put the new do in both dictionaries # Put the new do in both dictionaries
self.doId2do[doId] = distObj self.doId2do[doId] = distObj
self.doId2cdc[doId] = cdc self.doId2cdc[doId] = cdc
return distObj return distObj
def handleDisable(self, di): def handleDisable(self, di):
# Get the DO Id # Get the DO Id
doId = di.getArg(STUint32) doId = di.getArg(STUint32)
# disable it. # disable it.
self.disableDoId(doId) self.disableDoId(doId)
return None return None
def disableDoId(self, doId): def disableDoId(self, doId):
# Make sure the object exists # Make sure the object exists
if self.doId2do.has_key(doId): if self.doId2do.has_key(doId):
# Look up the object # Look up the object
distObj = self.doId2do[doId] distObj = self.doId2do[doId]
# remove the object from both dictionaries # remove the object from both dictionaries
del(self.doId2do[doId]) del(self.doId2do[doId])
del(self.doId2cdc[doId]) del(self.doId2cdc[doId])
assert(len(self.doId2do) == len(self.doId2cdc)) assert(len(self.doId2do) == len(self.doId2cdc))
# cache the object # cache the object
self.cache.cache(distObj) self.cache.cache(distObj)
else: else:
ClientRepository.notify.warning("Disable failed. DistObj " + ClientRepository.notify.warning("Disable failed. DistObj " +
str(doId) + str(doId) +
" is not in dictionary") " is not in dictionary")
return None return None
def handleDelete(self, di): def handleDelete(self, di):
# Get the DO Id # Get the DO Id
doId = di.getArg(STUint32) doId = di.getArg(STUint32)
# If it is in the dictionaries, remove it. # If it is in the dictionaries, remove it.
if self.doId2do.has_key(doId): if self.doId2do.has_key(doId):
obj = self.doId2do[doId] obj = self.doId2do[doId]
# Remove it from the dictionaries # Remove it from the dictionaries
del(self.doId2do[doId]) del(self.doId2do[doId])
del(self.doId2cdc[doId]) del(self.doId2cdc[doId])
# Sanity check the dictionaries # Sanity check the dictionaries
assert(len(self.doId2do) == len(self.doId2cdc)) assert(len(self.doId2do) == len(self.doId2cdc))
# Delete the object itself # Delete the object itself
obj.delete() obj.delete()
# If it is in the cache, remove it. # If it is in the cache, remove it.
elif self.cache.contains(doId): elif self.cache.contains(doId):
self.cache.delete(doId) self.cache.delete(doId)
# Otherwise, ignore it # Otherwise, ignore it
else: else:
ClientRepository.notify.warning( ClientRepository.notify.warning(
"Asked to delete non-existent DistObj " + str(doId)) "Asked to delete non-existent DistObj " + str(doId))
return None return None
def handleUpdateField(self, di): def handleUpdateField(self, di):
# Get the DO Id # Get the DO Id
doId = di.getArg(STUint32) doId = di.getArg(STUint32)
#print("Updating " + str(doId)) #print("Updating " + str(doId))
# Find the DO # Find the DO
assert(self.doId2do.has_key(doId)) assert(self.doId2do.has_key(doId))
do = self.doId2do[doId] do = self.doId2do[doId]
# Find the cdc # Find the cdc
assert(self.doId2cdc.has_key(doId)) assert(self.doId2cdc.has_key(doId))
cdc = self.doId2cdc[doId] cdc = self.doId2cdc[doId]
# Let the cdc finish the job # Let the cdc finish the job
cdc.updateField(do, di) cdc.updateField(do, di)
return None return None
def handleUnexpectedMsgType(self, msgType, di): def handleUnexpectedMsgType(self, msgType, di):
ClientRepository.notify.warning( ClientRepository.notify.warning(
"Ignoring unexpected message type: " + "Ignoring unexpected message type: " +
str(msgType) + str(msgType) +
" in state: " + " in state: " +
self.fsm.getCurrentState().getName()) self.fsm.getCurrentState().getName())
return None return None
def sendSetShardMsg(self, shardId): def sendSetShardMsg(self, shardId):
datagram = Datagram.Datagram() datagram = Datagram.Datagram()
# Add message type # Add message type
datagram.addUint16(CLIENT_SET_SHARD) datagram.addUint16(CLIENT_SET_SHARD)
# Add shard id # Add shard id
datagram.addUint32(shardId) datagram.addUint32(shardId)
# send the message # send the message
self.send(datagram) self.send(datagram)
return None return None
def sendSetZoneMsg(self, zoneId): def sendSetZoneMsg(self, zoneId):
datagram = Datagram.Datagram() datagram = Datagram.Datagram()
# Add message type # Add message type
datagram.addUint16(CLIENT_SET_ZONE) datagram.addUint16(CLIENT_SET_ZONE)
# Add zone id # Add zone id
datagram.addUint16(zoneId) datagram.addUint16(zoneId)
# send the message # send the message
self.send(datagram) self.send(datagram)
return None return None
def sendUpdate(self, do, fieldName, args): def sendUpdate(self, do, fieldName, args):
# Get the DO id # Get the DO id
doId = do.doId doId = do.doId
# Get the cdc # Get the cdc
assert(self.doId2cdc.has_key(doId)) assert(self.doId2cdc.has_key(doId))
cdc = self.doId2cdc[doId] cdc = self.doId2cdc[doId]
# Let the cdc finish the job # Let the cdc finish the job
cdc.sendUpdate(self, do, fieldName, args) cdc.sendUpdate(self, do, fieldName, args)
def send(self, datagram): def send(self, datagram):
self.cw.send(datagram, self.tcpConn) self.cw.send(datagram, self.tcpConn)
return None return None