Merge branch 'master' of https://github.com/remap/ndn-js
diff --git a/js/WebSocketTransport.js b/js/WebSocketTransport.js
index e2a78a6..fc17e85 100644
--- a/js/WebSocketTransport.js
+++ b/js/WebSocketTransport.js
@@ -231,7 +231,7 @@
 										if (LOG > 3) console.log("Fetch key according to keylocator");
 										var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit);
 										var interest = new Interest(keylocator.keyName.contentName.getPrefix(4));
-										interest.interestLifetime = 4.0;
+										interest.interestLifetime = 4000;  // milliseconds
 										self.expressInterest(ndn, interest, nextClosure);
 									}
 								}
diff --git a/js/XpcomTransport.js b/js/XpcomTransport.js
index 42273f2..57f9dd9 100644
--- a/js/XpcomTransport.js
+++ b/js/XpcomTransport.js
@@ -9,61 +9,183 @@
 // Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 // Components.utils.import("resource://gre/modules/NetUtil.jsm");
 
-var XpcomTransport = function XpcomTransport() { 
+var XpcomTransport = function XpcomTransport() {
+    this.ndn = null;
+    this.socket = null; // nsISocketTransport
+    this.outStream = null;
+    this.connectedHost = null;
+    this.connectedPort = null;
+    
     this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
         (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu", 
           "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"],
          9695);
 };
 
-XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
-    var binaryInterest = encodeToBinaryInterest(interest);
+/*
+ * Connect to the host and port in ndn.  This replaces a previous connection.
+ * Listen on the port to read an entire binary XML encoded response and call
+ *    listener.onReceivedData(data) where data is Uint8Array.
+ */
+XpcomTransport.prototype.connect = function(ndn, listener) {
+    if (this.socket != null) {
+        try {
+            this.socket.close(0);
+        } catch (ex) {
+			console.log("XpcomTransport socket.close exception: " + ex);
+		}
+        this.socket = null;
+    }
+    this.ndn = ndn;
+
+	var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
+        (Components.interfaces.nsISocketTransportService);
+	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
+        (Components.interfaces.nsIInputStreamPump);
+	this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null);
+    this.connectedHost = ndn.host;
+    this.connectedPort = ndn.port;
+    this.outStream = this.socket.openOutputStream(1, 0, 0);
+
+    var inStream = this.socket.openInputStream(0, 0, 0);
+	var dataListener = {
+		dataParts: [],
+        structureDecoder: new BinaryXMLStructureDecoder(),
+		
+		onStartRequest: function (request, context) {
+		},
+		onStopRequest: function (request, context, status) {
+		},
+		onDataAvailable: function (request, context, _inputStream, offset, count) {
+			try {
+				// Use readInputStreamToString to handle binary data.
+                // TODO: Can we go directly from the stream to Uint8Array?
+				var rawData = DataUtils.toNumbersFromString
+                    (NetUtil.readInputStreamToString(inStream, count));
+				
+                // Process multiple objects in this packet.
+                while(true) {
+                    // Scan the input to check if a whole ccnb object has been read.
+                    this.structureDecoder.seek(0);
+                    if (this.structureDecoder.findElementEnd(rawData)) {
+                        // Got the remainder of an object.  Report to the caller.
+                        this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
+                        listener.onReceivedData(DataUtils.concatArrays(this.dataParts));
+                    
+                        // Need to read a new object.
+                        rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
+                        this.dataParts = [];
+                        this.structureDecoder = new BinaryXMLStructureDecoder();
+                        if (rawData.length == 0)
+                            // No more data in the packet.
+                            return;
+                        
+                        // else loop back to decode.
+                    }
+                    else {
+                        // Save for a later call to concatArrays so that we only copy data once.
+                        this.dataParts.push(rawData);
+                        return;
+                    }
+                }
+			} catch (ex) {
+				console.log("XpcomTransport.onDataAvailable exception: " + ex);
+			}
+		}
+    };
+	
+	pump.init(inStream, -1, -1, 0, 0, true);
+    pump.asyncRead(dataListener, null);
+};
+
+/*
+ * Send the data over the connection created by connect.
+ */
+XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
+    if (this.socket == null) {
+        console.log("XpcomTransport connection is not established.");
+        return;
+    }
     
