Пример #1
0
    def doIteration(self, t):
        # Do the same as in mainLoop() first.
        self.runUntilCurrent()
        t2 = self.timeout()

        if t2 is not None:
            t = min(t, self.running and t2)

        SelectReactor.doIteration(self, t)
        self.loopCall()
Пример #2
0
    def doIteration(self, t):
        # Do the same as in mainLoop() first.
        self.runUntilCurrent()
        t2 = self.timeout()

        if t2 is not None:
            t = min(t, self.running and t2)

        SelectReactor.doIteration(self, t)
        self.loopCall()
class ReactorSlaveController(object):
    def __init__(self):
        self.keepGoing = True
        self.reactor = SelectReactor()
        installReactor(self.reactor)
        connection = self.reactor.connectTCP('localhost', 8000, factory)
        self.reactor.startRunning()
        self.futureCall = None
        self.futureCallTimeout = None
        pygame_test.prepare()
        
    def iterate(self):
        print 'in iterate'
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)
        #t2 = self.reactor.timeout()
        #print 'timeout', t2
        #t = self.reactor.running and t2
        #self.reactor.doIteration(t)

    def run(self):
	clock = pygame.time.Clock()
        self.reactor.callLater(20, stupidTest)
        while self.keepGoing:
            timeChange = clock.tick(FRAMES_PER_SECOND)
            if self.futureCall:
                self.futureCallTimeout -= timeChange
                print 'future call in', self.futureCallTimeout
                if self.futureCallTimeout <= 0:
                    self.futureCall()
                    self.futureCallTimeout = None
                    self.futureCall= None
            retval = pygame_test.iterate()
            if retval == False:
                thingInControl.stop()
            self.iterate()

    def stop(self):
        print 'stopping'
        self.reactor.stop()
        self.keepGoing = False

    def callLater(self, when, fn):
        self.futureCallTimeout = when*1000
        self.futureCall = fn
        print 'future call in', self.futureCallTimeout
Пример #4
0
    def doIteration(self, delay):
        """
        Perform a single iteration of the reactor. Here we make sure that
        all of our file descriptors that we need to watch are and then delegate
        the actual watching back to the twisted reactor.
        """

        self.callTimeouts(delay)
        return SelectReactor.doIteration(self, delay)
Пример #5
0
    def doIteration(self, delay):
        """
        Perform a single iteration of the reactor. Here we make sure that
        all of our file descriptors that we need to watch are and then delegate
        the actual watching back to the twisted reactor.
        """

        self.callTimeouts(delay)
        return SelectReactor.doIteration(self, delay)
