Skip to content

Commit

Permalink
serve on all hosts, look for mulitple paths
Browse files Browse the repository at this point in the history
  • Loading branch information
WillForan committed Dec 27, 2024
1 parent 92fe4f2 commit 917dee5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
29 changes: 18 additions & 11 deletions mrqart.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from tornado.httpserver import HTTPServer
from tornado.web import Application, RequestHandler
from websockets.asyncio.server import broadcast, serve
import glob

from template_checker import TemplateChecker

Expand Down Expand Up @@ -170,6 +171,7 @@ async def monitor_dirs(watcher, dcm_checker):
current_ses = STATE.get(hdr["Station"])

# only send to browser if new
# TODO: what if browser started up rate
if current_ses.update_isnew(hdr["SeriesNumber"], hdr["SequenceName"]):
logging.debug("first time seeing %s", current_ses)
msg = {
Expand Down Expand Up @@ -197,25 +199,29 @@ async def monitor_dirs(watcher, dcm_checker):
# broadcast(WS_CONNECTIONS, f"non-dicom file: {event}")


async def main(path):
async def main(paths):
"""
Run all services on different threads.
HTTP and inotify are forked. Websocket holds the main thread.
"""
dcm_checker = TemplateChecker()
watcher = aionotify.Watcher()
watcher.watch(
path=path,
flags=FOLLOW_FLAGS,
# NB. prev had just aionotify.Flags.CREATE but that triggers too early (partial file)
) # aionotify.Flags.MODIFY|aionotify.Flags.CREATE |aionotify.Flags.DELETE)
for path in paths:
watcher.watch(
path=path,
flags=FOLLOW_FLAGS,
# NB. prev had just aionotify.Flags.CREATE but that triggers too early (partial file)
) # aionotify.Flags.MODIFY|aionotify.Flags.CREATE |aionotify.Flags.DELETE)
for sub_path in glob.glob(path+"/*/"):
if os.path.isdir(sub_path):
watcher.watch( path=path, flags=FOLLOW_FLAGS)
asyncio.create_task(monitor_dirs(watcher, dcm_checker))

http_run()

# while True:
# await asyncio.sleep(.1)
async with serve(track_ws, "localhost", WS_PORT):
async with serve(track_ws, "0.0.0.0", WS_PORT):
await asyncio.get_running_loop().create_future() # run forever

watcher.close()
Expand All @@ -227,13 +233,14 @@ async def main(path):

# TODO: use argparser?
if len(sys.argv) > 1:
watch_dir = os.path.abspath(sys.argv[1])
watch_dirs = [os.path.abspath(x) for x in sys.argv[1:]]
else:
watch_dir = "/data/dicomstream/20241119.testMRQARAT.testMRQARAT/"
watch_dirs = ["/data/dicomstream/20241119.testMRQARAT.testMRQARAT/"]

if not os.path.isdir(watch_dir):
if not os.path.isdir(watch_dirs[0]):
raise Exception(f"{watch_dir} is not a directory!")

# TODO: watch all sub directories?
# watch_dir = os.path.join( FILEDIR, ...)
asyncio.run(main(watch_dir))
print(watch_dirs)
asyncio.run(main(watch_dirs))
4 changes: 3 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@

/* connects socket to main dispatcher `recievedMessages` */
function update_via_ws() {
const ws = new WebSocket("ws://127.0.0.1:5000/");
const host = "ws://" + location.hostname + ":5000/";
console.log("host",host)
const ws = new WebSocket(host);
ws.addEventListener('message', receivedMessage);
}

Expand Down

0 comments on commit 917dee5

Please sign in to comment.