-    var dataListener = {
+    var rawDataString = DataUtils.toString(data);
+	this.outStream.write(rawDataString, rawDataString.length);
+	this.outStream.flush();
+};
+
+XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
+    var thisXpcomTransport = this;
+    
+    if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
+      var dataListener = {
 		onReceivedData : function(data) {
 			if (data == null || data == undefined || data.length == 0)
-				dump("NDN.expressInterest: received empty data from socket.\n");
+				console.log("XpcomTransport: received empty data from socket.");
 			else {
                 var decoder = new BinaryXMLDecoder(data);
                 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) {
-                    // TODO: handle interest
-                    if (closure.upcall(Closure.UPCALL_INTEREST, null) == Closure.RESULT_OK)
-                        // success
-                        return true;
+                    // TODO: handle interest properly.  For now, assume the only use in getting
+                    //   an interest is knowing that the host is alive from NDN.ccndIdFetcher.
+					var pitEntry = NDN.getEntryForExpressedInterest(NDN.ccndIdFetcher);
+					if (pitEntry != null) {
+						// Remove PIT entry from NDN.PITTable.
+						var index = NDN.PITTable.indexOf(pitEntry);
+						if (index >= 0)
+							NDN.PITTable.splice(index, 1);
+                        
+                        pitEntry.closure.upcall(Closure.UPCALL_INTEREST, null);
+                    }
                 }
                 else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) {
                     var co = new ContentObject();
                     co.from_ccnb(decoder);
                    					
-                    // TODO: verify the content object and set kind to UPCALL_CONTENT.
-                    var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
-                               new UpcallInfo(ndn, interest, 0, co));
-                    if (result == Closure.RESULT_OK)
-                        // success
-                        return true;
-                    else if (result == Closure.RESULT_ERR)
-                        dump("NDN.expressInterest: upcall returned RESULT_ERR.\n");
-                    else if (result == Closure.RESULT_REEXPRESS) {
-                        XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
-                        return true;
+					var pitEntry = NDN.getEntryForExpressedInterest(co.name);
+					if (pitEntry != null) {
+						// Remove PIT entry from NDN.PITTable.
+                        // TODO: This needs to be a single thread-safe transaction.
+						var index = NDN.PITTable.indexOf(pitEntry);
+						if (index >= 0)
+							NDN.PITTable.splice(index, 1);
                     }
-                    else if (result == Closure.RESULT_VERIFY) {
-                        // TODO: force verification of content.
-                    }
-                    else if (result == Closure.RESULT_FETCHKEY) {
-                        // TODO: get the key in the key locator and re-call the interest
-                        //   with the key available in the local storage.
+   					if (pitEntry != null) {
+						var currentClosure = pitEntry.closure;
+                        
+                        // TODO: verify the content object and set kind to UPCALL_CONTENT.
+                        var result = currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
+                                    new UpcallInfo(thisXpcomTransport.ndn, null, 0, co));
+                        if (result == Closure.RESULT_OK) {
+                            // success
+                        }
+                        else if (result == Closure.RESULT_ERR)
+                            console.log("XpcomTransport: upcall returned RESULT_ERR.");
+                        else if (result == Closure.RESULT_REEXPRESS) {
+                            // TODO: Handl re-express interest.
+                        }
+                        else if (result == Closure.RESULT_VERIFY) {
+                            // TODO: force verification of content.
+                        }
+                        else if (result == Closure.RESULT_FETCHKEY) {
+                            // TODO: get the key in the key locator and re-call the interest
+                            //   with the key available in the local storage.
+                        }
                     }
                 }
                 else
                     console.log('Incoming packet is not Interest or ContentObject. Discard now.');
 			}
-            
-            return false;
 		}
-	}    
+	  }
+      
+      this.connect(ndn, dataListener);
+    }
     
-	XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
+    var binaryInterest = encodeToBinaryInterest(interest);
+    
+                        // TODO: This needs to be a single thread-safe transaction.
+	var pitEntry = new PITEntry(interest, closure);
+	NDN.PITTable.push(pitEntry);
+
+    this.send(binaryInterest);
 };
 
 /** Send outputData (Uint8Array) to host:port, read the entire response and call 
@@ -71,16 +193,16 @@
  *    false if need to keep reading.
  *  Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working .
  */