Пример #6
0
class NetworkServerView(pb.Root):
    """We SEND events to the server through this object"""
    STATE_PREPARING = 0
    STATE_CONNECTING = 1
    STATE_CONNECTED = 2
    STATE_DISCONNECTING = 3
    STATE_DISCONNECTED = 4

    #----------------------------------------------------------------------
    def __init__(self, evManager, sharedObjectRegistry):
        self.evManager = evManager
        self.evManager.RegisterListener( self )

        self.pbClientFactory = pb.PBClientFactory()
        self.state = NetworkServerView.STATE_PREPARING
        self.reactor = None
        self.server = None

        self.sharedObjs = sharedObjectRegistry

    #----------------------------------------------------------------------
    def AttemptConnection(self):
        print "attempting a connection to", serverHost, serverPort
        self.state = NetworkServerView.STATE_CONNECTING
        if self.reactor:
            self.reactor.stop()
            self.PumpReactor()
        else:
            self.reactor = SelectReactor()
            installReactor(self.reactor)
        connection = self.reactor.connectTCP(serverHost, serverPort,
                                             self.pbClientFactory)
        # TODO: make this anonymous login()
        #deferred = self.pbClientFactory.login(credentials.Anonymous())
        userCred = credentials.UsernamePassword(avatarID, 'pass1')
        controller = NetworkServerController( self.evManager )
        deferred = self.pbClientFactory.login(userCred, client=controller)
        deferred.addCallback(self.Connected)
        deferred.addErrback(self.ConnectFailed)
        self.reactor.startRunning()

    #----------------------------------------------------------------------
    def Disconnect(self):
        print "disconnecting"
        if not self.reactor:
            return
        print 'stopping the reactor'
        self.reactor.stop()
        self.PumpReactor()
        self.state = NetworkServerView.STATE_DISCONNECTING

    #----------------------------------------------------------------------
    def Connected(self, server):
        print "CONNECTED"
        self.server = server
        self.state = NetworkServerView.STATE_CONNECTED
        ev = ServerConnectEvent( server )
        self.evManager.Post( ev )

    #----------------------------------------------------------------------
    def ConnectFailed(self, server):
        print "CONNECTION FAILED"
        print server
        print 'quitting'
        self.evManager.Post( QuitEvent() )
        #self.state = NetworkServerView.STATE_PREPARING
        self.state = NetworkServerView.STATE_DISCONNECTED

    #----------------------------------------------------------------------
    def PumpReactor(self):
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)

    #----------------------------------------------------------------------
    def Notify(self, event):
        NSV = NetworkServerView
        if isinstance( event, TickEvent ):
            if self.state == NSV.STATE_PREPARING:
                self.AttemptConnection()
            elif self.state in [NSV.STATE_CONNECTED,
                                NSV.STATE_DISCONNECTING,
                                NSV.STATE_CONNECTING]:
                self.PumpReactor()
            return

        if isinstance( event, QuitEvent ):
            self.Disconnect()
            return

        ev = event
        if not isinstance( event, pb.Copyable ):
            evName = event.__class__.__name__
            copyableClsName = "Copyable"+evName
            if not hasattr( network, copyableClsName ):
                return
            copyableClass = getattr( network, copyableClsName )
            #NOTE, never even construct an instance of an event that
            # is serverToClient, as a side effect is often adding a
            # key to the registry with the local id().
            if copyableClass not in network.clientToServerEvents:
                return
            #print 'creating instance of copyable class', copyableClsName
            ev = copyableClass( event, self.sharedObjs )

        if ev.__class__ not in network.clientToServerEvents:
            #print "CLIENT NOT SENDING: " +str(ev)
            return

        if self.server:
            print " ====   Client sending", str(ev)
            remoteCall = self.server.callRemote("EventOverNetwork", ev)
        else:
            print " =--= Cannot send while disconnected:", str(ev)
Пример #7
0
class NetworkServerView(pb.Root):
    """We SEND events to the server through this object"""
    STATE_PREPARING = 0
    STATE_CONNECTING = 1
    STATE_CONNECTED = 2

    def __init__(self, evm, shared_obj_registry):
        self.evm = evm
        self.evm.register(self)

        self.pbClientFactory = pb.PBClientFactory()
        
        self.reactor = None
        self.server = None

        self.shared_objs = shared_obj_registry
        self.state = NetworkServerView.STATE_PREPARING
 
    def connected(self, server):
        print 'Connection to %s %s successful' %SERVERHOST, SERVERPORT

        self.state = NetworkServerView.STATE_CONNECTED
        
        self.server = server
        self.evm.post(ev.ServerConnected(server))
        
    def connection_failed(self, error):
        print "Connection failed with error ", error

    def attempt_connection(self):
        print "Attempting connection to", SERVERHOST, SERVERPORT
        
        self.state = NetworkServerView.STATE_CONNECTING
        
        if self.reactor:
            self.reactor.stop()
            self.pump_reactor()
        else:
            self.reactor = SelectReactor()
            installReactor(self.reactor)
        
        connection = self.reactor.connectTCP(SERVERHOST, SERVERPORT,
                                             self.pbClientFactory)
        deferred = self.pbClientFactory.getRootObject()        
        deferred.addCallback(self.connected)
        deferred.addErrback(self.connection_failed)
        self.reactor.run()
        
    def pump_reactor(self):
        self.reactor.runUntilCurrent()
        self.reactor.doIteration(0)

        
    def disconnect(self):
        print "disconnecting"

    def notify(self, evt):
        if evt.id == 'Tick':
            if self.state == NetworkServerView.STATE_PREPARING:
                self.attempt_connection()
            elif self.state in [NetworkServerView.STATE_CONNECTING]:
                self.pump_reactor()
