This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bootrap separated modules, still working on koa to serve streams and …
…the file server
- Loading branch information
1 parent
c90c917
commit 6426f5c
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Adaptor's SSD server | ||
const log = require('../config/logger'); | ||
const common = require('../common'); | ||
const config = require('../config/config'); | ||
const ip = require('ip').address(); | ||
const { uuid, urn, machinePort, filePort } = config.app.simulator; | ||
const SSDP = require('node-ssdp').Server; | ||
|
||
const ssdpOptions = { | ||
location: `${ip}:${machinePort}:${filePort}`, | ||
udn: uuid, | ||
adInterval: 10000, | ||
allowWildcards: true, | ||
}; | ||
|
||
const server = new SSDP(ssdpOptions); | ||
server.on('advertise-alive', log.debug); | ||
server.on('advertise-bye', log.debug); | ||
server.on('error', (err) => { | ||
common.processError(err, true); | ||
}); | ||
|
||
server.addUSN(`urn:schemas-mtconnect-org:service:${urn}:1`); | ||
|
||
process.on('exit', server.stop); | ||
|
||
module.exports = server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const log = require('../config/logger'); | ||
const { PassThrough } = require('stream'); | ||
// const common = require('../common'); | ||
const config = require('../config/config'); | ||
const byline = require('byline'); | ||
const koa = require('koa'); | ||
const app = koa(); | ||
const fs = require('fs'); | ||
const { inputFile, machinePort } = config.app.simulator; | ||
|
||
/** | ||
* writeDataLoop() sends machine data to the Agent in loop | ||
* | ||
* @param {Object} socket | ||
* @param {Object} count | ||
* @param {Object} delay | ||
*/ | ||
// function writeDataLoop(socket, countValue, delay) { | ||
// let count = countValue; | ||
// while (count) { | ||
// lineReader.eachLine(inputFile, (line) => { | ||
// setTimeout(() => { | ||
// try { | ||
// socket.write(`${line}\n`); | ||
// } catch (e) { | ||
// common.processError(`Error: ${e}`, false); | ||
// } | ||
// }, Math.floor(Math.random() * delay)); // Simulate delay | ||
// }); | ||
// count = count - 1; | ||
// } | ||
// } | ||
|
||
|
||
/** | ||
* Simulator (adapter) | ||
*/ | ||
|
||
// machine.on('connection', (socket) => { | ||
// log.debug('Machine connected'); | ||
// writeDataLoop(socket, 100, 10000); | ||
// socket.on('data', (data) => { | ||
// console.log('Received:', data.toString()); | ||
// }) | ||
// }); | ||
|
||
// machine.on('error', (err) => { | ||
// common.processError(`${err}`, true); | ||
// }); | ||
|
||
// logger | ||
|
||
app.use(function *logger(next) { | ||
const start = new Date; | ||
yield next; | ||
const ms = new Date - start; | ||
log.info('%s %s - %s', this.method, this.url, ms); | ||
}); | ||
|
||
// response | ||
|
||
const stream = fs.createReadStream(inputFile); | ||
|
||
app.use(function *response() { | ||
this.body = byline.createStream(stream) | ||
.on('error', this.onerror) | ||
.pipe(PassThrough()); | ||
}); | ||
|
||
app.listen(machinePort); | ||
console.info(`starting server on localhost:${machinePort}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const adapter = require('./adapter'); |