blob: c6658cd9f7d1749dfc7ee6ac8981e406a181ea35 [file] [log] [blame]
Meki Cherkaoui8f173612012-06-06 01:05:40 -07001
2
3var lwNDN = function lwNDN(host,port){
4 this.host = host;
5 this.port = port;
6};
7
8lwNDN.prototype.createRoute = function(host,port){
9 this.host=host;
10 this.port=port;
11}
12
13lwNDN.prototype.get = function(message){
14 if(this.host!=null && this.port!=null){
15 var output ='';
16 message = message.trim();
17 if(message==null || message =="" ){
18 console.log('INVALID INPUT TO GET');
19 return null;
20 }
21
22
23 //var array = ContentName.createNameArray(message);
24
25 int = new Interest(new ContentName(message));
26
27 int.InterestLifetime = 4200;
28
29 var hex = encodeToHexInterest(int);
30
31 //var result = get_java_socket_bridge().connectAndStart(ndnurl,ndnport,hex);
32
33 var result = get(this.host,this.port, hex);
34
35
36 if(LOG>0)console.log('BINARY RESPONSE IS ' +result);
37
38 if(result==null || result==undefined || result =="" ){
39 /*if(result[0] != '0'||result[1]!='4') {
40 if(LOG>2)console.log('INVALID ANSWER');
41 }*/
42 return null;
43 }
44
45 else{
46
47 co = decodeHexContentObject(result);
48
49 if(LOG>2) {
50 console.log('DECODED CONTENT OBJECT');
51 console.log(co);
52 }
53 return co;
54 }
55 }
56 else{
57
58 console.log('ERROR URL OR PORT NOT SET');
59
60 return null;
61
62 }
63
64
65}
66
67
68lwNDN.prototype.put = function(name,content){
69 if(this.host!=null && this.port!=null){
70
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070071 var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd");
72
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070073 if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070074 alert("Cannot contact router");
75
76 return null;
77 }
78
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070079 var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest;
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070080
Meki Cherkaoui8f173612012-06-06 01:05:40 -070081 name = name.trim();
82
83 var fe = new ForwardingEntry('selfreg',new ContentName(name),null, null, 3,2147483647);
84
85 var bytes = encodeForwardingEntry(fe);
86
87
88 var si = new SignedInfo();
89 si.setFields();
90
91 var co = new ContentObject(new ContentName(),si,bytes,new Signature());
92 co.sign();
93
94 var coBinary = encodeToBinaryContentObject(co);
95
Axel Colin de Verdiered363e632012-06-06 05:16:43 -070096 //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 -070097
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070098 var interestName = new ContentName(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070099
100 int = new Interest(interestName);
Jeff Thompson86aea882012-09-29 17:32:48 -0700101 int.scope = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700102
103 var hex = encodeToHexInterest(int);
104
105 console.log('GOING TO PUT INTEREST OBJECT');
106
107 console.log(hex);
108
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700109 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700111
112 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
113
114 //if(LOG>3)console.log('DATA ');
115
116 //if(LOG>3)console.log(result);
117
118 //interest = decodeHexInterest(result);
119
120 //console.log('SUCCESSFULLY PARSED INTEREST');
121
122 console.log('CREATING ANSWER');
123 var si = new SignedInfo();
124 si.setFields();
125
126 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700127
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700128 var co = new ContentObject(new ContentName(name),si,answer,new Signature());
129 co.sign();
130
131
132 var outputHex = encodeToHexContentObject(co);
133
134 //console.log('SENDING ANSWER');
135
136 //return get_java_socket_bridge().putAnswer(outputHex,name);
137
138
139 var result = put(this.host,this.port, hex,name,outputHex);
140
141
142 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700143 }
144 else{
145
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700146
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700147 console.log('ERROR URL OR PORT NOT SET');
148
149 return null;
150
151 }
152
153
154}