-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta-data.py
145 lines (109 loc) · 3.09 KB
/
meta-data.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
###############################################################################
#
# Filename: meta-data.py
# Author: Jose R. Ortiz and ... (hopefully some students contribution)
#
# Description:
# MySQL support library for the DFS project. Database info for the
# metadata server.
#
# Please modify globals with appropiate info.
from mds_db import *
from Packet import *
import sys
import SocketServer
def usage():
print """Usage: python %s <port, default=8000>""" % sys.argv[0]
sys.exit(0)
class MetadataTCPHandler(SocketServer.BaseRequestHandler):
def handle_reg(self, db, p):
"""Register a new client to the DFS ACK if successfully REGISTERED
NAK if problem, DUP if the IP and port already registered
"""
try:
addr,port = p.getAddr(),p.getPort()
# Fill condition:
if p.BuildRegPacket(addr,port):
self.request.sendall("ACK")
else:
self.request.sendall("DUP")
except:
self.request.sendall("NAK")
def handle_list(self, db):
"""Get the file list from the database and send list to client"""
try:
pass
# Fill code here
except:
self.request.sendall("NAK")
def handle_put(self, db, p):
"""Insert new file into the database and send data nodes to save
the file.
"""
# Fill code
if db.InsertFile(info[0], info[1]):
pass
# Fill code
else:
self.request.sendall("DUP")
def handle_get(self, db, p):
"""Check if file is in database and return list of
server nodes that contain the file.
"""
# Fill code to get the file name from packet and then
# get the fsize and array of metadata server
if fsize:
# Fill code
self.request.sendall(p.getEncodedPacket())
else:
self.request.sendall("NFOUND")
def handle_blocks(self, db, p):
"""Add the data blocks to the file inode"""
# Fill code to get file name and blocks from
# packet
# Fill code to add blocks to file inode
def handle(self):
# Establish a connection with the local database
db = mds_db("dfs.db")
db.Connect()
# Define a packet object to decode packet messages
p = Packet()
# Receive a msg from the list, data-node, or copy clients
msg = self.request.recv(1024)
print msg, type(msg)
# Decode the packet received
p.DecodePacket(msg)
# Extract the command part of the received packet
cmd = p.getCommand()
# Invoke the proper action
if cmd == "reg":
# Registration client
self.handle_reg(db, p)
elif cmd == "list":
pass
# Client asking for a list of files
# Fill code
elif cmd == "put":
pass
# Client asking for servers to put data
# Fill code
elif cmd == "get":
pass
# Client asking for servers to get data
# Fill code
elif cmd == "dblks":
pass
# Client sending data blocks for file
# Fill code
db.Close()
if __name__ == "__main__":
HOST, PORT = "", 8000
if len(sys.argv) > 1:
try:
PORT = int(sys.argv[1])
except:
usage()
server = SocketServer.TCPServer((HOST, PORT), MetadataTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()