-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnvironmentServer.js
61 lines (49 loc) · 1.37 KB
/
EnvironmentServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = class EnvironmentServer
{
constructor(observationSpace, actionSpace)
{
this.observationSpace = observationSpace;
this.actionSpace = actionSpace;
this.createApp();
}
createApp()
{
var app = express();
var router = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
router.get('/config', async (req, res) => {
res.json({
observation_space: this.observationSpace,
action_space: this.actionSpace
});
});
router.post('/step', async (req, res) => {
var action = req.body.action;
action = Array.isArray(action) ? action.map((e) => parseInt(e)) : parseInt(action); //TODO: handle multidim
var [obs, r, done, info] = await this.step(action);
res.json([obs, r, done, info]);
});
router.get('/reset', async (req, res) => {
var obs = await this.reset();
res.json(obs);
});
app.use('/', router); //TODO: configurable base path?
this.app = app;
}
listen(port)
{
this.server = this.app.listen(port);
console.log('Listening on port ' + this.server.address().port);
}
async step(action)
{
throw new Error("Step must be implemented");
}
async reset()
{
throw new Error("Reset must be implemented");
}
}
var express = require('express');
var bodyParser = require('body-parser');