コード例 #1
0
class ThreadedRunner(Runnable):

    def __init__(self, runnable):
        self._runnable = runnable
        self._notifier = Event()
        self._result = None
        self._error = None
        self._traceback = None
        self._thread = None

    def run(self):
        try:
            self._result = self._runnable()
        except:
            self._error, self._traceback = sys.exc_info()[1:]
        self._notifier.set()

    __call__ = run

    def run_in_thread(self, timeout):
        self._thread = Thread(self, name=TIMEOUT_THREAD_NAME)
        self._thread.setDaemon(True)
        self._thread.start()
        self._notifier.wait(timeout)
        return self._notifier.isSet()

    def get_result(self):
        if self._error:
            raise self._error, None, self._traceback
        return self._result

    def stop_thread(self):
        self._thread.stop()
コード例 #2
0
ファイル: jython.py プロジェクト: Apaking/robotframework
 def execute(self, runnable):
     runner = Runner(runnable)
     thread = Thread(runner, name='RobotFrameworkTimeoutThread')
     thread.setDaemon(True)
     thread.start()
     thread.join(int(self._timeout * 1000))
     if thread.isAlive():
         thread.stop()
         raise TimeoutError(self._error)
     return runner.get_result()
コード例 #3
0
ファイル: jython.py プロジェクト: micken42/robot-demo
 def execute(self, runnable):
     runner = Runner(runnable)
     thread = Thread(runner, name='RobotFrameworkTimeoutThread')
     thread.setDaemon(True)
     thread.start()
     thread.join(int(self._timeout * 1000))
     if thread.isAlive():
         thread.stop()
         raise TimeoutError(self._error)
     return runner.get_result()
