0% found this document useful (0 votes)
5 views1 page

CNfinaltouch

The document contains various implementations of networking protocols and algorithms, including a Data Link Layer protocol, Distance Vector Routing, and a broadcast tree algorithm. It also covers error detection using CRC and simple encryption/decryption methods. Each section provides code snippets demonstrating the functionality of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

CNfinaltouch

The document contains various implementations of networking protocols and algorithms, including a Data Link Layer protocol, Distance Vector Routing, and a broadcast tree algorithm. It also covers error detection using CRC and simple encryption/decryption methods. Each section provides code snippets demonstrating the functionality of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

GBN: DIST VEC:

import random class DistanceVectorRouting:


import time def _init_(self, graph):
class DataLinkLayer: self.graph = graph
def _init_(self, window_size, total_frames): self.nodes = graph.keys()
self.window_size = window_size self.routing_table = {node: {n: float('inf') for n in self.nodes} for node in
self.frames = list(range(1, total_frames + 1)) self.nodes}
self.base = 0 for node in self.nodes:
def send_frames(self): self.routing_table[node][node] = 0
for i in range(self.base, min(self.base + self.window_size, len(self.frames))): for neighbor, cost in graph[node].items():
print(f"Sending frame {self.frames[i]}") self.routing_table[node][neighbor] = cost
def receive_ack(self): def update_table(self, node):
if random.random() > 0.7: updated = False
print("ACK lost!"); return None for neighbor, cost in self.graph[node].items():
return random.choice(self.frames[self.base:self.base + self.window_size]) for dest in self.nodes:
def slide_window(self, ack): new_cost = self.routing_table[node][neighbor] +
self.base = max(self.base, self.frames.index(ack) + 1) self.routing_table[neighbor][dest]
print(f"Sliding window to frame {self.base + 1}") if new_cost < self.routing_table[node][dest]:
def run_protocol(self): self.routing_table[node][dest] = new_cost
while self.base < len(self.frames): updated = True
self.send_frames() return updated
ack = self.receive_ack() def run(self):
if ack: while any(self.update_table(node) for node in self.nodes): pass
print(f"Received ACK for frame {ack}") def display(self):
self.slide_window(ack) for node, table in self.routing_table.items():
else: print(f"Node {node}: {table}")
print("Timeout! Resending frames...") graph = {
time.sleep(1) 'A': {'B': 1, 'C': 4},
protocol = DataLinkLayer(window_size=4, total_frames=10) 'B': {'A': 1, 'C': 2, 'D': 7},
protocol.run_protocol() 'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 7, 'C': 1}
DIJK: }
import random dvr = DistanceVectorRouting(graph)
import time dvr.run()
class DataLinkLayer: dvr.display()
def _init_(self, window_size, total_frames):
self.window_size = window_size LEAKY:
self.frames = list(range(1, total_frames + 1)) storage=0
self.base = 0 no_of_queries=4
def send_frames(self): bucket_size=10
for i in range(self.base, min(self.base + self.window_size, len(self.frames))): input_size=4
print(f"Sending frame {self.frames[i]}") output_size=1
def receive_ack(self): for i in range(0,no_of_queries):
if random.random() > 0.7: print(f"\nQuery {i+1}:")
print("ACK lost!"); return None size_left=bucket_size-storage
return random.choice(self.frames[self.base:self.base + self.window_size]) if input_size<=size_left:
def slide_window(self, ack): storage+=input_size
self.base = max(self.base, self.frames.index(ack) + 1) print(f"Packet added, size: {input_size}. Current storage:
print(f"Sliding window to frame {self.base + 1}") {storage}/{bucket_size}")
def run_protocol(self): else:
while self.base < len(self.frames): print(f"Packet loss, size: {input_size}. Not enough space")
self.send_frames() print(f"buffer size={storage} out of the bucket size={bucket_size}")
ack = self.receive_ack() storage = max(0, storage - output_size)
if ack:
print(f"Received ACK for frame {ack}") MERG SORTING:
self.slide_window(ack) def merge_sort(buffer):
else: if len(buffer) > 1:
print("Timeout! Resending frames...") mid = len(buffer) // 2
time.sleep(1) left_half = buffer[:mid]
protocol = DataLinkLayer(window_size=4, total_frames=10) right_half = buffer[mid:]
protocol.run_protocol() merge_sort(left_half)
merge_sort(right_half)
CRC: i=j=k=0
def compute_crc(data, generator): while i < len(left_half) and j < len(right_half):
data = data + '0' * (len(generator) - 1) if left_half[i][0] < right_half[j][0]:
data = list(data) buffer[k] = left_half[i]
generator = list(generator) i += 1
for i in range(len(data) - len(generator) + 1): else:
if data[i] == '1': buffer[k] = right_half[j]
for j in range(len(generator)): j += 1
data[i + j] = str(int(data[i + j]) ^ int(generator[j])) k += 1
remainder = ''.join(data[-(len(generator) - 1):]) while i < len(left_half):
return remainder buffer[k] = left_half[i]
def main(): i += 1; k += 1
binary_data = input("Enter the binary data: ") while j < len(right_half):
crc12_poly = "1100000001111" buffer[k] = right_half[j]
crc16_poly = "11000000000000101" j += 1; k += 1
crc_ccitt_poly = "10001000000100001" def sort_frames(buffer):
crc12 = compute_crc(binary_data, crc12_poly) print("Unsorted Buffer:", buffer)
crc16 = compute_crc(binary_data, crc16_poly) merge_sort(buffer)
crc_ccitt = compute_crc(binary_data, crc_ccitt_poly) print("Sorted Buffer:", buffer)
print("\nComputed CRC Codes:") return buffer
print(f"CRC-12: {crc12}") buffer = [(3, "Frame3"), (2, "Frame1"), (1, "Frame2"), (4, "Frame4")]
print(f"CRC-16: {crc16}") sorted_frames = sort_frames(buffer)
print(f"CRC-CCITT: {crc_ccitt}")
if _name_ == "_main_":
main()
BROADCAST:
import heapq
def broadcast_tree(graph, start):
visited, edges, cost, heap = set(), [], 0, [(0, start, None)]
while heap:
c, node, parent = heapq.heappop(heap)
if node not in visited:
visited.add(node)
cost += c
if parent: edges.append((parent, node, c))
for neighbor, weight in graph[node]:
if neighbor not in visited:
heapq.heappush(heap, (weight, neighbor, node))
return edges, cost
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('A', 1), ('D', 2), ('E', 3)],
'C': [('A', 4), ('D', 1)],
'D': [('B', 2), ('C', 1), ('E', 2)],
'E': [('B', 3), ('D', 2)],
}
tree, cost = broadcast_tree(graph, 'A')
print("Edges:", tree, "\nTotal Cost:", cost)

ENC & DEC:


def shift_encrypt(data, shift):
encrypted_data = ""
for char in data:
if char.isalpha():
start = ord('A') if char.isupper() else ord('a')
encrypted_data += chr((ord(char) - start + shift) % 26 + start)
else:
encrypted_data += char
return encrypted_data
def shift_decrypt(data, shift):
return shift_encrypt(data, -shift)
if __name__ == "__main__":
original_data = "Hello, this is a secret message!"
shift = 4
encrypted_data = shift_encrypt(original_data, shift)
print(encrypted_data)
decrypted_data = shift_decrypt(encrypted_data, shift)
print(decrypted_data)

You might also like