Compare commits
No commits in common. "cc02fc9974c9c4675ed0ec02256da3a9083ec173" and "d9d3e46d4411c520e5d07fbe0d376c942f37bc05" have entirely different histories.
cc02fc9974
...
d9d3e46d44
@ -1,45 +0,0 @@
|
|||||||
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()
|
|
@ -35,12 +35,7 @@ class TestClient:
|
|||||||
async def receive(self) -> None:
|
async def receive(self) -> None:
|
||||||
while (self.connected):
|
while (self.connected):
|
||||||
response: str = await self.connection.recv()
|
response: str = await self.connection.recv()
|
||||||
data: dict = json.loads(response)
|
|
||||||
print(f"Received: {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__":
|
if __name__ == "__main__":
|
||||||
client = TestClient()
|
client = TestClient()
|
||||||
|
22
server.py
22
server.py
@ -1,6 +1,5 @@
|
|||||||
from websockets import *
|
from websockets import *
|
||||||
from TimeWaster import *
|
import asyncio, json, signal, sys
|
||||||
import asyncio, json, signal, sys, time, random, subprocess, os
|
|
||||||
|
|
||||||
class WebSocketServer:
|
class WebSocketServer:
|
||||||
server: Server = None
|
server: Server = None
|
||||||
@ -18,23 +17,14 @@ class WebSocketServer:
|
|||||||
|
|
||||||
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
|
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
|
||||||
print(f"{connection.remote_address} Connected")
|
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
|
connected: bool = True
|
||||||
task: asyncio.Task = None
|
|
||||||
while (connected):
|
while (connected):
|
||||||
raw: str = await connection.recv()
|
raw: str = await connection.recv()
|
||||||
os.write(stdio[1], bytes("job"))
|
message = json.loads(raw)
|
||||||
if task != None and task.cancel(): print("TASK CANCELED")
|
print(f"Received: {message["message"]} width id {message["ID"]}")
|
||||||
task = asyncio.create_task(self.respond(raw, connection))
|
response = {"ID": message["ID"], "message": f"received: {message["message"]}"}
|
||||||
|
await connection.send(json.dumps(response))
|
||||||
async def respond(self, raw: str, connection: ServerConnection) -> None:
|
print("Server replied")
|
||||||
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
|
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
|
||||||
print("Server controller. Now die")
|
print("Server controller. Now die")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user