Skip to content

Commit 74465a1

Browse files
Mary ellen KerrGitHub Enterprise
authored andcommitted
RIC-1076: Add the TcpRangeCounter Client to the CodeRT Samples (#61)
* A new Node.js Express Client sample is provided that sends events to the TcpRangeCounter sample. * Removed some dependencies, removed HCL image, moved some styling to style.css, put under client-ui folder.
1 parent 2114f90 commit 74465a1

14 files changed

Lines changed: 1252 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ art-comp-test/tests/**/*_target/*
99
art-comp-test/**/.vscode/art_build_settings.json
1010
art-comp-test/testLib_target/*
1111
art-comp-test/tests/*/logs/application.log
12+
art-samples/TcpRangeCounter/client-ui/node_modules
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TcpRangeCounter_Client</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.validation.validationbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
21+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
22+
<nature>com.hcl.nodejs.jsnature.nature</nature>
23+
</natures>
24+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<installed facet="nodejs.app" version="1.0"/>
4+
</faceted-project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TcpRangeCounter Client
2+
3+
This Node.js Express sample client works with the TcpRangeCounter sample by sending events to the TcpRangeCounter sample. The TcpRangeCounter sample implements a counter that starts at a min value and increments until it reaches a max value. It embeds a TCP server which allows it to be dynamically configured by means of sending TCP requests. These requests corresponds to the events of the Events protocol:
4+
5+
* setMin: Sets the min value of the range (default 0).
6+
7+
* setMax: Sets the max value of the range (default 10).
8+
9+
* setDelta: Sets the delta by which the counter increments in each step (default 1; Use a negative value to count down instead of up).
10+
11+
* resumeCounting: Resume the counter when it has stopped.
12+
13+
The following are instructions to run this sample client.
14+
15+
* Build and start the TcpRangeCounter sample by following the directions in its README file.
16+
17+
* Start this sample client by entering 'npm install' and then 'npm start' in a terminal session. The client listens on port 3000.
18+
19+
* Invoke the sample client in a web browser at http://localhost:3000/.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var createError = require('http-errors');
2+
var express = require('express');
3+
var path = require('path');
4+
var bodyParser = require('body-parser');
5+
6+
var indexRouter = require('./routes/index');
7+
var usersRouter = require('./routes/users');
8+
9+
var app = express();
10+
11+
// view engine setup
12+
app.set('views', path.join(__dirname, 'views'));
13+
app.set('view engine', 'ejs');
14+
15+
app.use(express.json());
16+
app.use(express.urlencoded({ extended: false }));
17+
app.use(express.static(path.join(__dirname, 'public')));
18+
app.use(bodyParser.json());
19+
20+
app.use('/', indexRouter);
21+
app.use('/users', usersRouter);
22+
23+
// catch 404 and forward to error handler
24+
app.use(function(req, res, next) {
25+
next(createError(404));
26+
});
27+
28+
// error handler
29+
app.use(function(err, req, res, next) {
30+
// set locals, only providing error in development
31+
res.locals.message = err.message;
32+
res.locals.error = req.app.get('env') === 'development' ? err : {};
33+
34+
// render the error page
35+
res.status(err.status || 500);
36+
res.render('error');
37+
});
38+
39+
module.exports = app;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('TcpRangeCounter_Client:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

0 commit comments

Comments
 (0)