package pl.mkaczara.bch.encoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import pl.mkaczara.bch.BCH;
import pl.mkaczara.bch.math.BinPolynomial;
import pl.mkaczara.bch.code.CyclicCode;
import pl.mkaczara.bch.encoder.worker.BCHEncoderWorker;
/**
* Klasa obiektow reprezentujacych kodery kodu BCH
*
* @author Kuba
*/
public class BCHEncoder {
/**
* Kod
*/
private CyclicCode code;
/**
* ThreadPool dla workerow
*/
private ExecutorService workerExecutorSvc = Executors.newCachedThreadPool(); //Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
/**
* Tworzy obiekt kodera BCH
* @param code kod
*/
public BCHEncoder(CyclicCode code) {
this.code = code;
}
/**
* Koduje informacje kodem BCH
*
* @param input tablica bajtow z informacjami
* @return tablica bajtow ze slowami kodu BCH
*/
public byte[] encode(byte[] input){
if(workerExecutorSvc.isShutdown()){
workerExecutorSvc = Executors.newCachedThreadPool(); //Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
}
ArrayList<BinPolynomial> notEncoded = BCH.bytesToPolynomials(input, code.getK()-1);
ArrayList<BinPolynomial> encoded = new ArrayList<>();
ArrayList<BCHEncoderWorker> workers = new ArrayList<>();
List<Future<BinPolynomial>> futures = new ArrayList<>();
for(BinPolynomial info : notEncoded){
BCHEncoderWorker tmp = new BCHEncoderWorker(code);
tmp.setInput(info);
workers.add(tmp);
}
try {
futures = workerExecutorSvc.invokeAll(workers);
workerExecutorSvc.shutdownNow();
} catch (InterruptedException ex) {
}
for(Future<BinPolynomial> future : futures){
try {
encoded.add(future.get());
} catch (InterruptedException | ExecutionException ex) {
}
}
return BCH.polynomialsToBytes(encoded, code.getN()-1);
}
} // end BCHEncoder