Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Stop simulators when there are no clients #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as history from 'connect-history-api-fallback'
import * as express from 'express'
import * as http from 'http'
import * as minimist from 'minimist'
import { reaction } from 'mobx'
import { IObservableValue } from 'mobx'
import * as favicon from 'serve-favicon'
import * as sio from 'socket.io'
import * as webpack from 'webpack'
Expand Down Expand Up @@ -34,9 +36,10 @@ const server = http.createServer(app)
const sioNetwork = sio(server, { parser: NUClearNetProxyParser } as any)

// Initialize socket.io namespace immediately to catch reconnections.
WebSocketProxyNUClearNetServer.of(WebSocketServer.of(sioNetwork.of('/nuclearnet')), {
fakeNetworking: withVirtualRobots,
})
const webSocketProxyNUClearNetServer = WebSocketProxyNUClearNetServer.of(
WebSocketServer.of(sioNetwork.of('/nuclearnet')),
{ fakeNetworking: withVirtualRobots },
)

const devMiddleware = webpackDevMiddleware(compiler, {
publicPath: '/',
Expand Down Expand Up @@ -73,7 +76,21 @@ function init() {
{ frequency: 60, simulator: ChartSimulator.of() },
],
})
virtualRobots.startSimulators()
let stopSimulators: () => void | undefined
reaction(
() => webSocketProxyNUClearNetServer.numConnections > 0,
enable => {
if (enable) {
// tslint:disable-next-line no-console
console.log('Client connected, starting simulator')
stopSimulators = virtualRobots.startSimulators()
} else if (stopSimulators) {
// tslint:disable-next-line no-console
console.log('No clients remain connected, stopping simulator')
}
},
{ fireImmediately: true },
)
Copy link
Member Author

@BrendanAnnable BrendanAnnable Sep 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uglier than needed I suspect. Perhaps VirtualRobots should instead expose a more flexible API that would be more suitable for reacting to an observable value.

Something like:

reaction(() => {
  () => now(frequency) && numConections > 0,
  enabled => enabled && virtualRobots.tick(),
  { fireImmediately: true },
})

Will try this later.

}

if (nbsFile) {
Expand Down
15 changes: 11 additions & 4 deletions src/server/nuclearnet/web_socket_proxy_nuclearnet_server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { action } from 'mobx'
import { observable } from 'mobx'
import { NUClearNetOptions } from 'nuclearnet.js'
import { NUClearNetPeer } from 'nuclearnet.js'
import { NUClearNetPacket } from 'nuclearnet.js'
Expand All @@ -21,6 +23,8 @@ type Opts = {
* improved to have more intelligent multiplexing.
*/
export class WebSocketProxyNUClearNetServer {
@observable numConnections: number = 0

constructor(private server: WebSocketServer, private nuclearnetClient: NUClearNetClient) {
server.onConnection(this.onClientConnection)
}
Expand All @@ -30,8 +34,11 @@ export class WebSocketProxyNUClearNetServer {
return new WebSocketProxyNUClearNetServer(server, nuclearnetClient)
}

private onClientConnection = (socket: WebSocket) => {
@action.bound
private onClientConnection(socket: WebSocket) {
this.numConnections++
WebSocketServerClient.of(this.nuclearnetClient, socket)
socket.onDisconnect(action(() => this.numConnections--))
}
}

Expand Down Expand Up @@ -149,9 +156,9 @@ class PacketProcessor {
this.sendReliablePacket(event, packet)
} else if (this.isEventBelowLimit(event)) {
this.sendUnreliablePacket(event, packet)
}/* else {
// This event is unreliable and already at the limit, simply drop the packet.
}*/
} else {
return // The event is unreliable and already at the limit, simply drop the packet.
}
}

private isEventBelowLimit(event: string) {
Expand Down
4 changes: 4 additions & 0 deletions src/server/nuclearnet/web_socket_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class WebSocket {
this.sioSocket.on(event, cb)
}

onDisconnect(cb: () => void) {
this.on('disconnect', cb)
}

send(event: string, ...args: any[]) {
this.sioSocket.emit(event, ...args)
}
Expand Down
26 changes: 21 additions & 5 deletions src/server/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as history from 'connect-history-api-fallback'
import * as express from 'express'
import * as http from 'http'
import * as minimist from 'minimist'
import { reaction } from 'mobx'
import * as favicon from 'serve-favicon'
import * as sio from 'socket.io'

Expand Down Expand Up @@ -34,6 +35,11 @@ server.listen(port, () => {
console.log(`NUsight server started at http://localhost:${port}`)
})

const webSocketProxyNUClearNetServer = WebSocketProxyNUClearNetServer.of(
WebSocketServer.of(sioNetwork.of('/nuclearnet')),
{ fakeNetworking: withVirtualRobots },
)

if (withVirtualRobots) {
const virtualRobots = VirtualRobots.of({
fakeNetworking: true,
Expand All @@ -44,9 +50,19 @@ if (withVirtualRobots) {
{ frequency: 10, simulator: ChartSimulator.of() },
],
})
virtualRobots.startSimulators()
let stopSimulators: () => void | undefined
reaction(
() => webSocketProxyNUClearNetServer.numConnections > 0,
enable => {
if (enable) {
// tslint:disable-next-line no-console
console.log('Client connected, starting simulator')
stopSimulators = virtualRobots.startSimulators()
} else if (stopSimulators) {
// tslint:disable-next-line no-console
console.log('No clients remain connected, stopping simulator')
}
},
{ fireImmediately: true },
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Remove duplication between prod.ts and dev.ts

}

WebSocketProxyNUClearNetServer.of(WebSocketServer.of(sioNetwork.of('/nuclearnet')), {
fakeNetworking: withVirtualRobots,
})