HydroRTCServer

HydroRTCServer

new HydroRTCServer()

Server-side implementation for the HydroRTC system Works as a connection host intermediary for client class

Source:

Methods

(static) connectPeers(data)

Forwards the connection request to the receiver peer, if the receiver peer is found in the list of connected peers.

Parameters:
Name Type Description
data Object

An object containing information about the connection request.

Properties
Name Type Description
requestorName string

The name of the peer requesting the connection.

recieverPeerName string

The name of the peer to receive the connection request.

request string

The type of connection request (e.g., "collaborative").

Source:
Fires:
  • event:'connect-request'

(static) getAddress() → {String}

Source:
Returns:
  • The address of a given host and port
Type
String

(static) getDirectories(source) → {Array.<string>}

This function uses the readdirSync method from the fs module to read the contents of the directory at the given path. It then filters the results to only include directories and maps the directory names to an array. If no directories are found, the function returns an array containing the original path.

Parameters:
Name Type Description
source string

The path to the directory.

Source:
Returns:
  • An array of directory names.
Type
Array.<string>

(static) getFiles(source) → {Array.<string>}

This function uses the readdirSync method from the fs module to read the contents of the directory at the given path. It then filters the results to only include files and maps the file names to an array.

Parameters:
Name Type Description
source string

The path to the directory.

Source:
Returns:
  • An array of file names.
Type
Array.<string>

(static) getPeerBySocketId(socketId) → {Object|undefined}

This method searches the this.peers array for a peer object with the matching socketId and returns it. If no matching peer is found, it returns undefined.

Parameters:
Name Type Description
socketId string

The socket ID of the peer to retrieve.

Source:
Returns:
  • The peer object if found, or undefined if not found.
Type
Object | undefined

(static) getPeerID(peer, socketId)

Emits an event with the peer's clientName and clientID.

Parameters:
Name Type Description
peer Object

The peer object containing the clientName property.

socketId string

The socket ID of the peer.

Source:
Fires:
  • event:'peer-id-value'

(static) getPeers(peer)

Retrieves and broadcasts the list of connected peers.

Parameters:
Name Type Description
peer Object

The current peer requesting the list of peers.

Source:
Fires:
  • event:'peers'

(static) getPeerwithStreamData() → {Object|null}

Iterates through the peers array in reverse order (from most recent to oldest) and returns the first peer that has the "has-stream-data" property set to true. If no such peer is found, it returns null.

Source:
Returns:
  • Returns the most recent peer with stream data, or null if no such peer is found.
Type
Object | null

(static) getSmartDataIntervalCallback(peer) → {number}

This method sets up an interval that emits smart data to the specified peer at a regular interval. It retrieves the smart data from the this.smartDataSharing.data[this.smartDataSharing.resolution] array, and for each file in the array, it:

  1. Retrieves the file data using the imageHandler function.
  2. Emits the file data to the peer using the "smart-data" event, along with the resolution, row number, filename, and usecase.
  3. Increments the count of emitted files.

If all files have been emitted, the interval is cleared.

The method returns the ID of the interval that was set up, which can be used to clear the interval later if needed.

Parameters:
Name Type Description
peer Object

The peer object to which the smart data will be emitted.

Source:
Returns:
  • The ID of the interval that was set up to emit the smart data.
Type
number

(static) getTask(peer)

This method is called when a peer requests a task. It logs the request and then iterates through the this.peers array to find the requesting peer. Once the requesting peer is found, a task is assigned to the peer by emitting the "task" event with the task data.

The task is selected by taking the peerNo modulo the length of the this.tasks array, which ensures that the tasks are distributed evenly among the peers.

The task data is sent to the peer using the "task" event, along with the "usecase" property set to "distributed-data-analysis-and-processing".

Parameters:
Name Type Description
peer Object

The peer object that requested the task.

Source:

(static) handleDisconnect(socket) → {void}

Removes the disconnected peer from the this.peers array, emits a "delete-db" event to the peer's socket, and logs a message to the console.

Parameters:
Name Type Description
socket Object

The socket object representing the disconnected peer.

Source:
Returns:
Type
void

(static) handleHDF5(peer) → {void}

This method processes an HDF5 file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/hdf5/file.h5',
  socketId: 'abc123'
};
this.handleHDF5(peer);

(static) handleNetCDF(peer) → {void}

This method processes a NetCDF file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/netcdf/file.nc',
  socketId: 'abc123'
};
this.handleNetCDF(peer);

(static) handleTIFF(peer) → {void}

