ProcessLauncher

ProcessLauncher

Deprecated

This class has been migrated to wslink
When all clients have been migrated to wslink, this class will be removed.

The ProcessLauncher is used in ParaViewWeb to start a new remote
server instance to perform interactive 3D post-processing using either
a VTK or a ParaView backend.

import ProcessLauncher from 'paraviewweb/src/IO/Core/ProcessLauncher';

processLauncher = new ProcessLauncher('/paraview');

constructor(endPoint)

Create a process launcher that will perform request to a remote
server using the provided endpoint url.

start(config)

Submit a request for a new remote process.

The config object get posted via a POST request to the endpoint provided
at creation time.

The current ParaViewWeb server side Launcher expect at least
the following object.

{
application: 'NameOfTheProcess',
}

But additional key/value pair can be added depending on the needs of the targeted process.

Once the remote process became ready a notification will be send.

fetchConnection(sessionId)

Trigger a request for getting the full connection information
based on an existing sessionId.

stop(connection)

Trigger a request to terminate a remote process using the connection
object that was provided at start time.

listConnections()

Return the list of already establised connections. (From that instance)

onProcessReady(callback) : subscription

Register a callback for when a remote process become available after a start() request.

The callback function will then receive a json object describing how to connect to that remote process.

{
sessionURL: 'ws://myServer/proxy?sessionId=asdfwefasdfwerfqerfse',
maybe: 'something else too'
}

onProcessStopped(callback) : subscription

Register a callback for when a stop request is performed.

onFetch(callback) : subscription

Register a callback for when a fetchConnection request is performed.

onError(callback) : subscription

Register a callback for when an error occured regardless of the request.

destroy()

Free memory and detatch any listener.

Source

index.js
import Monologue from 'monologue.js';

const PROCESS_READY_TOPIC = 'launcher.process.ready';
const PROCESS_STOPPED_TOPIC = 'launcher.process.stopped';
const CONNECTION_INFO_TOPIC = 'launcher.info.connection';
const ERROR_TOPIC = 'launcher.error';

const connections = [];

// DEPRECATED: replaced by wslink/js/src/ProcessLauncher
export default class ProcessLauncher {
constructor(endPoint) {
this.endPoint = endPoint;
}

start(config) {
const xhr = new XMLHttpRequest();
const url = this.endPoint;

xhr.open('POST', url, true);
xhr.responseType = 'json';
const supportsJson = 'response' in xhr && xhr.responseType === 'json';

xhr.onload = (e) => {
const response = supportsJson ? xhr.response : JSON.parse(xhr.response);
if (xhr.status === 200 && !response.error) {
// Add connection to our global list
connections.push(response);
this.emit(PROCESS_READY_TOPIC, response);
return;
}
this.emit(ERROR_TOPIC, response);
};

xhr.onerror = (e) => {
this.emit(ERROR_TOPIC, xhr.response);
};

xhr.send(JSON.stringify(config));
}

fetchConnection(sessionId) {
const xhr = new XMLHttpRequest();
const url = [this.endPoint, sessionId].join('/');

xhr.open('GET', url, true);
xhr.responseType = 'json';
const supportsJson = 'response' in xhr && xhr.responseType === 'json';

xhr.onload = (e) => {
if (this.status === 200) {
this.emit(
CONNECTION_INFO_TOPIC,
supportsJson ? xhr.response : JSON.parse(xhr.response)
);
return;
}
this.emit(ERROR_TOPIC, xhr.response);
};

xhr.onerror = (e) => {
this.emit(ERROR_TOPIC, xhr.response);
};

xhr.send();
}

stop(connection) {
const xhr = new XMLHttpRequest();
const url = [this.endPoint, connection.id].join('/');

xhr.open('DELETE', url, true);
xhr.responseType = 'json';
const supportsJson = 'response' in xhr && xhr.responseType === 'json';

xhr.onload = (e) => {
if (this.status === 200) {
const response = supportsJson ? xhr.response : JSON.parse(xhr.response);
// Remove connection from the list
// FIXME / TODO
this.emit(PROCESS_STOPPED_TOPIC, response);
return;
}
this.emit(ERROR_TOPIC, xhr.response);
};
xhr.onerror = (e) => {
this.emit(ERROR_TOPIC, xhr.response);
};
xhr.send();
}

/* eslint-disable class-methods-use-this */
listConnections() {
return connections;
}

onProcessReady(callback) {
return this.on(PROCESS_READY_TOPIC, callback);
}

onProcessStopped(callback) {
return this.on(PROCESS_STOPPED_TOPIC, callback);
}

onFetch(callback) {
return this.on(CONNECTION_INFO_TOPIC, callback);
}

onError(callback) {
return this.on(ERROR_TOPIC, callback);
}

destroy() {
this.off();
this.endPoint = null;
}
}
Monologue.mixInto(ProcessLauncher);