forked from eybisi/theFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.py
109 lines (88 loc) · 3.53 KB
/
nameserver.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import rpyc
import uuid
import threading
import math
import random
import ConfigParser
import signal
import pickle
import sys
import os
import socket
from pprint import pprint
from rpyc.utils.server import ThreadedServer
def int_handler(signal, frame):
pickle.dump((NameServerService.exposed_NameServer.file_table,
NameServerService.exposed_NameServer.block_mapping),
open('fs.img', 'wb'))
sys.exit(0)
def set_conf():
conf = ConfigParser.ConfigParser()
conf.readfp(open('dfs.conf'))
NameServerService.exposed_NameServer.block_size = int(conf.get('nameServer', 'block_size'))
NameServerService.exposed_NameServer.replication_factor = int(conf.get('nameServer', 'replication_factor'))
storageList = conf.get('nameServer', 'storageList').split(',')
for s in storageList:
id, addr, port = s.split(":")
NameServerService.exposed_NameServer.storageList[id]=(addr, port)
if os.path.isfile('fs.img'):
NameServerService.exposed_NameServer.file_table,
NameServerService.exposed_NameServer.block_mapping = pickle.load(open('fs.img', 'rb'))
class NameServerService(rpyc.Service):
def on_connect(self,conn):
print("[+] Got connection from {}".format(conn._channel.stream.sock.getpeername()))
def on_disconnect(self,conn):
print("[+] Client disconnected {}".format(conn._channel.stream.sock.getpeername()))
class exposed_NameServer():
file_table = {}
block_mapping = {}
storageList = {}
block_size = 0
replication_factor = 0
def exposed_get(self, fname):
mapping = self.file_table[fname]
print("[+] Get {} called".format(fname))
return mapping
def exposed_put(self, fname, size):
print("[+] Put {} called".format(fname))
if self.exists(fname):
pass
self.file_table[fname] = []
num_blocks = self.calc_num_blocks(size)
blocks = self.alloc_blocks(fname, num_blocks)
return blocks
def exposed_delete_file_entry(self, fname):
del self.file_table[fname]
print("[+] File deleted {} ".format(fname))
return
def exposed_get_file_table_entry(self, fname):
if fname in self.file_table:
print("[+] Get file {}".format(fname))
return self.file_table[fname]
else:
print("[-] File is not in table {}".format(fname))
return None
def exposed_get_file_table(self):
return self.file_table
def exposed_get_block_size(self):
return self.block_size
def exposed_get_storage_list(self):
return self.storageList
def calc_num_blocks(self, size):
return int(math.ceil(float(size)/self.block_size))
def exists(self, fname):
return fname in self.file_table
def alloc_blocks(self, dest, num):
blocks = []
for i in range(0, num):
block_uuid = uuid.uuid1()
nodes_ids = random.sample(self.storageList.keys(), self.replication_factor)
blocks.append((block_uuid, nodes_ids))
self.file_table[dest].append((block_uuid, nodes_ids))
print("[+] {} Blocks allocated".format(num))
return blocks
if __name__ == "__main__":
set_conf()
signal.signal(signal.SIGINT, int_handler)
t = ThreadedServer(NameServerService, port = 5353)
t.start()