blob: 8dde5c36aa3781847fcc4b0e82663590cd3012b5 [file] [log] [blame]
Jeff Thompson745026e2012-10-13 12:49:20 -07001/*
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 Thompson5b265a72012-11-12 01:13:08 -08007var LOG = 3;
8
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(); }
15 * }
Jeff Thompsone06b31e2012-09-30 17:19:19 -070016 */
Jeff Thompson5b265a72012-11-12 01:13:08 -080017var NDN = function NDN(settings) {
18 settings = (settings || {});
19 this.host = (settings.host || "localhost");
20 this.port = (settings.port || 9696);
21 var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); });
22 this.transport = getTransport();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070023};
24
Jeff Thompson5b265a72012-11-12 01:13:08 -080025
26/* Java Socket Bridge and XPCOM transport */
27
Jeff Thompsone06b31e2012-09-30 17:19:19 -070028NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070029 this.host=host;
30 this.port=port;
31}
32
Jeff Thompsone06b31e2012-09-30 17:19:19 -070033NDN.prototype.get = function(message){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070034 if(this.host!=null && this.port!=null){
35 var output ='';
36 message = message.trim();
37 if(message==null || message =="" ){
38 console.log('INVALID INPUT TO GET');
39 return null;
40 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080041
42
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070043 //var array = Name.createNameArray(message);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070044
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070045 int = new Interest(new Name(message));
Meki Cherkaoui8f173612012-06-06 01:05:40 -070046
47 int.InterestLifetime = 4200;
Jeff Thompson5b265a72012-11-12 01:13:08 -080048
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049 var hex = encodeToHexInterest(int);
Jeff Thompson5b265a72012-11-12 01:13:08 -080050
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051 //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
Jeff Thompson5b265a72012-11-12 01:13:08 -080052
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053 var result = get(this.host,this.port, hex);
54
55
56 if(LOG>0)console.log('BINARY RESPONSE IS ' +result);
Jeff Thompson5b265a72012-11-12 01:13:08 -080057
Meki Cherkaoui8f173612012-06-06 01:05:40 -070058 if(result==null || result==undefined || result =="" ){
59 /*if(result[0] != '0'||result[1]!='4') {
60 if(LOG>2)console.log('INVALID ANSWER');
61 }*/
62 return null;
63 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080064
Meki Cherkaoui8f173612012-06-06 01:05:40 -070065 else{
Jeff Thompson5b265a72012-11-12 01:13:08 -080066
Meki Cherkaoui8f173612012-06-06 01:05:40 -070067 co = decodeHexContentObject(result);
68
69 if(LOG>2) {
70 console.log('DECODED CONTENT OBJECT');
71 console.log(co);
72 }
73 return co;
74 }
75 }
76 else{
77
78 console.log('ERROR URL OR PORT NOT SET');
79
80 return null;
81
82 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080083
Meki Cherkaoui8f173612012-06-06 01:05:40 -070084
85}
86
Jeff Thompsone06b31e2012-09-30 17:19:19 -070087NDN.prototype.put = function(name,content){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070088 if(this.host!=null && this.port!=null){
Jeff Thompson5b265a72012-11-12 01:13:08 -080089
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070090 var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
Jeff Thompson5b265a72012-11-12 01:13:08 -080091
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070093 alert("Cannot contact router");
Jeff Thompson5b265a72012-11-12 01:13:08 -080094
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070095 return null;
96 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080097
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070098 var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
Jeff Thompson5b265a72012-11-12 01:13:08 -080099
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700100 name = name.trim();
Jeff Thompson5b265a72012-11-12 01:13:08 -0800101
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700102 var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800103
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700104 var bytes = encodeForwardingEntry(fe);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800105
106
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700107 var si = new SignedInfo();
108 si.setFields();
Jeff Thompson5b265a72012-11-12 01:13:08 -0800109
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700110 var co = new ContentObject(new Name(),si,bytes,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700111 co.sign();
Jeff Thompson5b265a72012-11-12 01:13:08 -0800112
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700113 var coBinary = encodeToBinaryContentObject(co);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800114
Axel Colin de Verdiered363e632012-06-06 05:16:43 -0700115 //var ccnxnodename = unescape('%E0%A0%1E%099h%F9t%0C%E7%F46%1B%AB%F5%BB%05%A4%E5Z%AC%A5%E5%8Fs%ED%DE%B8%E0%13%AA%8F');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800116
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700117 var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700118
119 int = new Interest(interestName);
Jeff Thompson86aea882012-09-29 17:32:48 -0700120 int.scope = 1;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800121
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700122 var hex = encodeToHexInterest(int);
123
124 console.log('GOING TO PUT INTEREST OBJECT');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800125
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700126 console.log(hex);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800127
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700128 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700129
Jeff Thompson5b265a72012-11-12 01:13:08 -0800130
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700131 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800132
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700133 //if(LOG>3)console.log('DATA ');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800134
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700135 //if(LOG>3)console.log(result);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800136
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700137 //interest = decodeHexInterest(result);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800138
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700139 //console.log('SUCCESSFULLY PARSED INTEREST');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800140
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700141 console.log('CREATING ANSWER');
142 var si = new SignedInfo();
143 si.setFields();
Jeff Thompson5b265a72012-11-12 01:13:08 -0800144
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700145 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700146
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700147 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700148 co.sign();
Jeff Thompson5b265a72012-11-12 01:13:08 -0800149
150
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700151 var outputHex = encodeToHexContentObject(co);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800152
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700153 //console.log('SENDING ANSWER');
154
155 //return get_java_socket_bridge().putAnswer(outputHex,name);
156
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700157 var result = put(this.host,this.port, hex,name,outputHex);
158
159
160 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700161 }
162 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700163 console.log('ERROR URL OR PORT NOT SET');
164
165 return null;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700166 }
Jeff Thompson34419762012-10-15 22:24:12 -0700167}
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700168
Jeff Thompson34419762012-10-15 22:24:12 -0700169/** Encode name as an Interest. If template is not null, use its attributes.
170 * Send the interest to host:port, read the entire response and call
171 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -0700172 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -0700173 */
174NDN.prototype.expressInterest = function(
175 // Name
176 name,
177 // Closure
178 closure,
179 // Interest
180 template) {
181 if (this.host == null || this.port == null) {
182 dump('ERROR host OR port NOT SET\n');
183 return;
184 }
185
Jeff Thompson5b265a72012-11-12 01:13:08 -0800186 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -0700187 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -0700188 interest.minSuffixComponents = template.minSuffixComponents;
189 interest.maxSuffixComponents = template.maxSuffixComponents;
190 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
191 interest.exclude = template.exclude;
192 interest.childSelector = template.childSelector;
193 interest.answerOriginKind = template.answerOriginKind;
194 interest.scope = template.scope;
195 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -0700196 }
197 else
Jeff Thompson741108b2012-10-15 23:07:09 -0700198 interest.interestLifetime = 4200;
Jeff Thompson97f27432012-10-16 00:28:03 -0700199
Jeff Thompson5b265a72012-11-12 01:13:08 -0800200 this.transport.expressInterest(this, interest, closure);
Jeff Thompson34419762012-10-15 22:24:12 -0700201};
202
Jeff Thompson5b265a72012-11-12 01:13:08 -0800203
204NDN.prototype.registerPrefix = function(name, closure, flag) {
205 return this.transport.registerPrefix(this, name, closure, flag);
206}