コード例 #4
0
class GameBase(Runnable):
    components = GameComponent
    canvas = Canvas
    thread = Thread
    dbg = Graphics
    db_image = BufferedImage
    
    running = False
    is_paused = False
    game_over = False
    is_applet = False
    size_changed = False
    game_time = GameTime()
    updates = 0
    
    NO_DELAYS_PER_YIELD = 16
    MAX_FRAME_SKIPS = 5
    period = None
    game_start_time = None
    frames_skipped = None
    
    gelapsed_before = System.nanoTime()
    gelapsed_after = System.nanoTime()
    
    MAX_STAT_INTERVAL = 1
    NUM_FPS = 10
    stats_interval = 0
    prev_stats_time = 0
    total_elapsed_time = 0
    time_spend_in_game = 0
    frame_count = 0
    fps_store = []
    stats_count = 0
    average_fps = 0
    total_frames_skipped = 0
    ups_store = 0
    average_ups = 0
    df = DecimalFormat("0.##")


    def set_size(self, size):
        self.canvas.setSize(size)
        self.size_changed = True


    def set_resolution(self, size):
        self.set_size(size)


    def get_resolution(self):
        return Dimension(self.db_image.getWidth(), self.db_image.getHeight())


    def get_is_active(self):
        if self.is_paused == True:
            return False
        else:
            return True


    def get_is_applet(self):
        return self.is_applet


    def set_is_applet(self, flag):
        if type(flag) is not type(True):
            raise Exception("Flag passed to set_is_applet not a boolean. Got %s instead." % type(flag))
        self.is_applet = flag


    def get_stats_avg_fps(self):
        return self.average_fps


    def get_stats_avg_ups(self):
        return self.average_ups


    def __init__(self, size, fps):
        self.set_size(size)
        Component.setPreferredSize(size)
        Component.setMinimumSize(size)
        Component.setMaximumSize(size)
        
        Component.setBackground(Color.white)
        
        Component.setFocusable(True)
        Component.requestFocus()
        
        db_image = BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB)
        
        self.fps_store = self.NUM_FPS
        self.ups_store = self.NUM_FPS
        i = 0
        if i < self.NUM_FPS:
            self.fps_store[i] = 0
            self.ups_store[i] = 0
            i += 1


    def run(self):
        before_time = None
        after_time = None
        time_diff = None
        sleep_time = None
        over_sleep_time = 0
        no_delays = 0
        excess = 0
        
        game_start_time = System.nanoTime()
        prev_stats_time = game_start_time
        before_time = game_start_time
        
        running = True
        
        while running:
            self.game_update()
            self.game_render()
            self.paint_screen()
            
            after_time = System.nanoTime()
            time_diff = after_time - before_time
            sleep_time = (self.period - time_diff) - over_sleep_time
            
            if sleep_time > 0:
                try:
                    Thread.sleep(sleep_time)
                except InterruptedException as e:
                    pass
                over_sleep_time = (System.nanoTime() - after_time) - sleep_time
            else:
                excess -= sleep_time
                over_sleep_time = 0
                
                if (no_delays + 1) >= self.NO_DELAYS_PER_YIELD:
                    Thread.yield()
                    no_delays = 0
            
            before_time = System.nanoTime()
            
            skips = 0
            
            while excess > self.period and skips < self.MAX_FRAME_SKIPS:
                excess -= self.period
                self.game_update()
                skips += 1
            
            self.frames_skipped += skips
            
            self.store_stats()
        
        self.print_stats()
        System.exit(0)


    def store_stats(self):
        self.frame_count += 1
        self.stats_interval += self.period
        
        if self.stats_interval >= self.MAX_STAT_INTERVAL:
            time_now = System.nanoTime()
            self.time_spend_in_game = time_now - self.game_start_time
            
            real_elapsed_time = time_now - self.prev_stats_time
            self.total_elapsed_time += real_elapsed_time
            
            self.total_frames_skipped += self.frames_skipped
            
            if self.total_elapsed_time > 0:
                actual_fps = self.frame_count / self.total_elapsed_time
                actual_ups = (self.frame_count + self.total_frames_skipped) / self.total_elapsed_time
            
            self.fps_store[self.stats_count % self.NUM_FPS] = actual_fps
            self.ups_store[self.stats_count % self.NUM_FPS] = actual_ups
            self.stats_count += 1
            
            i = 0
            if i < self.NUM_FPS:
                total_fps = self.fps_store[i]
                total_ups = self.ups_store[i]
                i += 1
            
            if self.stats_count < self.NUM_FPS:
                self.average_fps = total_fps / self.stats_count
                self.average_ups = total_ups / self.stats_count
            else:
                self.average_fps = total_fps / self.NUM_FPS
                self.average_ups = total_ups / self.NUM_FPS
            
            self.frames_skipped = 0
            self.prev_stats_time = time_now
            self.stats_interval = 0


    def game_update(self):
        self.gelapsed_after = System.nanoTime()
        
        self.game_time.elapsed_game_time.set_span(self.gelapsed_before, self.gelapsed_after)
        self.game_time.elapsed_real_time.set_span(self.gelapsed_before, self.gelapsed_after)
        
        self.game_time.total_game_time.set_span(self.game_start_time, self.gelapsed_after)
        self.game_time.total_real_time.set_span(self.game_start_time, self.gelapsed_after)
        
        self.gelapsed_before = System.nanoTime()
        
        if self.running and self.is_paused is not True and self.game_over is not True:
            for item in self.components.getComponents():
                item.update(self.game_time)
            self.updates += 1


    def get_render(self):
        if self.db_image is None or self.size_changed:
            size = Dimension(Component.getWidth(), Component.getHeight())
            self.set_size(size)
            Component.setPreferredSize(size)
            Component.setMinimumSize(size)
            Component.setMaximumSize(size)
            self.size_changed = False
            
            try:
                self.db_image = BufferedImage(Component.getWidth(), Component.getHeight(), BufferedImage.TYPE_INT_RGB)
            except JavaException as e:
                self.db_image = None
                System.out.println("Render Error: %s" % e)
                System.out.println("Render Error: Buffer not initialized properly")
                System.out.println("Render Error: Resolving...")


    def paint_screen(self):
        bs = Canvas.getBufferStrategy()
        if bs is None:
            Canvas.createBufferStrategy(3)
        
        g = bs.getDrawGraphics()
        g.drawImage(self.db_image, 0, 0, Component.getWidth(), Component.getHeight(), None)
        g.dispose()
        bs.show()


    def start_game(self):
        if self.running:
            return
        self.thread = Thread(self)
        self.thread.start()
        self.running = True


    def stop_game(self):
        if self.running is None:
            return
        self.thread.stop()
        self.running = False


    def set_preferred_fps(self, fps):
        self.period = 1000/fps


    def reset_elapsed_time(self):
        self.game_time.elapsed_game_time.set_span(0)
        self.game_time.elapsed_real_time.set_span(0)


    def add_component(self, game_component):
        game_component.initialize()
        game_component.load_content()
        self.components.add(game_component)


    def remove_component(self, game_component):
        game_component.unload_content()
        self.components.remove(game_component)


    def resume_game(self):
        self.is_paused = False


    def pause_game(self):
        self.is_paused = True


    def print_stats(self):
        System.out.println("Frame Count/Loss: %s / %s" % (self.frame_count, self.total_frames_skipped))
        System.out.println("Average FPS: %s" % self.df.format(self.average_fps))
        System.out.println("Average UPS: %s" % self.df.format(self.average_ups))
        System.out.println("TIme Spend: %s secs" % self.time_spend_in_game)
        System.out.println("Total Updates: %s" % self.updates)
        System.out.println("dbImage: %s" % self.db_image.toString())
