blob: ff00f2fc8c6f5915ef7554d11e211a80b4a9ff9a [file] [log] [blame]
Florin Corase04c2992017-03-01 08:17:34 -08001#!/usr/bin/env python
2
3import socket
4import sys
Florin Corasd79b41e2017-03-04 05:37:52 -08005import time
Florin Corase04c2992017-03-01 08:17:34 -08006
7# action can be reflect or drop
8action = "drop"
Florin Corasf6d68ed2017-05-07 19:12:02 -07009test = 0
10
11def test_data (data, n_rcvd):
12 n_read = len (data);
13 for i in range(n_read):
14 expected = (n_rcvd + i) & 0xff
15 byte_got = ord (data[i])
16 if (byte_got != expected):
17 print("Difference at byte {}. Expected {} got {}"
18 .format(n_rcvd + i, expected, byte_got))
19 return n_read
Florin Corase04c2992017-03-01 08:17:34 -080020
21def handle_connection (connection, client_address):
22 print("Received connection from {}".format(repr(client_address)))
Florin Corasf6d68ed2017-05-07 19:12:02 -070023 n_rcvd = 0
Florin Corase04c2992017-03-01 08:17:34 -080024 try:
25 while True:
26 data = connection.recv(4096)
27 if not data:
28 break;
Florin Corasf6d68ed2017-05-07 19:12:02 -070029 if (test == 1):
30 n_rcvd += test_data (data, n_rcvd)
Florin Corase04c2992017-03-01 08:17:34 -080031 if (action != "drop"):
32 connection.sendall(data)
33 finally:
34 connection.close()
35
36def run_server(ip, port):
37 print("Starting server {}:{}".format(repr(ip), repr(port)))
38 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Corasd79b41e2017-03-04 05:37:52 -080039 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Florin Corase04c2992017-03-01 08:17:34 -080040 server_address = (ip, int(port))
41 sock.bind(server_address)
42 sock.listen(1)
43
44 while True:
45 connection, client_address = sock.accept()
46 handle_connection (connection, client_address)
47
48def prepare_data():
49 buf = []
50 for i in range (0, pow(2, 16)):
51 buf.append(i & 0xff)
52 return bytearray(buf)
53
54def run_client(ip, port):
55 print("Starting client {}:{}".format(repr(ip), repr(port)))
56 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Corasd79b41e2017-03-04 05:37:52 -080057 server_address = (ip, port)
Florin Corase04c2992017-03-01 08:17:34 -080058 sock.connect(server_address)
59
60 data = prepare_data()
Florin Corasd79b41e2017-03-04 05:37:52 -080061 n_rcvd = 0
62 n_sent = len (data)
Florin Corase04c2992017-03-01 08:17:34 -080063 try:
64 sock.sendall(data)
Florin Corasd79b41e2017-03-04 05:37:52 -080065
66 timeout = time.time() + 2
67 while n_rcvd < n_sent and time.time() < timeout:
68 tmp = sock.recv(1500)
69 tmp = bytearray (tmp)
70 n_read = len(tmp)
71 for i in range(n_read):
72 if (data[n_rcvd + i] != tmp[i]):
73 print("Difference at byte {}. Sent {} got {}"
74 .format(n_rcvd + i, data[n_rcvd + i], tmp[i]))
75 n_rcvd += n_read
76
77 if (n_rcvd < n_sent or n_rcvd > n_sent):
78 print("Sent {} and got back {}".format(n_sent, n_rcvd))
79 else:
80 print("Got back what we've sent!!");
81
Florin Corase04c2992017-03-01 08:17:34 -080082 finally:
83 sock.close()
84
85def run(mode, ip, port):
86 if (mode == "server"):
87 run_server (ip, port)
88 elif (mode == "client"):
89 run_client (ip, port)
90 else:
91 raise Exception("Unknown mode. Only client and server supported")
92
93if __name__ == "__main__":
94 if (len(sys.argv)) < 4:
Florin Corasf6d68ed2017-05-07 19:12:02 -070095 raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]")
96 if (len(sys.argv) == 6):
Florin Corase04c2992017-03-01 08:17:34 -080097 action = sys.argv[4]
Florin Corasf6d68ed2017-05-07 19:12:02 -070098 test = int(sys.argv[5])
Florin Corase04c2992017-03-01 08:17:34 -080099
Florin Corasd79b41e2017-03-04 05:37:52 -0800100 run (sys.argv[1], sys.argv[2], int(sys.argv[3]))