-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiPhone_Splitter.py
113 lines (71 loc) · 2.71 KB
/
iPhone_Splitter.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
# Takes data from iPhone SensorLog and splits messages by message type found in header bit. Only 1 type right now
# Sends to other ports that read the messages.
# Listens on port from iPhone Sensor Log, UDP: UDP_PORT
# Sends all messages to the recorder port if "record" is in the arguments list
import socket
import time
import struct
import sys
from datetime import datetime
record = False
for item in sys.argv:
if item == "record":
record = True
# Sends data to these ports:
UDP_IP = "127.0.0.1" #standard ip udp (localhost)
UDP_PORT = 20777
UDP_PORT_RECORD = 20776 #Recorder
UDP_PORT_0 = 20778 #Type 0, SensorLog
MESSAGE = "23,567,32,4356,456,132,4353467" #init message
# Type R - initiate socket and send first message
sock_R = None
if record:
sock_R = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet, UDP
try:
sock_R.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT_RECORD))
except:
print('Initial message failed -- Recorder')
# Type 0 - initiate socket and send first message
sock_0 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet, UDP
try:
sock_0.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT_0))
except:
print('Initial message failed!')
#Receiver socket from game UDP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.settimeout(15)
server_socket.bind(('', UDP_PORT))
count = 0
# Messages from SensorLog must be buffered as they come in separate messages.
buffer = ''
while True:
message = None
try:
message, address = server_socket.recvfrom(1024000)
except KeyboardInterrupt:
print("User stopped: iPhone_Splitter.")
exit()
except:
print("Timeout: Splitter waiting for more data from port:", UDP_PORT, str(datetime.now()))
#print(message)
#server_socket.sendto(message, address)
if message is not None:
# Must gather up messages, split by newline '\n'
messageStrings = message.decode("utf-8").split('\n',1)
if len(messageStrings) == 2:
buffer = buffer + messageStrings[0]
remaining = messageStrings[1]
# Send gathered up message
sock_0.sendto(buffer.encode(), (UDP_IP, UDP_PORT_0))
#print(buffer)
#print("\n")
if record:
sock_R.sendto(buffer.encode(), (UDP_IP, UDP_PORT_RECORD))
# Clear buffer to remaining portion
buffer = remaining
if count % 1000 == 0:
print ("Total message count: " + str(count))
count = count + 1
else:
# gather message into buffer
buffer = buffer + messageStrings[0]