Spawning Stream Cache Server
In this section we will be making use of the Node.js Child Process module to spawn Stream Cache Servers as POST requests come into our application server.
An instance of the ts3d_sc_server application is required for every streaming session of the HOOPS Web viewer. This application has a number of configuration options which can be set via the command line, however, for this tutorial we will be focusing on only a small subset of them.
Every time the user makes a POST request to /spawn endpoint we will create a new Stream Cache Server and return its details to the client. This will allow them to create a Communicator.WebViewer object on the viewer and connect to the newly spawned server. Every instance of ts3d_sc_server needs to run on its own unique port for WebSocket and http connections. As well, our application will need to assign unique ports for each instance.
Start by adding the following require statements to the top of spawner.js:
Add the following code to the bottom of the Spawner class constructor:
We will use these variables to track and assign ports for the instances that we spawn. When a process is spawned, we will check to see if there are any available ports that can be reused. If there are, we will pop one from the appropriate array, otherwise we will allocate a new one. When instances terminate, we will return their ports to the available array and remove it from our active spawns.
In order to track our active spawns, we will make a small class to hold information about each instance that we spawn. Add the SpawnInfo class right above where your Spawner class is declared:
Now that we have a convenient structure for storing information about an instance, we can implement our spawnViewer method:
Examining the code for this method, we can see that command-line arguments are built up for each viewer instance before they are spawned.
We are temporarily adding an event handler to the child process 'exit' event, as well as immediately sending a response to the client. Later in this tutorial we will learn how to receive and process status update events, such as ready and disconnect, from our viewer instances. We will also delay the response to the client until the viewer has started and is ready to receive a WebSocket connection. This will simplify the call from the front-end by making sure the Stream Cache Server is ready to go before the response is sent.
In this tutorial we are specifying only a subset of the possible configuration options for the ts3d_sc_server. Review our Converter Commnad-Line Options reference for a complete list.
We now are spawning Stream Cache Servers when the "Create New Viewer" button is pressed. Pressing the button multiple times will cause multiple servers to spawn.
The spawned server will terminate automatically if it does not receive an incoming connection in a short period of time. If we wait a moment, we will see corresponding messages in the terminal indicating the Stream Cache Servers have exited cleanly:
We are now ready to monitor and receive updates from ts3d_sc_server.