Skip to content

Commit

Permalink
Merge pull request #44 from X-R-G-B/dev
Browse files Browse the repository at this point in the history
Release v0.1
  • Loading branch information
Saverio976 authored Jun 9, 2023
2 parents ad056d9 + 6ee9b2b commit 411bdab
Show file tree
Hide file tree
Showing 145 changed files with 6,319 additions and 429 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/c-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ jobs:
if [ ! -f 'coding-style-reports.log' ]; then exit 0; fi
if [ -s 'coding-style-reports.log' ]; then cat 'coding-style-reports.log' && exit 1; fi
working-directory: ${{ env.workdir }}

tests_run:
runs-on: ubuntu-latest
container: epitechcontent/epitest-docker:latest
needs: [banana]
env:
workdir: ./src/SERVER

steps:
- uses: actions/checkout@v3
- name: Install Deps
run: make tests_run
working-directory: ${{ env.workdir }}
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,8 @@ tags
# Persistent undo
[._]*.un~

### VisualStudioCode ###
*.vscode/*
!.vscode/settings.json
.vscode/*
.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
Expand Down Expand Up @@ -1003,3 +1002,7 @@ FodyWeavers.xsd
zappy_ai
zappy_gui
zappy_server
vgcore.*
*.clang-format
*.vscode/
*.idea
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ fclean: $(TARGET_ai)_fclean
# ----------------------------------------------------------------------------

re: fclean all

# ----------------------------------------------------------------------------
# TESTS
# ----------------------------------------------------------------------------

tests_run: fclean
$(MAKE) -C $(TARGET_server_DIR) tests_run
Binary file added src/AI/joblib/food.joblib
Binary file not shown.
2 changes: 2 additions & 0 deletions src/AI/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ exclude = [
"Makefile",
"/zappy_ai.py"
]
[tool.ruff]
ignore = ["E501"]
13 changes: 8 additions & 5 deletions src/AI/zappy_ai.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python3

from zappy_ia.Client import Client
from zappy_ia.IA import IA
import argparse
import sys


class Argparse(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
sys.stderr.write("error: %s\n" % message)
self.print_help()
sys.exit(84)

Expand All @@ -21,8 +20,12 @@ def error(self, message):

argParse.add_argument("-p", type=int, help="port number", required=True)
argParse.add_argument("-n", type=str, help="name of the team", required=True)
argParse.add_argument("-h", type=str, help="name of the machine", default="localhost")
argParse.add_argument("-help", action="help", help="show this help message and exit")
argParse.add_argument(
"-h", type=str, help="name of the machine", default="localhost"
)
argParse.add_argument(
"-help", action="help", help="show this help message and exit"
)

args = argParse.parse_args()
port = args.p
Expand Down
69 changes: 39 additions & 30 deletions src/AI/zappy_ia/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@

import socket
import select
import sys
import threading
import time
from typing import List


class Client:
def __init__(self, port: int, server_ip: str = "localhost"):
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_ip = server_ip
self.isConnected = False
self.port = port
self.client_socket: socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_ip: int = server_ip
self.isConnected: bool = False
self.port: int = port

self.inTreatment = 0
self.inTreatment: int = 0

self.stopLock = threading.Lock()
self.stop = False
self.stopLock: threading.Lock = threading.Lock()
self.stop: bool = False

self.receivedLock = threading.Lock()
self.messReceived = []
self.receivedLock: threading.Lock = threading.Lock()
self.messReceived: List[str] = []

self.sendLock = threading.Lock()
self.messToSend = []
self.sendLock: threading.Lock = threading.Lock()
self.messToSend: List[str] = []

self.thread = threading.Thread(target=self.connect)
self.thread: threading.Thread = threading.Thread(target=self.connect)
self.thread.start()
time.sleep(0.1)

Expand All @@ -35,9 +36,11 @@ def connect(self):
self.client_socket.setblocking(False)

self.stopLock.acquire()
while self.isConnected and self.stop == False:
while self.isConnected and self.stop is False:
self.stopLock.release()
read_sockets, write_sockets, error_sockets = select.select([self.client_socket], [self.client_socket], [self.client_socket], 0)
read_sockets, write_sockets, error_sockets = select.select(
[self.client_socket], [self.client_socket], [self.client_socket], 0
)
self.read(read_sockets)
self.write(write_sockets)
self.handleError(error_sockets)
Expand All @@ -46,58 +49,64 @@ def connect(self):
self.client_socket.close()

def read(self, read_sockets):
for socket in read_sockets:
if (socket == self.client_socket):
message = socket.recv(2048).decode()
for socket_ in read_sockets:
if socket_ == self.client_socket:
message = socket_.recv(2048).decode()
if message:
self.receivedLock.acquire()
print("Recv: ", end="")
print(message.split("\n")[:-1])
self.messReceived.insert(0, message)
self.receivedLock.release()
if (self.inTreatment > 0):
if self.inTreatment > 0:
self.inTreatment -= 1
else:
self.client_socket.close()
self.isConnected = False

def write(self, write_sockets):
for socket in write_sockets:
if (socket == self.client_socket and self.inTreatment < 10):
for socket_ in write_sockets:
if socket_ == self.client_socket and self.inTreatment < 10:
self.sendLock.acquire()
if (len(self.messToSend) != 0):
if len(self.messToSend) != 0:
message = self.messToSend[-1]
print("Send: ", end="")
print(message.split("\n")[:-1])
self.messToSend = self.messToSend[:-1]
self.sendLock.release()
if (message != '\n'):
if message != "\n":
self.inTreatment += 1
self.client_socket.sendall(message.encode())
else:
self.sendLock.release()

def handleError(self, error_sockets):
for socket in error_sockets:
if (socket == self.client_socket):
for socket_ in error_sockets:
if socket_ == self.client_socket:
raise Exception("Socket error")

def input(self, message: str):
def input(self, message: str, arg: str = ""):
if arg != "":
message += " " + arg + "\n"
self.sendLock.acquire()
self.messToSend.insert(0, message)
self.sendLock.release()

def output(self) -> str:
res = ""
self.receivedLock.acquire()
if (len(self.messReceived) != 0):
if len(self.messReceived) != 0:
message = self.messReceived[-1]
self.messReceived = self.messReceived[:-1]
self.receivedLock.release()
if (message != "" or message != "\n"):
if message != "" or message != "\n":
res = message
else:
self.receivedLock.release()
time.sleep(0.1)
return res

def stopClient(self):
self.stopLock.acquire()
self.stopLock.acquire()
self.stop = True
self.stopLock.release()
self.stopLock.release()
Loading

0 comments on commit 411bdab

Please sign in to comment.