123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- import socket
- import sys
- import threading
- import numpy as np
- from socket import error as SocketError
- from robot import Robot
- class TagServer(threading.Thread):
- def __init__(self, port, board_size):
- threading.Thread.__init__(self)
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server_address = ('0.0.0.0', port)
- print ('Starting up on %s port %s' % server_address)
- sock.bind(server_address)
- sock.listen(1)
- sock.settimeout(1)
- self.sock = sock
- self.clients_threads = []
- self.tag = None
- self.board_size = board_size
- self.runV = True
- self.start()
- def updatePosition (self, idd, posx, posy, course):
- for thread in self.clients_threads:
- if thread.robot is not None and thread.robot.idd == idd:
- msg = "P "
- msg = msg + str(posx)+","
- msg = msg + str(posy)+","
- msg = msg + str(course)+","
- msg = msg + str(thread.robot.idd)+","
- msg = msg + str(thread.robot.xsize)+","
- msg = msg + str(thread.robot.ysize)+","
- msg = msg + str(thread.robot.xtag)+","
- msg = msg + str(thread.robot.ytag)
- thread.robot.posx = posx
- thread.robot.posy = posy
- thread.robot.course = course
- self.brodcast(msg)
- # check for collision
- for i in self.robots():
- if i != self.tag:
- continue
- for j in self.robots():
- if i == j:
- continue
- if i.intersect(j):
- self.tag = j
- self.brodcast("RUN "+str(self.tag.id)+" 10")
- def startGame (self):
- 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
- def brodcast(self, msg):
- for thread in self.clients_threads:
- if thread.robot is not None:
- thread.send(msg+"\n")
- def run(self):
- while self.runV:
- try:
- (clientsock, (ip, port)) = self.sock.accept()
- newthread = TagThread(ip, port, clientsock, self)
- self.clients_threads.append(newthread)
- except socket.timeout as err:
- pass
- self.sock.close()
- def robots(self):
- r = []
- for thread in self.clients_threads:
- if thread.robot is not None:
- r.append(thread.robot)
- return r
- def stop(self):
- for thread in self.clients_threads:
- thread.stop()
- self.runV = False
- class TagThread(threading.Thread):
- def __init__(self, ip, port, socket, server):
- threading.Thread.__init__(self)
- self.ip = ip
- self.port = port
- self.sock = socket
- self.points = 0
- self.robot = None
- self.bz = server.board_size
- self.server = server
- socket.settimeout(1)
- self.runV = True
- self.start()
- def run(self):
- while self.runV:
- try:
- data = self.sock.recv(64).decode('ascii')
- except socket.timeout as err:
- continue
- except SocketError as e:
- self.robot = None
- 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))
- self.send("OK "+str(self.bz[0])+","+str(self.bz[1])+"\n")
- if data[0] == "P":
- posx, posy, curse = data[1].split(",")
- posx, posy, curse = float(posx), float(posy), float(curse)
- self.server.updatePosition(self.robot.idd, posx, posy, curse)
- except Exception as e:
- print(e)
- self.send("ERR\n")
- self.sock.close()
- def send(self, msg):
- self.sock.sendall(msg.encode('ascii'))
- def stop(self):
- self.runV = False
- class TagClient(threading.Thread):
- def __init__(sself, ip, port):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server_address = (ip, port)
- sock.connect(server_address)
- self.sock = sock
- self.stop = None
- self.start = None
- self.tagger = None
- self.position = None
- def info (self, idd, xsize, ysize, xtag, ytag):
- message = "SUB "+idd+","+xsize+","+ysize+","+xtag+","+ytag+"\n"
- self.sock.sendall(message.encode('ascii'))
- self.start()
- def run(self):
- while True:
- try:
- data = self.sock.recv(64).decode('ascii')
- 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
- def startCallback (self, function):
- self.start = function
- def taggerCallback (self, function):
- self.tagger = function
- def positionCallback (self, function):
- self.position = function
- def boardinfoCallback (self, function):
- self.boardinfo = function
- # Somente na simulacao
- def setPosition(px, py):
- message = "P "+px+","+py+"\n"
- self.sock.sendall(message.encode('ascii'))
|