This method processes a TIFF file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/tiff/file.tiff',
  socketId: 'abc123'
};
this.handleTIFF(peer);

(static) imageHandler(file) → {string}

This function uses the readFileSync method from the fs module to read the contents of the image file at the given path. It then encodes the contents as a base64 string and returns it.

Parameters:
Name Type Description
file string

The path to the image file.

Source:
Returns:
  • The base64-encoded contents of the image file.
Type
string

(static) joinServer(socket, peer) → {void}

Joins a peer to the existing server and gets broadcasted to the exisiting peer list.

Parameters:
Name Type Description
socket Object

Socket representing the peer's connection

peer Object

Information about the joining peer.

Source:
Returns:

registers the peer on the server.

Type
void

(static) largeFileHandler(file) → {Promise.<Buffer>}

This function uses the createReadStream method from the fs module to read the contents of the file at the given path in chunks. It then concatenates the chunks into a single Buffer and returns it as a Promise.

Parameters:
Name Type Description
file string

The path to the large file.

Source:
Returns:
  • A Promise that resolves to a Buffer containing the contents of the file.
Type
Promise.<Buffer>

(static) runServer()

This method is responsible for starting the server and listening for incoming connections. It first checks if the server can run on the given port and hostname, and then starts the server and logs the address it's listening on.

The server is started using the listen method of the this.server object, which is likely an instance of a web server like Express or Node.js's built-in HTTP server. The listen method takes the port and hostname as arguments, and a callback function that is called when the server starts listening.

Inside the callback function, the address method of the server is called to get the address the server is listening on, and this information is logged to the console.

Source:

(static) sendFileNames(peer) → {void}

This method retrieves the list of file names for a given data type and sends it to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: 'tiff',
  socketId: 'abc123'
};
this.sendFileNames(peer);

(static) serveFile(response, filename) → {Promise.<void>}

Reads and serves a specified file to the client's response

Parameters:
Name Type Description
response Object

Response object to send data back to the client

filename String

Path of the file to be served

Source:
Returns:

Serves the HTML page

Type
Promise.<void>

(static) setTasks(tasks)

Parameters:
Name Type Description
tasks Object
Source:

(static) smartDataShare(peer)

Reads the data from the specified directory, organizes it by resolution and row, and sets up an interval to periodically share the data with the peer.

Parameters:
Name Type Description
peer Object

An object containing information about the peer requesting smart data sharing.

Properties
Name Type Description
clientName string

The name of the peer.

dataPath string

The path to the directory containing the data to be shared.

resolution string

The resolution of the data to be shared.

frequency number

The frequency at which the data should be shared.

Source:

(static) streamData(socket, data)

Streams the requested data to the user's socket.

Parameters:
Name Type Description
socket Object

The current socket used by the user.

data Object

The peer data available.

Source:
Fires:
  • event:'data-stream'

(static) taskResult(peer)

Logs the peer's submission of the task result, checks if the result is valid, and logs a message indicating that the result has been saved.

Parameters:
Name Type Description
peer Object

An object containing information about the peer submitting the task result.

Properties
Name Type Description
name string

The name of the peer.

task string

The name of the task.

result Array

The result of the task.

Source:

(static) updatePeerProperty(peerName, property, value) → {null}

Iterates through the peers array, finds the peer with the given name, and updates the specified property with the provided value.

Parameters:
Name Type Description
peerName string

The name of the peer whose property needs to be updated.

property string

The name of the property to be updated.

value any

The new value for the property.

Source:
Returns:
  • Returns null if the property was successfully updated.
Type
null

(static) updateSmartDataShare(peer)

Logs the peer's request to update the smart data configuration, updates the resolution and frequency properties, clears the old smart data interval, and creates a new interval based on the updated properties.

Parameters:
Name Type Description
peer Object

An object containing information about the peer requesting the smart data sharing update.

Properties
Name Type Description
name string

The name of the peer.

resolution string

The new resolution of the data to be shared.

frequency number

The new frequency at which the data should be shared.

Source:

(static) userNameTaken(username) → {boolean}

This method checks the list of connected peers to see if the given username is already in use.

Parameters:
Name Type Description
username string

The username to check.

Source:
Returns:
  • True if the username is already taken, false otherwise.
Type
boolean

(static) validateUser(socket, data) → {EventEmitter}

validates that a user is already registered on the live server.

Parameters:
Name Type Description
socket Object

current socket used by the user

data Object

peer data available

Source:
Returns:
Type
EventEmitter

HydroRTCServer

new HydroRTCServer()

Sets all the required information for a server through a specific port.

