viewer port cleanup (#615)

* cleaned up port logic

* bump viewer version
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-10-02 13:58:51 -07:00
1 parent 50d9aa1341
commit 9d15ba6a2a
12 files changed
+88 -87

No files matched your search

+2 -2
View File
@@ -164,8 +164,8 @@ class ViewerConfig(PrintableConfig):
start_train: bool = True
"""whether to immediately start training upon loading viewer
if False, will just visualize dataset but you can toggle training in viewer"""
zmq_port: int = 6000
"""the zmq port to connect to for communication"""
zmq_port: Optional[int] = None
"""The zmq port to connect to for communication. If None, find an available port."""
launch_bridge_server: bool = True
"""whether or not to launch the zmq bridge server"""
websocket_port: int = 7007
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "viewer",
"homepage": ".",
"version": "22-10-01-0",
"version": "22-10-02-0",
"private": true,
"dependencies": {
"@emotion/react": "^11.10.4",
@@ -18,7 +18,7 @@ function getParam(param_name) {
}
function getWebsocketEndpoint() {
const endpoint = getParam('websocket_url');
const endpoint = getParam('websocket_port');
return endpoint;
}
@@ -26,13 +26,13 @@ export default function Banner() {
const dispatch = useDispatch();
let open_modal = true;
const websocket_url_from_argument = getWebsocketEndpoint();
if (websocket_url_from_argument !== undefined) {
const websocket_port_from_argument = getWebsocketEndpoint();
if (websocket_port_from_argument !== undefined) {
open_modal = false;
dispatch({
type: 'write',
path: 'websocketState/websocket_url',
data: websocket_url_from_argument,
path: 'websocketState/websocket_port',
data: websocket_port_from_argument,
});
}
@@ -13,6 +13,7 @@ import {
} from '@mui/material';
import ContentCopyRoundedIcon from '@mui/icons-material/ContentCopyRounded';
import { useSelector } from 'react-redux';
import WebSocketUrlField from '../WebSocketUrlField';
interface LandingModalProps {
@@ -56,9 +57,12 @@ export default function LandingModel(props: LandingModalProps) {
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
const text_intro = `At the top right is the Status Panel where you can connect to a WebSocket and see the train/eval status of your NeRF model. The WebSocket URL takes the form <domain-or-ip_address>:<port>.`;
const text_intro = `At the top right is the Status Panel where you can connect to a WebSocket and see the train/eval status of your NeRF model. The WebSocket Port takes the form <domain-or-ip_address>:<port>.`;
const command = `ssh -L localhost:7007:localhost:7007 USER@REMOTE.SERVER.IP`;
const websocket_port = useSelector(
(state) => state.websocketState.websocket_port,
);
const command = `ssh -L ${websocket_port}:localhost:${websocket_port} USER@REMOTE.SERVER.IP`;
const platform = window.navigator.platform.toLowerCase();
let initial_tab = 0;
@@ -113,7 +117,7 @@ export default function LandingModel(props: LandingModalProps) {
<h2>Getting Started</h2>
<p>{text_intro}</p>
<p>
You can enter your WebSocket URL in the Status Panel or here:
You can enter your WebSocket Port in the Status Panel or here:
</p>
<WebSocketUrlField />
<h3>Remote Server</h3>
@@ -16,12 +16,13 @@ export default function WebSocketContextFunction({ children }) {
let socket = null;
// this code will rerender anytime the webosocket changes now
const websocket_url = useSelector(
(state) => state.websocketState.websocket_url,
const websocket_port = useSelector(
(state) => state.websocketState.websocket_port,
);
const connect = () => {
const url = `ws://${websocket_url}/`;
const url = `ws://localhost:${websocket_port}/`;
console.log(url);
socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
socket.onopen = () => {
@@ -57,7 +58,7 @@ export default function WebSocketContextFunction({ children }) {
return () => {
socket.close();
};
}, [websocket_url]);
}, [websocket_port]);
connect();
ws = {
@@ -4,15 +4,15 @@ import { TextField } from '@mui/material';
import { useDispatch, useSelector } from 'react-redux';
export default function WebSocketUrlField() {
const websocket_url = useSelector(
(state) => state.websocketState.websocket_url,
const websocket_port = useSelector(
(state) => state.websocketState.websocket_port,
);
const dispatch = useDispatch();
const websocket_url_onchange = (event) => {
const websocket_port_onchange = (event) => {
const value = event.target.value;
dispatch({
type: 'write',
path: 'websocketState/websocket_url',
path: 'websocketState/websocket_port',
data: value,
});
};
@@ -20,10 +20,10 @@ export default function WebSocketUrlField() {
return (
<TextField
className="WebSocketUrlField"
label="WebSocket URL"
label="WebSocket Port"
variant="outlined"
value={websocket_url}
onChange={websocket_url_onchange}
value={websocket_port}
onChange={websocket_port_onchange}
size="small"
/>
);
+1 -1
View File
@@ -3,7 +3,7 @@ import { split_path } from './utils';
const initialState = {
// the websocket connection state
websocketState: {
websocket_url: 'localhost:7007',
websocket_port: 7007,
isConnected: false,
},
// the webrtc connection state
+11 -55
View File
@@ -15,7 +15,7 @@
"""Server bridge to faciliate interactions between python backend and javascript front end"""
import sys
from typing import Callable, List, Optional, Tuple
from typing import List, Optional, Tuple
import dcargs
import msgpack
@@ -35,33 +35,6 @@ from nerfstudio.viewer.server.state.node import find_node, get_tree, walk
from nerfstudio.viewer.server.state.state_node import StateNode
from nerfstudio.viewer.server.video_stream import SingleFrameStreamTrack
MAX_ATTEMPTS = 1000
DEFAULT_ZMQ_METHOD = "tcp"
DEFAULT_ZMQ_PORT = 6000
DEFAULT_WEBSOCKET_PORT = 8051
def find_available_port(func: Callable, default_port: int, max_attempts: int = MAX_ATTEMPTS, **kwargs) -> None:
"""Finds and attempts to connect to a port
Args:
func: function used on connecting to port
default_port: the default port
max_attempts: max number of attempts to try connection. Defaults to MAX_ATTEMPTS.
"""
for i in range(max_attempts):
port = default_port + i
try:
return func(port, **kwargs), port
except (OSError, zmq.error.ZMQError):
print(f"Port: {port:d} in use, trying another...", file=sys.stderr)
except Exception as e:
print(type(e))
raise
raise (
Exception(f"Could not find an available port in the range: [{default_port:d}, {max_attempts + default_port:d})")
)
def force_codec(pc: RTCPeerConnection, sender: RTCRtpSender, forced_codec: str) -> None:
"""Sets the codec preferences on a connection between sender and reciever
@@ -169,42 +142,25 @@ class ZMQWebSocketBridge:
context = zmq.Context()
def __init__(
self,
zmq_port: Optional[int] = None,
host: str = "127.0.0.1",
websocket_address: str = "0.0.0.0",
websocket_port: Optional[int] = None,
):
def __init__(self, zmq_port: int, websocket_port: int):
self.zmq_port = zmq_port
self.host = host
self.websocket_address = websocket_address
self.websocket_pool = set()
self.app = self.make_app()
self.ioloop = tornado.ioloop.IOLoop.current()
self.pcs = set()
self.video_tracks = set()
if self.zmq_port is None:
# zmq
zmq_url = f"tcp://127.0.0.1:{self.zmq_port:d}"
self.zmq_socket, self.zmq_stream, self.zmq_url = self.setup_zmq(zmq_url)
def f(port):
return self.setup_zmq(f"{DEFAULT_ZMQ_METHOD:s}://{self.host:s}:{port:d}")
# websocket
listen_kwargs = {"address": "0.0.0.0"}
self.app.listen(websocket_port, **listen_kwargs)
self.websocket_port = websocket_port
self.websocket_url = f"0.0.0.0:{self.websocket_port}"
(self.zmq_socket, self.zmq_stream, self.zmq_url), _ = find_available_port(f, DEFAULT_ZMQ_PORT)
self.zmq_port = int(self.zmq_url.split(":")[-1])
else:
zmq_url = f"{DEFAULT_ZMQ_METHOD:s}://{self.host:s}:{self.zmq_port:d}"
self.zmq_socket, self.zmq_stream, self.zmq_url = self.setup_zmq(zmq_url)
listen_kwargs = {"address": self.websocket_address}
if websocket_port is None:
_, self.websocket_port = find_available_port(self.app.listen, DEFAULT_WEBSOCKET_PORT, **listen_kwargs)
else:
self.app.listen(websocket_port, **listen_kwargs)
self.websocket_port = websocket_port
self.websocket_url = f"{self.websocket_address}:{self.websocket_port}"
# state tree
self.state_tree = get_tree(StateNode)
def __str__(self) -> str:
+15 -2
View File
@@ -19,11 +19,12 @@ Code to use the viewer as a subprocess.
import atexit
import os
import signal
import socket
import subprocess
import sys
import threading
import time
from typing import Union
from typing import Optional, Union
from rich.console import Console
@@ -32,7 +33,9 @@ from nerfstudio.viewer.server import server
CONSOLE = Console()
def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, log_filename: Union[str, None]):
def run_viewer_bridge_server_as_subprocess(
websocket_port: int, zmq_port: Optional[int] = None, log_filename: Union[str, None] = None
):
"""Runs the viewer bridge server as a subprocess.
Args:
@@ -44,6 +47,15 @@ def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, l
None
"""
args = [sys.executable, "-u", "-m", server.__name__]
# find an available port for zmq
if zmq_port is None:
sock = socket.socket()
sock.bind(("", 0))
zmq_port = sock.getsockname()[1]
string = f"Using ZMQ port: {zmq_port}"
CONSOLE.print(f"[bold red]{string}")
args.append("--zmq-port")
args.append(str(zmq_port))
args.append("--websocket-port")
@@ -83,3 +95,4 @@ def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, l
t1.daemon = True
t1.start()
atexit.register(cleanup, process)
return zmq_port
+25 -1
View File
@@ -15,10 +15,12 @@
"""Generic utility functions
"""
from typing import Any, Dict, List, Optional, Tuple
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple
import numpy as np
import torch
import zmq
def get_chunks(
@@ -86,3 +88,25 @@ def get_intrinsics_matrix_and_camera_to_world_h(
)
return intrinsics_matrix, camera_to_world_h
def find_available_port(func: Callable, default_port: int, max_attempts: int = 1000, **kwargs) -> None:
"""Finds and attempts to connect to a port
Args:
func: function used on connecting to port
default_port: the default port
max_attempts: max number of attempts to try connection. Defaults to MAX_ATTEMPTS.
"""
for i in range(max_attempts):
port = default_port + i
try:
return func(port, **kwargs), port
except (OSError, zmq.error.ZMQError):
print(f"Port: {port:d} in use, trying another...", file=sys.stderr)
except Exception as e:
print(type(e))
raise
raise (
Exception(f"Could not find an available port in the range: [{default_port:d}, {max_attempts + default_port:d})")
)
+8 -5
View File
@@ -212,8 +212,8 @@ class ViewerState:
# start the viewer bridge server
websocket_port = self.config.websocket_port
self.log_filename.parent.mkdir(exist_ok=True)
run_viewer_bridge_server_as_subprocess(
self.config.zmq_port, websocket_port, log_filename=str(self.log_filename)
zmq_port = run_viewer_bridge_server_as_subprocess(
websocket_port, zmq_port=self.config.zmq_port, log_filename=str(self.log_filename)
)
# TODO(ethan): move this into the writer such that it's at the bottom
# of the logging stack and easy to see and click
@@ -221,8 +221,8 @@ class ViewerState:
console.line()
json_filename = os.path.join(os.path.dirname(__file__), "../app/package.json")
version = load_from_json(Path(json_filename))["version"]
self.viewer_url = f"https://viewer.nerf.studio/versions/{version}/?websocket_url=localhost:{websocket_port}"
viewer_url_local = f"http://localhost:4000/?websocket_url=localhost:{websocket_port}"
self.viewer_url = f"https://viewer.nerf.studio/versions/{version}/?websocket_port={websocket_port}"
viewer_url_local = f"http://localhost:4000/?websocket_port={websocket_port}"
pub_open_viewer_instructions_string = f"[Public] Open the viewer at {self.viewer_url}"
dev_open_viewer_instructions_string = f"[Local] Open the viewer at {viewer_url_local}"
console.rule(characters="=")
@@ -230,7 +230,10 @@ class ViewerState:
console.print(dev_open_viewer_instructions_string)
console.rule(characters="=")
console.line()
self.vis = Viewer(zmq_port=self.config.zmq_port)
self.vis = Viewer(zmq_port=zmq_port)
else:
assert self.config.zmq_port is not None
self.vis = Viewer(zmq_port=self.config.zmq_port)
# viewer specific variables
self.prev_camera_matrix = None
+1 -1
View File
@@ -12,7 +12,7 @@ def test_run_subprocess():
zmq_port = 6000 + 10 # don't use the default ports to avoid conflicting with current training jobs
websocket_port = 7007 + 10
print("Starting the viewer bridge server as a subprocess.")
run_viewer_bridge_server_as_subprocess(zmq_port, websocket_port, log_filename=None)
run_viewer_bridge_server_as_subprocess(websocket_port, zmq_port=zmq_port, log_filename=None)
print("Started the viewer bridge server as a subprocess.")
# do something will the viewer bridge server runs
time.sleep(1)