Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 1 | |
| 2 | |
| 3 | var lwNDN = function lwNDN(host,port){ |
| 4 | this.host = host; |
| 5 | this.port = port; |
| 6 | }; |
| 7 | |
| 8 | lwNDN.prototype.createRoute = function(host,port){ |
| 9 | this.host=host; |
| 10 | this.port=port; |
| 11 | } |
| 12 | |
| 13 | lwNDN.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 | |
| 68 | lwNDN.prototype.put = function(name,content){ |
| 69 | if(this.host!=null && this.port!=null){ |
| 70 | |
Axel Colin de Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 71 | var co = this.get("/%C1.M.S.localhost/%C1.M.SRV/ccnd"); |
| 72 | |
| 73 | if(!co || !co.SignedInfo || !co.SignedInfo.Publisher || !co.SignedInfo.Publisher.PublisherPublicKeyDigest){ |
| 74 | alert("Cannot contact router"); |
| 75 | |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | var ccnxnodename = co.SignedInfo.Publisher.PublisherPublicKeyDigest; |
| 80 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 81 | 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 Verdiere | d363e63 | 2012-06-06 05:16:43 -0700 | [diff] [blame] | 96 | //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] | 97 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 98 | var interestName = new ContentName(['ccnx',ccnxnodename,'selfreg',coBinary]); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 99 | |
| 100 | int = new Interest(interestName); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 101 | int.scope = 1; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 102 | |
| 103 | var hex = encodeToHexInterest(int); |
| 104 | |
| 105 | console.log('GOING TO PUT INTEREST OBJECT'); |
| 106 | |
| 107 | console.log(hex); |
| 108 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 109 | //var result = put(this.host,this.port, hex,name); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 110 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 111 | |
| 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 Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 127 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 128 | 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 Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 143 | } |
| 144 | else{ |
| 145 | |
Meki Cherkaoui | 81bfc28 | 2012-06-06 03:23:25 -0700 | [diff] [blame] | 146 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 147 | console.log('ERROR URL OR PORT NOT SET'); |
| 148 | |
| 149 | return null; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | |
| 154 | } |