def test_core_key_update(self): k = Key() CoreKeyTests.assert_key_initance(self, k) key0 = k.key k.update() CoreKeyTests.assert_key_initance(self, k) self.assertNotEqual(k.key, key0) key_rand = os.urandom(Key.KEY_OCTETS) k.update(key_rand) CoreKeyTests.assert_key_initance(self, k) self.assertEqual(k.key, key_rand)
class Node(node.Node): ATTRS = ('id', 'office_addr', 'key', 'status') @classmethod def make_node_assets(cls): byebye_nodes = threading.Event() start_up_orig = make_start_up_orig() _queue_darkness = queue.Queue() d = { 'byebye_nodes': byebye_nodes, 'start_up_orig': start_up_orig, '_queue_darkness': _queue_darkness, } return d def __init__(self, **kwargs): '''\ simulator 用 node を初期化する。 ''' self.office_addr = (kwargs.get('host', None), kwargs.get('port', None)) super().__init__(*self.office_addr) for attr, value in kwargs.items(): if attr in ('host', 'port'): continue setattr(self, attr, value) self.key = Key() self.status = 'active' self.nodes = [] self.im_ready = threading.Event() self.office_door = threading.Lock() self.office_addr_assigned = threading.Event() self.master_hand_path = get_master_hand_path(SIMULATION_DIR, self.start_up_orig) def run(self): self._open_office() et = self.get_elapsed_time() d_attrs = self.get_attrs() self.put_on_darkness(d_attrs, et) self._steal_master_palm() self.regist() logger.info(f"byebye_nodes.wait()") self.byebye_nodes.wait() logger.info(f"release_clients()") self.release_clients() # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! logger.info(f"node_udp_office.shutdown()") self.node_udp_office.shutdown() # close node office !!!!!!!!!!!!!!!!!! self.node_open_office.join() # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! logger.info(f"byebye_nodes leave !") def _open_office(self): logger.info(f"") node_open_office = NodeOpenOffice(self) self.node_open_office = node_open_office logger.info(f"{self}.open_office(), node_open_office.start()") node_open_office.start() # wait a minute # to set node_open_office instance # to node instance # in NodeOpenOffice.run() logger.info(f"node_open_office.wait()") node_open_office.in_serve_forever.wait() self.office_addr_assigned.set() logger.info(f"node_open_office={node_open_office}") # node.office_addr が決定している。 return node_open_office def get_info(self): return f"{':'.join(map(str, self.office_addr))}:{self.key}" + '\n' def regist(self): logger.info(f"regist(), master_hand_path={self.master_hand_path}") os.makedirs(os.path.dirname(self.master_hand_path), exist_ok=True) with open(self.master_hand_path, 'a') as master: print(self.get_info(), end='', file=master) # msg = 'I am Node.' # recver_addr = ('localhost', 222) # self.sock.sendto(msg, recver_addr) # recved_msg, who = self.sock.recvfrom(1024) self.im_ready.set() def _steal_master_palm(self): if not os.path.isfile(self.master_hand_path): logger.info( f"not found 'master_hand_path={self.master_hand_path}'") return None logger.info(f"found 'master_hand_path={self.master_hand_path}'") with open(self.master_hand_path) as master_palm: node_lines = master_palm.read() return node_lines def _force_shutdown(self): self.disappear() def set_attrs(self, d={}): attrs = d for attr, value in attrs.items(): setattr(self, attr, value) def get_attrs(self, keys=()): if not keys: keys = self.ATTRS d = {} for key in keys: d[key] = getattr(self, key) return d def put_on_darkness(self, obj, et): pds = pickle.dumps(obj) tup = (et, pds) self._queue_darkness.put(tup) def get_elapsed_time(self): et = elapsed_time(self.start_up_orig) return et def appear(self): super().appear() def disappear(self): super().disappear() def release_clients(self): logger.info(f"{self} clients={self.node_udp_office.clients}") for node_office_client in self.node_udp_office.clients: node_office_client.byebye() def __str__(self): return 'Node(id={}, addr={})'.format(self.id, self.udp_ip) def update(self, k=b''): self.key.update(k)