|
@@ -4,6 +4,9 @@ import threading
|
|
|
import numpy as np
|
|
|
from socket import error as SocketError
|
|
|
from robot import Robot
|
|
|
+import time
|
|
|
+
|
|
|
+run_time = 5
|
|
|
|
|
|
class TagServer(threading.Thread):
|
|
|
def __init__(self, port, board_size):
|
|
@@ -22,11 +25,19 @@ class TagServer(threading.Thread):
|
|
|
self.tag = None
|
|
|
self.board_size = board_size
|
|
|
|
|
|
+ self.paused = True
|
|
|
self.runV = True
|
|
|
+
|
|
|
+ self.last = 0
|
|
|
+ self.mutex = threading.Lock()
|
|
|
+ self.mutex2 = threading.Lock()
|
|
|
self.start()
|
|
|
|
|
|
def updatePosition (self, idd, posx, posy, course):
|
|
|
- for thread in self.clients_threads:
|
|
|
+ self.mutex.acquire()
|
|
|
+ clients_threads = self.clients_threads
|
|
|
+ self.mutex.release()
|
|
|
+ for thread in clients_threads:
|
|
|
if thread.robot is not None and thread.robot.idd == idd:
|
|
|
msg = "P "
|
|
|
msg = msg + str(posx)+","
|
|
@@ -43,6 +54,7 @@ class TagServer(threading.Thread):
|
|
|
self.brodcast(msg)
|
|
|
|
|
|
|
|
|
+ self.mutex2.acquire()
|
|
|
for i in self.robots():
|
|
|
if i != self.tag:
|
|
|
continue
|
|
@@ -50,24 +62,32 @@ class TagServer(threading.Thread):
|
|
|
if i == j:
|
|
|
continue
|
|
|
if i.intersect(j):
|
|
|
- self.tag = j
|
|
|
- self.brodcast("RUN "+str(self.tag.id)+" 10")
|
|
|
-
|
|
|
- def startGame (self):
|
|
|
+ if self.last + run_time <= time.time():
|
|
|
+ print(i.idd, " pegou ", j.idd)
|
|
|
+ self.tag = j
|
|
|
+ self.brodcast("RUN "+str(self.tag.idd)+","+str(run_time))
|
|
|
+ self.last = time.time()
|
|
|
+ break
|
|
|
+ self.mutex2.release()
|
|
|
+
|
|
|
+ def stopGame (self):
|
|
|
+ self.paused = True
|
|
|
self.brodcast("STOP")
|
|
|
|
|
|
def startGame (self):
|
|
|
self.brodcast("START")
|
|
|
- if self.tag is None:
|
|
|
- tag = np.random.choice(self.robots(), 1)
|
|
|
- if len(tag) == 1:
|
|
|
- self.tag = tag
|
|
|
- self.brodcast("RUN "+str(self.tag.id)+" 10")
|
|
|
- return True
|
|
|
- return False
|
|
|
+ r = self.robots()
|
|
|
+ if len(r) >= 1:
|
|
|
+ tag = np.random.choice(r, 1)
|
|
|
+ self.tag = tag[0]
|
|
|
+ self.paused = False
|
|
|
+ self.brodcast("RUN "+str(self.tag.idd)+","+str(run_time))
|
|
|
|
|
|
def brodcast(self, msg):
|
|
|
- for thread in self.clients_threads:
|
|
|
+ self.mutex.acquire()
|
|
|
+ clients_threads = self.clients_threads
|
|
|
+ self.mutex.release()
|
|
|
+ for thread in clients_threads:
|
|
|
if thread.robot is not None:
|
|
|
thread.send(msg+"\n")
|
|
|
|
|
@@ -76,20 +96,29 @@ class TagServer(threading.Thread):
|
|
|
try:
|
|
|
(clientsock, (ip, port)) = self.sock.accept()
|
|
|
newthread = TagThread(ip, port, clientsock, self)
|
|
|
+ self.mutex.acquire()
|
|
|
self.clients_threads.append(newthread)
|
|
|
+ self.mutex.release()
|
|
|
+ self.stopGame()
|
|
|
except socket.timeout as err:
|
|
|
pass
|
|
|
self.sock.close()
|
|
|
|
|
|
def robots(self):
|
|
|
r = []
|
|
|
- for thread in self.clients_threads:
|
|
|
+ self.mutex.acquire()
|
|
|
+ clients_threads = self.clients_threads
|
|
|
+ self.mutex.release()
|
|
|
+ for thread in clients_threads:
|
|
|
if thread.robot is not None:
|
|
|
r.append(thread.robot)
|
|
|
return r
|
|
|
|
|
|
def stop(self):
|
|
|
- for thread in self.clients_threads:
|
|
|
+ self.mutex.acquire()
|
|
|
+ clients_threads = self.clients_threads
|
|
|
+ self.mutex.release()
|
|
|
+ for thread in clients_threads:
|
|
|
thread.stop()
|
|
|
self.runV = False
|
|
|
|
|
@@ -110,13 +139,14 @@ class TagThread(threading.Thread):
|
|
|
|
|
|
socket.settimeout(1)
|
|
|
self.runV = True
|
|
|
+ self.mutex = threading.Lock()
|
|
|
self.start()
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
while self.runV:
|
|
|
try:
|
|
|
- data = self.sock.recv(64).decode('ascii')
|
|
|
+ data = self.sock.recv(128).decode('ascii')
|
|
|
except socket.timeout as err:
|
|
|
continue
|
|
|
except SocketError as e:
|
|
@@ -124,6 +154,7 @@ class TagThread(threading.Thread):
|
|
|
break
|
|
|
data = data.split(" ")
|
|
|
try:
|
|
|
+
|
|
|
if data[0] == "SUB":
|
|
|
idd, xsize, ysize, xtag, ytag = data[1].split(",")
|
|
|
self.robot = Robot(int(idd), float(xsize), float(ysize), float(xtag), float(ytag))
|
|
@@ -133,62 +164,123 @@ class TagThread(threading.Thread):
|
|
|
posx, posy, curse = float(posx), float(posy), float(curse)
|
|
|
self.server.updatePosition(self.robot.idd, posx, posy, curse)
|
|
|
except Exception as e:
|
|
|
- print(e)
|
|
|
+ raise
|
|
|
self.send("ERR\n")
|
|
|
self.sock.close()
|
|
|
|
|
|
def send(self, msg):
|
|
|
+ self.mutex.acquire()
|
|
|
self.sock.sendall(msg.encode('ascii'))
|
|
|
+ self.mutex.release()
|
|
|
|
|
|
def stop(self):
|
|
|
self.runV = False
|
|
|
|
|
|
class TagClient(threading.Thread):
|
|
|
- def __init__(sself, ip, port):
|
|
|
+ def __init__(self, ip, port):
|
|
|
+ threading.Thread.__init__(self)
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
server_address = (ip, port)
|
|
|
sock.connect(server_address)
|
|
|
+ sock.settimeout(1)
|
|
|
self.sock = sock
|
|
|
- self.stop = None
|
|
|
- self.start = None
|
|
|
- self.tagger = None
|
|
|
+ self.stopf = None
|
|
|
+ self.startf = None
|
|
|
+ self.runf = None
|
|
|
self.position = None
|
|
|
|
|
|
+
|
|
|
+ self.runing = False
|
|
|
+ self.tag = None
|
|
|
+ self.positions = []
|
|
|
+ self.board = None
|
|
|
+
|
|
|
+ self.mutex = threading.Lock()
|
|
|
+
|
|
|
def info (self, idd, xsize, ysize, xtag, ytag):
|
|
|
+ idd, xsize, ysize, xtag, ytag = str(idd), str(xsize), str(ysize), str(xtag), str(ytag)
|
|
|
message = "SUB "+idd+","+xsize+","+ysize+","+xtag+","+ytag+"\n"
|
|
|
self.sock.sendall(message.encode('ascii'))
|
|
|
+ self.runV = True
|
|
|
self.start()
|
|
|
|
|
|
def run(self):
|
|
|
- while True:
|
|
|
+ data = ""
|
|
|
+ while self.runV:
|
|
|
try:
|
|
|
- data = self.sock.recv(64).decode('ascii')
|
|
|
+ data += self.sock.recv(256).decode('ascii')
|
|
|
+ except socket.timeout as err:
|
|
|
+ continue
|
|
|
except SocketError as e:
|
|
|
self.robot = None
|
|
|
break
|
|
|
- data = data.split("\n")[0].split(" ")
|
|
|
- if data[0] == "OK":
|
|
|
- self.board_size = self.position(data[1].split(","))
|
|
|
- print("SUB OK")
|
|
|
- elif data[0] == "P" and self.position is not None:
|
|
|
- self.position(data[1].split(","))
|
|
|
|
|
|
- def stopCallback (self, function):
|
|
|
- self.stop = function
|
|
|
+ if "\n" not in data:
|
|
|
+ continue
|
|
|
|
|
|
- def startCallback (self, function):
|
|
|
- self.start = function
|
|
|
+ tmp = data.split("\n")
|
|
|
+ if tmp[-1] != '':
|
|
|
+ data = tmp[-1]
|
|
|
+ tmp = tmp[0:-1]
|
|
|
+ else:
|
|
|
+ data = ""
|
|
|
+
|
|
|
+ for d in tmp:
|
|
|
+ d = d.split(" ")
|
|
|
+ if d[0] == "OK":
|
|
|
+ x, y = d[1].split(",")
|
|
|
+ x, y = float(x), float(y)
|
|
|
+ self.board = (x, y)
|
|
|
+
|
|
|
+ elif d[0] == "P":
|
|
|
+ posx, posy, course, idd, xsize, ysize, xtag, ytag = d[1].split(",")
|
|
|
+ posx = float(posx)
|
|
|
+ posy = float(posy)
|
|
|
+ course = float(course)
|
|
|
+ idd = int(idd)
|
|
|
+ xsize = float(xsize)
|
|
|
+ ysize = float(ysize)
|
|
|
+ xtag = float(xtag)
|
|
|
+ ytag = float(ytag)
|
|
|
+
|
|
|
+ self.mutex.acquire()
|
|
|
+ self.positions.append((posx, posy, course, idd, xsize, ysize, xtag, ytag))
|
|
|
+ self.mutex.release()
|
|
|
+
|
|
|
+ elif d[0] == "START":
|
|
|
+ print(d)
|
|
|
+ self.runing = True
|
|
|
+
|
|
|
+ elif d[0] == "STOP":
|
|
|
+ print(d)
|
|
|
+ self.runing = False
|
|
|
+
|
|
|
+ elif d[0] == "RUN":
|
|
|
+ print(d)
|
|
|
+ idd, interval = d[1].split(",")
|
|
|
+ self.tag = (int(idd), float(interval)+time.time())
|
|
|
+ self.sock.close()
|
|
|
|
|
|
- def taggerCallback (self, function):
|
|
|
- self.tagger = function
|
|
|
+ def getStatus(self):
|
|
|
+ return self.runing
|
|
|
|
|
|
- def positionCallback (self, function):
|
|
|
- self.position = function
|
|
|
+ def getBoard(self):
|
|
|
+ return self.board
|
|
|
|
|
|
- def boardinfoCallback (self, function):
|
|
|
- self.boardinfo = function
|
|
|
+ def getPosition(self):
|
|
|
+ i = self.positions
|
|
|
+ self.mutex.acquire()
|
|
|
+ self.positions = []
|
|
|
+ self.mutex.release()
|
|
|
+ return i
|
|
|
+
|
|
|
+ def getTagInfo(self):
|
|
|
+ return self.tag
|
|
|
|
|
|
|
|
|
- def setPosition(px, py):
|
|
|
- message = "P "+px+","+py+"\n"
|
|
|
+ def setPosition(self, px, py, course):
|
|
|
+ message = "P "+str(px)+","+str(py)+","+str(course)+"\n"
|
|
|
self.sock.sendall(message.encode('ascii'))
|
|
|
+
|
|
|
+ def stop(self):
|
|
|
+ self.runV = False
|