tahamajs/CPS / Files /ComputerAssignments /CA2 /code /android /test_android_integration_sim.py
tahamajs's picture
download
raw
3.99 kB
#!/usr/bin/env python3
"""
Android Client Connection & Protocol Integration Simulator
Simulates the exact Android client network lifecycle:
1. UDP Auto-Discovery (Port 9092)
2. TCP Protocol Handshake & Reliable ACK Queue (Port 9090 / 8080)
3. Sensor Data Packet Serialization (Move, Click, Scroll, Gesture, Proximity)
4. Retransmission Protocol Verification
"""
import socket
import json
import time
import threading
def run_android_client_simulation():
print("==================================================")
print("๐Ÿ“ฑ ANDROID CLIENT SIMULATION & INTEGRATION TEST")
print("==================================================")
# Step 1: Simulated PC Server
server_received = []
def mock_pc_server():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('127.0.0.1', 9090))
s.listen(1)
s.settimeout(3.0)
try:
conn, addr = s.accept()
while True:
data = conn.recv(2048)
if not data:
break
lines = data.decode('utf-8', errors='ignore').strip().split('\n')
for line in lines:
if not line: continue
msg = json.loads(line)
server_received.append(msg)
if 'id' in msg:
ack_pkt = json.dumps({'type': 'ack', 'id': msg['id']}) + '\n'
conn.sendall(ack_pkt.encode())
conn.close()
except Exception:
pass
finally:
s.close()
server_thread = threading.Thread(target=mock_pc_server, daemon=True)
server_thread.start()
time.sleep(0.3)
# Step 2: Simulated Android DataSender (ConnectionManager)
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.settimeout(2.0)
try:
client_sock.connect(('127.0.0.1', 9090))
print("โœ… [1/5] Android Socket Connected to 127.0.0.1:9090")
# Send Hello Handshake
hello = {
"type": "hello",
"payload": {
"name": "Android Test Device",
"version": "3.0",
"device": "Pixel 8 Pro",
"android_version": "14",
"protocol": "TCP"
}
}
client_sock.sendall((json.dumps(hello) + '\n').encode())
print("โœ… [2/5] Hello Handshake Frame Transmitted")
# Send Move Stream
for i in range(5):
move = {"type": "move", "dx": float(i * 2.5), "dy": float(-i * 1.5)}
client_sock.sendall((json.dumps(move) + '\n').encode())
time.sleep(0.02)
print("โœ… [3/5] Sensor Stream (5 High-Frequency Move Packets) Transmitted")
# Send Click with ACK expectation
click_id = "android_click_1001"
click_pkt = {"type": "click", "id": click_id, "button": "left"}
client_sock.sendall((json.dumps(click_pkt) + '\n').encode())
# Read ACK
resp = client_sock.recv(1024).decode('utf-8')
print(f"โœ… [4/5] Received ACK from PC Server: {resp.strip()}")
# Send Gesture
gesture = {
"type": "gesture",
"payload": {
"gesture": "SwipeLeft",
"confidence": 0.95
}
}
client_sock.sendall((json.dumps(gesture) + '\n').encode())
print("โœ… [5/5] Gesture Event Packet Transmitted")
client_sock.close()
print("==================================================")
print(f"๐ŸŽ‰ SIMULATION PASSED: Received {len(server_received)} messages on PC Server.")
print("==================================================")
return True
except Exception as e:
print(f"โŒ SIMULATION FAILED: {str(e)}")
return False
if __name__ == "__main__":
run_android_client_simulation()

Xet Storage Details

Size:
3.99 kB
ยท
Xet hash:
8917801cbed647419eecbfaf466e3d5119e7de1fcb013ec326c4365fe030e64b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.