blob: d68196ae46abc049ee2fe4cb4cf9fb29a3fa8e37 [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
Meki Cherkaoui8f173612012-06-06 01:05:40 -070071 name = name.trim();
72
73 var fe = new ForwardingEntry('selfreg',new ContentName(name),null, null, 3,2147483647);
74
75 var bytes = encodeForwardingEntry(fe);
76
77
78 var si = new SignedInfo();
79 si.setFields();
80
81 var co = new ContentObject(new ContentName(),si,bytes,new Signature());
82 co.sign();
83
84 var coBinary = encodeToBinaryContentObject(co);
85
86 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');
87
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070088 var interestName = new ContentName(['ccnx',ccnxnodename,'selfreg',coBinary]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070089
90 int = new Interest(interestName);
91 int.Scope = 1;
92
93 var hex = encodeToHexInterest(int);
94
95 console.log('GOING TO PUT INTEREST OBJECT');
96
97 console.log(hex);
98
Meki Cherkaoui81bfc282012-06-06 03:23:25 -070099 //var result = put(this.host,this.port, hex,name);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700100
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700101
102 //if(LOG>3)console.log('received interest'); //from host'+ host +':'+port+' with name '+name);
103
104 //if(LOG>3)console.log('DATA ');
105
106 //if(LOG>3)console.log(result);
107
108 //interest = decodeHexInterest(result);
109
110 //console.log('SUCCESSFULLY PARSED INTEREST');
111
112 console.log('CREATING ANSWER');
113 var si = new SignedInfo();
114 si.setFields();
115
116 var answer = DataUtils.toNumbersFromString(content);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700117
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700118 var co = new ContentObject(new ContentName(name),si,answer,new Signature());
119 co.sign();
120
121
122 var outputHex = encodeToHexContentObject(co);
123
124 //console.log('SENDING ANSWER');
125
126 //return get_java_socket_bridge().putAnswer(outputHex,name);
127
128
129 var result = put(this.host,this.port, hex,name,outputHex);
130
131
132 return result;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700133 }
134 else{
135
Meki Cherkaoui81bfc282012-06-06 03:23:25 -0700136
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700137 console.log('ERROR URL OR PORT NOT SET');
138
139 return null;
140
141 }
142
143
144}