Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle SIGINT signal from user - Zappy AI #245

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)