blob: 178639bd1c9cd5e2e5702fa631b2871dcde4cc6c [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);
Jeff Thompson48aa4922013-03-02 11:39:22 -080047 if (LOG > 0) console.log('XpcomTransport: Connected to ' + ndn.host + ":" + ndn.port);
Jeff Thompson3b6bf982013-01-13 20:00:03 -080048 this.connectedHost = ndn.host;
49 this.connectedPort = ndn.port;
50 this.outStream = this.socket.openOutputStream(1, 0, 0);
51
52 var inStream = this.socket.openInputStream(0, 0, 0);
53 var dataListener = {
Jeff Thompson26325842013-01-26 20:03:25 -080054 elementReader: new BinaryXmlElementReader(ndn),
Jeff Thompson3b6bf982013-01-13 20:00:03 -080055
56 onStartRequest: function (request, context) {
57 },
58 onStopRequest: function (request, context, status) {
59 },
60 onDataAvailable: function (request, context, _inputStream, offset, count) {
61 try {
62 // Use readInputStreamToString to handle binary data.
63 // TODO: Can we go directly from the stream to Uint8Array?
Jeff Thompson24189e52013-01-20 23:29:51 -080064 this.elementReader.onReceivedData(DataUtils.toNumbersFromString
65 (NetUtil.readInputStreamToString(inStream, count)));
Jeff Thompson3b6bf982013-01-13 20:00:03 -080066 } catch (ex) {
Jeff Thompson24189e52013-01-20 23:29:51 -080067 console.log("XpcomTransport.onDataAvailable exception: " + ex + "\n" + ex.stack);
Jeff Thompson3b6bf982013-01-13 20:00:03 -080068 }
69 }
70 };
71
72 pump.init(inStream, -1, -1, 0, 0, true);
73 pump.asyncRead(dataListener, null);
Jeff Thompsonf668acb2013-01-26 20:29:46 -080074
75 onopenCallback();
Jeff Thompson3b6bf982013-01-13 20:00:03 -080076};
77
78/*
79 * Send the data over the connection created by connect.
80 */
81XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
82 if (this.socket == null) {
83 console.log("XpcomTransport connection is not established.");
84 return;
85 }
Jeff Thompson17a9da82012-11-12 01:11:01 -080086
Jeff Thompson3b6bf982013-01-13 20:00:03 -080087 var rawDataString = DataUtils.toString(data);
88 this.outStream.write(rawDataString, rawDataString.length);
89 this.outStream.flush();
90};