コード例 #5
0
ファイル: vpn.py プロジェクト: cygmris/enos
class VPNService(Container, MultiPointVPNService):
    def __init__(self):
        Resource.__init__(self, "MultiPointVPNService")
        print "MultiPoint VPN Service is starting"
        self.vpnIndex = {}
        self.vpnIndexById = {}
        self.topology = None
        self.coretopology = None
        self.lock = threading.Lock()
        self.properties['mat'] = True  # Default.
        self.loadService()
        self.saveThread = Thread(target=self.autosave)
        self.saveThread.start()
        if self.topology != None:
            self.setscc()

    def setscc(self):
        self.SCC = SdnControllerClient()
        self.sccThread = JavaThread(self.SCC)
        self.sccThread.start()
        self.VPNcallback = VpnCallback("MP-VPN Service", self)
        setcallback(self.VPNcallback)
        self.SCC.setCallback(self.VPNcallback)

    def settopos(self, popstoponame, coretoponame):
        self.properties['topology'] = popstoponame
        self.topology = Container.getContainer(self.properties['topology'])
        self.properties['coretopology'] = coretoponame
        self.coretopology = Container.getContainer(
            self.properties['coretopology'])
        # We can now set up the callback
        self.setscc()
        self.saveService()

    def shutdown(self):
        self.saveThread.stop()
        if self.sccThread != None:
            self.sccThread.stop()
            self.SCC.clearCallback()
        MultiPointVPNServiceFactory.delete()

    def autosave(self):
        while True:
            if not self.topology == None:
                self.saveService()
                for (x, vpn) in self.vpnIndex.items():
                    vpn.saveVPN()
            time.sleep(60)

    def saveService(self):
        self.properties['sid'] = self.sid
        if self.topology != None:
            self.properties['topology'] = self.topology.getResourceName()
        if self.coretopology != None:
            self.properties[
                'coretopology'] = self.coretopology.getResourceName()
        try:
            self.save()
        except:
            print "Failed to save VPN Service\n", sys.exc_info()[0]

    def loadService(self):
        stored = Container.getContainer(self.getResourceName())
        mapResource(obj=self, resource=stored)
        if 'topology' in self.properties:
            self.topology = Container.getContainer(self.properties['topology'])
        if 'coretopology' in self.properties:
            self.coretopology = Container.getContainer(
                self.properties['coretopology'])
        vpns = self.loadResources({"resourceType": "VPN"})
        for v in vpns:
            vpn = VPN(v.getResourceName())
            vpn.loadVPN(self)
            self.vpnIndex[v.getResourceName()] = vpn
            self.vpnIndexById[vpn.vid] = vpn
        if not 'mat' in self.properties:
            self.properties['mat'] = True

    def newVid(self):
        with self.lock:
            while True:
                v = random.randint(1, 65535)
                if not v in self.vpnIndexById:
                    return v

    def getSite(self, s):
        global sites
        if s in sites:
            return sites[s]
        else:
            return None

    def getHost(self, s):
        return tbns[s]

    def addVpn(self, vpn):
        self.vpnIndex[vpn.name] = vpn
        self.vpnIndexById[vpn.vid] = vpn
        self.saveResource(vpn)

    def createVPN(self, vpnname):
        if vpnname in self.vpnIndex:
            return None
        vpn = VPN(vpnname, vs=self)
        self.vid = self.newVid()
        self.addVpn(vpn)
        return vpn

    def deleteVPN(self, vpnname):
        if not vpnname in self.vpnIndex:
            print "vpn name %s not found" % vpnname
            return
        vpn = self.vpnIndex[vpnname]
        for h in vpn.hostsites.keys():
            self.delhostbymac(vpn, h)
        for s in vpn.vpnsites.values():
            self.delsite(vpn, s)
        for p in vpn.pops.values():
            self.delpop(vpn, p)
        self.vpnIndex.pop(vpn.name)
        self.vpnIndexById.pop(vpn.vid)
        self.deleteResource(vpn)
        print "VPN %s removed successfully." % (vpn.name)
        return True

    def getVPN(self, vpnname):
        if not vpnname in self.vpnIndex:
            return None
        return self.vpnIndex[vpnname]

    def addPOP(self, vpn, pop):
        if not vpn.addpop(pop):
            return False
        return True

    def deletePOP(self, vpn, pop):
        if not vpn.delpop(pop):
            return False
        return True

    def addSite(self, vpn, site, vlan):
        if not vpn.addsite(site, vlan):
            # possible issues: duplicated site
            return False
        return True

    def deleteSite(self, vpn, site):
        if not vpn.delsite(site):
            return False
        return True

    def addhostbymac(self, vpn, site, mac):
        if not vpn.addhostbymac(site, mac):
            print "Error while adding host."
            return
        print "Host %s has been added into VPN %s successfully at site %s" % (
            mac, vpn.name, site['name'])

    def delhostbymac(self, vpn, mac):
        if not vpn.delhostbymac(mac):
            print "Error while deleting host."
            return