Source:

Methods

(static) connectPeers(data)

Forwards the connection request to the receiver peer, if the receiver peer is found in the list of connected peers.

Parameters:
Name Type Description
data Object

An object containing information about the connection request.

Properties
Name Type Description
requestorName string

The name of the peer requesting the connection.

recieverPeerName string

The name of the peer to receive the connection request.

request string

The type of connection request (e.g., "collaborative").

Source:
Fires:
  • event:'connect-request'

(static) getAddress() → {String}

Source:
Returns:
  • The address of a given host and port
Type
String

(static) getDirectories(source) → {Array.<string>}

This function uses the readdirSync method from the fs module to read the contents of the directory at the given path. It then filters the results to only include directories and maps the directory names to an array. If no directories are found, the function returns an array containing the original path.

Parameters:
Name Type Description
source string

The path to the directory.

Source:
Returns:
  • An array of directory names.
Type
Array.<string>

(static) getFiles(source) → {Array.<string>}

This function uses the readdirSync method from the fs module to read the contents of the directory at the given path. It then filters the results to only include files and maps the file names to an array.

Parameters:
Name Type Description
source string

The path to the directory.

Source:
Returns:
  • An array of file names.
Type
Array.<string>

(static) getPeerBySocketId(socketId) → {Object|undefined}

This method searches the this.peers array for a peer object with the matching socketId and returns it. If no matching peer is found, it returns undefined.

Parameters:
Name Type Description
socketId string

The socket ID of the peer to retrieve.

Source:
Returns:
  • The peer object if found, or undefined if not found.
Type
Object | undefined

(static) getPeerID(peer, socketId)

Emits an event with the peer's clientName and clientID.

Parameters:
Name Type Description
peer Object

The peer object containing the clientName property.

socketId string

The socket ID of the peer.

Source:
Fires:
  • event:'peer-id-value'

(static) getPeers(peer)

Retrieves and broadcasts the list of connected peers.

Parameters:
Name Type Description
peer Object

The current peer requesting the list of peers.

Source:
Fires:
  • event:'peers'

(static) getPeerwithStreamData() → {Object|null}

Iterates through the peers array in reverse order (from most recent to oldest) and returns the first peer that has the "has-stream-data" property set to true. If no such peer is found, it returns null.

Source:
Returns:
  • Returns the most recent peer with stream data, or null if no such peer is found.
Type
Object | null

(static) getSmartDataIntervalCallback(peer) → {number}

This method sets up an interval that emits smart data to the specified peer at a regular interval. It retrieves the smart data from the this.smartDataSharing.data[this.smartDataSharing.resolution] array, and for each file in the array, it:

  1. Retrieves the file data using the imageHandler function.
  2. Emits the file data to the peer using the "smart-data" event, along with the resolution, row number, filename, and usecase.
  3. Increments the count of emitted files.

If all files have been emitted, the interval is cleared.

The method returns the ID of the interval that was set up, which can be used to clear the interval later if needed.

Parameters:
Name Type Description
peer Object

The peer object to which the smart data will be emitted.

Source:
Returns:
  • The ID of the interval that was set up to emit the smart data.
Type
number

(static) getTask(peer)

This method is called when a peer requests a task. It logs the request and then iterates through the this.peers array to find the requesting peer. Once the requesting peer is found, a task is assigned to the peer by emitting the "task" event with the task data.

The task is selected by taking the peerNo modulo the length of the this.tasks array, which ensures that the tasks are distributed evenly among the peers.

The task data is sent to the peer using the "task" event, along with the "usecase" property set to "distributed-data-analysis-and-processing".

Parameters:
Name Type Description
peer Object

The peer object that requested the task.

Source:

(static) handleDisconnect(socket) → {void}

Removes the disconnected peer from the this.peers array, emits a "delete-db" event to the peer's socket, and logs a message to the console.

Parameters:
Name Type Description
socket Object

The socket object representing the disconnected peer.

Source:
Returns:
Type
void

(static) handleHDF5(peer) → {void}

This method processes an HDF5 file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/hdf5/file.h5',
  socketId: 'abc123'
};
this.handleHDF5(peer);

(static) handleNetCDF(peer) → {void}

This method processes a NetCDF file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/netcdf/file.nc',
  socketId: 'abc123'
};
this.handleNetCDF(peer);

(static) handleTIFF(peer) → {void}

This method processes a TIFF file request from a client. It reads the file, extracts the data, and sends it back to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: '/path/to/tiff/file.tiff',
  socketId: 'abc123'
};
this.handleTIFF(peer);

