Skip to content

Commit

Permalink
support closing the server
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 27, 2016
1 parent b60de0a commit 4fa3e7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 24 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@

'use strict';

class ServerHelper {
constructor(hftServer, dnsServer, ports) {
this._hftServer = hftServer;
this._dnsServer = dnsServer;
this._ports = ports;
}
get ports() {
return this._ports;
}
close() {
if (this._dnsServer) {
this._dnsServer.close();
this._dnsServer = null;
}
if (this._hftServer) {
this._hftServer.close();
this._hftServer = null;
}
}
}

function startServer(options) {
return new Promise(function(resolve, reject) {
const DNSServer = require('./dnsserver');
Expand All @@ -39,11 +60,12 @@ function startServer(options) {
const server = new HFTServer(options);
let responsesNeeded = 1;
let usedPorts;
let dns;

function reportReady() {
--responsesNeeded;
if (responsesNeeded === 0) {
resolve(usedPorts);
resolve(new ServerHelper(server, dns, usedPorts));
}
}

Expand All @@ -59,7 +81,7 @@ function startServer(options) {
// This doesn't need to dynamicallly check for a change in ip address
// because it should only be used in a static ip address sitaution
// since DNS has to be static for our use-case.
const dns = new DNSServer({address: options.address || iputils.getIpAddresses()[0]});
dns = new DNSServer({address: options.address || iputils.getIpAddresses()[0]});
dns.on('listening', reportReady);
dns.on('error', (err) => {
console.error('You specified --dns but happyFunTimes could not use port 53.');
Expand Down
4 changes: 2 additions & 2 deletions server/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ function exitBecauseAlreadyRunning() {
function startServer() {
const server = require('./server');
server.start(args)
.then((ports) => {
console.log("Listening on ports:", ports);
.then((helper) => {
console.log("Listening on ports:", helper.ports);
})
.catch((err) => {
console.error("error:", err);
Expand Down

0 comments on commit 4fa3e7a

Please sign in to comment.