+ /*
 XpcomTransport.readAllFromSocket = function(host, port, outputData, listener) {
 	var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
         (Components.interfaces.nsISocketTransportService);
-	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
-        (Components.interfaces.nsIInputStreamPump);
 	var transport = transportService.createTransport(null, 0, host, port, null);
 	var outStream = transport.openOutputStream(1, 0, 0);
     var rawDataString = DataUtils.toString(outputData);
 	outStream.write(rawDataString, rawDataString.length);
 	outStream.flush();
+    outStream.close();
 	var inStream = transport.openInputStream(0, 0, 0);
 	var dataListener = {
 		dataParts: [],
@@ -91,7 +213,6 @@
 		},
 		onStopRequest: function (request, context, status) {
 			inStream.close();
-			outStream.close();
 		},
 		onDataAvailable: function (request, context, _inputStream, offset, count) {
             if (this.dataIsConsumed)
@@ -134,12 +255,15 @@
                     }
                 }
 			} catch (ex) {
-				dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n");
+				console.log("readAllFromSocket.onDataAvailable exception: " + ex);
 			}
 		}
     };
 	
+	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
+        (Components.interfaces.nsIInputStreamPump);
 	pump.init(inStream, -1, -1, 0, 0, true);
     pump.asyncRead(dataListener, null);
 }
+*/
 
diff --git a/js/ndnProtocol.xpi b/js/ndnProtocol.xpi
index 9f9690c..2b880b0 100644
--- a/js/ndnProtocol.xpi
+++ b/js/ndnProtocol.xpi
Binary files differ
diff --git a/js/ndnProtocol/components/ndnProtocolService.js b/js/ndnProtocol/components/ndnProtocolService.js
index ca8cc1b..a6f7f01 100644
--- a/js/ndnProtocol/components/ndnProtocolService.js
+++ b/js/ndnProtocol/components/ndnProtocolService.js
@@ -29,13 +29,39 @@
 

     newURI: function(aSpec, aOriginCharset, aBaseURI)

     {

-        // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.

-        var spec = aSpec.trim();

-        var preSearch = spec.split('?', 1)[0];

-        var searchAndHash = spec.substr(preSearch.length).trim();

-

         var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);

-        uri.spec = preSearch.trim() + searchAndHash;

+

+        // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.

+        var uriParts = NdnProtocolInfo.splitUri(aSpec);

+        if (aBaseURI == null || uriParts.name.length < 1 || uriParts.name[0] == '/')

+            // Just reconstruct the trimmed URI.

+            uri.spec = "ndn:" + uriParts.name + uriParts.search + uriParts.hash;

+        else {

+            // Make a URI relative to the base name up to the file name component.

+            var baseUriParts = NdnProtocolInfo.splitUri(aBaseURI.spec);

+            var baseName = new Name(baseUriParts.name);

+            var iFileName = baseName.indexOfFileName();

+            

+            var relativeName = uriParts.name;

+            // Handle ../

+            while (true) {

+                if (relativeName.substr(0, 2) == "./")

+                    relativeName = relativeName.substr(2);

+                else if (relativeName.substr(0, 3) == "../") {

+                    relativeName = relativeName.substr(3);

+                    if (iFileName > 0)

+                        --iFileName;

+                }

+                else

+                    break;

+            }

+            

+            var prefixUri = "/";

+            if (iFileName > 0)

+                prefixUri = new Name(baseName.components.slice(0, iFileName)).to_uri() + "/";

+            uri.spec = "ndn:" + prefixUri + relativeName + uriParts.search + uriParts.hash;

+        }

+        

         return uri;

     },

 

