blob: 5934cdd67b0988dcf9bd1a8e0e7a60370bc60abc [file] [log] [blame]
Jeff Thompson745026e2012-10-13 12:49:20 -07001/*
2 * @author: ucla-cs
3 * 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 Thompsone06b31e2012-09-30 17:19:19 -07007/**
8 * host is default '127.0.0.1'.
9 * port is default 9695.
10 */
11var NDN = function NDN(host, port){
12 this.host = (host || '127.0.0.1');
13 this.port = (port || 9695);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070014};
15
Jeff Thompsone06b31e2012-09-30 17:19:19 -070016NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070017 this.host=host;
18 this.port=port;
19}
20
Jeff Thompsone06b31e2012-09-30 17:19:19 -070021NDN.prototype.get = function(message){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070022 if(this.host!=null && this.port!=null){
23 var output ='';
24 message = message.trim();
25 if(message==null || message =="" ){
26 console.log('INVALID INPUT TO GET');
27 return null;
28 }
29
30
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070031 //var array = Name.createNameArray(message);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070032
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070033 int = new Interest(new Name(message));
Meki Cherkaoui8f173612012-06-06 01:05:40 -070034
35 int.InterestLifetime = 4200;
36
37 var hex = encodeToHexInterest(int);
38
39 //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
40
41 var result = get(this.host,this.port, hex);
42
43
44 if(LOG>0)console.log('BINARY RESPONSE IS ' +result);
45
46 if(result==null || result==undefined || result =="" ){
47 /*if(result[0] != '0'||result[1]!='4') {
48 if(LOG>2)console.log('INVALID ANSWER');
49 }*/
50 return null;
51 }
52
53 else{
54
55 co = decodeHexContentObject(result);
56
57 if(LOG>2) {
58 console.log('DECODED CONTENT OBJECT');
59 console.log(co);
60 }
61 return co;
62 }
63 }
64 else{
65
66 console.log('ERROR URL OR PORT NOT SET');
67
68 return null;
69
70 }
71
72
73}
74
Jeff Thompsone06b31e2012-09-30 17:19:19 -070075NDN.prototype.put = function(name,content){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076 if(this.host!=null && this.port!=null){
77
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070078 var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
79
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070080 if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070081 alert("Cannot contact router");
82
83 return null;
84 }
85
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070086 var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070087
Meki Cherkaoui8f173612012-06-06 01:05:40 -070088 name = name.trim();
89
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070090 var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070091
92 var bytes = encodeForwardingEntry(fe);
93
94
95 var si = new SignedInfo();
96 si.setFields();
97
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070098 var co = new ContentObject(new Name(),si,bytes,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070099 co.sign();
100
101 var coBinary = encodeToBinaryContentObject(co);
102
Axel Colin de Verdiered363e632012-06-06 05:16:43 -0700103 //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');
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700104
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700105 var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700106
107 int = new Interest(interestName);
Jeff Thompson86aea882012-09-29 17:32:48 -0700108 int.scope = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700109
110 var hex = encodeToHexInterest(int);
111
112 console.log('GOING TO PUT INTEREST OBJECT');
113
114 console.log(hex);
115
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700116 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700117
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700118
119 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
120
121 //if(LOG>3)console.log('DATA ');
122
123 //if(LOG>3)console.log(result);
124
125 //interest = decodeHexInterest(result);
126
127 //console.log('SUCCESSFULLY PARSED INTEREST');
128
129 console.log('CREATING ANSWER');
130 var si = new SignedInfo();
131 si.setFields();
132
133 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700134
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700135 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700136 co.sign();
137
138
139 var outputHex = encodeToHexContentObject(co);
140
141 //console.log('SENDING ANSWER');
142
143 //return get_java_socket_bridge().putAnswer(outputHex,name);
144
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700145 var result = put(this.host,this.port, hex,name,outputHex);
146
147
148 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700149 }
150 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700151 console.log('ERROR URL OR PORT NOT SET');
152
153 return null;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700154 }
Jeff Thompson34419762012-10-15 22:24:12 -0700155}
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700156
Jeff Thompson34419762012-10-15 22:24:12 -0700157/** Encode name as an Interest. If template is not null, use its attributes.
158 * Send the interest to host:port, read the entire response and call
159 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -0700160 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -0700161 */
162NDN.prototype.expressInterest = function(
163 // Name
164 name,
165 // Closure
166 closure,
167 // Interest
168 template) {
169 if (this.host == null || this.port == null) {
170 dump('ERROR host OR port NOT SET\n');
171 return;
172 }
173
174 interest = new Interest(name);
175 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -0700176 interest.minSuffixComponents = template.minSuffixComponents;
177 interest.maxSuffixComponents = template.maxSuffixComponents;
178 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
179 interest.exclude = template.exclude;
180 interest.childSelector = template.childSelector;
181 interest.answerOriginKind = template.answerOriginKind;
182 interest.scope = template.scope;
183 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -0700184 }
185 else
Jeff Thompson741108b2012-10-15 23:07:09 -0700186 interest.interestLifetime = 4200;
Jeff Thompson97f27432012-10-16 00:28:03 -0700187
188 var encoder = new BinaryXMLEncoder();
189 interest.to_ccnb(encoder);
190 var outputData = DataUtils.toString(encoder.getReducedOstream());
191 encoder = null;
Jeff Thompson34419762012-10-15 22:24:12 -0700192
193 var dataListener = {
194 onReceivedData : function(result) {
195 if (result == null || result == undefined || result.length == 0)
196 listener.onReceivedContentObject(null);
197 else {
198 var decoder = new BinaryXMLDecoder(result);
199 var co = new ContentObject();
200 co.from_ccnb(decoder);
201
202 if(LOG>2) {
203 dump('DECODED CONTENT OBJECT\n');
204 dump(co);
205 dump('\n');
206 }
207
208 // TODO: verify the content object and set kind to UPCALL_CONTENT.
Jeff Thompson741108b2012-10-15 23:07:09 -0700209 var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
210 new UpcallInfo(this, interest, 0, co));
211 // TODO: Check result for Closure.RESULT_OK, etc.
Jeff Thompson34419762012-10-15 22:24:12 -0700212 }
213 }
214 }
215
Jeff Thompson97f27432012-10-16 00:28:03 -0700216 // The application includes a source file that defines readAllFromSocket
217 // according to the application's communication method.
218 readAllFromSocket(this.host, this.port, outputData, dataListener);
Jeff Thompson34419762012-10-15 22:24:12 -0700219};
220