blob: 2acb031974d743cf369a23d46eec24710363a95b [file] [log] [blame]
Meki Cherkaoui8f173612012-06-06 01:05:40 -07001
2
Jeff Thompsone06b31e2012-09-30 17:19:19 -07003/**
4 * host is default '127.0.0.1'.
5 * port is default 9695.
6 */
7var NDN = function NDN(host, port){
8 this.host = (host || '127.0.0.1');
9 this.port = (port || 9695);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070010};
11
Jeff Thompsone06b31e2012-09-30 17:19:19 -070012NDN.prototype.createRoute = function(host,port){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070013 this.host=host;
14 this.port=port;
15}
16
Jeff Thompsone06b31e2012-09-30 17:19:19 -070017NDN.prototype.get = function(message){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070018 if(this.host!=null && this.port!=null){
19 var output ='';
20 message = message.trim();
21 if(message==null || message =="" ){
22 console.log('INVALID INPUT TO GET');
23 return null;
24 }
25
26
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070027 //var array = Name.createNameArray(message);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070028
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070029 int = new Interest(new Name(message));
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030
31 int.InterestLifetime = 4200;
32
33 var hex = encodeToHexInterest(int);
34
35 //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
36
37 var result = get(this.host,this.port, hex);
38
39
40 if(LOG>0)console.log('BINARY RESPONSE IS ' +result);
41
42 if(result==null || result==undefined || result =="" ){
43 /*if(result[0] != '0'||result[1]!='4') {
44 if(LOG>2)console.log('INVALID ANSWER');
45 }*/
46 return null;
47 }
48
49 else{
50
51 co = decodeHexContentObject(result);
52
53 if(LOG>2) {
54 console.log('DECODED CONTENT OBJECT');
55 console.log(co);
56 }
57 return co;
58 }
59 }
60 else{
61
62 console.log('ERROR URL OR PORT NOT SET');
63
64 return null;
65
66 }
67
68
69}
70
71
Jeff Thompsone06b31e2012-09-30 17:19:19 -070072NDN.prototype.put = function(name,content){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070073 if(this.host!=null && this.port!=null){
74
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070075 var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
76
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070078 alert("Cannot contact router");
79
80 return null;
81 }
82
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070084
Meki Cherkaoui8f173612012-06-06 01:05:40 -070085 name = name.trim();
86
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070087 var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070088
89 var bytes = encodeForwardingEntry(fe);
90
91
92 var si = new SignedInfo();
93 si.setFields();
94
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070095 var co = new ContentObject(new Name(),si,bytes,new Signature());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070096 co.sign();
97
98 var coBinary = encodeToBinaryContentObject(co);
99
Axel Colin de Verdiered363e632012-06-06 05:16:43 -0700100 //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 -0700101
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700102 var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700103
104 int = new Interest(interestName);
Jeff Thompson86aea882012-09-29 17:32:48 -0700105 int.scope = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700106
107 var hex = encodeToHexInterest(int);
108
109 console.log('GOING TO PUT INTEREST OBJECT');
110
111 console.log(hex);
112
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700113 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700114
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700115
116 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
117
118 //if(LOG>3)console.log('DATA ');
119
120 //if(LOG>3)console.log(result);
121
122 //interest = decodeHexInterest(result);
123
124 //console.log('SUCCESSFULLY PARSED INTEREST');
125
126 console.log('CREATING ANSWER');
127 var si = new SignedInfo();
128 si.setFields();
129
130 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700131
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700132 var co = new ContentObject(new Name(name),si,answer,new Signature());
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700133 co.sign();
134
135
136 var outputHex = encodeToHexContentObject(co);
137
138 //console.log('SENDING ANSWER');
139
140 //return get_java_socket_bridge().putAnswer(outputHex,name);
141
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700142 var result = put(this.host,this.port, hex,name,outputHex);
143
144
145 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700146 }
147 else{
148
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700149
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700150 console.log('ERROR URL OR PORT NOT SET');
151
152 return null;
153
154 }
155
156
157}