blob: 2b32df83035957ed4e2a7b8046b31cf2136c7cd6 [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
75
Jeff Thompsone06b31e2012-09-30 17:19:19 -070076NDN.prototype.put = function(name,content){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070077 if(this.host!=null && this.port!=null){
78
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070079 var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
80
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070082 alert("Cannot contact router");
83
84 return null;
85 }
86
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070087 var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070088
Meki Cherkaoui8f173612012-06-06 01:05:40 -070089 name = name.trim();
90
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070091 var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070092
93 var bytes = encodeForwardingEntry(fe);
94
95
96 var si = new SignedInfo();
97 si.setFields();
98
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070099 var co = new ContentObject(new Name(),si,bytes,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700100 co.sign();
101
102 var coBinary = encodeToBinaryContentObject(co);
103
Axel Colin de Verdiered363e632012-06-06 05:16:43 -0700104 //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 -0700105
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700106 var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700107
108 int = new Interest(interestName);
Jeff Thompson86aea882012-09-29 17:32:48 -0700109 int.scope = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
111 var hex = encodeToHexInterest(int);
112
113 console.log('GOING TO PUT INTEREST OBJECT');
114
115 console.log(hex);
116
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700117 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700118
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700119
120 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
121
122 //if(LOG>3)console.log('DATA ');
123
124 //if(LOG>3)console.log(result);
125
126 //interest = decodeHexInterest(result);
127
128 //console.log('SUCCESSFULLY PARSED INTEREST');
129
130 console.log('CREATING ANSWER');
131 var si = new SignedInfo();
132 si.setFields();
133
134 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700135
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700136 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700137 co.sign();
138
139
140 var outputHex = encodeToHexContentObject(co);
141
142 //console.log('SENDING ANSWER');
143
144 //return get_java_socket_bridge().putAnswer(outputHex,name);
145
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700146 var result = put(this.host,this.port, hex,name,outputHex);
147
148
149 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700150 }
151 else{
152
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700153
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700154 console.log('ERROR URL OR PORT NOT SET');
155
156 return null;
157
158 }
159
160
161}