-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.py
76 lines (60 loc) · 3.02 KB
/
segment.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
import random
from functools import reduce
# #################################################################################################################### #
# Segment #
# #
# Description: #
# The segment is a segment of data to be transferred on a communication channel. #
# #
# #
# Notes: #
# This file is not to be changed. #
# #
# #
# #################################################################################################################### #
class Segment():
def __init__(self):
self.seqnum = -1
self.acknum = -1
self.payload = ''
self.checksum = 0
self.startIteration = 0
self.startDelayIteration = 0
def setData(self,seq,data):
self.seqnum = seq
self.acknum = -1
self.payload = data
self.checksum = 0
str = self.to_string()
self.checksum = self.calc_checksum(str)
def setAck(self,ack):
self.seqnum = -1
self.acknum = ack
self.payload = ''
self.checksum = 0
str = self.to_string()
self.checksum = self.calc_checksum(str)
def setStartIteration(self,iteration):
self.startIteration = iteration
def getStartIteration(self):
return self.startIteration
def setStartDelayIteration(self,iteration):
self.startDelayIteration = iteration
def getStartDelayIteration(self):
return self.startDelayIteration
def to_string(self):
return "seq: {0}, ack: {1}, data: {2}"\
.format(self.seqnum,self.acknum,self.payload)
def checkChecksum(self):
cs = self.calc_checksum(self.to_string())
return cs == self.checksum
def calc_checksum(self,str):
return reduce(lambda x,y:x+y, map(ord, str))
def printToConsole(self):
print(self.to_string())
# Function to cause an error - Do not modify
def createChecksumError(self):
if not self.payload:
return
char = random.choice(self.payload)
self.payload = self.payload.replace(char, 'X', 1)