@@ -44,21 +70,12 @@
         var thisNdnProtocol = this;

         

         try {            

-            // Decode manually since nsIURI doesn't have selectors for hash, etc.

-            var spec = aURI.spec.trim();

-            var preHash = spec.split('#', 1)[0];

-            var hash = spec.substr(preHash.length).trim();

-            var preSearch = preHash.split('?', 1)[0];

-            var search = preHash.substr(preSearch.length).trim();

-            // Set nameString to the preSearch without the protocol.

-            var nameString = preSearch.trim();

-            if (nameString.indexOf(':') >= 0)

-                nameString = nameString.substr(nameString.indexOf(':') + 1).trim();

+            var uriParts = NdnProtocolInfo.splitUri(aURI.spec);

     

             var template = new Interest(new Name([]));

             // Use the same default as NDN.expressInterest.

             template.interestLifetime = 4000; // milliseconds

-            var searchWithoutNdn = extractNdnSearch(search, template);

+            var searchWithoutNdn = extractNdnSearch(uriParts.search, template);

             

             var segmentTemplate = new Interest(new Name([]));

             // Only use the interest selectors which make sense for fetching further segments.

@@ -67,14 +84,14 @@
             segmentTemplate.interestLifetime = template.interestLifetime;

     

             var requestContent = function(contentListener) {                

-                var name = new Name(nameString);

+                var name = new Name(uriParts.name);

                 // TODO: Strip off an ending implicit digest before checking the last component?

                 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);

                 

                 // Use the same NDN object each time.

                 thisNdnProtocol.ndn.expressInterest(name, 

                     new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber, 

-                            aURI.originCharset, searchWithoutNdn + hash, segmentTemplate),

+                            aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),

                     template);

             };

 

@@ -136,7 +153,7 @@
     }

     

     // Now that we're connected, report the host and port.

-    setConnectedNdnHub(this.ndn.host, this.ndn.port);

+    NdnProtocolInfo.setConnectedNdnHub(this.ndn.host, this.ndn.port);

 

     // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.

     var segmentNumber = null;

diff --git a/js/ndnProtocol/content/ndnToolbar.js b/js/ndnProtocol/content/ndnToolbar.js
index 20518a3..0c34fb2 100644
--- a/js/ndnProtocol/content/ndnToolbar.js
+++ b/js/ndnProtocol/content/ndnToolbar.js
@@ -8,17 +8,8 @@
   }
 
   // Parse the same as in ndnProtocolService newChannel.
-  var spec = window._content.document.location.href.trim();
-  var preHash = spec.split('#', 1)[0];
-  var hash = spec.substr(preHash.length).trim();
-  var preSearch = preHash.split('?', 1)[0];
-  var search = preHash.substr(preSearch.length).trim();
-  // Set nameString to the preSearch without the protocol.
-  var nameString = preSearch.trim();
-  if (nameString.indexOf(':') >= 0)
-    nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
-
-  var name = new Name(nameString);
+  var uriParts = NdnProtocolInfo.splitUri(window._content.document.location.href);
+  var name = new Name(uriParts.name);
   var indexOfVersion = getIndexOfVersion(name);
   if (indexOfVersion < 0) {
     alert("The ndn address does not have a version");
@@ -26,9 +17,10 @@
   }
 
   var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
-  var searchWithChildSelector = (search == "" ? "?" : search + "&") + "ndn.ChildSelector=1";
+  var searchWithChildSelector = 
+      (uriParts.search == "" ? "?" : uriParts.search + "&") + "ndn.ChildSelector=1";
     
-  var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + hash;
+  var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
   window._content.document.location = uri;
 } 
 
@@ -51,4 +43,4 @@
    document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port);
 }
 
-window.addEventListener("load", function() { addNdnHubChangedListener(onNdnHubChanged); }, false);
+window.addEventListener("load", function() { NdnProtocolInfo.addNdnHubChangedListener(onNdnHubChanged); }, false);
diff --git a/js/ndnProtocol/modules/NdnProtocolInfo.jsm b/js/ndnProtocol/modules/NdnProtocolInfo.jsm
index a2fb10d..3a5fc79 100644
--- a/js/ndnProtocol/modules/NdnProtocolInfo.jsm
+++ b/js/ndnProtocol/modules/NdnProtocolInfo.jsm
@@ -3,7 +3,7 @@
  * See COPYING for copyright and distribution information.
  */
 