Пример #8
0
class NetworkServerView(pb.Root):
	"""We SEND events to the server through this object"""
	STATE_PREPARING = 0
	STATE_CONNECTING = 1
	STATE_CONNECTED = 2
	STATE_DISCONNECTING = 3
	STATE_DISCONNECTED = 4

	#----------------------------------------------------------------------
	def __init__(self, evManager, sharedObjectRegistry):
		self.evManager = evManager
		self.evManager.RegisterListener( self )

		self.pbClientFactory = pb.PBClientFactory()
		self.state = NetworkServerView.STATE_PREPARING
		self.reactor = None
		self.server = None

		self.sharedObjs = sharedObjectRegistry

	#----------------------------------------------------------------------
	def AttemptConnection(self):
		print "attempting a connection to", serverHost, serverPort
		self.state = NetworkServerView.STATE_CONNECTING
		if self.reactor:
			self.reactor.stop()
			self.PumpReactor()
		else:
			self.reactor = SelectReactor()
			installReactor(self.reactor)
		connection = self.reactor.connectTCP(serverHost, serverPort,
		                                     self.pbClientFactory)
		deferred = self.pbClientFactory.getRootObject()
		deferred.addCallback(self.Connected)
		deferred.addErrback(self.ConnectFailed)
		self.reactor.startRunning()

	#----------------------------------------------------------------------
	def Disconnect(self):
		print "disconnecting"
		if not self.reactor:
			return
		print 'stopping the reactor'
		self.reactor.stop()
                self.PumpReactor()
		self.state = NetworkServerView.STATE_DISCONNECTING

	#----------------------------------------------------------------------
	def Connected(self, server):
		print "CONNECTED"
		self.server = server
		self.state = NetworkServerView.STATE_CONNECTED
		ev = ServerConnectEvent( server )
		self.evManager.Post( ev )

	#----------------------------------------------------------------------
	def ConnectFailed(self, server):
		print "CONNECTION FAILED"
		#self.state = NetworkServerView.STATE_PREPARING
		self.state = NetworkServerView.STATE_DISCONNECTED

	#----------------------------------------------------------------------
	def PumpReactor(self):
		self.reactor.runUntilCurrent()
		self.reactor.doIteration(0)

	#----------------------------------------------------------------------
	def Notify(self, event):
		NSV = NetworkServerView
		if isinstance( event, TickEvent ):
			if self.state == NSV.STATE_PREPARING:
				self.AttemptConnection()
			elif self.state in [NSV.STATE_CONNECTED,
			                    NSV.STATE_DISCONNECTING,
			                    NSV.STATE_CONNECTING]:
				self.PumpReactor()
			return

		if isinstance( event, QuitEvent ):
			self.Disconnect()
			return

		ev = event
		if not isinstance( event, pb.Copyable ):
			evName = event.__class__.__name__
			copyableClsName = "Copyable"+evName
			if not hasattr( network, copyableClsName ):
				return
			copyableClass = getattr( network, copyableClsName )
			ev = copyableClass( event, self.sharedObjs )

		if ev.__class__ not in network.clientToServerEvents:
			#print "CLIENT NOT SENDING: " +str(ev)
			return
			
		if self.server:
			print " ====   Client sending", str(ev)
			remoteCall = self.server.callRemote("EventOverNetwork",
			                                    ev)
		else:
			print " =--= Cannot send while disconnected:", str(ev)
