-
Notifications
You must be signed in to change notification settings - Fork 0
Server
This P2P server allows peers to register, unregister, and manage files. It uses Flask for the web server and supports various endpoints for peer and file management.
The server configuration is stored in config.yaml:
port: 8000 # Port to listen for incoming control messages
peer:
port: 8001 # Port to listen for incoming data or to request data
auth:
token: "secret" # Token to authenticateURL: /register
Method: POST
Description: Registers a peer in the server and returns a token for future requests.
Expected JSON:
{
"client_id": "string",
"ip": "string",
"port": int,
"genericToken": "string"
}Responses:
-
200 OK:
{ "token": "string" } -
400 Bad Request:
{ "message": "Peer already registered" } -
401 Unauthorized:
{ "message": "Invalid token" }
URL: /unregister
Method: POST
Description: Unregisters a peer and removes all associated files.
Expected JSON:
{
"token": "string"
}Responses:
-
200 OK:
{ "message": "Peer unregistered" } -
404 Not Found:
{ "message": "Invalid request" } -
404 Not Found:
{ "message": "Peer not found" }
URL: /add_file
Method: POST
Description: Adds a file to the server.
Expected JSON:
{
"token": "string",
"file_name": "string"
}Responses:
-
200 OK:
{ "message": "File added" } -
400 Bad Request:
{ "message": "Invalid request" } -
404 Not Found:
{ "message": "Peer not found" }
URL: /get_files
Method: GET
Description: Retrieves a list of files available on the server.
Expected JSON:
{
"token": "string"
}Responses:
-
200 OK:
{ "files": ["string"] } -
400 Bad Request:
{ "message": "Invalid request" } -
404 Not Found:
{ "message": "Peer not found" }
URL: /get_file
Method: GET
Description: Retrieves the address of a file from the server.
Expected JSON:
{
"token": "string",
"file_name": "string"
}Responses:
-
200 OK:
{ "file_address": "string" } -
400 Bad Request:
{ "message": "Invalid request" } -
404 Not Found:
{ "message": "File not found" } -
401 Unauthorized:
{ "message": "Peer not found" }
Represents a file in the system.
class File:
def __init__(self, filename: str, Peer: 'Peer') -> None:
self.filename = filename
self.Peer = Peer
def __str__(self) -> str:
return self.filenameRepresents a peer in the system.
class Peer:
def __init__(self, client_id: str, ip: str, port: int, token: str) -> None:
self.ip = ip
self.port = port
self.client_id = client_id
self.token = token
def __iter__(self) -> iter:
return iter(self.fileList)Handles peer and file management.
class PeerHandler:
def __init__(self) -> None:
self.peerList = []
self.fileList = []
def removePeerFiles(self, client_id: str) -> None:
for file in self.fileList:
if file.Peer.client_id == client_id:
self.fileList.remove(file)
def addFile(self, filename: str, token: str) -> None:
for peer in self.peerList:
if peer.token == token:
newFile = File(filename, peer)
self.fileList.append(newFile)
def addPeer(self, client_id: str, ip: str, port: int, token: str) -> None:
newPeer = Peer(client_id, ip, port, token)
self.peerList.append(newPeer)
def removePeer(self, token: str) -> None:
for peer in self.peerList:
if peer.token == token:
client_id = peer.client_id
self.peerList.remove(peer)
self.removePeerFiles(client_id)
def getFileList(self) -> list[str]:
fileList = []
for file in self.fileList:
fileList.append(file.filename)
return fileList
def getFileAddress(self, filename: str) -> str | None:
for file in self.fileList:
if file.filename == filename:
return f"grpc://{file.Peer.ip}:{file.Peer.port}/{filename}"
return None
def authPeer(self, token: str) -> bool:
for peer in self.peerList:
if peer.token == token:
return True
return False
def peerExists(self, client_id: str) -> bool:
for peer in self.peerList:
if peer.client_id == client_id:
return True
return FalseLoads configuration from config.yaml.
import yaml
class config:
def __init__(self) -> None:
with open('config.yaml') as file:
config = yaml.load(file, Loader=yaml.FullLoader)
self.token = config['auth']['token']
self.control_port = config['control']['port']
self.dataPort = config['peer']['port']