blob: 98cce3032e704217e860165013355ae21e7c94bd [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;
16 this.connectedHost = null;
17 this.connectedPort = null;
18
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/*
26 * Connect to the host and port in ndn. This replaces a previous connection.
Jeff Thompson07f15fb2013-01-20 20:32:29 -080027 * Listen on the port to read an entire binary XML encoded element and call
Jeff Thompson26325842013-01-26 20:03:25 -080028 * ndn.onReceivedElement(element).
Jeff Thompson3b6bf982013-01-13 20:00:03 -080029 */
Jeff Thompson26325842013-01-26 20:03:25 -080030XpcomTransport.prototype.connect = function(ndn) {
Jeff Thompson3b6bf982013-01-13 20:00:03 -080031 if (this.socket != null) {
32 try {
33 this.socket.close(0);
34 } catch (ex) {
35 console.log("XpcomTransport socket.close exception: " + ex);
36 }
37 this.socket = null;
38 }
39 this.ndn = ndn;
40
41 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
42 (Components.interfaces.nsISocketTransportService);
43 var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
44 (Components.interfaces.nsIInputStreamPump);
45 this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null);
46 this.connectedHost = ndn.host;
47 this.connectedPort = ndn.port;
48 this.outStream = this.socket.openOutputStream(1, 0, 0);
49
50 var inStream = this.socket.openInputStream(0, 0, 0);
51 var dataListener = {
Jeff Thompson26325842013-01-26 20:03:25 -080052 elementReader: new BinaryXmlElementReader(ndn),
Jeff Thompson3b6bf982013-01-13 20:00:03 -080053
54 onStartRequest: function (request, context) {
55 },
56 onStopRequest: function (request, context, status) {
57 },
58 onDataAvailable: function (request, context, _inputStream, offset, count) {
59 try {
60 // Use readInputStreamToString to handle binary data.
61 // TODO: Can we go directly from the stream to Uint8Array?
Jeff Thompson24189e52013-01-20 23:29:51 -080062 this.elementReader.onReceivedData(DataUtils.toNumbersFromString
63 (NetUtil.readInputStreamToString(inStream, count)));
Jeff Thompson3b6bf982013-01-13 20:00:03 -080064 } catch (ex) {
Jeff Thompson24189e52013-01-20 23:29:51 -080065 console.log("XpcomTransport.onDataAvailable exception: " + ex + "\n" + ex.stack);
Jeff Thompson3b6bf982013-01-13 20:00:03 -080066 }
67 }
68 };
69
70 pump.init(inStream, -1, -1, 0, 0, true);
71 pump.asyncRead(dataListener, null);
72};
73
74/*
75 * Send the data over the connection created by connect.
76 */
77XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
78 if (this.socket == null) {
79 console.log("XpcomTransport connection is not established.");
80 return;
81 }
Jeff Thompson17a9da82012-11-12 01:11:01 -080082
Jeff Thompson3b6bf982013-01-13 20:00:03 -080083 var rawDataString = DataUtils.toString(data);
84 this.outStream.write(rawDataString, rawDataString.length);
85 this.outStream.flush();
86};
87
88XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
89 var thisXpcomTransport = this;
90
Jeff Thompson26325842013-01-26 20:03:25 -080091 if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port)
92 this.connect(ndn);
Jeff Thompson17a9da82012-11-12 01:11:01 -080093
Jeff Thompson26325842013-01-26 20:03:25 -080094 ndn.expressInterestHelper(interest, closure);
Jeff Thompson17a9da82012-11-12 01:11:01 -080095};
Jeff Thompson24189e52013-01-20 23:29:51 -080096