Skip to content

Commit

Permalink
seems really good (except ws/update)
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Oct 3, 2023
1 parent 23d7b2e commit 1b26254
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 40 deletions.
13 changes: 7 additions & 6 deletions htagweb/appserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import redys.v2

from htagweb.server import hrserver
from htagweb.server.client import HrPilot
from htagweb.server.client import HrClient

logger = logging.getLogger(__name__)
####################################################
Expand Down Expand Up @@ -135,6 +135,7 @@ async def _sendback(self,websocket, txt:str) -> bool:
return False

async def loop_tag_update(self, event, websocket):
#TODO: there is trouble here sometimes ... to fix !
with redys.v2.AClient() as bus:
await bus.subscribe(event)

Expand All @@ -149,7 +150,7 @@ async def on_connect(self, websocket):
#====================================================== get the event
fqn=websocket.path_params.get("fqn","")
uid=websocket.scope["uid"]
event=HrPilot(uid,fqn).event_response+"_update"
event=HrClient(uid,fqn).event_response+"_update"
#======================================================

await websocket.accept()
Expand All @@ -165,7 +166,7 @@ async def on_receive(self, websocket, data):
data = crypto.decrypt(data.encode(),parano_seed( uid )).decode()
data=json.loads(data)

p=HrPilot(uid,fqn)
p=HrClient(uid,fqn)

actions=await p.interact( oid=data["id"], method_name=data["method"], args=data["args"], kargs=data["kargs"], event=data.get("event") )

Expand All @@ -175,7 +176,7 @@ async def on_disconnect(self, websocket, close_code):
#====================================================== get the event
fqn=websocket.path_params.get("fqn","")
uid=websocket.scope["uid"]
event=HrPilot(uid,fqn).event_response+"_update"
event=HrClient(uid,fqn).event_response+"_update"
#======================================================

with redys.v2.AClient() as bus:
Expand Down Expand Up @@ -307,7 +308,7 @@ async def serve(self, request, obj ) -> HTMLResponse:
""" % locals()

p = HrPilot(uid,fqn,js,self.sesprovider.__name__)
p = HrClient(uid,fqn,js,self.sesprovider.__name__)

args,kargs = commons.url2ak(str(request.url))
html=await p.start(*args,**kargs)
Expand All @@ -318,7 +319,7 @@ async def HRHttp(self,request) -> PlainTextResponse:
fqn = request.path_params.get("fqn","")
seed = parano_seed( uid )

p=HrPilot(uid,fqn)
p=HrClient(uid,fqn)
data = await request.body()

if self.parano:
Expand Down
33 changes: 26 additions & 7 deletions htagweb/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ def importClassFromFqn(fqn_norm:str) -> type:



def process(hid,event_response,event_interact,fqn,js,init,sesprovidername):
def process(uid,hid,event_response,event_interact,fqn,js,init,sesprovidername):
#''''''''''''''''''''''''''''''''''''''''''''''''''''
if sesprovidername is None:
sesprovidername="MemDict"
import htagweb.sessions
FactorySession=getattr(htagweb.sessions,sesprovidername)
#''''''''''''''''''''''''''''''''''''''''''''''''''''

uid=hid.split("_")[0]

pid = os.getpid()
async def loop():
with redys.v2.AClient() as bus:
Expand Down Expand Up @@ -200,10 +198,7 @@ def killall(ps:dict):

print("hrserver_orchestrator stopped")

async def hrserver():
s=redys.v2.Server()
s.start()

async def wait_redys():
bus=redys.v2.AClient()
while 1:
try:
Expand All @@ -213,8 +208,32 @@ async def hrserver():
pass
await asyncio.sleep(0.1)

async def wait_hrserver():
bus=redys.v2.AClient()
while 1:
try:
if await bus.get("hrserver_orchestrator_running"):
break
except Exception as e:
print(e)
await asyncio.sleep(0.5)


async def kill_hrserver():
bus=redys.v2.AClient()
await bus.publish( EVENT_SERVER, dict(cmd=CMD_EXIT) ) # kill orchestrator loop


async def hrserver():
s=redys.v2.Server()
s.start()

await wait_redys()

await hrserver_orchestrator()

s.stop()




Expand Down
6 changes: 4 additions & 2 deletions htagweb/server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

TIMEOUT=20 # sec to wait answer from redys server #TODO: set better

class HrPilot:
class HrClient:
def __init__(self,uid:str,fqn:str,js:str=None,sesprovidername=None):
""" !!!!!!!!!!!!!!!!!!!! if js|sesprovidername is None : can't do a start() !!!!!!!!!!!!!!!!!!!!!!"""
self.uid=uid
self.fqn=fqn
self.js=js
self.bus = redys.v2.AClient()
Expand Down Expand Up @@ -47,6 +48,7 @@ async def start(self,*a,**k) -> str:

# start the process app
assert await self.bus.publish( EVENT_SERVER , dict(
uid=self.uid,
hid=self.hid,
event_response=self.event_response,
event_interact=self.event_interact,
Expand Down Expand Up @@ -99,7 +101,7 @@ async def clean():

async def main():
uid ="u1"
p=HrPilot(uid,"obj:App","//")
p=HrClient(uid,"obj:App","//")
#~ html=await p.start()
#~ print(html)

Expand Down
1 change: 0 additions & 1 deletion htagweb/simpleserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ async def serve(self, request, obj ) -> HTMLResponse:
jsinc = ""
jsinc += "\nasync function _read_(x) {return x}\n"
jsinc += "\nasync function _write_(x) {return x}\n"
#TODO: consider https://developer.chrome.com/blog/removing-document-write/

jsbootstrap="""
%(jsinc)s
Expand Down
43 changes: 19 additions & 24 deletions test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import multiprocessing,threading
import time
from htagweb.appserver import processHrServer
from htagweb.server.client import HrPilot
from htagweb.server import kill_hrserver, wait_hrserver
from htagweb.server.client import HrClient
import redys.v2
import threading

Expand All @@ -14,40 +15,34 @@ def server():
p=multiprocessing.Process(target=processHrServer)
p.start()

asyncio.run( wait_hrserver() )

yield "x"

p.terminate()
asyncio.run( kill_hrserver() )


@pytest.mark.asyncio
async def test_base( server ):

# while 1:
# try:
# if await redys.v2.AClient().get("hrserver_orchestrator_running"):
# break
# except Exception as e:
# print(e)
# await asyncio.sleep(0.5)


uid ="u1"
p=HrPilot(uid,"test_hr:App","//")
# html=await p.start()
# assert html.startswith("<!DOCTYPE html><html>")
p=HrClient(uid,"test_hr:App","//")
html=await p.start()
assert html.startswith("<!DOCTYPE html><html>")

# actions=await p.interact( oid="ut", method_name="doit", args=[], kargs={}, event={} )
# assert "update" in actions
actions=await p.interact( oid="ut", method_name="doit", args=[], kargs={}, event={} )
assert "update" in actions




if __name__=="__main__":
p=multiprocessing.Process(target=processHrServer)
try:
p.start()
time.sleep(1)

asyncio.run( test_base(42) )
finally:
p.terminate()
pass
# p=multiprocessing.Process(target=processHrServer)
# try:
# p.start()
# time.sleep(1)

# asyncio.run( test_base(42) )
# finally:
# asyncio.run( kill_hrserver() )

0 comments on commit 1b26254

Please sign in to comment.