-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
280 lines (219 loc) · 9.15 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 2 01:46:07 2019
@author: Shashwat Kathuria
"""
# Client Side Program
# Physical Layer and Data Link Layer Implementation
# Importing socket library for socket programming
import socket, random
import pylab as plt
def main():
# Getting required input
generator = raw_input("Enter the generator function in 0s and 1s : ")
finalDecodedMessage = ""
while True:
# Configuration for socket
s = socket.socket()
port = 9999
try:
# Connecting
s.connect(("127.0.0.1", port))
except:
break
# Getting received message from server
receivedMessage = s.recv(1024).decode()
# Decoding message from Physical Layer which itself takes help from
# Data Link Layer for error detection
physicalLayer = PhysicalLayer(receivedMessage)
decodedMessage = physicalLayer.decode(generator)
# Printing Answer
if decodedMessage != None:
print("\nDecoded Frame Message : " + str(decodedMessage))
finalDecodedMessage += str(decodedMessage)
else:
print("\nError detected in data by CRC (Cyclic Redundancy Check).")
# Plotting and showing encodings through graphs
physicalLayer.plotManchesterEncoding()
physicalLayer.plotOriginalEncoding()
# Closing connection
s.close()
print("--------------------------------------")
# Printing final answer
print("\nFinal Decoded Message : " + str(finalDecodedMessage))
print("--------------------------------------")
class PhysicalLayer():
def __init__(self, bits):
"""Function to initialize Physical Layer Object."""
self.message = ""
self.bits = bits
self.decodedMessage = ""
self.time = list(range(len(self.bits)))
self.manchesterEncoding = bits
self.manchesterYVal = []
self.originalYVal = []
def decode(self, generator):
"""Function to decode data using Manchester Encoding.
Generator variable used to pass onto Data Link Layer Object."""
# Variable to keep track of decoded part of Manchester Encoding
temp = ""
print("\nManchester Encoding : \n" + str(self.bits))
# Getting values for manchester encoding graph plot
yVal = [int(x) for x in list(self.bits)]
temp = []
for val in yVal:
if val == 0:
temp.append(-1)
else:
temp.append(1)
yVal = temp
self.manchesterYVal = yVal
temp = ""
# Decoding Manchester encoding in pairs
for i in range(0, len(self.bits), 2):
# Getting bits pair
s = self.bits[i: i + 2]
# If Low High then 1
if s == "01":
temp += "1"
# If High Low then 0
elif s == "10":
temp += "0"
# Storing answer
self.bits = temp
# Getting values for original encoding graph plot
tempOriginalYVal = [int(x) for x in self.bits]
for y in tempOriginalYVal:
self.originalYVal.append(y)
self.originalYVal.append(y)
# Decoded to original encoding with CRC Remainder
print("\nOriginal Encoding With CRC Remainder : \n" + str(self.bits))
choice = raw_input("\nDo you deliberately want to introduce errors into the frame signal to check the program?Press 1 for yes else give any other input : ")
# Introducing Errors Deliberately if choice is 1
if choice == "1":
temp = list(self.bits)
# For 5 iterations
for i in range(5):
index = random.randint(0, len(self.bits) - 1)
if temp[index] == "0":
temp[index] = "1"
elif temp[index] == "1":
temp[index] = "0"
self.bits = "".join(temp)
print("\nOriginal Encoding With CRC Remainder After Introducing Deliberate Errors : \n" + str(self.bits))
# Getting checksum
CRCchecksum = DataLinkLayer(self.bits, generator).CRCdetectError()
# Printing checksum
print("\nCRC Remainder : \n" + str(CRCchecksum))
# Checking further cases
if CRCchecksum == '0' * (len(generator) - 1):
self.bits = self.bits[:-(len(generator) - 1)]
# Error case
if len(self.bits) % 8 != 0:
print("\nError detected in data. Number of bits not a multiple of 8.\n")
return None
# Correct case
else:
print("\nNo Error.")
print("\nOriginal Encoding Without CRC Remainder : \n" + str(self.bits))
return self.bitsToString()
# Error case
else:
print("\nError detected in data. CRC Remainder is not equal to " + str('0' * (len(generator) - 1)))
return None
def bitsToString(self):
"""Function to convert a stream of bits into string using ascii and bit values."""
# Lists to store bits and chars
chars = []
bitsArray = []
# Storing all bits in an array
for i in self.bits:
bitsArray.append(int(i))
# Converting 8 bits (each byte) into a char
for b in range(len(bitsArray) // 8):
# Getting 8 bits/a byte
byte = bitsArray[b*8:(b+1)*8]
# Converting to a char and then appending to list of chars
# Base 2 for bit
chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
# Concatenating chars and storing
self.decodedMessage = ''.join(chars)
# Returning answer
return self.decodedMessage
def plotManchesterEncoding(self):
"""Function to plot Manchester Encoding."""
# Plotting and configuring the graph
plt.figure("Graph")
plt.title("Manchester Encoding")
plt.clf()
plt.xlim(0, len(self.time))
plt.xlabel("Time || Manchester Encoding")
plt.ylabel("Encoding Value")
plt.plot(self.time, self.manchesterYVal, drawstyle='steps-post')
plt.show()
def plotOriginalEncoding(self):
"""Function to plot Original Encoding."""
# Plotting and configuring the graph
plt.figure("Graph")
plt.title("Original Encoding")
plt.clf()
plt.xlim(0, len(self.time))
plt.xlabel("Time || Original Encoding")
plt.ylabel("Encoding Value")
plt.plot(self.time, self.originalYVal, drawstyle='steps-post')
plt.show()
class DataLinkLayer():
def __init__(self, bits, generator):
"""Function to initialize Data Link Layer Object."""
self.bits = bits
self.keyLength = len(generator)
self.appendedData = self.bits + "0" * (self.keyLength - 1)
self.generator = generator
def CRCdetectError(self):
"""Function to encode data using CRC(Cyclic Redundancy Checksum)."""
divisor = self.generator
divident = self.appendedData
# Number of bits to be xored
numBits = len(self.generator)
# Subpart substring
subpartSubstring = self.appendedData[0 : numBits]
while numBits < len(self.appendedData):
# If Leftmost bit is 1
if subpartSubstring[0] == '1':
# Using self.generator and appending a data bit at the end
subpartSubstring = self.XOR(self.generator, subpartSubstring) + self.appendedData[numBits]
# Else if leftmost bit is 0
else:
# Using all '0's generator
subpartSubstring = self.XOR('0'*numBits, subpartSubstring) + divident[numBits]
# increment numBits to move further
numBits += 1
# For the last nth bits, otherwise out of bound occurs due to numBits
# If Leftmost bit is 1
if subpartSubstring[0] == '1':
# Using self.generator
subpartSubstring = self.XOR(divisor, subpartSubstring)
# Else if leftmost bit is 0
else:
# Using all '0's generator
subpartSubstring = self.XOR('0' * numBits, subpartSubstring)
# Returning checksum answer
checksum = subpartSubstring
return checksum
def XOR(self, messagePartition, generator):
"""Function to xor a messagePartition and generator.
Also cutting of first bit of xor."""
# Variable required
self.xor = ""
# Iterating through bits at respective positions
for bit1, bit2 in zip(messagePartition, generator):
# XORing
if bit1 == bit2:
self.xor = self.xor + "0"
else:
self.xor = self.xor + "1"
# Returning answer
return self.xor[1 : ]
# Calling main function
if __name__ == "__main__":
main()