Skip to content

Commit

Permalink
Merge pull request #245 from FppEpitech/feat/handle-sigint-signal-fro…
Browse files Browse the repository at this point in the history
…m-user-zappy-ai

Handle SIGINT signal from user - Zappy AI
  • Loading branch information
Marius-P1 authored Jun 22, 2024
2 parents b446ba2 + 592d963 commit 24f1abd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions ai/src/AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ def __init__(self, host, port, teamName):
self.threads = []
self.creationTime = time.time_ns()
self.myuuid = str(uuid.uuid4())
self.isRunning = True


def serverCommunicationInThread(self):
"""
Handle the communication with the server in a thread
"""
while True:
while self.isRunning:
if self.player.currentMode == Mode.REGROUP and self.player.isLeader == Role.SLAVE:
break
for _ in range(0, len(self.player.callbacks)):
Expand All @@ -83,7 +84,7 @@ def serverCommunicationInThread(self):
self.player.commands.pop(0)
self.player.callbacks.pop(0)
print("Regrouping Start", flush=True, file=sys.stderr)
while True:
while self.isRunning:
responses = self.api.receiveData(0.1)
if responses is not None :
responses = responses.split("\n")
Expand Down
7 changes: 6 additions & 1 deletion ai/src/Network/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def sendData(self, data : str, timeout : int = None):
the timeout to wait for the server to be ready to receive data
(default is None which means no timeout)
"""
if -1 in self.outputs:
return
_, write, _ = select.select([], self.outputs, [], timeout)

if data[-1] != '\n':
Expand All @@ -109,6 +111,8 @@ def receiveData(self, timeout : float = None):
the timeout to wait for the server to send data
(default is None which means no timeout)
"""
if -1 in self.inputs:
return None
readable, _, _ = select.select(self.inputs, [], [], timeout)
for s in readable:
if s == self.sock:
Expand Down Expand Up @@ -157,7 +161,8 @@ def initConnection(self, teamName : str, fileName : str = ""):
data = data.split(' ')
else:
clientNum = received.replace('\n', '')
data = self.receiveData().replace('\n', '').split(' ')
data = self.receiveData()
data = data[0:data.find('\n')].split(' ')

if len(data) != 2:
raise APIException("invalid map size", fileName)
Expand Down
16 changes: 12 additions & 4 deletions ai/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,19 @@ def main():
host, port, teamName = getArgs()
try:
ai = AI(host, port, teamName)
ai.run()
try:
ai.run()
except KeyboardInterrupt:
ai.api.close()
ai.isRunning = False
ai.threads[0].join()
print("The AI has been stopped by a keyboard interrupt", file=sys.stderr)
except PlayerDeathException as e:
print(e, file=sys.stderr)
if ai.player.isLeader == Role.LEADER:
print("The leader is dead, the AI will fork", file=sys.stderr)
main() # TODO: Need to be fixed (can't connect if there is no slot available)
sys.exit(0)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(84)


if __name__ == "__main__":
Expand All @@ -88,3 +95,4 @@ def main():
except Exception as e:
print(e, file=sys.stderr)
sys.exit(84)
sys.exit(0)

0 comments on commit 24f1abd

Please sign in to comment.