s243a

freenet.client

Oct 22nd, 2014
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. //Code blow: s243a pearltree node at: http://www.pearltrees.com/s243a/freenet/id12827249
  2. //------------------------
  3. /* This code is part of Freenet. It is distributed under the GNU General
  4.  * Public License, version 2 (or at your option any later version). See
  5.  * http://www.gnu.org/ for further details of the GPL. */
  6. package freenet.client;
  7.  
  8. import com.onionnetworks.fec.FECCode;
  9. import com.onionnetworks.fec.PureCode;
  10.  
  11. import freenet.support.LRUMap;
  12. import freenet.support.Logger;
  13.  
  14. /**
  15.  * FECCodec implementation using the onion code.
  16.  */
  17. public class StandardOnionFECCodec extends FECCodec {
  18.     // REDFLAG: How big is one of these?
  19.     private static final int MAX_CACHED_CODECS = 8;
  20.  
  21.     static boolean noNative;
  22.  
  23.     private static final LRUMap<MyKey, StandardOnionFECCodec> recentlyUsedCodecs = LRUMap.createSafeMap();
  24.  
  25.     private static class MyKey implements Comparable<MyKey> {
  26.         /** Number of input blocks */
  27.         int k;
  28.         /** Number of output blocks, including input blocks */
  29.         int n;
  30.  
  31.         public MyKey(int k, int n) {
  32.             this.n = n;
  33.             this.k = k;
  34.         }
  35.  
  36.         @Override
  37.         public boolean equals(Object o) {
  38.             if(o instanceof MyKey) {
  39.                 MyKey key = (MyKey)o;
  40.                 return (key.n == n) && (key.k == k);
  41.             } else return false;
  42.         }
  43.  
  44.         @Override
  45.         public int hashCode() {
  46.             return (n << 16) + k;
  47.         }
  48.  
  49.         @Override
  50.         public int compareTo(MyKey o) {
  51.             if(n > o.n) return 1;
  52.             if(n < o.n) return -1;
  53.             if(k > o.k) return 1;
  54.             if(k < o.k) return -1;
  55.             return 0;
  56.         }
  57.     }
  58.  
  59.     public synchronized static FECCodec getInstance(int dataBlocks, int checkBlocks) {
  60.         if(checkBlocks == 0 || dataBlocks == 0)
  61.             throw new IllegalArgumentException("data blocks "+dataBlocks+" check blocks "+checkBlocks);
  62.         MyKey key = new MyKey(dataBlocks, checkBlocks + dataBlocks);
  63.         StandardOnionFECCodec codec = recentlyUsedCodecs.get(key);
  64.         if(codec != null) {
  65.             recentlyUsedCodecs.push(key, codec);
  66.             return codec;
  67.         }
  68.         codec = new StandardOnionFECCodec(dataBlocks, checkBlocks + dataBlocks);
  69.         recentlyUsedCodecs.push(key, codec);
  70.         while(recentlyUsedCodecs.size() > MAX_CACHED_CODECS) {
  71.             recentlyUsedCodecs.popKey();
  72.         }
  73.         return codec;
  74.     }
  75.  
  76.     public StandardOnionFECCodec(int k, int n) {
  77.         super(k, n);
  78.  
  79.         loadFEC();
  80.     }
  81.  
  82.     @Override
  83.     protected void loadFEC() {
  84.         synchronized(this) {
  85.             if(fec != null) return;
  86.         }
  87.         FECCode fec2 = null;
  88.         if(k >= n) throw new IllegalArgumentException("n must be >k: n = "+n+" k = "+k);
  89.         if(k > 256 || n > 256) Logger.error(this, "Wierd FEC parameters? k = "+k+" n = "+n);
  90.         // native code segfaults if k < 256 and n > 256
  91.         // native code segfaults if n > k*2 i.e. if we have extra blocks beyond 100% redundancy
  92.         // FIXME: NATIVE FEC DISABLED PENDING FIXING THE SEGFAULT BUG (easily reproduced with check blocks > data blocks)
  93.         // AND A COMPETENT CODE REVIEW!!!
  94.         // SEGFAULT BUGS ARE USUALLY REMOTELY EXPLOITABLE!!!
  95. //      if((!noNative) && k <= 256 && n <= 256 && n <= k*2) {
  96. //          System.out.println("Creating native FEC: n="+n+" k="+k);
  97. //          System.out.flush();
  98. //          try {
  99. //              fec2 = new Native8Code(k,n);
  100. //              Logger.minor(this, "Loaded native FEC.");
  101. //
  102. //          } catch (Throwable t) {
  103. //              if(!noNative) {
  104. //                  System.err.println("Failed to load native FEC: "+t);
  105. //                  t.printStackTrace();
  106. //              }
  107. //              Logger.error(this, "Failed to load native FEC: "+t+" (k="+k+" n="+n+ ')', t);
  108. //
  109. //              if(t instanceof UnsatisfiedLinkError)
  110. //                  noNative = true;
  111. //          }
  112. //      } // FIXME 16-bit native FEC???
  113.  
  114.         if (fec2 != null){
  115.             synchronized(this) {
  116.             fec = fec2;
  117.             }
  118.         } else  {
  119.             fec2 = new PureCode(k,n);
  120.             synchronized(this) {
  121.                 fec = fec2;
  122.             }
  123.         }
  124.  
  125.         // revert to below if above causes JVM crashes
  126.         // Worst performance, but decode crashes
  127.         // fec = new PureCode(k,n);
  128.         // Crashes are caused by bugs which cause to use 320/128 etc. - n > 256, k < 256.
  129.     }
  130.  
  131.     @Override
  132.     public int countCheckBlocks() {
  133.         return n-k;
  134.     }
  135.  
  136.     @Override
  137.     public String toString() {
  138.         return super.toString()+":n="+n+",k="+k;
  139.     }
  140.  
  141.     @Override
  142.     public short getAlgorithm() {
  143.         return Metadata.SPLITFILE_ONION_STANDARD;
  144.     }
  145. }
Add Comment
Please, Sign In to add comment