class ManageMicrophone(Thread): logger = logging.getLogger() def __init__(self, lock_clients, list_client): Thread.__init__(self) self.logger.debug("Initializing microphone thread") self.lock = lock_clients self.lclients = list_client self.mic = Microphone() self.mic.createMicroStream() def stop(self): self.logger.debug("Stop recording") self.continuer = False def run(self): self.logger.debug("Starting microphone") self.continuer = True buff = bytearray() while (self.continuer): if (len(self.lclients) != 0): buff = self.mic.getDataFromBuffer() if (self.lock.acquire(False)): for i in range(len(self.lclients)): self.lclients[i].setData(buff) self.lock.release() else: time.sleep(0.01) self.cleanup() def cleanup(self): self.logger.debug("Stoping microphone thread") self.mic.closeMicroStream()
class Server: def __init__(self, port): self.logger = logging.getLogger() self.logger.debug("Initializing server") self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind(("", port)) self.socket.listen() self.lock_client = Lock() self.list_client = [] def getClients(self): self.logger.debug("The server is getting the list of clients") return self.list_client def sendJson(self): self.logger.debug("The server is sending a json file to the clients") #self.json_data = SendJson("37.59.57.203", 55555, "json/manifest.JSON") self.json_data = SendJson(True, "37.59.57.203", 55555, "json/manifest.JSON") self.json_data.sendingJson() def createMicrophone(self): self.logger.debug("The server is starting the microphone thread") self.micro = Microphone() self.micro.createMicroStream() def whileAccept(self): self.logger.debug("The server is accepting client") continuer = True while (continuer): try: print(len(self.list_client)) conn, addr = self.socket.accept() #self.lock_client.acquire(True) self.list_client.append( ManageClient(conn, addr, self.lock_client, self.list_client)) self.list_client[-1].setMicrophone(self.micro) self.list_client[-1].start() #self.lock_client.release() except KeyboardInterrupt: continuer = False except Exception as e: self.logger.debug(e) continuer = False self.cleanup() def cleanup(self): self.logger.debug("The server is starting to clean up") self.micro.closeMicroStream() self.socket.shutdown(socket.SHUT_RDWR) self.socket.close()