-var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub"];
+var EXPORTED_SYMBOLS = ["NdnProtocolInfo"];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
@@ -11,20 +11,23 @@
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 
-var ndnHubHost = null;
-var ndnHubPort = null;
-var ndnHubChangedListenerList = [];
+var NdnProtocolInfo = function NdnProtocolInfo(){
+};
+
+NdnProtocolInfo.ndnHubHost = null;
+NdnProtocolInfo.ndnHubPort = null;
+NdnProtocolInfo.ndnHubChangedListenerList = [];
 
 /*
  * When the NDN hub host or port is changed, the system calls listener(host, port).
  * If the current host and port are not null, call listener with the values to initialize.
  */
-function addNdnHubChangedListener(listener) {
-    ndnHubChangedListenerList.push(listener);
+NdnProtocolInfo.addNdnHubChangedListener = function(listener) {
+    NdnProtocolInfo.ndnHubChangedListenerList.push(listener);
     
-    if (ndnHubHost != null && ndnHubPort != null) {
+    if (NdnProtocolInfo.ndnHubHost != null && NdnProtocolInfo.ndnHubPort != null) {
         try {
-            listener(ndnHubHost, ndnHubPort);
+            listener(NdnProtocolInfo.ndnHubHost, NdnProtocolInfo.ndnHubPort);
         }
         catch (ex) {
             // Ignore error from the listener.
@@ -36,19 +39,47 @@
  * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
  * listener in ndnHubChangedListenerList.
  */
-function setConnectedNdnHub(host, port) {
-    if (host == ndnHubHost && port == ndnHubPort)
+NdnProtocolInfo.setConnectedNdnHub = function(host, port) {
+    if (host == NdnProtocolInfo.ndnHubHost && port == NdnProtocolInfo.ndnHubPort)
         // No change.
         return;
     
-    ndnHubHost = host;
-    ndnHubPort = port;
-    for (var i = 0; i < ndnHubChangedListenerList.length; ++i) {
+    NdnProtocolInfo.ndnHubHost = host;
+    NdnProtocolInfo.ndnHubPort = port;
+    for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) {
         try {
-            ndnHubChangedListenerList[i](host, port);
+            NdnProtocolInfo.ndnHubChangedListenerList[i](host, port);
         }
         catch (ex) {
             // Ignore error from the listener.
         }
     }
-}
\ No newline at end of file
+}
+
+/*
+ * Split the URI spec and return an object with protocol (including ':'), name, 
+ *   search (including '?') and hash value (including '#').  
+ * All result strings are trimmed.  This does not unescape the name.
+ * The name may include a host and port.  
+ */
+NdnProtocolInfo.splitUri = function(spec) {
+    spec = spec.trim();
+    var result = {};
+    var preHash = spec.split('#', 1)[0];
+    result.hash = spec.substr(preHash.length).trim();
+    var preSearch = preHash.split('?', 1)[0];
+    result.search = preHash.substr(preSearch.length).trim();
+    
+    preSearch = preSearch.trim();
+    var colonIndex = preSearch.indexOf(':');
+    if (colonIndex >= 0) {
+        result.protocol = preSearch.substr(0, colonIndex + 1).trim();
+        result.name = preSearch.substr(colonIndex + 1).trim();    
+    }
+    else {
+        result.protocol = "";
+        result.name = preSearch;
+    }
+    
+    return result;
+}
diff --git a/js/ndnProtocol/modules/ndn-js.jsm b/js/ndnProtocol/modules/ndn-js.jsm
index ab41f5e..32705a3 100644
--- a/js/ndnProtocol/modules/ndn-js.jsm
+++ b/js/ndnProtocol/modules/ndn-js.jsm
@@ -317,61 +317,183 @@
 // Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 // Components.utils.import("resource://gre/modules/NetUtil.jsm");
 
-var XpcomTransport = function XpcomTransport() { 
+var XpcomTransport = function XpcomTransport() {
+    this.ndn = null;
+    this.socket = null; // nsISocketTransport
+    this.outStream = null;
+    this.connectedHost = null;
+    this.connectedPort = null;
+    
     this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
         (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu", 
           "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"],
          9695);
 };
 
-XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
-    var binaryInterest = encodeToBinaryInterest(interest);
+/*
+ * Connect to the host and port in ndn.  This replaces a previous connection.
+ * Listen on the port to read an entire binary XML encoded response and call
+ *    listener.onReceivedData(data) where data is Uint8Array.
+ */
+XpcomTransport.prototype.connect = function(ndn, listener) {
+    if (this.socket != null) {
+        try {
+            this.socket.close(0);
+        } catch (ex) {
+			console.log("XpcomTransport socket.close exception: " + ex);
+		}
+        this.socket = null;
+    }
+    this.ndn = ndn;
+
+	var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
+        (Components.interfaces.nsISocketTransportService);
+	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
+        (Components.interfaces.nsIInputStreamPump);
+	this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null);
+    this.connectedHost = ndn.host;
+    this.connectedPort = ndn.port;
+    this.outStream = this.socket.openOutputStream(1, 0, 0);
+
+    var inStream = this.socket.openInputStream(0, 0, 0);
+	var dataListener = {
+		dataParts: [],
+        structureDecoder: new BinaryXMLStructureDecoder(),
+		
+		onStartRequest: function (request, context) {
+		},
+		onStopRequest: function (request, context, status) {
+		},
+		onDataAvailable: function (request, context, _inputStream, offset, count) {
+			try {
+				// Use readInputStreamToString to handle binary data.
+                // TODO: Can we go directly from the stream to Uint8Array?
+				var rawData = DataUtils.toNumbersFromString
+                    (NetUtil.readInputStreamToString(inStream, count));
+				
+                // Process multiple objects in this packet.
+                while(true) {
+                    // Scan the input to check if a whole ccnb object has been read.
+                    this.structureDecoder.seek(0);
+                    if (this.structureDecoder.findElementEnd(rawData)) {
+                        // Got the remainder of an object.  Report to the caller.
+                        this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
+                        listener.onReceivedData(DataUtils.concatArrays(this.dataParts));
+                    
+                        // Need to read a new object.
+                        rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
+                        this.dataParts = [];
+                        this.structureDecoder = new BinaryXMLStructureDecoder();
+                        if (rawData.length == 0)
+                            // No more data in the packet.
+                            return;
+                        
+                        // else loop back to decode.
+                    }
+                    else {
+                        // Save for a later call to concatArrays so that we only copy data once.
+                        this.dataParts.push(rawData);
+                        return;
+                    }
+                }
+			} catch (ex) {
+				console.log("XpcomTransport.onDataAvailable exception: " + ex);
+			}
+		}
+    };
+	
+	pump.init(inStream, -1, -1, 0, 0, true);
+    pump.asyncRead(dataListener, null);
+};
+
+/*
+ * Send the data over the connection created by connect.
+ */
+XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
+    if (this.socket == null) {
+        console.log("XpcomTransport connection is not established.");
+        return;
+    }
     
-    var dataListener = {
+    var rawDataString = DataUtils.toString(data);
+	this.outStream.write(rawDataString, rawDataString.length);
+	this.outStream.flush();
+};
+
+XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
+    var thisXpcomTransport = this;
+    
+    if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
+      var dataListener = {
 		onReceivedData : function(data) {
 			if (data == null || data == undefined || data.length == 0)
-				dump("NDN.expressInterest: received empty data from socket.\n");
+				console.log("XpcomTransport: received empty data from socket.");
 			else {
                 var decoder = new BinaryXMLDecoder(data);
                 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) {
-                    // TODO: handle interest
-                    if (closure.upcall(Closure.UPCALL_INTEREST, null) == Closure.RESULT_OK)
-                        // success
-                        return true;
+                    // TODO: handle interest properly.  For now, assume the only use in getting
+                    //   an interest is knowing that the host is alive from NDN.ccndIdFetcher.
+					var pitEntry = NDN.getEntryForExpressedInterest(NDN.ccndIdFetcher);
+					if (pitEntry != null) {
+						// Remove PIT entry from NDN.PITTable.
+						var index = NDN.PITTable.indexOf(pitEntry);
+						if (index >= 0)
+							NDN.PITTable.splice(index, 1);
+                        
+                        pitEntry.closure.upcall(Closure.UPCALL_INTEREST, null);
+                    }
                 }
                 else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) {
                     var co = new ContentObject();
                     co.from_ccnb(decoder);
                    					
-                    // TODO: verify the content object and set kind to UPCALL_CONTENT.
-                    var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
-                               new UpcallInfo(ndn, interest, 0, co));
-                    if (result == Closure.RESULT_OK)
-                        // success
-                        return true;
-                    else if (result == Closure.RESULT_ERR)
-                        dump("NDN.expressInterest: upcall returned RESULT_ERR.\n");
-                    else if (result == Closure.RESULT_REEXPRESS) {
-                        XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
-                        return true;
+					var pitEntry = NDN.getEntryForExpressedInterest(co.name);
+					if (pitEntry != null) {
+						// Remove PIT entry from NDN.PITTable.
+                        // TODO: This needs to be a single thread-safe transaction.
+						var index = NDN.PITTable.indexOf(pitEntry);
+						if (index >= 0)
+							NDN.PITTable.splice(index, 1);
                     }
-                    else if (result == Closure.RESULT_VERIFY) {
-                        // TODO: force verification of content.
-                    }
-                    else if (result == Closure.RESULT_FETCHKEY) {
-                        // TODO: get the key in the key locator and re-call the interest
-                        //   with the key available in the local storage.
+   					if (pitEntry != null) {
+						var currentClosure = pitEntry.closure;
+                        
+                        // TODO: verify the content object and set kind to UPCALL_CONTENT.
+                        var result = currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
+                                    new UpcallInfo(thisXpcomTransport.ndn, null, 0, co));
+                        if (result == Closure.RESULT_OK) {
+                            // success
+                        }
+                        else if (result == Closure.RESULT_ERR)
+                            console.log("XpcomTransport: upcall returned RESULT_ERR.");
+                        else if (result == Closure.RESULT_REEXPRESS) {
+                            // TODO: Handl re-express interest.
+                        }
+                        else if (result == Closure.RESULT_VERIFY) {
+                            // TODO: force verification of content.
+                        }
+                        else if (result == Closure.RESULT_FETCHKEY) {
+                            // TODO: get the key in the key locator and re-call the interest
+                            //   with the key available in the local storage.
+                        }
                     }
                 }
                 else
                     console.log('Incoming packet is not Interest or ContentObject. Discard now.');
 			}
-            
-            return false;
 		}
-	}    
+	  }
+      
+      this.connect(ndn, dataListener);
+    }
     
