12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import json
- from node import Node
- from network_api import NetworkAPI
- from multiprocessing import Process
- import random
- import time
- import os
- def create_node(meta):
- node = Node(meta)
- node.run()
- if __name__ == "__main__":
- names = os.listdir(r'data\raft_front_end\public\log')
- content = {}
- for name in names:
- path = 'data/raft_front_end/public/log/' + name
- os.remove(path)
- num = input("[-]请输入节点数量:")
- ip = "localhost"
- base_port = 10000
- client_port = 9999
- nodes_meta = []
- network_api = NetworkAPI((ip, client_port))
- for i in range(int(num)):
- node_meta = {
- "id": str(i),
- "addr": ("localhost", base_port + i),
- "peers": {str(j): ("localhost", base_port + j) for j in range(int(num)) if j != i},
- }
- nodes_meta.append(node_meta)
- # create_node(node_meta)
- p = Process(target=create_node, args=(node_meta,), daemon=True)
- p.start()
- address = random.choice(nodes_meta)["addr"]
- # while True:
- # print('233ok')
- # time.sleep(30)
- time.sleep(10)
- while True:
- message = input("[-]请输入要记录的日志:(输入exit退出)")
- if message == 'exit':
- break
- # message = '233ok'
- data = {
- "type": "client_append_entries",
- "timestamp": int(time.time()),
- "message": message,
- }
- print("send: ", data)
- network_api.send(data, address)
- # time.sleep(5)
- names = os.listdir(r'data\raft_front_end\public\log')
- content = {
- 'log': []
- }
- for name in names:
- if name.endswith('log.json'):
- path = 'data/raft_front_end/public/log/' + name
- with open(path, 'r') as f:
- data = json.load(f)
- content['log'].append(data)
- with open('data/raft_front_end/public/log/log.json', 'w') as f:
- json.dump(content, f, indent=4)
|