(static) imageHandler(file) → {string}

This function uses the readFileSync method from the fs module to read the contents of the image file at the given path. It then encodes the contents as a base64 string and returns it.

Parameters:
Name Type Description
file string

The path to the image file.

Source:
Returns:
  • The base64-encoded contents of the image file.
Type
string

(static) joinServer(socket, peer) → {void}

Joins a peer to the existing server and gets broadcasted to the exisiting peer list.

Parameters:
Name Type Description
socket Object

Socket representing the peer's connection

peer Object

Information about the joining peer.

Source:
Returns:

registers the peer on the server.

Type
void

(static) largeFileHandler(file) → {Promise.<Buffer>}

This function uses the createReadStream method from the fs module to read the contents of the file at the given path in chunks. It then concatenates the chunks into a single Buffer and returns it as a Promise.

Parameters:
Name Type Description
file string

The path to the large file.

Source:
Returns:
  • A Promise that resolves to a Buffer containing the contents of the file.
Type
Promise.<Buffer>

(static) runServer()

This method is responsible for starting the server and listening for incoming connections. It first checks if the server can run on the given port and hostname, and then starts the server and logs the address it's listening on.

The server is started using the listen method of the this.server object, which is likely an instance of a web server like Express or Node.js's built-in HTTP server. The listen method takes the port and hostname as arguments, and a callback function that is called when the server starts listening.

Inside the callback function, the address method of the server is called to get the address the server is listening on, and this information is logged to the console.

Source:

(static) sendFileNames(peer) → {void}

This method retrieves the list of file names for a given data type and sends it to the client.

Parameters:
Name Type Description
peer Object

The peer object containing the client's information.

Source:
Returns:
Type
void
Example
const peer = {
  clientName: 'myClient',
  dataPath: 'tiff',
  socketId: 'abc123'
};
this.sendFileNames(peer);

(static) serveFile(response, filename) → {Promise.<void>}

Reads and serves a specified file to the client's response

Parameters:
Name Type Description
response Object

Response object to send data back to the client

filename String

Path of the file to be served

Source:
Returns:

Serves the HTML page

Type
Promise.<void>

(static) setTasks(tasks)

Parameters:
Name Type Description
tasks Object
Source:

(static) smartDataShare(peer)

Reads the data from the specified directory, organizes it by resolution and row, and sets up an interval to periodically share the data with the peer.

Parameters:
Name Type Description
peer Object

An object containing information about the peer requesting smart data sharing.

Properties
Name Type Description
clientName string

The name of the peer.

dataPath string

The path to the directory containing the data to be shared.

resolution string

The resolution of the data to be shared.

frequency number

The frequency at which the data should be shared.

Source:

(static) streamData(socket, data)

Streams the requested data to the user's socket.

Parameters:
Name Type Description
socket Object

The current socket used by the user.

data Object

The peer data available.

Source:
Fires:
  • event:'data-stream'

(static) taskResult(peer)

Logs the peer's submission of the task result, checks if the result is valid, and logs a message indicating that the result has been saved.

Parameters:
Name Type Description
peer Object

An object containing information about the peer submitting the task result.

Properties
Name Type Description
name string

The name of the peer.

task string

The name of the task.

result Array

The result of the task.

Source:

(static) updatePeerProperty(peerName, property, value) → {null}

Iterates through the peers array, finds the peer with the given name, and updates the specified property with the provided value.

Parameters:
Name Type Description
peerName string

The name of the peer whose property needs to be updated.

property string

The name of the property to be updated.

value any

The new value for the property.

Source:
Returns:
  • Returns null if the property was successfully updated.
Type
null

(static) updateSmartDataShare(peer)

Logs the peer's request to update the smart data configuration, updates the resolution and frequency properties, clears the old smart data interval, and creates a new interval based on the updated properties.

Parameters:
Name Type Description
peer Object

An object containing information about the peer requesting the smart data sharing update.

Properties
Name Type Description
name string

The name of the peer.

resolution string

The new resolution of the data to be shared.

frequency number

The new frequency at which the data should be shared.

Source:

(static) userNameTaken(username) → {boolean}

This method checks the list of connected peers to see if the given username is already in use.

Parameters:
Name Type Description
username string

The username to check.

Source:
Returns:
  • True if the username is already taken, false otherwise.
Type
boolean

(static) validateUser(socket, data) → {EventEmitter}

validates that a user is already registered on the live server.

Parameters:
Name Type Description
socket Object

current socket used by the user

data Object

peer data available

Source:
Returns:
Type
EventEmitter