-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDT.py
130 lines (100 loc) · 4.53 KB
/
RDT.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
import Network
import argparse
from time import sleep
import hashlib
class Packet:
## the number of bytes used to store packet length
seq_num_S_length = 10
length_S_length = 10
## length of md5 checksum in hex
checksum_length = 32
def __init__(self, seq_num, msg_S):
self.seq_num = seq_num
self.msg_S = msg_S
@classmethod
def from_byte_S(self, byte_S):
if Packet.corrupt(byte_S):
raise RuntimeError('Cannot initialize Packet: byte_S is corrupt')
#extract the fields
seq_num = int(byte_S[Packet.length_S_length : Packet.length_S_length+Packet.seq_num_S_length])
msg_S = byte_S[Packet.length_S_length+Packet.seq_num_S_length+Packet.checksum_length :]
return self(seq_num, msg_S)
def get_byte_S(self):
#convert sequence number of a byte field of seq_num_S_length bytes
seq_num_S = str(self.seq_num).zfill(self.seq_num_S_length)
#convert length to a byte field of length_S_length bytes
length_S = str(self.length_S_length + len(seq_num_S) + self.checksum_length + len(self.msg_S)).zfill(self.length_S_length)
#compute the checksum
checksum = hashlib.md5((length_S+seq_num_S+self.msg_S).encode('utf-8'))
checksum_S = checksum.hexdigest()
#compile into a string
return length_S + seq_num_S + checksum_S + self.msg_S
@staticmethod
def corrupt(byte_S):
#extract the fields
length_S = byte_S[0:Packet.length_S_length]
seq_num_S = byte_S[Packet.length_S_length : Packet.seq_num_S_length+Packet.seq_num_S_length]
checksum_S = byte_S[Packet.seq_num_S_length+Packet.seq_num_S_length : Packet.seq_num_S_length+Packet.length_S_length+Packet.checksum_length]
msg_S = byte_S[Packet.seq_num_S_length+Packet.seq_num_S_length+Packet.checksum_length :]
#compute the checksum locally
checksum = hashlib.md5(str(length_S+seq_num_S+msg_S).encode('utf-8'))
computed_checksum_S = checksum.hexdigest()
#and check if the same
return checksum_S != computed_checksum_S
class RDT:
## latest sequence number used in a packet
seq_num = 1
## buffer of bytes read from network
byte_buffer = ''
def __init__(self, role_S, server_S, port):
self.network = Network.NetworkLayer(role_S, server_S, port)
def disconnect(self):
self.network.disconnect()
def rdt_1_0_send(self, msg_S):
p = Packet(self.seq_num, msg_S)
self.seq_num += 1
self.network.udt_send(p.get_byte_S())
def rdt_1_0_receive(self):
ret_S = None
byte_S = self.network.udt_receive()
self.byte_buffer += byte_S
#keep extracting packets - if reordered, could get more than one
while True:
#check if we have received enough bytes
if(len(self.byte_buffer) < Packet.length_S_length):
return ret_S #not enough bytes to read packet length
#extract length of packet
length = int(self.byte_buffer[:Packet.length_S_length])
if len(self.byte_buffer) < length:
return ret_S #not enough bytes to read the whole packet
#create packet from buffer content and add to return string
p = Packet.from_byte_S(self.byte_buffer[0:length])
ret_S = p.msg_S if (ret_S is None) else ret_S + p.msg_S
#remove the packet bytes from the buffer
self.byte_buffer = self.byte_buffer[length:]
#if this was the last packet, will return on the next iteration
def rdt_2_1_send(self, msg_S):
pass
def rdt_2_1_receive(self):
pass
def rdt_3_0_send(self, msg_S):
pass
def rdt_3_0_receive(self):
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='RDT implementation.')
parser.add_argument('role', help='Role is either client or server.', choices=['client', 'server'])
parser.add_argument('server', help='Server.')
parser.add_argument('port', help='Port.', type=int)
args = parser.parse_args()
rdt = RDT(args.role, args.server, args.port)
if args.role == 'client':
rdt.rdt_1_0_send('MSG_FROM_CLIENT')
sleep(2)
print(rdt.rdt_1_0_receive())
rdt.disconnect()
else:
sleep(1)
print(rdt.rdt_1_0_receive())
rdt.rdt_1_0_send('MSG_FROM_SERVER')
rdt.disconnect()