forked from schteppe/remote-physics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotephysics.js
143 lines (130 loc) · 3.76 KB
/
remotephysics.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Load external node.js modules
var express = require('express')
, http = require('http')
, WebSocketServer = require('websocket').server
, Buffer = require('buffer').Buffer
, fs = require("fs")
, spawn = require('child_process').spawn
, exec = require('child_process').exec
, config = require('./config')
, M3D = require('./public/javascripts/m3d')
, path = require('path');
// Express framework settings
var app = express();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// WebSocket Connections
var connections = [];
var luafile; // quick fix
app.get(/^\/demos\/([a-z]+\.lua)$/, function(req, res){
luafile = __dirname+"/agx/demos/"+req.params[0];
if(path.existsSync(luafile)) {
if(connections.length >= config.agxMaxInstances)
res.render('toomany',{
title:config.title,
numcurrent:connections.length,
public_root:config.public_root
})
else {
res.render('demo',{
title:config.title,
public_root:config.public_root,
websocket_url:config.websocket_url,
websocketport:config.port,
websockethost:config.host,
});
}
} else {
res.render('notfound',{
title:config.title,
public_root:config.public_root,
file:req.params[0]
});
}
});
app.get('/', function(req, res){
fs.readdir(__dirname+"/agx/demos",function(err,files){
// Get lua files from folder
var demos = [];
for(var i=0; i<files.length; i++){
if(files[i].match(/[a-z]+\.lua/))
demos.push(files[i]);
};
// View overview page
res.render('index',{
title:config.title,
public_root:config.public_root,
demos:demos
});
});
});
app.get('/doc', function(req, res){
res.render('doc',{public_root:config.public_root});
});
// Start Webserver
var server = http.createServer(app).listen(config.port);
console.log("Express server listening on port "+config.port);
// Start the WebSocketServer
var wss = new WebSocketServer({httpServer: server});
var idCounter = 0;
wss.on('request', function(req){
console.log("WS request from "+req.origin);
if(connections.length >= config.agxMaxInstances || // Reject if we have too many agx instances
req.origin.indexOf(config.host) == -1) { // Or if the client isn't on our website
req.reject(null, req.origin);
console.log("Rejected WS request from "+req.origin);
return;
}
// Accept connection
var connection = req.accept(null, req.origin);
connection.id = idCounter++;
connections.push(connection);
// Start a new world
var args = [luafile];
for(var i in config.agxFlags)
args.push(config.agxFlags[i]);
var world = new M3D.AgxWorld(config.agxCommand, args);
world.addEventListener('construct',function(){
// Send world info (geometries etc) as JSON
connection.send(world.toJSONString());
// Simulation loop
setInterval(function(){
world.step();
}, world.dt*1000);
});
var broadCastCount = 0;
world.addEventListener('change',function(){
if(broadCastCount % (world.skip+1) == 0)
connection.send(world.toBuffer());
broadCastCount++;
});
// Message
connection.on('message', function(message) {
switch(message.type){
case 'utf8':
break;
case 'binary':
world.handleBufferMessage(message.binaryData);
break;
}
});
// Close
connection.on('close', function(reason) {
// Delete the world, close agx
world.destruct();
// Delete connection from "global" array
for(var i=0; i<connections.length; i++)
if(connections[i].id == connection.id)
connections.splice(i,1);
});
});