-	XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
+    var binaryInterest = encodeToBinaryInterest(interest);
+    
+                        // TODO: This needs to be a single thread-safe transaction.
+	var pitEntry = new PITEntry(interest, closure);
+	NDN.PITTable.push(pitEntry);
+
+    this.send(binaryInterest);
 };
 
 /** Send outputData (Uint8Array) to host:port, read the entire response and call 
@@ -379,16 +501,16 @@
  *    false if need to keep reading.
  *  Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working .
  */
+ /*
 XpcomTransport.readAllFromSocket = function(host, port, outputData, listener) {
 	var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
         (Components.interfaces.nsISocketTransportService);
-	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
-        (Components.interfaces.nsIInputStreamPump);
 	var transport = transportService.createTransport(null, 0, host, port, null);
 	var outStream = transport.openOutputStream(1, 0, 0);
     var rawDataString = DataUtils.toString(outputData);
 	outStream.write(rawDataString, rawDataString.length);
 	outStream.flush();
+    outStream.close();
 	var inStream = transport.openInputStream(0, 0, 0);
 	var dataListener = {
 		dataParts: [],
@@ -399,7 +521,6 @@
 		},
 		onStopRequest: function (request, context, status) {
 			inStream.close();
-			outStream.close();
 		},
 		onDataAvailable: function (request, context, _inputStream, offset, count) {
             if (this.dataIsConsumed)
@@ -442,14 +563,17 @@
                     }
                 }
 			} catch (ex) {
-				dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n");
+				console.log("readAllFromSocket.onDataAvailable exception: " + ex);
 			}
 		}
     };
 	
+	var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
+        (Components.interfaces.nsIInputStreamPump);
 	pump.init(inStream, -1, -1, 0, 0, true);
     pump.asyncRead(dataListener, null);
 }
+*/
 
 /*
  * This class defines MOME types based on the filename extension.