-
Notifications
You must be signed in to change notification settings - Fork 1
/
dLayer.py
105 lines (71 loc) · 2.04 KB
/
dLayer.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
from pLayer import physicalLayer
ob=physicalLayer()
def xor(a, b):
res = ""
for i in range(1, len(b)):
if a[i] == b[i]:
res+='0'
else:
res+='1'
return res
def mod2div(bitsequence, divisor):
pick = len(divisor)
tmp = bitsequence[0: pick]
while pick < len(bitsequence):
if tmp[0] == '1':
tmp = xor(divisor, tmp) + bitsequence[pick]
else:
tmp = xor('0' * pick, tmp) + bitsequence[pick]
pick += 1
if tmp[0] == '1':
tmp = xor(divisor, tmp)
else:
tmp = xor('0' * pick, tmp)
checkword = tmp
return checkword
class dataLinkLayer:
def framing(self, datagram):
l = len(datagram)
output=""
flag = "1111"
i=0
while(i<l):
#global i
if i+3<=l-1 and datagram[i]=='1' and datagram[i+1]=='1' and datagram[i+2]=='1' and datagram[i+3]=='1':
output+="11110"
i=i+4
else:
output+=datagram[i]
i+=1
out= flag+output+flag
return out
def deframing(self,frame):
i=4
output=""
l=len(frame)
while(i<l-4):
if i+3<=l-5 and frame[i]=='1' and frame[i+1]=='1' and frame[i+2]=='1' and frame[i+3]=='1':
output+="1111"
i=i+5
else:
output+=frame[i]
i+=1
return output
def CRCmaker(self, bitsequence):
divisor = "1011"
divlen = len(divisor)
newSeq = bitsequence + '0' * (divlen - 1)
rem = mod2div(newSeq, divisor)
codeword = bitsequence + rem
return codeword
def CRCchecker(self,bitsequence):
divisor = "1011"
divlen = len(divisor)
# Appends n-1 zeroes at end of data
newSeq = bitsequence + '0' * (divlen - 1)
rem = mod2div(newSeq, divisor)
a = int(rem,2)
if a==0:
print("NO ERROR")
else:
print("ERROR DETECTED")