blob: f963a780779dc11acdce7e1d2e93a5acad309970 [file] [log] [blame]
Wentao Shangc0311e52012-12-03 10:38:23 -08001/**
Jeff Thompson5b265a72012-11-12 01:13:08 -08002 * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
4 * This class represents the top-level object for communicating with an NDN host.
5 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006
Jeff Thompson3c263812012-12-01 17:20:28 -08007var LOG = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -08008
Jeff Thompsone06b31e2012-09-30 17:19:19 -07009/**
Jeff Thompson5b265a72012-11-12 01:13:08 -080010 * settings is an associative array with the following defaults:
11 * {
12 * host: 'localhost',
13 * port: 9696,
14 * getTransport: function() { return new WebSocketTransport(); }
Wentao Shangc0311e52012-12-03 10:38:23 -080015 * onopen: function() { if (LOG > 3) console.log("NDN connection established."); }
16 * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); }
Jeff Thompson5b265a72012-11-12 01:13:08 -080017 * }
Jeff Thompsone06b31e2012-09-30 17:19:19 -070018 */
Jeff Thompson5b265a72012-11-12 01:13:08 -080019var NDN = function NDN(settings) {
20 settings = (settings || {});
21 this.host = (settings.host || "localhost");
22 this.port = (settings.port || 9696);
23 var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); });
Wentao Shang0e291c82012-12-02 23:36:29 -080024 this.transport = getTransport();
25 this.readyStatus = NDN.UNOPEN;
26 // Event handler
Wentao Shangc0311e52012-12-03 10:38:23 -080027 this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); });
28 this.onclose = (settings.onclose || function() { if (LOG > 3) console.log("NDN connection closed."); });
Meki Cherkaoui8f173612012-06-06 01:05:40 -070029};
30
Wentao Shang0e291c82012-12-02 23:36:29 -080031NDN.UNOPEN = 0; // created but not opened yet
32NDN.OPENED = 1; // connection to ccnd opened
33NDN.CLOSED = 2; // connection to ccnd closed
Jeff Thompson5b265a72012-11-12 01:13:08 -080034
Wentao Shangc0311e52012-12-03 10:38:23 -080035NDN.InterestTimeOut = 5000; // 5000 ms timeout for pending interest
36
Jeff Thompson5b265a72012-11-12 01:13:08 -080037/* Java Socket Bridge and XPCOM transport */
38
Jeff Thompsone06b31e2012-09-30 17:19:19 -070039NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070040 this.host=host;
41 this.port=port;
42}
43
Jeff Thompson34419762012-10-15 22:24:12 -070044/** Encode name as an Interest. If template is not null, use its attributes.
45 * Send the interest to host:port, read the entire response and call
46 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -070047 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -070048 */
49NDN.prototype.expressInterest = function(
50 // Name
51 name,
52 // Closure
53 closure,
54 // Interest
55 template) {
56 if (this.host == null || this.port == null) {
57 dump('ERROR host OR port NOT SET\n');
58 return;
59 }
60
Jeff Thompson5b265a72012-11-12 01:13:08 -080061 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -070062 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -070063 interest.minSuffixComponents = template.minSuffixComponents;
64 interest.maxSuffixComponents = template.maxSuffixComponents;
65 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
66 interest.exclude = template.exclude;
67 interest.childSelector = template.childSelector;
68 interest.answerOriginKind = template.answerOriginKind;
69 interest.scope = template.scope;
70 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -070071 }
72 else
Jeff Thompson741108b2012-10-15 23:07:09 -070073 interest.interestLifetime = 4200;
Jeff Thompson97f27432012-10-16 00:28:03 -070074
Jeff Thompson5b265a72012-11-12 01:13:08 -080075 this.transport.expressInterest(this, interest, closure);
Jeff Thompson34419762012-10-15 22:24:12 -070076};
77
Jeff Thompson5b265a72012-11-12 01:13:08 -080078
79NDN.prototype.registerPrefix = function(name, closure, flag) {
80 return this.transport.registerPrefix(this, name, closure, flag);
81}