Merge pull request 'spamCheck' (#1) from spamCheck into master

Reviewed-on: #1
This commit is contained in:
nolan 2025-10-07 18:35:43 -04:00
commit cc02fc9974
3 changed files with 67 additions and 7 deletions

45
TimeWaster.py Normal file
View File

@ -0,0 +1,45 @@
import time, uuid, signal, random, os
from enum import Enum
class JobStatus(Enum):
QUEUED = 1
FINISHED = 2
ERRORED = 3
class TimeWaster:
def __init__(self):
self.running = True
self.jobs: dict = {}
def handleNewJob(self):
msg = os.read(0, 1024)
if (msg) == "job":
jobId: uuid.UUID = uuid.uuid4()
self.jobs[jobId] = JobResult(jobId, JobStatus.QUEUED, "exists I guess")
os.write(1, self.jobs[jobId])
def handleCancelJob(self):
pass
def quit(self):
self.running = False
def main(self):
while self.running:
if len(self.jobs) == 0:
self.handleNewJob()
else:
job: JobResult = self.jobs[self.jobs.keys[0]]
time.sleep(random.randrange(1, 10) / 10)
job.status = JobStatus.FINISHED
job.result = "done i guess"
os.write(1, job)
class JobResult:
def __init__(self, uuid: uuid.UUID, status: JobStatus, result) -> None:
self.uuid = uuid
self.status = status
self.result = result
if __name__ == "__main__":
TimeWaster().main()

View File

@ -35,7 +35,12 @@ class TestClient:
async def receive(self) -> None:
while (self.connected):
response: str = await self.connection.recv()
data: dict = json.loads(response)
print(f"Received: {response}")
print(f"CURRENT MESSAGE ID: {self.msgId} - RECEIVED MESSAGE ID: {data["ID"]}")
if (data["ID"] != self.msgId - 1):
print("MESSAGE DISCARDED\n")
else:
print("MESSAGE UP TO DATE. ACCEPTED\n")
if __name__ == "__main__":
client = TestClient()

View File

@ -1,5 +1,6 @@
from websockets import *
import asyncio, json, signal, sys
from TimeWaster import *
import asyncio, json, signal, sys, time, random, subprocess, os
class WebSocketServer:
server: Server = None
@ -17,14 +18,23 @@ class WebSocketServer:
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
print(f"{connection.remote_address} Connected")
stdio: tuple(int, int) = os.pipe2()
worker: subprocess.Popen = subprocess.Popen(["python3", "TimeWaster.py"], stdin=stdio[0], stdout=stdio[1])
connected: bool = True
task: asyncio.Task = None
while (connected):
raw: str = await connection.recv()
message = json.loads(raw)
print(f"Received: {message["message"]} width id {message["ID"]}")
response = {"ID": message["ID"], "message": f"received: {message["message"]}"}
await connection.send(json.dumps(response))
print("Server replied")
os.write(stdio[1], bytes("job"))
if task != None and task.cancel(): print("TASK CANCELED")
task = asyncio.create_task(self.respond(raw, connection))
async def respond(self, raw: str, connection: ServerConnection) -> None:
message = json.loads(raw)
print(f"Received: {message["message"]} width id {message["ID"]}")
await asyncio.sleep(random.randrange(1, 10) / 10)
response = {"ID": message["ID"], "message": f"received: {message["message"]}"}
await connection.send(json.dumps(response))
print("Server replied")
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
print("Server controller. Now die")