-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_namenode.py
69 lines (63 loc) · 3.13 KB
/
run_namenode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import socket
import json
from common import load_config
from namenoode import FileSystem
normal_message = {
"success": True,
"message": "ack"
}
normal_message_bytes = bytes(json.dumps(normal_message).encode('utf-8'))
if __name__ == "__main__":
# socket.setdefaulttimeout(20)
config = load_config("config/namenode.json")
client_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ""
port = config["client_comm_port"]
client_server_socket.bind((host, port))
client_server_socket.listen(config["max_client_num"])
file_system = FileSystem(config=config["file_system_config"])
while True:
client, addr = client_server_socket.accept()
print("client address: {}".format(addr))
# recv command from client
client_command = client.recv(4096).decode('utf-8')
client_command = json.loads(client_command, encoding='utf-8')
if client_command["command"] == "test_list":
file_system.test_out()
client.send(normal_message_bytes)
if client_command["command"] == "list":
res = file_system.list_path(client_command["args"]["path"])
client.send(bytes(json.dumps(res).encode('utf-8')))
elif client_command["command"] == "update_block_status":
file_system.update_block(client_command["args"]["fid"], client_command["args"]["block_info"])
client.send(normal_message_bytes)
elif client_command["command"] == "update_file_status":
file_system.update_file(client_command["args"]["fid"], client_command["args"]["file_status"])
client.send(normal_message_bytes)
elif client_command["command"] == "file_status":
fid = client_command["args"].get("fid", None)
path = client_command["args"].get("path", None)
status = file_system.file_status(fid, path)
client.send(bytes(json.dumps(status).encode('utf-8')))
elif client_command["command"] == "new_block":
blk_info = file_system.request_new_block(client_command["args"]["fid"])
client.send(bytes(json.dumps(blk_info).encode('utf-8')))
elif client_command["command"] == "create":
path = client_command["args"].get("path", "")
name = client_command["args"].get("name", None)
res = file_system.create(path, name)
client.send(bytes(json.dumps(res).encode('utf-8')))
elif client_command["command"] == "delete":
fid = client_command["args"].get("fid", None)
path = client_command["args"].get("path", None)
res = file_system.delete(fid, path)
client.send(bytes(json.dumps(res).encode('utf-8')))
elif client_command["command"] == "mkdir":
path = client_command["args"].get("path", "")
name = client_command["args"].get("name", None)
res = file_system.mkdir(path, name)
client.send(bytes(json.dumps(res).encode('utf-8')))
else:
res = {"success": False, "message": "invalid command"}
client.send(bytes(json.dumps(res).encode('utf-8')))
client.close()