def __init__(self, initial_level, screen_dims): self.level = initial_level self.screen_dims = screen_dims self.block_manager = BlockManager(initial_level=self.level, screen_dims=screen_dims) self.paddle = Paddle() self.ball = Ball(starting_y=self.block_manager.get_ball_start_y()) self.human_scoreboard = ScoreBoard(player="Human", screen_dims=screen_dims) self.computer_scoreboard = ScoreBoard(player="Computer", screen_dims=screen_dims) self.level_label = LevelLabel(initial_level=self.level, screen_dims=screen_dims)
def main(argv): #Load configuration print '\nLoading Configuration' CONFIG = load_configuration() #Load database print '\nLoading Database' DATABASE = Database(CONFIG) #Load encryption key print '\nLoading encryption key' CRYPTO = Crypto(CONFIG, DATABASE) #Create RPC manager print '\nCreating RPC manager' RPC_MANAGER = RPCManager(CONFIG, DATABASE, CRYPTO) #Create server print '\nCreating server' SERVER = Server(CONFIG, DATABASE, CRYPTO) #Create block manager print '\nCreating block manager' BLOCK_MANAGER = BlockManager(CONFIG, DATABASE, CRYPTO, RPC_MANAGER, SERVER) SERVER.set_blocks_manager(BLOCK_MANAGER) #Connect to nodes print '\nConnecting to network' RPC_MANAGER.connect(BLOCK_MANAGER) while True: try: time.sleep(1) os.system('clear') print 'Ready for Interruption' cmd = raw_input() if cmd in [ 'shutdown', 'SHUTDOWN', '^C', '^Z', 'exit', 'EXIT', 'close', 'CLOSE' ]: break except KeyboardInterrupt: break print '\nShutdown signal received, stopping everything' SERVER.shutdown() RPC_MANAGER.shutdown() BLOCK_MANAGER.shutdown() print 'All was correctly stopped, exiting' sys.exit(0)
def main(argv): #load configuration try: configuration_file = open('config.json','r') CONFIG = json.loads(configuration_file.read()) check_configuration(CONFIG) configuration_file.close() print 'Configuration loaded.' except IOError: print 'missing configuration file' sys.exit(2) except ValueError: print 'configuration file is corrupt' sys.exit(2) print Globals.RESOURCES_RESOURCE_CODE #Load database print '\nLoading Database' DATABASE = Database(CONFIG) #Load encryption key print '\nLoading encryption key' CRYPTO = Crypto(CONFIG, DATABASE) #Create network manager print '\nCreating network manager' NETWORK_MANAGER = NetworkManager(CONFIG, DATABASE, CRYPTO) #Create server print '\nCreating server' SERVER = Server(CONFIG, CRYPTO) #Create block manager print '\nCreating block manager' BLOCK_MANAGER = BlockManager(CONFIG, DATABASE, CRYPTO, NETWORK_MANAGER, SERVER) SERVER.set_blocks_manager(BLOCK_MANAGER) #Connect to nodes print '\nConnecting to network' NETWORK_MANAGER.connect_to_all(BLOCK_MANAGER) print '\nStartup complete, waiting for synchronization' while True: try: cmd = raw_input() if cmd in ['shutdown', 'SHUTDOWN', '^C', '^Z', 'exit', 'EXIT', 'close', 'CLOSE']: break except KeyboardInterrupt: break print 'Shutdown signal received, stopping everything' SERVER.shutdown() NETWORK_MANAGER.shutdown() print 'All was correctly stopped, exiting' sys.exit(0)
def do_SparkListenerBlockManagerAdded(self, data): bm = BlockManager(data) self.block_managers.append(bm)
if params['input'] is not None: inFile = open(params['input'], 'r') except: print( f"Cannot access {params['input']}. Standard input will be used insted." ) try: if params['output'] is not None: outFile = open(params['output'], 'w', newline='\n') except: print( f"Cannot open {params['output']}. Standard output will be used insted." ) lexer = CompilerLexer() parser = CompilerParser() tokens = lexer.tokenize(inFile.read()) try: (tree, memory) = parser.parse(tokens) except TypeError: exit(1) except Exception as error: print(error) exit(1) bm = BlockManager(tree) printer = Printer(bm, memory) outFile.write(printer.print()) inFile.close() outFile.close()
def main(argv): #args: cache_size total (in MB) cache_size = int(argv[0]) block_manager = BlockManager() cluster = Cluster(block_manager) simulator = Simulator(cluster, user_number) cur_dir = os.getcwd() ## get current working directory summary_lru = open("%s/summary_lru.txt" % cur_dir, "w") summary_lrc = open("%s/summary_lrc.txt" % cur_dir, "w") summary_lrc_con = open("%s/summary_lrc_con.txt" % cur_dir, "w") summary_lrc_agg = open("%s/summary_lrc_agg.txt" % cur_dir, "w") summary_memtune = open("%s/summary_memtune.txt" % cur_dir, "w") for filename in os.listdir("job-profile"): #for repeat in range(1,50): job_name = filename.split('.')[0] Simulator.jobs[0] = job_name summary = open( "%s/summary.txt" % cur_dir, "a+") # log for the cache performance of different cache policies summary.write("Job %s\n" % (job_name)) summary.write("%s MB memory\n" % (cache_size)) simulator.cluster.block_manager.set_policy(LRUPolicy(cache_size)) [ total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss ] = simulator.run() print total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss summary.write( "%s: %s\t%s\t%s\n" % (simulator.cluster.block_manager.policy.name, float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) summary_lru.write( "%s\t%s\t%s\n" % (float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) #simulator.reset() simulator.cluster.block_manager.set_policy(LRCPolicy(cache_size)) [ total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss ] = simulator.run() print total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss summary.write( "%s: %s\t%s\t%s\n" % (simulator.cluster.block_manager.policy.name, float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) summary_lrc.write( "%s\t%s\t%s\n" % (float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) #simulator.reset() simulator.cluster.block_manager.set_policy( LRCConservativePolicy(cache_size)) [ total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss ] = simulator.run() print total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss summary.write( "%s: %s\t%s\t%s\n" % (simulator.cluster.block_manager.policy.name, float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) summary_lrc_con.write( "%s\t%s\t%s\n" % (float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) #simulator.reset() simulator.cluster.block_manager.set_policy( LRCAggressivePolicy(cache_size)) [ total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss ] = simulator.run() print total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss summary.write( "%s: %s\t%s\t%s\n" % (simulator.cluster.block_manager.policy.name, float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) summary_lrc_agg.write( "%s\t%s\t%s\n" % (float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) #simulator.reset() simulator.cluster.block_manager.set_policy(MemTune(cache_size)) [ total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss ] = simulator.run() print total_hit, total_miss, total_task_hit, total_task_miss, total_stage_hit, total_stage_miss summary.write( "%s: %s\t%s\t%s\n" % (simulator.cluster.block_manager.policy.name, float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss))) summary_memtune.write( "%s\t%s\t%s\n" % (float(total_hit) / (total_hit + total_miss), float(total_task_hit) / (total_task_hit + total_task_miss), float(total_stage_hit) / (total_stage_hit + total_stage_miss)))
class BreakoutGame: def __init__(self, initial_level, screen_dims): self.level = initial_level self.screen_dims = screen_dims self.block_manager = BlockManager(initial_level=self.level, screen_dims=screen_dims) self.paddle = Paddle() self.ball = Ball(starting_y=self.block_manager.get_ball_start_y()) self.human_scoreboard = ScoreBoard(player="Human", screen_dims=screen_dims) self.computer_scoreboard = ScoreBoard(player="Computer", screen_dims=screen_dims) self.level_label = LevelLabel(initial_level=self.level, screen_dims=screen_dims) def reset_game(self): self.ball.reset_game() self.paddle.reset_game() def play_game(self): """move the ball, check for collisions against edges and objects""" blnCollision = False # Check if there are any blocks left | Increment level if all destroyed if len(self.block_manager.blocks) == 0: self.level += 1 self.level_label.increment_level() self.block_manager.add_blocks(lvl=self.level) # Check if collided with gutter | Computer wins elif self.ball.location[1][0] <= self.screen_dims[1][0]: blnCollision = True self.computer_scoreboard.increment_score( ) # Computer player gets a point self.reset_game() # Reset Game # Check if collided with wall (Left/Right) | Invert Ball elif self.ball.location[0][ 0] - self.ball.move_increment < self.screen_dims[0][ 0] or self.ball.location[0][ 1] + self.ball.move_increment > self.screen_dims[0][1]: blnCollision = True self.ball.invert_ball(blnReverse=True) self.ball.move() # Check if collided with wall (Top) | Invert Ball & Change Direction elif self.ball.location[1][ 1] + self.ball.move_increment > self.screen_dims[1][1]: blnCollision = True self.ball.change_direction() self.ball.invert_ball(blnReverse=False) self.ball.move() # Check if collided with paddle | Invert Ball & Change Direction elif Intersect(self.ball.location, self.paddle.location): blnCollision = True self.ball.change_direction() self.ball.invert_ball(blnReverse=False) self.ball.move() # Check if collided with block | Human wins else: blocks_to_remove = [] for key in self.block_manager.blocks: block_x = self.block_manager.blocks[key] if Intersect(self.ball.location, block_x.location): blnCollision = True block_x.destroy_block() # Hide Block blocks_to_remove.append(key) # Mark for removal # Invert Ball self.ball.change_direction() self.ball.invert_ball(blnReverse=False) self.ball.move() # Human player gets a point self.human_scoreboard.increment_score() break # Remove block from block manager for key in blocks_to_remove: self.block_manager.blocks.pop(key) # Otherwise just move the ball if not blnCollision: self.ball.move() return blnCollision