def build_space(self): self.forward_rules = {} for rule in self.rules: if not self.forward_rules.has_key(rule['action']['output']): self.forward_rules[rule['action']['output']] = Space() self.forward_rules[rule['action']['output']].plus(Space(match=rule['match'])) self.add_ec([platform.node()], Space(areas=[''.ljust(336, '*')])) log('finish build ec')
def run(self): while True: msg = self.queue.get() if msg['cpid'] == self.cpid: data = msg['data'] if data['type'] == 'add_rules': log('add_rules') self.rules = [] self.ecs = [] self.rules.extend(data['rules']) self.build_space() elif data['type'] == 'verify': log('verify') self.init_ec() elif data['type'] == 'ec': log('ec') self.calc_ec(msg['in_port'], data['route'], data['space']) elif data['type'] == 'update_add_rule': log('update_add_rule') self.update_space(data['rule']) elif data['type'] == 'update_request': log('update_request') for ec in self.ecs: s = Space(areas=data['space']) s.multiply(ec['space']) if len(s.areas) > 0: seq = self.gen_seq() self.received_seqs.append(seq) m = { 'cpid': self.cpid, 'src': platform.node(), 'data': { 'seq': seq, 'type': 'update', 'from': data['from'], 'route': ec['route'], 'space': s.areas } } self.flood(json.dumps(m)) elif data['type'] == 'update': log('update') if data['seq'] in self.received_seqs: continue # 1. flood out self.received_seqs.append(data['seq']) m = { 'cpid': self.cpid, 'src': platform.node(), 'data': data } self.flood(json.dumps(m), msg['in_port']) # 2. do local update self.do_update(data['from'], data['route'], data['space'])
def __init__(self): self.numCalls = 0 self.input_dim = 2 self.bounds = [(-5, 10), (0, 15)] self.fmin = 0.497910 self.min = [-3, 12] self.ismax = -1 self.name = 'DiscreteBranin' self.discreteIdx = [0, 1] self.categoricalIdx = [0, 1] self.spaces = [Space(-5, 10, True), Space(0, 15, True)]
def get_init_ec_space(self): for ec in self.ecs: print ec['route'] if ec['route'][0] == platform.node(): print 'return' return ec['space'] result = Space() for s in self.forward_rules.values(): result.plus(s) return result.notme()
def do_update(self, from_, route, space): for ec in self.ecs: if ec['route'][-1] == from_: ec['route'].extend(route) ec['space'].plus(Space(areas=space)) log('updated') self.dump_ecs()
def calc_ec(self, in_port, route, areas): if not self.forward_rules.has_key(in_port): return space = Space(areas=areas) space.multiply(self.forward_rules[in_port]) if len(space.areas) == 0: return route.insert(0, platform.node()) self.add_ec(route, space) msg = { 'cpid': self.cpid, 'src': platform.node(), 'data': { 'type': 'ec', 'route': route, 'space': space.areas } } if len(space.areas) > 0: self.flood(json.dumps(msg), in_port) self.dump_ecs()
def update_space(self, rule): self.rules.append(rule) self.build_space() space = Space(match=rule['match']) msg = { 'cpid': self.cpid, 'src': platform.node(), 'data': { 'type': 'update_request', 'from': platform.node(), 'space': space.areas } } self.unicast(json.dumps(msg), rule['action']['output'])