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