Skip to content

Commit

Permalink
Merge pull request #41 from martius-lab/carina/basic-versioning
Browse files Browse the repository at this point in the history
Added basic versioning
  • Loading branch information
Cari1111 authored Dec 18, 2023
2 parents 62c4f62 + 35647f1 commit 6863860
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 17 deletions.
3 changes: 1 addition & 2 deletions teamprojekt_competition_server/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@


class COMPClient(CommandLocator):
"""client that manages the connection over the protocoll with the server"""
"""client that manages the connection over the protocol with the server"""

def __init__(self, agent) -> None:
self.connected = False
self.version = 1
self.agent = agent

def connect_client(self, token: str):
Expand Down
17 changes: 15 additions & 2 deletions teamprojekt_competition_server/client/client_protocol.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""class for client protocol"""
import logging as log

from twisted.internet.interfaces import IAddress
from twisted.protocols import amp
from twisted.internet.protocol import ClientFactory, Protocol

from ..shared.commands import StartGame, EndGame, Step, Auth

VERSION = 1


class COMPClientProtocol(amp.AMP):
"""protocol for the client"""
Expand All @@ -17,6 +20,16 @@ def __init__(self, agent, token, boxReceiver=None, locator=None):
self.agent = agent
self.token = token

def connectionMade(self):
"""is called when the connection to the server is made"""
log.debug("connected to server")
return super().connectionMade()

def connectionLost(self, reason):
"""is called when the connection to the server is made"""
log.debug(f"disconnected from the server. reason: {reason}")
return super().connectionLost(reason)

def start_game(self, game_id: int):
"""is called when the server starts the game
Expand Down Expand Up @@ -50,7 +63,7 @@ def step(self, obv):
"""is called when the server wants the client to make a step
Args:
env (int): enviroment given by the server
env (int): environment given by the server
Returns:
{"action": int}: action that should be executed
Expand All @@ -67,7 +80,7 @@ def auth(self):
Returns:
{"token": String}: the clients auth token
"""
return {"token": str.encode(self.token), "version": 1}
return {"token": str.encode(self.token), "version": VERSION}

Auth.responder(auth)

Expand Down
10 changes: 7 additions & 3 deletions teamprojekt_competition_server/client/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""main class with dummy agend for testing"""
from .agent import COMPAgent
"""main class with dummy agent for testing"""
import logging as log

from .agent import COMPAgent
import random

log.basicConfig(level=log.DEBUG)


# run with "python -m teamprojekt_competition_server.client.main"

if __name__ == "__main__":
Expand All @@ -14,7 +18,7 @@ def step(self, obv: list[float]) -> list[float]:
"""dummy step function
Args:
obv (list(float)): enviroment
obv (list[float]): environment
Returns:
list(float): action
Expand Down
2 changes: 1 addition & 1 deletion teamprojekt_competition_server/server/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class COMPServerFactory(ServerFactory):
"""factory for COMP servers"""

def buildProtocol(self, addr: IAddress) -> Protocol | None:
"""builds the protocoll"""
"""builds the protocol"""
protocol: COMPServerProtocol = COMPServerProtocol()

new_player = COMPPlayer(protocol)
Expand Down
10 changes: 10 additions & 0 deletions teamprojekt_competition_server/server/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def add_player(self, player: IPlayer) -> None:

player.id = len(self.players) - 1

def delete_player(self, player_id: int):
"""delete a player from the player array
Args:
player_id (int): ID of the player
"""
self.players.pop(player_id)
for id in range(player_id, len(self.players)):
self.players[id].id = id

def add_player_to_queue(self, player_id: int):
"""adds a player to the queue
Expand Down
2 changes: 1 addition & 1 deletion teamprojekt_competition_server/server/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_action(self, obv, result_callback) -> IAction:
"""gets an action from the player
Args:
obv (Any): obserervation
obv (Any): observation
result_callback (Callable): callback
Returns:
Expand Down
11 changes: 8 additions & 3 deletions teamprojekt_competition_server/server/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ def connected():
result_callback=lambda x: game_manager.add_player_to_queue(self.id)
)

def disconnected():
"""remove the player from the game manager"""
game_manager.delete_player(player_id=self.id)

self.connection.addConnectionMadeCallback(connected)
self.connection.addConnectionLostCallback(disconnected)

def authenticate(self, result_callback):
"""authenticates player
Args: resul_callback (callback function)
Args: result_callback (callback function)
Returns: token (string)"""
return self.connection.get_token(result_callback)
self.connection.get_token(result_callback)

def notify_start(self):
"""notifies start of game"""
Expand All @@ -37,7 +42,7 @@ def get_action(self, obv, result_callback):
Args:
obv(any): observation"""
return self.connection.get_step(obv, result_callback)
self.connection.get_step(obv, result_callback)

def notify_end(self, result, stats):
"""called when game ends
Expand Down
20 changes: 15 additions & 5 deletions teamprojekt_competition_server/server/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from ..shared.commands import Auth, StartGame, EndGame, Step

VERSION: int = 1


class COMPServerProtocol(amp.AMP):
"""amp protocol for a COMP server"""
Expand Down Expand Up @@ -38,7 +40,7 @@ def connectionMade(self) -> None:
"""called upon connectionMade event"""
addr: IAddress = self.transport.getPeer() # type: ignore
debug_msg = f"connected to client with IP: {addr.host}"
debug_msg_rest = f", Port: {addr.port} viae {addr.type}"
debug_msg_rest = f", Port: {addr.port} via {addr.type}"
log.debug(debug_msg + debug_msg_rest)
# broadcast to callbacks
for c in self.connection_made_callbacks:
Expand All @@ -48,6 +50,7 @@ def connectionMade(self) -> None:

def connectionLost(self, reason):
"""called upon connectionLost event"""
log.debug("connection to client lost")
for c in self.connection_lost_callbacks:
c()

Expand All @@ -59,9 +62,16 @@ def get_token(self, return_callback: Callable[[str], None]) -> None:
Args:
game (Game): game that starts
"""
self.callRemote(Auth).addCallback(
callback=lambda res: return_callback(res["token"])
)

def callback(res):
if res["version"] == VERSION:
return_callback(res["token"])
else:
log.error("Client with wrong version tried to authenticate.")
# TODO send error to client
self.transport.loseConnection()

self.callRemote(Auth).addCallback(callback=callback)

def notify_start(self) -> None:
"""starts the game
Expand All @@ -72,7 +82,7 @@ def notify_start(self) -> None:
return self.callRemote(StartGame, game_id=222)

def get_step(self, obv, return_callback: Callable[[list], None]) -> None:
"""perfroms step requested by player"""
"""performs step requested by player"""

# TODO the obv is currently cast to int, as only ints are allowed.
return self.callRemote(Step, obv=obv).addCallback(
Expand Down

0 comments on commit 6863860

Please sign in to comment.