Skip to content

Commit

Permalink
node-mock and node-client check if there is a wifi address
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Mar 19, 2021
1 parent 1ef6c5c commit 5ab4d60
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 39 deletions.
62 changes: 42 additions & 20 deletions discover-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
// log
// search a WiFi interface to serve
var os = require('os');
var interfaces = os.networkInterfaces();
var bindInteface = null;
var bindAddress = null;
var wifiAddressPrefix = '192.168.0'
Object.keys(interfaces).forEach(function (iName) {
interfaces[iName].forEach(function (ipInfo) {
if (ipInfo.address.startsWith(wifiAddressPrefix)) {
console.log(`-- Found WiFi interface named ${iName} with adress ${ipInfo.address}`);
bindInteface = iName;
bindAddress = ipInfo.address;
}
});
});
if (bindInteface == null || bindAddress == null) {
console.log('-- WARNING: It was not able to find a WiFi interface');
}

// log funcs
function logStatusObject(name, obj) {
console.log("-- " + name + " = " + JSON.stringify(obj, null, 2));
}
Expand All @@ -7,43 +26,43 @@ function logStatus(msg) {
}

// ssdp m-search
var found = false;
var Client = require('node-ssdp').Client
// location request
const assert = require('assert').strict;
const url = require('url');
const http = require('http');
const SERVICE_TYPE = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"
var SSDPClient = require('node-ssdp').Client
const GINGA_SSDP_ST = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"
const BaseURL = "GingaCC-Server-BaseURL"
const SecureBaseURL = "GingaCC-Server-SecureBaseURL"

// ssdp m-search
client = new Client();
client = new SSDPClient({
// uncoment to only use wifi interface
// interfaces: [
// bindInteface
// ],
reuseAddr: false
});

client.on('response', function (headers, statusCode, rinfo) {
if (found) return;
found = true;

// log
assert(statusCode == 200);
logStatus('m-search response from ' + rinfo.address);
logStatusObject("response headers ", headers);
// get location

// get LOCATION header
var location = 'undefined'
if (headers["LOCATION"])
location = headers["LOCATION"];
console.log()

// perform request
// perform request to LOCATION
const uri = new url.URL(location);
const options = {
hostname: uri.hostname,
port: uri.port,
path: uri.pathname,
// url: "http://139.82.153.96:44642/location",
method: 'GET'
}
var req = http.request(options, response => {
var headers = response.headers;
logStatus('response from ' + location);
logStatus('get response from ' + location);
logStatusObject("headers ", headers);
logStatus("GingaCC-Server BaseURL=" + headers[BaseURL.toLowerCase()])
logStatus("GingaCC-Server BaseURL=" + headers[SecureBaseURL.toLowerCase()])
Expand All @@ -52,9 +71,12 @@ client.on('response', function (headers, statusCode, rinfo) {
req.end()
});

logStatus('Perform search for GingaCC-Server ...');
console.log()
logStatus(`m-search for ${GINGA_SSDP_ST}`);
client.search(GINGA_SSDP_ST);

setInterval(function () {
client.search(SERVICE_TYPE);
logStatus(`m-search for ${GINGA_SSDP_ST}`);
client.stop()
client.search(GINGA_SSDP_ST);
// client.search('ssdp:all')
}, 4000)
65 changes: 46 additions & 19 deletions discover-mock-server.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
// search a WiFi interface to serve
const ip = require("ip")
// ssdp
const Server = require("node-ssdp").Server
const SERVICE_TYPE = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"
// ws routes
var os = require('os');
var interfaces = os.networkInterfaces();
var serveInteface = null;
var serveAddress = null;
const wifiAddressPrefix = '192.168.0'
Object.keys(interfaces).forEach(function (iName) {
interfaces[iName].forEach(function (ipInfo) {
if (ipInfo.address.startsWith(wifiAddressPrefix)) {
console.log(`-- Found WiFi interface named ${iName} with adress ${ipInfo.address}`);
serveInteface = iName;
serveAddress = ipInfo.address;
}
});
});
if (serveInteface == null || serveAddress == null) {
console.log('-- WARNING: It was not able to find a WiFi interface');
}

// create WebService /location route
const express = require("express");
const app = express();
const port = 44642;

// start ws routes
const wsport = 44642;
app.get("/location", (req, res) => {
res.header("Ext", "");
res.header("GingaCC-Server-BaseURL", "http://" + ip.address() + ":44642");
res.header("GingaCC-Server-SecureBaseURL", "https://" + ip.address() + ":44642");
res.header("GingaCC-Server-BaseURL", "http://" + serveAddress + ":" + wsport);
res.header("GingaCC-Server-SecureBaseURL", "https://" + serveAddress + ":" + wsport);
res.header("GingaCC-Server-PairingMethods", "qcode,kex");
res.header("GingaCC-Server-Manufacturer", "TeleMidia");
res.header("GingaCC-Server-ModelName", "TeleMidia GingaCC-Server Mock");
res.header("GingaCC-Server-FriendlyName", "TeleMidia Ginga Mock ");
res.header("SERVER", "TeleMidia Ginga Mock");
res.send();
});
app.listen(port, () => {
console.log(`-- GingaCC-Server listening on port ${port}.`)
app.listen(wsport, () => {
console.log(`-- GingaCC - Server WS listening on port ${wsport}.`);
});

// start ssdp
const server = new Server({
location: "http://" + ip.address() + ":44642" + "/location",
suppressRootDeviceAdvertisements: true

// start SSDP
var SSDPServer = require('node-ssdp').Server;
const SERVICE_TYPE = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"
const server = new SSDPServer({
location: "http://" + (serveAddress ? serveAddress: ip.address()) + ":" + wsport + "/location",
suppressRootDeviceAdvertisements: true,
// uncoment to only use wifi interface
// interfaces: [
// serveInteface
// ],
reuseAddr: true,
adInterval: 30000 // hight notify interval because m-search response is more important
});
server.addUSN(SERVICE_TYPE)
server.addUSN(SERVICE_TYPE);
server.start()
.catch(e => {
console.log("-- Failed to start GingaCC-Server SSDP:", e)
console.log("-- Failed to start GingaCC-Server SSDP:", e);
})
.then(() => {
console.log("-- GingaCC-Server SSDP started.")
})
console.log("-- GingaCC-Server SSDP started.");
})

process.on('exit', function(){
server.stop() // advertise shutting down and stop listening
})

0 comments on commit 5ab4d60

Please sign in to comment.