-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacket.py
128 lines (99 loc) · 3.62 KB
/
Packet.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
# -*- coding: utf-8 -*-
# Víctor A. Hernández & José R. Ortiz
# CCOM 4017 – Operating Systems
# Assignment 4: Packet.py (comes with 7 other files)
# Packet creation support library for the DFS project
import json
class Packet:
def __init__(self):
self.commands = ["reg", "list", "put", "get", "dblks"]
self.packet = {}
def getEncodedPacket(self):
"""
Returns a serialized packet ready to send through the network.
First you need to build the packets. See BuildXPacket functions.
"""
return json.dumps(self.packet)
def getCommand(self):
"""Returns the command type of a packet"""
if self.packet.has_key("command"):
return self.packet["command"]
def getAddr(self):
"""Returns the IP address of the server"""
if self.packet.has_key("addr"):
return self.packet["addr"]
def getPort(self):
"""Returns the port number of the server"""
if self.packet.has_key("port"):
return self.packet["port"]
def DecodePacket(self, packet):
"""Receives a serialized message and turns it into a packet object."""
self.packet = json.loads(packet)
def BuildRegPacket(self, addr, port):
"""Builds a registration packet"""
self.packet = {"command": "reg", "addr": addr, "port": port}
def BuildListPacket(self):
"""Builds a list packet for file listing"""
self.BuildCommand("list")
def BuildListResponse(self, lfiles):
"""Builds a list response packet"""
self.packet = {"files": lfiles}
def getFileArray(self):
"""Returns the files of the packet"""
if self.packet.has_key("files"):
return self.packet["files"]
def BuildGetPacket(self, fname):
"""Build a get packet to get fname."""
self.BuildCommand("get")
self.packet["fname"] = fname
def BuildPutPacket(self, fname, fsize):
"""Builds a put packet to put file name and file size."""
self.BuildCommand("put")
self.packet["fname"] = fname
self.packet["fsize"] = fsize
def BuildDataBlockPacket(self, fname, block_list):
"""Builds a data block packet. Contains the file name and the list of blocks for the file"""
self.BuildCommand("dblks")
self.packet["blocks"] = block_list
self.packet["fname"] = fname
def BuildGetDataBlockPacket(self, blockid):
"""Builds a get data block packet. Useful when requesting a data block to a data node."""
self.BuildCommand("get")
self.packet["blockid"] = blockid
def getBlockID(self):
"""Returns the block_id from a packet."""
return self.packet["blockid"]
def getFileInfo(self):
"""Returns the file info in a packet."""
if self.packet.has_key("fname") and self.packet.has_key("fsize"):
return self.packet["fname"], self.packet["fsize"]
def getFileName(self):
"""Returns the file name in a packet."""
if self.packet.has_key("fname"):
return self.packet["fname"]
def getFileSize(self):
"""Returns the file size in a packet."""
if self.packet.has_key("fsize"):
return self.packet["fsize"]
def BuildGetResponse(self, metalist, fsize):
"""Builds a list of data node servers with the blocks of a file, and file size."""
self.packet["servers"] = metalist
self.packet["fsize"] = fsize
def BuildPutResponse(self, metalist):
"""
Builds a list of data node servers where a file's data blocks can be stored.
(i.e. a list of available data servers).
"""
self.packet["servers"] = metalist
def getDataNodes(self):
"""Returns a list of data servers"""
if self.packet.has_key("servers"):
return self.packet["servers"]
def getDataBlocks(self):
"""Returns a list of data blocks"""
if self.packet.has_key("blocks"):
return self.packet["blocks"]
def BuildCommand(self, cmd):
"""Builds a packet type"""
if cmd in self.commands:
self.packet = {"command": cmd}