SISO2014Javascript

advertisement
A Javascript Implementation of the
Binary DIS Protocol
Don McGregor, Don Brutzman, Curt Blais,
MOVES Institute
mcgredo@nps.edu
Javascript & Web Networking
• Javascript? That’s just a toy language,
right?
• Not any more. Simulation applications can
be written in Javascript, and there are
significant advantages to doing so
• New Javascript networking standards
(Websockets, WebRTC) allow low latency,
high (ish) performance directly into the web
page
2
Web Networking
WebRTC (Javascript UDP)
Web Page:
Internet Explorer
Web Page:
Firefox Mobile
Websocket
(Javascript TCP socket)
Web Server
Content: Javascript,
3D, images, graphing,
3
Maps, mash-ups
Web Networking
• Websockets allow low latency TCP sockets
between web page pages and servers in
Javascript—no polling, not wrapped in HTTP
• WebRTC can allow low latency UDP Javascript
messages directly between web pages
• Performance is not bad--~5K messages per
second to desktop clients for websockets
• It’s all unicast—no broadcast or multicast, with all
that implies
4
Why Do It?
• Compelling economic & system management
advantages for web-based simulation
– Distribute all applications from a central web server,
where it can be easily upgraded
– Cross-platform, including mobile
– Moves at web speed—can mash up with maps,
graphics, WebGL, Twitter, IM, …
– Scale out on cloud, low barrier to entry. Cloud-side
compute resources are effectively infinite
– How many desktop apps do you use today, vs how
many web based applications? That’s because the
economics are that compelling
5
What Message Format?
These are the pipes;
What format should we
send state information such as
position and orientation?
6
Message Format: JSON
•
•
•
•
One option is to send messages in JSON format
This is the approach taken by WebLVC
But this requires you to specify the format of the JSON
Lots of standards meetings
7
Message Format: DIS
• Classical Javascript wasn’t very good at
handling binary
• More modern Javascript can use
“ArrayBuffers” to manipulate binary data
• Support is good (~80% of current browsers
in the wild, will only get better as old
browsers are upgraded). See
http://caniuse.com
• So why not use this to implement classic
IEEE-1278.1?
8
DIS in Javascript
• Strong standards support, lots of gateway
support for HLA, TENA, AIS, other protocols
to DIS
• It’s already standardized
• DIS can be verbose and carries redundant
information compared to HLA RPR-FOM,
which is what WebLVC is based on
• Protocol issues such as heartbeat
9
Using Javascript DIS
// Receive and decode a message
NetworkSingleton.prototype.onMessage = function(evt)
{
// convert from binary to javascript object
var pduFactory = new dis.PduFactory();
var pdu = pduFactory.createPdu(evt.data);
switch(pdu.pduType)
{
case 1: …..
// Convert to local (or lat/lon/alt) coordinates
var localCoordinates =
rangeCoordinates.ECEFtoENU(espdu.entityLocation.x,
espdu.entityLocation.y,
espdu.entityLocation.z);
// Encode DIS
var dataBuffer = new ArrayBuffer(1500);
var os = new dis.OutputStream(dataBuffer);
espdu.encodeToBinaryDIS(os);
var trimmedData = dataBuffer.slice(0, os.currentPosition);
websocketConnection.send(trimmedData);
10
Performance Compared to JSON
• Binary DIS vs JSON-encoded DIS, longer bars better
• Yellow JSON DIS, red binary DIS
11
Performance Notes
• See http://jsperf.com/javascript-dis-native-vs-json/2, benchmark your
own browser, or create new benchmarks
• Both JSON and binary formats can be workable; depending on
Javascript engine they can even be about the same
• Browsers and Javascript engines matter a lot! JSON can be as fast as
binary on some browsers and Javascript engines (IE 11, for example)
• JSON can be faster on IE 11 than binary on Firefox
• I suspect in the long term results will converge towards the current
Safari benchmarks as Javascript engines improve
• OK performance on mobile—and it works on mobile!
• OK for a few thousand messages per second into the web page
(depending on lots of stuff) which puts about a 10% load on desktop
CPU
• In general, the simulation implementation is more likely to be an issue
than the networking overhead, particularly with 3D.
12
Binary Javascript DIS Vs WebLVC
• WebLVC JSON update messages have less state information and can
be parsed somewhat faster, but it’s in the same ballpark
• WebLVC can also avoid DIS heartbeats
• http://jsperf.com/javascript-dis/13
13
Google Maps Example
• Application on server side listens for traditional
native UDP DIS on the local ethernet network. In
this case, AIS (real time commercial ship
locations) is being translated into DIS by JBUS
• Web page receives forwarded DIS messages via
websockets, decodes, does coordinate conversion
to lat/lon, displays icon on map. Also sends DIS
updates based on web browser geolocation
• Uses Google Maps Javascript API to implement a
real-time mapping application. Works on modern
browsers and mobile, including IOS and Android 14
Google Maps + DIS in Browser
15
Availability
• Free, BSD non-viral license
• https://svn.code.sf.net/p/open-dis/code/ open-dis-code/
– Includes server side code (to convert native UDP to websocket
transport) and client code in languages/javascript directory
• NodeJS is a scalable server-side Javascript
framework popular in Silicon Valley (used at
linkedin, twitter, ebay, etc.)
– NodeJS DIS javascript module available at
https://www.npmjs.org/package/open-dis
16
Future Work
• Open Street Map vs Google Maps to avoid proprietary
vendor dependencies, requirement to have a Google Maps
server inside classified areas
• Server side architecture for scalability: how much to put on
client vs server? How to limit traffic to the client? Inherent
unicast problems as the number of clients increase?
• Node.js transport gateways on the server side?
• Integration with voice, video in web browsers and DIS
intercom PDUs
• Integrate with <your favorite web-based application>
17
Download