コード例 #6
0
ファイル: vpn.py プロジェクト: esnet/enos
class VPNService(Container,MultiPointVPNService):
    def __init__(self):
        Resource.__init__(self,"MultiPointVPNService")
        print "MultiPoint VPN Service is starting"
        self.vpnIndex = {}
        self.vpnIndexById = {}
        self.topology = None
        self.coretopology = None
        self.lock = threading.Lock()
        self.properties['mat'] = True # Default.
        self.loadService()
        self.saveThread = Thread(target=self.autosave)
        self.saveThread.start()
        if self.topology != None:
            self.setscc()

    def setscc(self):
        self.SCC = SdnControllerClient()
        self.sccThread = JavaThread(self.SCC)
        self.sccThread.start()
        self.VPNcallback = VpnCallback("MP-VPN Service", self)
        setcallback(self.VPNcallback)
        self.SCC.setCallback(self.VPNcallback)

    def settopos(self, popstoponame, coretoponame):
        self.properties['topology'] = popstoponame
        self.topology = Container.getContainer(self.properties['topology'])
        self.properties['coretopology'] = coretoponame
        self.coretopology = Container.getContainer(self.properties['coretopology'])
        # We can now set up the callback
        self.setscc()
        self.saveService()

    def shutdown(self):
        self.saveThread.stop()
        if self.sccThread != None:
            self.sccThread.stop()
            self.SCC.clearCallback()
        MultiPointVPNServiceFactory.delete()

    def autosave(self):
        while True:
            if not self.topology == None:
                self.saveService()
                for (x,vpn) in self.vpnIndex.items():
                    vpn.saveVPN()
            time.sleep(60)

    def saveService(self):
        self.properties['sid'] = self.sid
        if self.topology != None:
            self.properties['topology'] = self.topology.getResourceName()
        if self.coretopology != None:
            self.properties['coretopology'] = self.coretopology.getResourceName()
        try:
            self.save()
        except:
            print "Failed to save VPN Service\n", sys.exc_info()[0]

    def loadService(self):
        stored = Container.getContainer(self.getResourceName())
        mapResource (obj=self,resource=stored)
        if 'topology' in self.properties:
            self.topology = Container.getContainer(self.properties['topology'])
        if 'coretopology' in self.properties:
            self.coretopology = Container.getContainer(self.properties['coretopology'])
        vpns = self.loadResources({"resourceType":"VPN"})
        for v in vpns:
            vpn = VPN(v.getResourceName())
            vpn.loadVPN(self)
            self.vpnIndex[v.getResourceName()] = vpn
            self.vpnIndexById[vpn.vid] = vpn
        if not 'mat' in self.properties:
            self.properties['mat'] = True

    def newVid(self):
        with self.lock:
            while True:
                v = random.randint(1,65535)
                if not v in self.vpnIndexById:
                    return v

    def getSite(self,s):
        global sites
        if s in sites:
            return sites[s]
        else:
            return None

    def getHost(self,s):
        return tbns[s]

    def addVpn(self,vpn):
        self.vpnIndex[vpn.name] = vpn
        self.vpnIndexById[vpn.vid] = vpn
        self.saveResource(vpn)

    def createVPN(self,vpnname):
        if vpnname in self.vpnIndex:
            return None
        vpn = VPN(vpnname,vs=self)
        self.vid = self.newVid()
        self.addVpn(vpn)
        return vpn

    def deleteVPN(self,vpnname):
        if not vpnname in self.vpnIndex:
            print "vpn name %s not found" % vpnname
            return
        vpn = self.vpnIndex[vpnname]
        for h in vpn.hostsites.keys():
            self.delhostbymac(vpn, h)
        for s in vpn.vpnsites.values():
            self.delsite(vpn, s)
        for p in vpn.pops.values():
            self.delpop(vpn, p)
        self.vpnIndex.pop(vpn.name)
        self.vpnIndexById.pop(vpn.vid)
        self.deleteResource(vpn)
        print "VPN %s removed successfully." % (vpn.name)
        return True

    def getVPN(self,vpnname):
        if not vpnname in self.vpnIndex:
            return None
        return self.vpnIndex[vpnname]

    def addPOP(self,vpn, pop):
        if not vpn.addpop(pop):
            return False
        return True

    def deletePOP(self,vpn, pop):
        if not vpn.delpop(pop):
            return False
        return True

    def addSite(self, vpn, site, vlan):
        if not vpn.addsite(site, vlan):
            # possible issues: duplicated site
            return False
        return True

    def deleteSite(self, vpn, site):
        if not vpn.delsite(site):
            return False
        return True

    def addhostbymac(self, vpn, site, mac):
        if not vpn.addhostbymac(site, mac):
            print "Error while adding host."
            return
        print "Host %s has been added into VPN %s successfully at site %s" % (mac, vpn.name, site['name'])

    def delhostbymac(self,vpn, mac):
        if not vpn.delhostbymac(mac):
            print "Error while deleting host."
            return