-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
254 lines (202 loc) · 7.35 KB
/
Copy pathserver.py
File metadata and controls
254 lines (202 loc) · 7.35 KB
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
'''
Important Note:
The server is intended to function as a dedicated server.
It is recommended that a client is not run on the same machine as a server.
'''
import Queue
import socket
import select
import sys
class Qu:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def usage():
print 'USAGE: python server.py <ADDRESS> <PORT> <MAXQUEUE> <BUFFERSIZE>'
exit(0)
address = '127.0.0.1'
if len(sys.argv) > 1:
if sys.argv[1] in ('-h', '-H', '--help', '--HELP'):
if(sys.argv[1].isDigit()):
address = int(sys.argv[1])
elif(not sys.argv[1].isDigit()):
address = sys.argv[1]
usage()
else:
address = sys.argv[1]
else:
usage()
maxQueue = 2
bufferSize = 1024
if len(sys.argv) > 2:
if sys.argv[2].isdigit():
port = int(sys.argv[2])
if port < 1000 or port > 65535:
usage()
else:
usage()
if len(sys.argv) > 3:
if sys.argv[3].isdigit():
maxQueue = int(sys.argv[3])
if maxQueue < 1 or maxQueue > 999:
usage()
else:
maxQueue = 2
if len(sys.argv) > 4:
if sys.argv[4].isdigit():
bufferSize = int(sys.argv[4])
if bufferSize < 32 or bufferSize > 99999:
usage()
else:
bufferSize = 1024
else:
bufferSize = 1024
roomCount = 0
cQ = Qu()
clientQueue = {}
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind((address, port))
serverSocket.listen(5)
inputs = [serverSocket]
output = []
messageQueue = {}
startGame = False
playerOne = ''
playerTwo = ''
winner = -1
wait = None
while inputs:
# select will return three lists containing subsets of the contents that were passed in
inputfd, outputfd, exceptfd = select.select(inputs, output, inputs)
for fd in inputfd:
if fd is serverSocket:
# a new connection is ready to be made to the server.
# we will need to accept it here, check to see if there are 2 players, and possible add to clientQueue
clientConnection, clientAddress = fd.accept()
# if there are < 2 people currently connected
if roomCount < maxQueue:
status = 'ready'
inputs.append(clientConnection)
# also put in the messageQueue for data the server will send; messageQueue acts as a buffer
messageQueue[clientConnection] = Queue.Queue()
messageQueue[clientConnection].put(status + str(roomCount))
output.append(clientConnection)
roomCount += 1
if roomCount == maxQueue:
startGame = True
# else, add to clientQueue and make status = 'false'
else:
status = 'queue'
clientQueue[clientConnection] = Queue.Queue()
cQ.enqueue(clientConnection)
messageQueue[clientConnection] = Queue.Queue()
messageQueue[clientConnection].put(status)
output.append(clientConnection)
else:
# a client has sent data, so the server needs to receive it
data = fd.recv(bufferSize)
if data:
if startGame == True:
if '1' in data:
playerOne = data[:1]
if wait is None:
wait = fd
else:
playerTwo = data[:1]
if wait is None:
wait = fd
if playerOne != '' and playerTwo != '':
if playerOne == 'R' and playerTwo == 'R':
winner = 0
elif playerOne == 'R' and playerTwo == 'P':
winner = 2
elif playerOne == 'R' and playerTwo == 'S':
winner = 1
elif playerOne == 'P' and playerTwo == 'R':
winner = 1
elif playerOne == 'P' and playerTwo == 'P':
winner = 0
elif playerOne == 'P' and playerTwo == 'S':
winner = 2
elif playerOne == 'S' and playerTwo == 'R':
winner = 2
elif playerOne == 'S' and playerTwo == 'P':
winner = 1
elif playerOne == 'S' and playerTwo == 'S':
winner = 0
else:
messageQueue[fd].put('wait')
if fd not in output:
output.append(fd)
else:
messageQueue[fd].put('wait')
if fd not in output:
output.append(fd)
if '1' in data:
playerOne = data[:1]
if wait is None:
wait = fd
else:
playerTwo = data[:2]
if wait is None:
wait = fd
if winner != -1:
messageQueue[fd].put(winner)
if fd not in output:
output.append(fd)
wait.send(str(winner))
wait = None
startGame = False
playerOne = ''
playerTwo = ''
winner = -1
roomCount = 2
else:
print 'Server received a message. Adding to messageQueue'
messageQueue[fd].put(data)
if fd not in output:
output.append(fd)
else:
# client has disconnected
print 'A client has disconnected. Cleaning output list and messageQueue'
if fd in output:
output.remove(fd)
inputs.remove(fd)
del messageQueue[fd]
fd.close()
roomCount -= 1
try:
if cQ.size >= 1:
nextClient = cQ.dequeue()
if len(clientQueue) >= 1:
print 'dequeued successfully'
inputs.append(nextClient)
status = 'ready'
messageQueue[nextClient] = Queue.Queue()
messageQueue[nextClient].put(status)
output.append(nextClient)
roomCount += 1
except Exception:
print ''
for fd in outputfd:
try:
if roomCount > 0:
message = messageQueue[fd].get_nowait()
fd.send(str(message))
except Queue.Empty:
output.remove(fd)
for fd in exceptfd:
# if there is an exception, remove that fd from input, clean up the messageQueue, and close the fd
inputs.remove(fd)
del messageQueue[fd]
if fd in output:
output.remove(fd)
fd.close()
serverSocket.close()