Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 2 | * @author: Wentao Shang |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | var WebSocketTransport = function WebSocketTransport() { |
| 7 | this.ws = null; |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 8 | this.connectedHost = null; // Read by NDN. |
| 9 | this.connectedPort = null; // Read by NDN. |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 10 | this.elementReader = null; |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 11 | this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort |
| 12 | (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu", |
| 13 | "E.ws.ndn.ucla.edu"], |
| 14 | 9696); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 15 | }; |
| 16 | |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Connect to the host and port in ndn. This replaces a previous connection and sets connectedHost |
| 19 | * and connectedPort. Once connected, call onopenCallback(). |
| 20 | * Listen on the port to read an entire binary XML encoded element and call |
| 21 | * ndn.onReceivedElement(element). |
| 22 | */ |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 23 | WebSocketTransport.prototype.connect = function(ndn, onopenCallback) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 24 | if (this.ws != null) |
| 25 | delete this.ws; |
| 26 | |
| 27 | this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 28 | if (LOG > 0) console.log('ws connection created.'); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 29 | this.connectedHost = ndn.host; |
| 30 | this.connectedPort = ndn.port; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 31 | |
| 32 | this.ws.binaryType = "arraybuffer"; |
| 33 | |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 34 | this.elementReader = new BinaryXmlElementReader(ndn); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 35 | var self = this; |
| 36 | this.ws.onmessage = function(ev) { |
| 37 | var result = ev.data; |
| 38 | //console.log('RecvHandle called.'); |
| 39 | |
| 40 | if(result == null || result == undefined || result == "" ) { |
| 41 | console.log('INVALID ANSWER'); |
| 42 | } else if (result instanceof ArrayBuffer) { |
| 43 | var bytearray = new Uint8Array(result); |
| 44 | |
Jeff Thompson | 13c2e7b | 2012-12-01 17:33:30 -0800 | [diff] [blame] | 45 | if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray)); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 46 | |
| 47 | try { |
Jeff Thompson | bb9802d | 2013-01-20 23:54:18 -0800 | [diff] [blame] | 48 | // Find the end of the binary XML element and call ndn.onReceivedElement. |
| 49 | self.elementReader.onReceivedData(bytearray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 50 | } catch (ex) { |
| 51 | console.log("NDN.ws.onmessage exception: " + ex); |
| 52 | return; |
| 53 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 58 | if (LOG > 3) console.log(ev); |
| 59 | if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.'); |
| 60 | if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 61 | // NDN.registerPrefix will fetch the ccndid when needed. |
| 62 | |
| 63 | onopenCallback(); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | this.ws.onerror = function(ev) { |
| 67 | console.log('ws.onerror: ReadyState: ' + this.readyState); |
| 68 | console.log(ev); |
| 69 | console.log('ws.onerror: WebSocket error: ' + ev.data); |
| 70 | } |
| 71 | |
| 72 | this.ws.onclose = function(ev) { |
| 73 | console.log('ws.onclose: WebSocket connection closed.'); |
| 74 | self.ws = null; |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 75 | |
| 76 | // Close NDN when WebSocket is closed |
| 77 | ndn.readyStatus = NDN.CLOSED; |
| 78 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 79 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 80 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 81 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 82 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Send the Uint8Array data. |
| 85 | */ |
| 86 | WebSocketTransport.prototype.send = function(data) { |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 87 | if (this.ws != null) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 88 | // If we directly use data.buffer to feed ws.send(), |
| 89 | // WebSocket may end up sending a packet with 10000 bytes of data. |
| 90 | // That is, WebSocket will flush the entire buffer |
| 91 | // regardless of the offset of the Uint8Array. So we have to create |
| 92 | // a new Uint8Array buffer with just the right size and copy the |
| 93 | // content from binaryInterest to the new buffer. |
| 94 | // ---Wentao |
| 95 | var bytearray = new Uint8Array(data.length); |
| 96 | bytearray.set(data); |
| 97 | this.ws.send(bytearray.buffer); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 98 | if (LOG > 3) console.log('ws.send() returned.'); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | console.log('WebSocket connection is not established.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 102 | } |