blob: ddd150d9093e5c5de56ac07b2819512b3cf2f586 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson17a9da82012-11-12 01:11:01 -08002 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 * Implement getAsync and putAsync used by NDN using nsISocketTransportService.
5 * This is used inside Firefox XPCOM modules.
6 */
7
8// Assume already imported the following:
9// Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
10// Components.utils.import("resource://gre/modules/NetUtil.jsm");
11
Jeff Thompson3b6bf982013-01-13 20:00:03 -080012var XpcomTransport = function XpcomTransport() {
13 this.ndn = null;
14 this.socket = null; // nsISocketTransport
15 this.outStream = null;
Jeff Thompsonf668acb2013-01-26 20:29:46 -080016 this.connectedHost = null; // Read by NDN.
17 this.connectedPort = null; // Read by NDN.
Jeff Thompson3b6bf982013-01-13 20:00:03 -080018
Jeff Thompson3fa84152012-12-16 17:11:42 -080019 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
20 (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu",
21 "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"],
22 9695);
Jeff Thompson17a9da82012-11-12 01:11:01 -080023};
24
Jeff Thompson3b6bf982013-01-13 20:00:03 -080025/*
Jeff Thompsonf668acb2013-01-26 20:29:46 -080026 * Connect to the host and port in ndn. This replaces a previous connection and sets connectedHost
27 * and connectedPort. Once connected, call onopenCallback().
Jeff Thompson07f15fb2013-01-20 20:32:29 -080028 * Listen on the port to read an entire binary XML encoded element and call
Jeff Thompson26325842013-01-26 20:03:25 -080029 * ndn.onReceivedElement(element).
Jeff Thompson3b6bf982013-01-13 20:00:03 -080030 */
Jeff Thompsonf668acb2013-01-26 20:29:46 -080031XpcomTransport.prototype.connect = function(ndn, onopenCallback) {
Jeff Thompson3b6bf982013-01-13 20:00:03 -080032 if (this.socket != null) {
33 try {
34 this.socket.close(0);
35 } catch (ex) {
36 console.log("XpcomTransport socket.close exception: " + ex);
37 }
38 this.socket = null;
39 }
40 this.ndn = ndn;
41
42 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
43 (Components.interfaces.nsISocketTransportService);
44 var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
45 (Components.interfaces.nsIInputStreamPump);
46 this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null);
47 this.connectedHost = ndn.host;
48 this.connectedPort = ndn.port;
49 this.outStream = this.socket.openOutputStream(1, 0, 0);
50
51 var inStream = this.socket.openInputStream(0, 0, 0);
52 var dataListener = {
Jeff Thompson26325842013-01-26 20:03:25 -080053 elementReader: new BinaryXmlElementReader(ndn),
Jeff Thompson3b6bf982013-01-13 20:00:03 -080054
55 onStartRequest: function (request, context) {
56 },
57 onStopRequest: function (request, context, status) {
58 },
59 onDataAvailable: function (request, context, _inputStream, offset, count) {
60 try {
61 // Use readInputStreamToString to handle binary data.
62 // TODO: Can we go directly from the stream to Uint8Array?
Jeff Thompson24189e52013-01-20 23:29:51 -080063 this.elementReader.onReceivedData(DataUtils.toNumbersFromString
64 (NetUtil.readInputStreamToString(inStream, count)));
Jeff Thompson3b6bf982013-01-13 20:00:03 -080065 } catch (ex) {
Jeff Thompson24189e52013-01-20 23:29:51 -080066 console.log("XpcomTransport.onDataAvailable exception: " + ex + "\n" + ex.stack);
Jeff Thompson3b6bf982013-01-13 20:00:03 -080067 }
68 }
69 };
70
71 pump.init(inStream, -1, -1, 0, 0, true);
72 pump.asyncRead(dataListener, null);
Jeff Thompsonf668acb2013-01-26 20:29:46 -080073
74 onopenCallback();
Jeff Thompson3b6bf982013-01-13 20:00:03 -080075};
76
77/*
78 * Send the data over the connection created by connect.
79 */
80XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
81 if (this.socket == null) {
82 console.log("XpcomTransport connection is not established.");
83 return;
84 }
Jeff Thompson17a9da82012-11-12 01:11:01 -080085
Jeff Thompson3b6bf982013-01-13 20:00:03 -080086 var rawDataString = DataUtils.toString(data);
87 this.outStream.write(rawDataString, rawDataString.length);
88 this.outStream.flush();
89};