Refined dispatcher and threading. Added lifecycle registry
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from . import server
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
import importlib
|
||||
from lifecyclemanager import manageLifecycle
|
||||
from .dispatchstate import buildState
|
||||
|
||||
def buildDispatch(request):
|
||||
dispatch = []
|
||||
|
||||
for key in request["module"]:
|
||||
dispatch.append(request["module"][key])
|
||||
|
||||
resolveddispatch = "plugins."+ ".".join(dispatch)
|
||||
|
||||
module = importlib.import_module(resolveddispatch)
|
||||
|
||||
mode = request.get("mode", "oneshot")
|
||||
|
||||
return module, mode
|
||||
@@ -0,0 +1,22 @@
|
||||
from .createdispatch import buildDispatch
|
||||
from .dispatchstate import buildState
|
||||
from core.execution.createthread import buildThread
|
||||
|
||||
import threading
|
||||
|
||||
def dispatchRequest(request, lifecycles):
|
||||
|
||||
module, mode = buildDispatch(request)
|
||||
|
||||
mode, lifecycles = buildState(module, mode, lifecycles)
|
||||
|
||||
if mode == "stop":
|
||||
return lifecycles
|
||||
|
||||
else:
|
||||
threadnum, threadname, lifecycles, threadlist = buildThread(module, lifecycles)
|
||||
|
||||
#print("Active Threads")
|
||||
#print(threadlist)
|
||||
|
||||
return threadnum, threadname, lifecycles, threadlist
|
||||
@@ -0,0 +1,41 @@
|
||||
import requests
|
||||
|
||||
from core import registerLifecycle
|
||||
|
||||
def filterMode(mode):
|
||||
if mode in ("continuous", "oneshot", "stop"):
|
||||
state = mode
|
||||
return state
|
||||
else: raise ValueError(f"Unsupported lifecycle mode: {mode}")
|
||||
|
||||
def buildState(module, mode, lifecycles):
|
||||
state = filterMode(mode)
|
||||
|
||||
registryvalue = state
|
||||
registryname = "mode"
|
||||
|
||||
if state != "stop":
|
||||
lifecycles = registerLifecycle(module, lifecycles, registryname, registryvalue)
|
||||
|
||||
elif state == "stop":
|
||||
if not module.name in lifecycles:
|
||||
|
||||
print(f"No registered lifecycle: {module.name}")
|
||||
|
||||
return None
|
||||
|
||||
elif "mode" in lifecycles[module.name] != "stop":
|
||||
lifecycles = registerLifecycle(module, lifecycles, registryname, registryvalue)
|
||||
|
||||
print(f"Stopping lifecycle: {module.name}")
|
||||
|
||||
#elif mode == "oneshot":
|
||||
#registryvalue = {"mode": "oneshot"}
|
||||
#lifecycles = registerLifecycle(module, lifecycles, registryname, registryvalue)
|
||||
|
||||
# else:
|
||||
#raise ValueError(f"Unsupported lifecycle mode: {mode}")
|
||||
|
||||
#state = registryvalue
|
||||
|
||||
return state, lifecycles
|
||||
@@ -0,0 +1,30 @@
|
||||
import socket
|
||||
import json
|
||||
|
||||
def startServer():
|
||||
from .dispatcher import dispatchRequest
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
server.bind(("127.0.0.1", 19002))
|
||||
|
||||
lifecycles = {}
|
||||
|
||||
server.listen()
|
||||
|
||||
while True:
|
||||
connection, address = server.accept()
|
||||
|
||||
while True:
|
||||
data = connection.recv(4096)
|
||||
|
||||
request = json.loads(data)
|
||||
|
||||
# if not data:
|
||||
# break
|
||||
|
||||
dispatch = dispatchRequest(request, lifecycles)
|
||||
|
||||
connection.close()
|
||||
|
||||
# server.close()
|
||||
Reference in New Issue
Block a user