Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
bootrap separated modules, still working on koa to serve streams and …
Browse files Browse the repository at this point in the history
…the file server
  • Loading branch information
andrewshatnyy committed Jan 24, 2017
1 parent c90c917 commit 6426f5c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ RUN npm install --only=dev

# TODO VOLUME /var/log/vimana
# TODO EXPORT ports
EXPOSE 7000
EXPOSE 8080


# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
"dependencies": {
"CBuffer": "^2.0.0",
"body-parser": "^1.15.2",
"byline": "^5.0.0",
"cbuffer-fusion": "^1.0.0",
"converter": "0.0.5",
"event-stream": "^3.3.4",
"expect.js": "^0.3.1",
"express": "^4.13.4",
"hashmap": "^2.0.6",
"ip": "1.1.3",
"koa": "^1.2.4",
"lokijs": "^1.3.16",
"md5": "^2.2.1",
"moment": "^2.14.1",
Expand Down
27 changes: 27 additions & 0 deletions src/simulator/adapter.js
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;
71 changes: 71 additions & 0 deletions src/simulator/device.js
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}`);
1 change: 1 addition & 0 deletions src/simulator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const adapter = require('./adapter');

0 comments on commit 6426f5c

Please sign in to comment.