Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 1 | |
| 2 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 3 | /** |
| 4 | * host is default '127.0.0.1'. |
| 5 | * port is default 9695. |
| 6 | */ |
| 7 | var NDN = function NDN(host, port){ |
| 8 | this.host = (host || '127.0.0.1'); |
| 9 | this.port = (port || 9695); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 10 | }; |
| 11 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 12 | NDN.prototype.createRoute = function(host,port){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 13 | this.host=host; |
| 14 | this.port=port; |
| 15 | } |
| 16 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 17 | NDN.prototype.get = function(message){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 18 | 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 Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 27 | //var array = Name.createNameArray(message); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 28 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 29 | int = new Interest(new Name(message)); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 30 | |
| 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 Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 72 | NDN.prototype.put = function(name,content){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 73 | if(this.host!=null && this.port!=null){ |
| 74 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 75 | var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd"); |
| 76 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 77 | if(!co || !co.signedInfo || !co.signedInfo.publisher || !co.signedInfo.publisher.publisherPublicKeyDigest){ |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 78 | alert("Cannot contact router"); |
| 79 | |
| 80 | return null; |
| 81 | } |
| 82 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 83 | var ccnxnodename = co.signedInfo.publisher.publisherPublicKeyDigest; |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 84 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 85 | name = name.trim(); |
| 86 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 87 | var fe = new ForwardingEntry('selfreg',new Name(name),null, null, 3,2147483647); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 88 | |
| 89 | var bytes = encodeForwardingEntry(fe); |
| 90 | |
| 91 | |
| 92 | var si = new SignedInfo(); |
| 93 | si.setFields(); |
| 94 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 95 | var co = new ContentObject(new Name(),si,bytes,new Signature()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 96 | co.sign(); |
| 97 | |
| 98 | var coBinary = encodeToBinaryContentObject(co); |
| 99 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 100 | //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 Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 102 | var interestName = new Name(['ccnx',ccnxnodename,'selfreg',coBinary]); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 103 | |
| 104 | int = new Interest(interestName); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 105 | int.scope = 1; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 106 | |
| 107 | var hex = encodeToHexInterest(int); |
| 108 | |
| 109 | console.log('GOING TO PUT INTEREST OBJECT'); |
| 110 | |
| 111 | console.log(hex); |
| 112 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 113 | //var result = put(this.host,this.port, hex,name); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 114 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 115 | |
| 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 Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 131 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 132 | var co = new ContentObject(new Name(name),si,answer,new Signature()); |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 133 | 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 Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 142 | var result = put(this.host,this.port, hex,name,outputHex); |
| 143 | |
| 144 | |
| 145 | return result; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 146 | } |
| 147 | else{ |
| 148 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 149 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 150 | console.log('ERROR URL OR PORT NOT SET'); |
| 151 | |
| 152 | return null; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | |
| 157 | } |