forked from astericks/kodekombat12
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
84 lines (62 loc) · 2.35 KB
/
server.py
File metadata and controls
84 lines (62 loc) · 2.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
####################################################################################
# variables b1,b2
# threads main,thread_to_read_bot1_move_from_file,thread_to_read_bot2_move_from_file
# locks bot1move_lock,bot2_move_lock
##################################################################################
#!/usr/bin/python
import threading
import time
import sys
b1='U' #bot1s current move
b2='R' #bot2s current move
bot1_move_lock=threading.Lock() #lock to provide synchronisation to b1
bot2_move_lock=threading.Lock() #lock ot provide syncrhonisation to b2
exitFlag = 0
class thread_to_read_bot1_move_from_file (threading.Thread):
def __init__(self, threadID,filename):
self.threadID = threadID
threading.Thread.__init__(self)
def run(self):
global b1
print "Starting " + str(self.threadID)
bot1_move_lock.acquire()
print "Before" + b1
b1='L'
print "After" + b1
bot1_move_lock.release()
print "Exiting " + str(self.threadID)
class thread_to_read_bot2_move_from_file (threading.Thread):
def __init__(self, threadID,filename):
self.threadID = threadID
threading.Thread.__init__(self)
def run(self):
global b1
print "Starting " + str(self.threadID)
bot2_move_lock.acquire()
print "Before" + b1
b2='L'
print "After" + b1
bot2_move_lock.release()
print "Exiting " + str(self.threadID)
#start threads
bot1_move_file=open("files/bot1_move","r")
bot2_move_file=open("files/bot2_move","r")
thread1 = thread_to_read_bot1_move_from_file(1,bot1_move_file)
thread2 = thread_to_read_bot2_move_from_file(1,bot2_move_file)
thread1.start()
thread2.start()
# the rest of the engine code which will constantly read the variables b1 and b2
# and update the map. The variables b1 and b2 will have to be locked before reading
# (or may b not)
# This is jus test code. b1 initaised as above . The threads individually write to
# it by reading from the respective files. This main thread will read b1 and b2 and
# move the bot accordingly.
while(True):
if ( thread1.isAlive() or thread2.isAlive()):
continue
if( bot1_move_lock.acquire(False) && bot2_move_lock_acquire(False)):
print b1,b2
break
else:
pass
print "Exiting main thread"