blob: eb67d76bd25f08222d9a099cafb2420a4889f181 [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
Jeff Thompsonbd25df22012-12-13 21:50:13 -080035NDN.ccndIdFetcher = '/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY';
Jeff Thompson5b265a72012-11-12 01:13:08 -080036
Jeff Thompsone06b31e2012-09-30 17:19:19 -070037NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070038 this.host=host;
39 this.port=port;
40}
41
Jeff Thompsonbe85be62012-12-13 22:32:01 -080042// For fetching data
43NDN.PITTable = new Array();
44
45var PITEntry = function PITEntry(interest, closure) {
46 this.interest = interest; // Interest
47 this.closure = closure; // Closure
48};
49
50// Return the longest entry from NDN.PITTable that matches name.
51NDN.getEntryForExpressedInterest = function(/*Name*/ name) {
52 // TODO: handle multiple matches? Maybe not from registerPrefix because multiple ContentObject
53 // could be sent for one Interest?
54 var result = null;
55
56 for (var i = 0; i < NDN.PITTable.length; i++) {
57 if (NDN.PITTable[i].interest.matches_name(name)) {
58 if (result == null ||
59 NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length)
60 result = NDN.PITTable[i];
61 }
62 }
63
64 return result;
65};
66
Jeff Thompson34419762012-10-15 22:24:12 -070067/** Encode name as an Interest. If template is not null, use its attributes.
68 * Send the interest to host:port, read the entire response and call
69 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -070070 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -070071 */
72NDN.prototype.expressInterest = function(
73 // Name
74 name,
75 // Closure
76 closure,
77 // Interest
78 template) {
79 if (this.host == null || this.port == null) {
80 dump('ERROR host OR port NOT SET\n');
81 return;
82 }
83
Jeff Thompson5b265a72012-11-12 01:13:08 -080084 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -070085 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -070086 interest.minSuffixComponents = template.minSuffixComponents;
87 interest.maxSuffixComponents = template.maxSuffixComponents;
88 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
89 interest.exclude = template.exclude;
90 interest.childSelector = template.childSelector;
91 interest.answerOriginKind = template.answerOriginKind;
92 interest.scope = template.scope;
93 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -070094 }
95 else
Jeff Thompson84db2632012-12-09 22:31:39 -080096 interest.interestLifetime = 4.0; // default interest timeout value in seconds.
Jeff Thompson97f27432012-10-16 00:28:03 -070097
Jeff Thompson5b265a72012-11-12 01:13:08 -080098 this.transport.expressInterest(this, interest, closure);
Jeff Thompson34419762012-10-15 22:24:12 -070099};
100
Jeff Thompson5b265a72012-11-12 01:13:08 -0800101
102NDN.prototype.registerPrefix = function(name, closure, flag) {
103 return this.transport.registerPrefix(this, name, closure, flag);
104}