Browse Source

Merge branch 'master' of ssh://git0.fmf.uni-lj.si:2222/studen/websocket

Andrej Studen/Merlin 3 years ago
parent
commit
3e0d9a65e1
1 changed files with 60 additions and 0 deletions
  1. 60 0
      websocketServer.py

+ 60 - 0
websocketServer.py

@@ -0,0 +1,60 @@
+import asyncio
+import websockets
+import sys
+import json
+import importlib
+import pathlib
+import os
+
+async def hello(websocket):
+    #websocket is a WebSocketServerProtocol
+    message = await websocket.recv()
+    host=websocket.remote_address[0]
+
+    #delegate parsing to subroutine
+    #assume the message will be
+    #pathToPythonCode:argument
+    #where argument is some sort of a string or a number
+
+    #pathToPythonCode can have replacementStrings 
+    #that reflect local software arrangement,
+    #for example _softwareSrc_ will get replaced by 
+    #software/src directory from setup.json
+
+    message=':'.join([host,message])
+
+    print(f"<<< FROM: {host}")
+    print(f"<<< {message}")
+
+    greeting = f"Hello {message}!"
+    run(message)
+    #await websocket.send(greeting)
+    #don't send anything, just log
+    print(f">>> {greeting}")
+
+async def main():
+    origins=["merlin","labkey-public","onko-nix",None]
+    #server="vangogh.fmf.uni-lj.si"
+    server="193.2.68.227"
+    async with websockets.serve(hello, server, 8765,origins=origins):
+        await asyncio.Future()  # run forever
+
+def run(message):
+    args=message.split(':')
+    scriptFile=args[1]
+    fsetup=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
+    with open(fsetup) as f:
+        setup=json.load(f)
+
+    scriptFile=scriptFile.replace('_softwareSrc_',setup['paths']['softwareSrc'])
+        
+    spath=pathlib.Path(scriptFile)
+    print('script: {} [{}]{}'.format(scriptFile,spath.parent,spath.stem))
+    sys.path.append(str(spath.parent))
+    #print(sys.path)
+    module=importlib.import_module(spath.stem)
+    module.main(args[0],args[2])
+	
+
+if __name__ == "__main__":
+    asyncio.run(main())