-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
324 lines (305 loc) · 10.4 KB
/
client.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from Communicator import Communicator
import socket,sys,json,os,time,pdb
import math
from Game import Game
from Board import Board
import argparse
class Client(Communicator):
def __init__(self):
self.GAME_TIMER = 100000 # in Milli Seconds
self.NETWORK_TIMER = 500
super(Client,self).__init__()
pass
def setNetworkTimer(self,Time_in_Seconds):
self.NETWORK_TIMER = Time_in_Seconds
def getNetworkTimer(self):
return self.NETWORK_TIMER
def getGameTimer(self):
return self.GAME_TIMER // 1000
def setGameTimer(self,Time_in_Seconds):
self.GAME_TIMER = Time_in_Seconds * 1000
def CheckExeFile(self,Execution_Command,Executable_File):
""" Checks the Existance of the Executable File and
if the extension of the file matches the command used to run it
Args:
Execution_Command : Command used to execute the Executable File (sh, python ./ etc)
Executable_File : The Executable File
Returns:
None
"""
Extension = Executable_File.split('.')
if(len(Extension) == 1):
return False
Extension = Extension[-1]
if(os.path.isfile(Executable_File)):
if(Execution_Command == './' or Execution_Command == 'sh'):
if(Extension == 'sh' or Extension == 'o'):
return True
else:
return False
elif(Execution_Command == 'java'):
if(Extension == 'java'):
return True
else:
return False
elif(Execution_Command == 'python'):
if(Extension == 'py'):
return True
else:
return False
else:
return False
def CreateChildProcess(self,Execution_Command,Executable_File):
""" Creates a Process, with which the client communicates.
Checks the existance of the Executable_File and some basic
checks for whether the Execution_Command used to run the code
matches the extension of the Executable File
Prints if error is found
Args:
Execution_Command : Command used to execute the Executable File (sh, python ./ etc)
Executable_File : The Executable File
Returns:
None
"""
if(self.CheckExeFile(Execution_Command,Executable_File)):
super(Client,self).CreateChildProcess(Execution_Command,Executable_File)
else:
print 'ERROR : EITHER FILE ', Executable_File,' DOES NOT EXIST',
print 'OR THE EXECUTION COMMAND TO RUN THE FILE ',Execution_Command,' IS INCORRECT'
def Connect2Server(self,server_address,port_no):
"""Connects to server with given IP Address and Port No.
Args:
server_address : IP Address
Port No : Port Number
Returns:
None
"""
self.clientSocket = socket.socket()
self.clientSocket.connect((server_address,port_no))
super(Client,self).setSocket(self.clientSocket,self.NETWORK_TIMER)
def SendData2Server(self,data):
""" Sends data (a dictionary) to the Server as a json object
In case action == 'FINISH', closes the pipe on this end
Args:
data : a dictionary of the following format:
{
meta : The meta data in case of an error ( UNEXPECTED STOP, WRONG MOVE etc.), otherwise ''
action : The action to be taken (KILLPROC, NORMAL, FINISH, INIT)
data : Move String or '' in case of an Error
}
Returns:
success_flag : True if successful in sending, False otherwise
"""
if((data['action'] == 'KILLPROC') or (data['action'] == 'FINISH')):
super(Client,self).closeChildProcess()
sendData = json.dumps(data)
success_flag = super(Client,self).SendDataOnSocket(sendData)
if(not success_flag):
print 'ERROR : FAILED TO SEND DATA TO SERVER'
super(Client,self).closeSocket()
elif((data['action'] == 'KILLPROC') or (data['action'] == 'FINISH')):
super(Client,self).closeSocket()
return success_flag
def RecvDataFromServer(self):
""" Receives data from the Server as a string, and Returns the Move.
Uses self.NETWORK_TIMER to decide how long to wait for input from Server
In case of an error, prints the error, and closes the pipe process
In case the last move is made by other client, closes the pipe process and
returns the data
Args:
None
Returns:
retData : String (Move) in case there are no errors, otherwise None
"""
data = super(Client,self).RecvDataOnSocket()
retData = None
if(data == None):
print 'ERROR : TIMEOUT ON SERVER END'
super(Client,self).closeChildProcess()
super(Client,self).closeSocket()
else:
data = json.loads(data)
if(data['action'] == 'NORMAL' or data['action'] == 'INIT'):
retData = data['data']
elif(data['action'] == 'KILLPROC'):
print 'ERROR : ' + data['meta'] + ' ON OTHER CLIENT'
super(Client,self).closeChildProcess()
super(Client,self).closeSocket()
elif(data['action'] == 'FINISH'):
super(Client,self).closeChildProcess()
super(Client,self).closeSocket()
retData = data['data']
return retData
def RecvDataFromProcess(self):
"""Receives Data from the process. This does not implement checks
on the validity of game moves. Hence, the retData from here is not final
, i.e, it may be different than what is sent to the server.
Note: The Action 'FINISH' is set internally by game, not by the network
Handles Errors like Exceptions thrown by process.
However, In case of a timeout, 'FINISH' may be thrown
Uses self.GAME_TIMER to decide how long to wait for a timeout.
For both the above cases, prints the error msg and closes the connection to
the process.
Args:
None
Returns:
retData : dictionary of the nature :
{
meta : '' / MetaData in case of an Error
action : 'NORMAL' / 'KILLPROC' in case of an Error
data : 'DATA' / '' in case of an Error
}
None in case of an error
"""
start_time = time.time()
BUFFER_TIMER = int(math.ceil(self.GAME_TIMER / 1000.0))
print 'Time remaining is: ' + str(BUFFER_TIMER) + 's'
data = super(Client,self).RecvDataOnPipe(BUFFER_TIMER)
end_time = time.time()
retData = None
if(data == None):
print 'ERROR : THIS CLIENT STOPPED UNEXPECTEDLY OR TIMED OUT'
super(Client,self).closeChildProcess()
retData = {'meta':'UNEXPECTED STOP','action':'KILLPROC','data':''}
else:
# 1 Milli Second Default
time_delta = max(1,int((end_time - start_time) * 1000))
self.GAME_TIMER -= time_delta
if(self.GAME_TIMER > 0):
retData = {'meta':'','action':'NORMAL','data':data}
else:
retData = {'meta':'TIMEOUT','action':'KILLPROC','data':''}
return retData
def SendData2Process(self,data):
""" Sends Data (Move) to the process. Handles the case if the process being communicated with has closed.
Args:
data : string data, to send the process (a game move)
Returns:
success_flag : A boolean flag to denote the data transfer to the process was successful or not.
"""
if(data[-1] != '\n'):
data = data + '\n'
success_flag = super(Client, self).SendDataOnPipe(data)
if(success_flag == False):
print 'ERROR : FAILED TO SEND DATA TO PROCESS'
super(Client,self).closeChildProcess()
return success_flag
def game_loop(game, args):
client = Client()
if args.exe.endswith('.py'):
client.CreateChildProcess('python', args.exe)
elif args.exe.endswith('.sh'):
client.CreateChildProcess('sh', args.exe)
else:
client.CreateChildProcess('sh', args.exe)
client.Connect2Server(args.ip, args.port)
server_string = client.RecvDataFromServer()
if(server_string is None):
print 'ERROR IN SETTING UP CONNECTIONS. SORRY'
sys.exit(0)
server_string_list = server_string.strip().split()
player_id = server_string_list[0]
board_size = int(server_string_list[1])
game_timer = int(server_string_list[2])
client.setGameTimer(game_timer)
print 'You are player ' + str(player_id)
print 'You are alloted a time of ' + str(game_timer) + 's\n'
client.SendData2Process(server_string)
if args.mode == 'GUI':
game.render_board.render(game)
elif args.mode == 'CUI':
game.render()
if player_id == '2':
move = client.RecvDataFromServer()
if move:
move = move.strip()
print "The other player played " + move
success = game.execute_move(move)
client.SendData2Process(move)
else:
sys.exit(0)
while(True):
move = client.RecvDataFromProcess()
if move['action'] == 'KILLPROC':
client.SendData2Server(move)
break
move['data'] = move['data'].strip()
print "You played " + move['data']
success = game.execute_move(move['data'])
message = {}
if success == 0:
message['data'] = ''
message['action'] = 'KILLPROC'
message['meta'] = 'INVALID MOVE'
print 'INVALID MOVE ON THIS CLIENT'
elif success == 2 or success == 3 or success == 4:
# 2 : Player 1 wins
# 3 : Player 2 wins
# 4 : Game Drawn
message['action'] = 'FINISH'
message['data'] = move['data']
if success == 2:
message['meta'] = '1 wins'
if(player_id == '1'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
elif success == 3:
message['meta'] = '2 wins'
if(player_id == '2'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
else:
message['meta'] = 'Game Drawn'
print 'GAME DRAWN'
elif success == 1:
message = move
client.SendData2Server(message)
if message['action'] == 'FINISH' or message['action'] == 'KILLPROC':
break
move = client.RecvDataFromServer()
if move:
move = move.strip()
print "The other player played " + move
success = game.execute_move(move)
if success == 2 or success == 3 or success == 4:
# 2 : Player 1 wins
# 3 : Player 2 wins
# 4 : Game Drawn
if success == 2:
if(player_id == '1'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
elif success == 3:
if(player_id == '2'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
else :
print 'GAME DRAWN'
break
else:
client.SendData2Process(move)
else:
break
client.closeChildProcess()
client.closeSocket()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'Tak client')
parser.add_argument('ip', metavar = '0.0.0.0', type = str, help = 'Server IP')
parser.add_argument('port', metavar = '10000', type = int, help = 'Server port')
parser.add_argument('exe', metavar = 'run.sh', type = str, help = 'Your executable')
parser.add_argument('-n', dest = 'n', metavar = 'N', type = int, default = 5, help = 'Tak board size')
parser.add_argument('-mode', dest = 'mode', type = str, default = 'GUI', help = 'How to render')
args = parser.parse_args()
game = Game(args.n, args.mode)
if args.mode != 'GUI':
game_loop(game, args)
else:
from threading import Thread
Th = Thread(target = lambda : game_loop(game, args))
Th.start()
game.init_display()
game.display.mainloop()