-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathphoneserver.py
287 lines (232 loc) · 8.25 KB
/
phoneserver.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding: utf-8 -*-
import socket
import threading
import socketserver
import struct
import json
import time
lock = threading.Lock()
clients = []
customs = []
#客户信息类,保存有客户的socket连接、客户惟一标识,
#用于将处理终端返回的数据发送给正确的用户
class ClientInfo(object):
#构造函数,request为该客户的socket信息
def __init__(self, request, ip, port):
super(ClientInfo, self).__init__()
self.request = request
self.ip = ip
self.port = port
self.client_info = {}
self.user_name = None
self.password = None
self.app = None
self.is_phone = False
#用户将使用的设备列表
self.pair_client = None
#手机当前状态
self.is_ready = False
#验证身份信息
def verify_identify(self, info):
self.client_info = info
self.user_name = info["userName"]
if "password" in info.keys():
self.password = info["password"]
if "type" in info.keys():
self.is_phone = info["type"]
if "app" in info.keys():
self.app = info["app"]
if (self.user_name is not None) and (self.password is not None):
return True
elif self.is_phone:
return True
else:
return False
#分配配对客户
def assign_pair_client(self, client):
if self.pair_client is None:
self.pair_client = client
def is_same_client(self, identy):
if self.user_name == identy:
return True
else:
return False
def is_custom(self):
if self.is_phone:
return False
else:
return True
def get_socket(self):
return self.request
class PhoneSocketServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
class PhoneSocketRequestHandler(socketserver.BaseRequestHandler):
def setup(self):
self.hasVerify = False
#缓冲区
self.buffer = bytearray()
ip = self.client_address[0]
port = self.client_address[1]
self.client = ClientInfo(self.request, ip, port)
print("IP:{0} Port:{1}的客户端请求加入".format(ip, port))
def handle(self):
while True:
try:
data = self.request.recv(1024)
if not data:
print("客户端失去连接")
break
print("服务器接收数据", data)
info, cinfo = self.handle_data(data)
print("服务器接收到的数据是", info, cinfo)
if self.hasVerify == False:
if cinfo is not None:
verify = self.client.verify_identify(cinfo)
with lock:
if self.client.is_custom():
customs.append(self.client)
else:
clients.append(self.client)
self.hasVerify = verify
if verify:
self.sendOpenAppCommand()
else:
self.responseMessage("请先验证身份!")
break
print("7")
for item in info:
print("8")
self.handout(item)
except Exception as e:
print("接收数据超时", e)
time.sleep(1)
def finish(self):
print("客户端失去连接", self.client, clients, customs)
with lock:
if self.client in clients:
clients.remove(self.client)
else:
customs.remove(self.client)
def sendOpenAppCommand(self):
print("sendOpenAppCommand")
app = self.client.app
if len(app) == 0:
return
message = {}
message["name"] = "OpenApp"
l = [{"bundleId" : self.client.app}]
message["params"] = l
msg = json.dumps(message)
print("msg = ", msg)
length = len(msg)
d = struct.pack("!2iI", 1, 2, length)
data = bytearray(d)
data.extend(msg.encode('utf-8'))
self.request.sendall(data)
print("发送打开app命令成功")
def handle_data(self, data):
self.buffer.extend(data)
identy = None
info = []
length = len(self.buffer)
start = 0
if length > 12:
while True:
print("开始解析数据")
head = self.buffer[start : start + 12]
print(head)
version, ptype, pLen = struct.unpack("!2iI", head)
print("解析数据成功", version, ptype, pLen)
left = length - start
if pLen == 0:
start = start + 12
elif pLen <= left:
s = start + 12
e = s + pLen
item = bytes(self.buffer[s : e])
print("数据为", item)
if ptype == 4 :
identy = json.loads(item.decode("utf-8"))
elif ptype == 3:
responseInfo = json.loads(item.decode("utf-8"))
self.handleCommandResponse(responseInfo)
else:
info.append(item)
start = start + 12 + pLen
else:
break
if length - start <= 12:
break
newbuffer = self.buffer[start:]
self.buffer = newbuffer
return info, identy
def is_custom_socket(self):
result = False
for item in customs:
if item.request == self.request:
result = True
break
return result
def responseMessage(self, message):
msg = json.dumps(message)
length = len(msg)
d = struct.pack("!2iI", 1, 3, length)
data = bytearray(d)
data.extend(msg.encode('utf-8'))
self.request.sendall(data)
print("返回信息:", message)
def handleCommandResponse(self, info):
commandName = info["name"]
result = info["params"]["result"]
print(commandName, "命令返回值为", result)
if commandName == "OpenApp":
self.client.is_ready = result
def handout(self, info):
if self.client.is_custom():
print("转发手机计算")
identy = self.client.user_name
self.send_task_phone(info, identy)
else:
print("转发客户", info)
self.send_result_custom(info, identy)
#给处理终端下发数据处理任务
def send_task_phone(self, info, identy):
value = json.loads(info)
value["identy"] = identy
message = json.dumps(value)
length = len(message)
d = struct.pack("!2iI", 1, 1, length)
data = bytearray(d)
data.extend(message.encode('utf-8'))
print("向手机发送数据", data)
if len(clients) > 0:
client = clients[0]
client.sendall(data)
else:
print("没有可用于计算的设备,请稍等")
#返回计算结果给客户
def send_result_custom(self, info):
value = json.loads(info)
identy = value.pop("identy")
message = json.dumps(value)
print("拼装返回包", message, identy)
length = len(message)
d = struct.pack("!2iI", 1, 1, length)
data = bytearray(d)
data.extend(message.encode('utf-8'))
print("发送返回包")
for custom in customs:
if custom.is_same_client(identy):
print("成功查询到目标客户", data)
custom.get_socket().sendall(data)
print("成功发送返回包")
break
if __name__ == "__main__":
# host = "172.18.18.244"
host = ''
server = PhoneSocketServer((host, 12345), PhoneSocketRequestHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
server_thread.join()
pass