def _waitForConnection(self): """ Handles waiting for WAMP connection before continuing loading program. """ if self.connection is None: prettyConsole.console('log', "Waiting for connection...") #Wait and test again self.retryWaits += 1 if self.retryWaits > 20: #wait about 10 seconds for connection. Then try reconnecting self.retryWaits = 0 self.reconnecting = False #prettyConsole.console('log', "aaaaaa") prettyConsole.console( 'log', 'Will attempt reconnect in %d seconds.' % (1 + 2**self.retryAttempts)) reactor.callLater(1 + 2**self.retryAttempts, self.reconnect) return reactor.callLater(0.5, self._waitForConnection) else: try: a = self.connection[0] except: prettyConsole.console('log', "Still waiting for connection...") ##Wait and test again self.retryWaits += 1 if self.retryWaits > 20: #wait about 20seconds for connection. Then try reconnecting self.retryWaits = 0 self.reconnecting = False #prettyConsole.console('log', "bbbb") prettyConsole.console( 'log', 'Will attempt reconnect in %d seconds.' % (1 + 2**self.retryAttempts)) reactor.callLater(1 + 2**self.retryAttempts, self.reconnect) return reactor.callLater(0.5, self._waitForConnection) else: prettyConsole.console('log', "Connected...") self.connected = True self.reconnecting = False self.retryWaits = 0 self.retryAttempts = 0 reactor.callLater(0.1, self._registerRPC) reactor.callLater(0.2, self._subscribeCommands) reactor.callLater(0.3, self._subscribeHeartbeats) ### Initialize reactor loops reactor.callLater(self.screenDelay, self._loopScreen) reactor.callLater(self.filenameDelay, self._loopFilename) reactor.callLater(self.sizeDelay, self._loopTileSize) reactor.callLater(self.sizeDelay, self._loopScreenSize) reactor.callLater(self.heartbeatDelay, self._loopHeartbeat) if self.fps: reactor.callLater(5, self._loopPrintFps)
def _subscribeCommands(self): """ Subscribes to incomming commands. """ try: d = yield self.connection[0].subscribe(self.controlWindow.receiveCommand, '%s.commands' % self.topicPrefix) self.subscriptions['commands'] = d except: prettyConsole.console('log', 'Command sub error')
def _receiveHeartbeats(self, recv): """ Tracks heartbeats from clients. Ignore 'recv'. """ #On hearbeat, reset counter. self.heartbeatCounter = 120 if self.slowed: prettyConsole.console('log', "Viewer connected. Resuming...") self.slowed = False
def _loopPrintFps(self): """ Print number of screen grabs per second. """ prettyConsole.console('update', "FPS: %0.1f" % (self.fps_counter/5.0)) self.fps_counter = 0 if self.fps: self.defereds['fps'] = reactor.callLater(5, self._loopPrintFps)
def _subscribeHeartbeats(self): """ Subscribes to incomming heartbeats. """ try: d = yield self.connection[0].subscribe(self._receiveHeartbeats, '%s.heartbeats' % self.topicPrefix) self.subscriptions['heartbeats'] = d except: prettyConsole.console('log', 'Heartbeat sub error')
def _registerRPC(self): """ Registers function for remote procedure calls. """ try: d = yield self.connection[0].register(self.tileset.wampSend, '%s.tilesetimage' % self.topicPrefix) self.rpcs['tileset'] = d except Exception as inst: prettyConsole.console('log', inst) reactor.callLater(1, self.reconnect)
def _loopPrintFps(self): """ Print number of screen grabs per second. """ prettyConsole.console('update', "FPS: %0.1f" % (self.fps_counter / 5.0)) self.fps_counter = 0 if self.fps: self.defereds['fps'] = reactor.callLater(5, self._loopPrintFps)
def _subscribeHeartbeats(self): """ Subscribes to incomming heartbeats. """ try: d = yield self.connection[0].subscribe( self._receiveHeartbeats, '%s.heartbeats' % self.topicPrefix) self.subscriptions['heartbeats'] = d except: prettyConsole.console('log', 'Heartbeat sub error')
def _subscribeCommands(self): """ Subscribes to incomming commands. """ try: d = yield self.connection[0].subscribe( self.controlWindow.receiveCommand, '%s.commands' % self.topicPrefix) self.subscriptions['commands'] = d except: prettyConsole.console('log', 'Command sub error')
def _registerRPC(self): """ Registers function for remote procedure calls. """ try: d = yield self.connection[0].register( self.tileset.wampSend, '%s.tilesetimage' % self.topicPrefix) self.rpcs['tileset'] = d except Exception as inst: prettyConsole.console('log', inst) reactor.callLater(1, self.reconnect)
def reconnect(self): """ Handles reconnecting to WAMP server. """ if self.reconnecting: #don't try to reconnect if it has already been tried prettyConsole.console('log', "Already reconnecting...") return self.reconnecting = True self.retryAttempts += 1 prettyConsole.console('log', "Reconnecting to server...") #Cancel pending callbacks for k, v in self.defereds.iteritems(): if v.active(): v.cancel() self.defereds.clear() #Try to cancel RPC and subscriptions for text, sub in self.subscriptions.iteritems(): try: sub.unsubscribe() except: prettyConsole.console('log', "Unable to unsubscribe to: %s" % text) for text, rpc in self.rpcs.iteritems(): try: rpc.unregister() except: prettyConsole.console('log', "Unable to unsubscribe to: %s" % text) #Reset back to original state self.connected = False try: self.connection[0].leave() except: prettyConsole.console('log', "Unable to cleanly close connection.") pass self.connection = None self.subscriptions.clear() self.rpcs.clear() #Restart connection self.connection = wamp_local.wampClient( "ws://router1.dfeverywhere.com:7081/ws", "tcp:router1.dfeverywhere.com:7081", self.web_topic, self.web_key) #self.connection[0].connect() reactor.callLater(0.5, self._waitForConnection)
def _waitForConnection(self): """ Handles waiting for WAMP connection before continuing loading program. """ if self.connection is None: prettyConsole.console('log', "Waiting for connection...") #Wait and test again self.retryWaits += 1 if self.retryWaits > 20: #wait about 10 seconds for connection. Then try reconnecting self.retryWaits = 0 self.reconnecting = False #prettyConsole.console('log', "aaaaaa") prettyConsole.console('log', 'Will attempt reconnect in %d seconds.' % (1 + 2 ** self.retryAttempts)) reactor.callLater(1 + 2 ** self.retryAttempts, self.reconnect) return reactor.callLater(0.5, self._waitForConnection) else: try: a = self.connection[0] except: prettyConsole.console('log', "Still waiting for connection...") ##Wait and test again self.retryWaits += 1 if self.retryWaits > 20: #wait about 20seconds for connection. Then try reconnecting self.retryWaits = 0 self.reconnecting = False #prettyConsole.console('log', "bbbb") prettyConsole.console('log', 'Will attempt reconnect in %d seconds.' % (1 + 2 ** self.retryAttempts)) reactor.callLater(1 + 2 ** self.retryAttempts, self.reconnect) return reactor.callLater(0.5, self._waitForConnection) else: prettyConsole.console('log', "Connected...") self.connected = True self.reconnecting = False self.retryWaits = 0 self.retryAttempts = 0 reactor.callLater(0.1, self._registerRPC) reactor.callLater(0.2, self._subscribeCommands) reactor.callLater(0.3, self._subscribeHeartbeats) ### Initialize reactor loops reactor.callLater(self.screenDelay, self._loopScreen) reactor.callLater(self.filenameDelay, self._loopFilename) reactor.callLater(self.sizeDelay, self._loopTileSize) reactor.callLater(self.sizeDelay, self._loopScreenSize) reactor.callLater(self.heartbeatDelay, self._loopHeartbeat) if self.fps: reactor.callLater(5, self._loopPrintFps)
def reconnect(self): """ Handles reconnecting to WAMP server. """ if self.reconnecting: #don't try to reconnect if it has already been tried prettyConsole.console('log', "Already reconnecting...") return self.reconnecting = True self.retryAttempts += 1 prettyConsole.console('log', "Reconnecting to server...") #Cancel pending callbacks for k, v in self.defereds.iteritems(): if v.active(): v.cancel() self.defereds.clear() #Try to cancel RPC and subscriptions for text, sub in self.subscriptions.iteritems(): try: sub.unsubscribe() except: prettyConsole.console('log', "Unable to unsubscribe to: %s" % text) for text, rpc in self.rpcs.iteritems(): try: rpc.unregister() except: prettyConsole.console('log', "Unable to unsubscribe to: %s" % text) #Reset back to original state self.connected = False try: self.connection[0].leave() except: prettyConsole.console('log', "Unable to cleanly close connection.") pass self.connection = None self.subscriptions.clear() self.rpcs.clear() #Restart connection self.connection = wamp_local.wampClient("ws://router1.dfeverywhere.com:7081/ws", "tcp:router1.dfeverywhere.com:7081", self.web_topic, self.web_key) #self.connection[0].connect() reactor.callLater(0.5, self._waitForConnection)
def _loopHeartbeat(self): """ Handles periodically decreasing heartbeat timer. """ if self.heartbeatCounter > 0: self.heartbeatCounter -= 1 if self.heartbeatCounter < 1: if not self.slowed: prettyConsole.console('log', "No viewers connected, slowing...") self.slowed = True else: self.slowed = False self.defereds['heartbeat'] = reactor.callLater(self.heartbeatDelay, self._loopHeartbeat)
def _loopScreen(self): """ Handles periodically running screen grabs. """ try: shot = self.shotFunction(self.window_hnd, debug=False) #Need to check that an image was returned. shot_x, shot_y = shot.size except: print("Error getting image. Exiting.") #reactor.stop() self.stopClean() return trimmedShot = utils.trim(shot, debug=False) if trimmedShot is not None: #Only send a full tile map every 20 cycles, otherwise just send changes #Is this needed anymore? Javascript expects full maps all the time. #This is slower with deferToThread if self.sendFullMaps or (self.screenCycles) % 20 == 0: tileMap = self.tileset.parseImageArray(trimmedShot, returnFullMap=True) #tileMap = yield threads.deferToThread(self.tileset.parseImageArray, trimmedShot, returnFullMap = True) else: tileMap = self.tileset.parseImageArray(trimmedShot, returnFullMap=False) #tileMap = yield threads.deferToThread(self.tileset.parseImageArray, trimmedShot, returnFullMap = False) else: #If there was an error getting the tilemap, fake one. prettyConsole.console('log', "Error reading game window.") tileMap = [] self._sendTileMap(tileMap) self.screenCycles += 1 if self.fps: self.fps_counter += 1 if self.slowed: self.defereds['screen'] = reactor.callLater( self.screenDelaySlowed, self._loopScreen) else: self.defereds['screen'] = reactor.callLater( self.screenDelay, self._loopScreen)
def _loopScreen(self): """ Handles periodically running screen grabs. """ try: shot = self.shotFunction(self.window_hnd, debug = False) #Need to check that an image was returned. shot_x, shot_y = shot.size except: print("Error getting image. Exiting.") #reactor.stop() self.stopClean() return trimmedShot = utils.trim(shot, debug = False) if trimmedShot is not None: #Only send a full tile map every 20 cycles, otherwise just send changes #Is this needed anymore? Javascript expects full maps all the time. #This is slower with deferToThread if self.sendFullMaps or (self.screenCycles) % 20 == 0: tileMap = self.tileset.parseImageArray(trimmedShot, returnFullMap = True) #tileMap = yield threads.deferToThread(self.tileset.parseImageArray, trimmedShot, returnFullMap = True) else: tileMap = self.tileset.parseImageArray(trimmedShot, returnFullMap = False) #tileMap = yield threads.deferToThread(self.tileset.parseImageArray, trimmedShot, returnFullMap = False) else: #If there was an error getting the tilemap, fake one. prettyConsole.console('log', "Error reading game window.") tileMap = [] self._sendTileMap(tileMap) self.screenCycles += 1 if self.fps: self.fps_counter += 1 if self.slowed: self.defereds['screen'] = reactor.callLater(self.screenDelaySlowed, self._loopScreen) else: self.defereds['screen'] = reactor.callLater(self.screenDelay, self._loopScreen)
import time import util.prettyConsole prettyConsole.console('log', 'Test1') time.sleep(1) prettyConsole.console('log', 'Test2') time.sleep(1) prettyConsole.console('log', 'Test3') time.sleep(1) prettyConsole.console('log', 'Test4') time.sleep(1) prettyConsole.console('log', 'Test5') time.sleep(1) prettyConsole.console('update', 'Tested') time.sleep(1) prettyConsole.console('update', 'Tested1') time.sleep(1) prettyConsole.console('update', 'Tested2') time.sleep(1) prettyConsole.console('update', 'Tested3') time.sleep(1) prettyConsole.console('log', 'Test6') time.sleep(1) prettyConsole.console('log', 'Test7') time.sleep(1) prettyConsole.console('log', 'Test8') time.sleep(1)