Пример #9
0
class NetworkServerView(pb.Root):
    """We SEND events to the server through this object"""
    STATE_PREPARING = 0
    STATE_CONNECTING = 1
    STATE_CONNECTED = 2
    STATE_DISCONNECTING = 3
    STATE_DISCONNECTED = 4

    #----------------------------------------------------------------------
    def __init__(self, evManager, sharedObjectRegistry):
            self.evManager = evManager
            self.evManager.RegisterListener( self )

            self.pbClientFactory = pb.PBClientFactory()
            self.state = NetworkServerView.STATE_PREPARING
            self.reactor = None
            self.server = None

            self.sharedObjs = sharedObjectRegistry

    #----------------------------------------------------------------------
    def AttemptConnection(self):
            print "attempting a connection to", serverHost, serverPort
            self.state = NetworkServerView.STATE_CONNECTING
            if self.reactor:
                    self.reactor.stop()
                    self.PumpReactor()
            else:
                    self.reactor = SelectReactor()
                    installReactor(self.reactor)
            connection = self.reactor.connectTCP(serverHost, serverPort,
                                                 self.pbClientFactory)
            deferred = self.pbClientFactory.getRootObject()
            deferred.addCallback(self.Connected)
            deferred.addErrback(self.ConnectFailed)
            self.reactor.startRunning()

    #----------------------------------------------------------------------
    def Disconnect(self):
            print "disconnecting"
            if not self.reactor:
                    return
            print 'stopping the reactor'
            self.reactor.stop()
            self.PumpReactor()
            self.state = NetworkServerView.STATE_DISCONNECTING

    #----------------------------------------------------------------------
    def Connected(self, server):
            print "CONNECTED"
            self.server = server
            self.state = NetworkServerView.STATE_CONNECTED
            ev = ServerConnectEvent( server )
            self.evManager.Post( ev )

    #----------------------------------------------------------------------
    def ConnectFailed(self, server):
            print "CONNECTION FAILED"
            #self.state = NetworkServerView.STATE_PREPARING
            self.state = NetworkServerView.STATE_DISCONNECTED

    #----------------------------------------------------------------------
    def PumpReactor(self):
            self.reactor.runUntilCurrent()
            self.reactor.doIteration(0)

    #----------------------------------------------------------------------
    def Notify(self, event):
            NSV = NetworkServerView
            if isinstance( event, TickEvent ):
                    if self.state == NSV.STATE_PREPARING:
                            self.AttemptConnection()
                    elif self.state in [NSV.STATE_CONNECTED,
                                        NSV.STATE_DISCONNECTING,
                                        NSV.STATE_CONNECTING]:
                            self.PumpReactor()
                    return

            if isinstance( event, QuitEvent ):
                    self.Disconnect()
                    return

            ev = event
            if not isinstance( event, pb.Copyable ):
                    evName = event.__class__.__name__
                    copyableClsName = "Copyable"+evName
                    if not hasattr( network, copyableClsName ):
                            return
                    copyableClass = getattr( network, copyableClsName )
                    #NOTE, never even construct an instance of an event that
                    # is serverToClient, as a side effect is often adding a
                    # key to the registry with the local id().
                    if copyableClass not in network.clientToServerEvents:
                        return
                    print 'creating instance of copyable class', copyableClsName
                    ev = copyableClass( event, self.sharedObjs )

            if ev.__class__ not in network.clientToServerEvents:
                    #print "CLIENT NOT SENDING: " +str(ev)
                    return
                    
            if self.server:
                    print " ====   Client sending", str(ev)
                    remoteCall = self.server.callRemote("EventOverNetwork", ev)
            else:
                    print " =--= Cannot send while disconnected:", str(ev)
 def doIteration(self, delay):
     print 'calling doIteration'
     SelectReactor.doIteration(self,delay)
     retval = pygame_test.iterate()
     if retval == False:
         thingInControl.stop()