-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.py
171 lines (148 loc) · 4.95 KB
/
sender.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
from os import path
from GUI import GUI
from packet import Packet, PacketType
import socket
import argparse
import pickle
"""
INITIALIZING GLOBAL VARIABLES
"""
PORT = 5000
FORMAT = 'utf-8'
SIZE = 4096
MSS = 1024
TIMEOUT = 5
countPacketRecvd = 0
countPacketSent = 0
"""
Gets the IP of the sender machine
:return: Returns the IP
"""
def get_ip_address() -> str:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
"""
Makes the data packet
:param data: Data to add to the packet
:param receiver_ip: IP Address of the receiver machine
:param receiver_port: Port for the receiver machine
:param s_port: Senders port
:param ack: Ack number
:param seq: Sequence number
:return: Returns the created data packet
"""
def makePacket(data, receiver_ip, receiver_port, s_port, ack, seq) -> Packet:
s_ip = get_ip_address()
packet = Packet(PacketType.DATA, s_ip, receiver_ip, s_port, receiver_port, ack, seq)
packet.add_data(data)
return packet
"""
Get the list of the data packets
:param string: user's input
:param addr: The address of the sender machine
:param src_port: The port of the sender machine
:return: Returns the list of the data packets
"""
def getPacketList(string, addr, src_port) -> list[Packet]:
global countPacketSent
global countPacketRecvd
packets = []
ack = 0
seq = 0
if not path.exists(string):
pointer = 0
while pointer < len(string):
packets.append(makePacket(string[pointer: pointer+MSS], addr[0], addr[1],src_port, ack, seq))
ack = 1 if ack == 0 else 0
seq = 1 if seq == 0 else 0
pointer += MSS
else:
f = open(string, 'r', encoding='utf-8')
while True:
d = f.read(MSS)
if not d:
break
packets.append(makePacket(d, addr[0], addr[1], src_port, ack, seq))
ack = 1 if ack == 0 else 0
seq = 1 if seq == 0 else 0
f.close()
packets.append(Packet(PacketType.EOF, get_ip_address()[0], addr[0], src_port, addr[1], ack, seq))
return packets
"""
Send the data packets to the connected receiver.
:param conn: Receiver's connected socket to receive data on
:param gui: object of GUI to updata data for the graph
"""
def sendPackets(packets, conn, gui) -> None:
global countPacketSent
global countPacketRecvd
expected_ack = 0
for i, p in enumerate(packets):
while True:
print(f"[SENDING {p.get_packet_type()} PACKET] {p}")
conn.send(pickle.dumps(p))
countPacketSent += 1
if p.get_packet_type() == PacketType.EOF.name:
print(f"[EOF DETECTED] Closing connection")
break
try:
ack = pickle.loads(conn.recv(SIZE))
countPacketRecvd += 1
except socket.timeout:
print("[TIMEOUT DETECTED]")
gui.update_data(0)
pass
else:
if ack.get_ack() == expected_ack:
gui.update_data(1)
break
else:
gui.update_data(0)
print(f"[DUPLICATE ACK DETECTED]{ack}")
expected_ack = 1 if expected_ack == 0 else 0
def show_stats(gui):
print("===================EOT===================")
print({"Count of Packets received": countPacketRecvd})
print({"Counts of Packets sent": countPacketSent})
print("===================EOT===================")
gui.draw()
'''
This is main of the program.
Parses the command line arguements and creates a socket to start the sender.
:raise KeyboardInterrupted: Shuts the server down.
:raise Exception: Shuts the server down.
'''
def main() -> None:
global countPacketSent
global countPacketRecvd
parser = argparse.ArgumentParser()
parser.add_argument('-s', type=str, dest='ip', required=True)
#Should be taking the proxy's IP address no the server directly
parser.add_argument('-p', type=int, default=PORT, dest='PORT')
args = parser.parse_args()
addr = (args.ip, args.PORT)
gui = GUI("Number of Packets sent", "Unique Packets Sent", "Sender Summary")
interrupted = False
try:
print(f'Starting Client on {addr}')
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(addr)
client.settimeout(TIMEOUT)
data = input('Enter file name or a string:')
packets = getPacketList(data, addr, client.getsockname())
sendPackets(packets, client, gui)
show_stats(gui)
client.close()
except KeyboardInterrupt as keyError:
show_stats(gui)
print(f'\nShutting Server - {repr(keyError)}')
assert not interrupted
except Exception as e:
show_stats(gui)
print(f'\nAn Exception Occured. Shutting Client - {repr(e)}')
assert not interrupted
if __name__ == '__main__':
main()