blob: 479ed30ef7cc11692ee6bf02b6ee73a9a45bb351 [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
35/* Java Socket Bridge and XPCOM transport */
36
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 Thompson34419762012-10-15 22:24:12 -070042/** Encode name as an Interest. If template is not null, use its attributes.
43 * Send the interest to host:port, read the entire response and call
44 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -070045 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -070046 */
47NDN.prototype.expressInterest = function(
48 // Name
49 name,
50 // Closure
51 closure,
52 // Interest
53 template) {
54 if (this.host == null || this.port == null) {
55 dump('ERROR host OR port NOT SET\n');
56 return;
57 }
58
Jeff Thompson5b265a72012-11-12 01:13:08 -080059 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -070060 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -070061 interest.minSuffixComponents = template.minSuffixComponents;
62 interest.maxSuffixComponents = template.maxSuffixComponents;
63 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
64 interest.exclude = template.exclude;
65 interest.childSelector = template.childSelector;
66 interest.answerOriginKind = template.answerOriginKind;
67 interest.scope = template.scope;
68 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -070069 }
70 else
Wentao Shangbd63e462012-12-03 16:19:33 -080071 interest.interestLifetime = 4200; // default interest timeout value
Jeff Thompson97f27432012-10-16 00:28:03 -070072
Jeff Thompson5b265a72012-11-12 01:13:08 -080073 this.transport.expressInterest(this, interest, closure);
Jeff Thompson34419762012-10-15 22:24:12 -070074};
75
Jeff Thompson5b265a72012-11-12 01:13:08 -080076
77NDN.prototype.registerPrefix = function(name, closure, flag) {
78 return this.transport.registerPrefix(this, name, closure, flag);
79}