#36 added two configs for test purposes

CipherConfiguration encrypts, compresses a file and vice-versa
TokenizerConfiguration decrypts, decompresses and reads tokens from a
given file
updated pipe-factories.conf file
parent 071889db
/bin
/target
/testdata/
teetime.variant.methodcallWithPorts.framework.core.pipe.SingleElementPipeFactory
teetime.variant.methodcallWithPorts.framework.core.pipe.OrderedGrowableArrayPipeFactory
teetime.variant.methodcallWithPorts.framework.core.pipe.UnorderedGrowablePipeFactory
teetime.variant.methodcallWithPorts.framework.core.pipe.SpScPipeFactory
teetime.framework.pipe.SingleElementPipeFactory
teetime.framework.pipe.OrderedGrowableArrayPipeFactory
teetime.framework.pipe.UnorderedGrowablePipeFactory
teetime.framework.pipe.SpScPipeFactory
......@@ -101,7 +101,7 @@
<version>2.5</version>
</plugin>
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
......
......@@ -54,11 +54,31 @@ public class PipeFactory {
return this.create(tc, PipeOrdering.QUEUE_BASED, true, 1);
}
/**
*
* @param tc
* @param ordering
* @param growable
* @param capacity
* @return
*/
public IPipe create(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable, final int capacity) {
IPipeFactory pipeFactory = getPipeFactory(tc, ordering, growable);
return pipeFactory.create(capacity);
}
/**
* Returns a PipeFactory Instance
*
* @param tc
* Communication type between two connected stages
* @param ordering
* Specifies the ordering behavior of the pipe
* @param growable
* Whether the queue size is fixed or not
* @return
* A PipeFactory, which provides suitable pipes
*/
public IPipeFactory getPipeFactory(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable) {
String key = this.buildKey(tc, ordering, growable);
IPipeFactory pipeFactory = this.pipeFactories.get(key);
......@@ -68,12 +88,23 @@ public class PipeFactory {
return pipeFactory;
}
/**
*
* @param pipeFactory
*/
public void register(final IPipeFactory pipeFactory) {
String key = this.buildKey(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
this.pipeFactories.put(key, pipeFactory);
LOGGER.info("Registered pipe factory: " + pipeFactory.getClass().getCanonicalName());
}
/**
*
* @param tc
* @param ordering
* @param growable
* @return
*/
private String buildKey(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable) {
return tc.toString() + ordering.toString() + growable;
}
......
package teetime.stage.io;
import teetime.framework.ConsumerStage;
import teetime.framework.OutputPort;
public class ByteArray2String extends ConsumerStage<byte[]> {
private final OutputPort<String> outputPort = this.createOutputPort();
@Override
protected void execute(final byte[] element) {
this.send(outputPort, new String(element));
}
public OutputPort<? extends String> getOutputPort() {
return outputPort;
}
}
package teetime.stage.io;
import java.io.File;
import java.io.FileOutputStream;
import teetime.framework.ConsumerStage;
public class ByteArrayFileWriter extends ConsumerStage<byte[]> {
private final File file;
public ByteArrayFileWriter(final File file) {
this.file = file;
}
@Override
protected void execute(final byte[] element) {
FileOutputStream fo;
// TODO check if file exists, otherwise create file
try {
fo = new FileOutputStream(this.file);
fo.write(element);
fo.close();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
package teetime.stage.io;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import teetime.framework.ConsumerStage;
import teetime.framework.OutputPort;
public class CipherByteArray extends ConsumerStage<byte[]> {
private final OutputPort<byte[]> outputPort = this.createOutputPort();
private Cipher cipher = null;
private final byte[] salt = { 't', 'e', 's', 't' };
private SecretKeySpec skeyspec = null;
public enum CipherMode {
ENCRYPT, DECRYPT
}
public CipherByteArray(final String password, final CipherMode mode) {
KeySpec keySpec = new PBEKeySpec(password.toCharArray(),
salt,
1024, 128);
SecretKey secretKey = null;
try {
secretKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").
generateSecret(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
try {
cipher = Cipher.getInstance(skeyspec.getAlgorithm());
if (mode == CipherMode.ENCRYPT) {
cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
} else {
cipher.init(Cipher.DECRYPT_MODE, skeyspec);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void execute(final byte[] element) {
byte[] output = null;
try {
output = cipher.doFinal(element);
} catch (Exception e) {
e.printStackTrace();
}
this.send(outputPort, output);
}
public OutputPort<? extends byte[]> getOutputPort() {
return outputPort;
}
}
package teetime.stage.io;
import java.io.File;
import java.io.IOException;
import teetime.framework.ConsumerStage;
import teetime.framework.HeadStage;
import teetime.framework.OutputPort;
import com.google.common.io.Files;
public class File2ByteArray extends ConsumerStage<File> implements HeadStage {
private final OutputPort<byte[]> outputPort = this.createOutputPort();
@Override
protected void execute(final File element) {
try {
byte[] cache = Files.toByteArray(element);
this.send(outputPort, cache);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public OutputPort<? extends byte[]> getOutputPort() {
return outputPort;
}
@Override
public boolean shouldBeTerminated() {
return false;
}
@Override
public void terminate() {
}
}
package teetime.stage.io;
import java.util.StringTokenizer;
import teetime.framework.ConsumerStage;
import teetime.framework.OutputPort;
public class Tokenizer extends ConsumerStage<String> {
private final OutputPort<String> outputPort = this.createOutputPort();
private final String regex;
public Tokenizer(final String regex) {
this.regex = regex;
}
@Override
protected void execute(final String element) {
StringTokenizer st = new StringTokenizer(element, regex);
while (st.hasMoreTokens()) {
this.send(outputPort, st.nextToken());
}
}
public OutputPort<? extends String> getOutputPort() {
return outputPort;
}
}
package teetime.stage.io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import teetime.framework.ConsumerStage;
import teetime.framework.OutputPort;
/**
* A stage to compress and decompress byte arrays
*
* @author Nelson Tavares de Sousa
*
*/
public class ZipByteArray extends ConsumerStage<byte[]> {
private final OutputPort<byte[]> outputPort = this.createOutputPort();
private final ZipMode mode;
public enum ZipMode {
COMP, DECOMP
}
public ZipByteArray(final ZipMode mode) {
this.mode = mode;
}
@Override
protected void execute(final byte[] element) {
byte[] cache = null;
try {
if (mode == ZipMode.COMP) {
cache = compress(element);
} else {
cache = decompress(element);
}
} catch (Exception e) {
e.printStackTrace();
}
this.send(outputPort, cache);
}
public static byte[] compress(final byte[] data) throws IOException {
Deflater deflater = new Deflater();
deflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer); // returns the generated code... index
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
deflater.end();
return output;
}
public static byte[] decompress(final byte[] data) throws IOException, DataFormatException {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
inflater.end();
return output;
}
public OutputPort<? extends byte[]> getOutputPort() {
return outputPort;
}
}
package teetime.examples.cipher;
import java.io.File;
import teetime.framework.AnalysisConfiguration;
import teetime.framework.pipe.PipeFactory;
import teetime.framework.pipe.PipeFactory.PipeOrdering;
import teetime.framework.pipe.PipeFactory.ThreadCommunication;
import teetime.stage.InitialElementProducer;
import teetime.stage.io.ByteArrayFileWriter;
import teetime.stage.io.CipherByteArray;
import teetime.stage.io.CipherByteArray.CipherMode;
import teetime.stage.io.File2ByteArray;
import teetime.stage.io.ZipByteArray;
import teetime.stage.io.ZipByteArray.ZipMode;
public class CipherConfiguration extends AnalysisConfiguration {
private final PipeFactory pipeFactory = PipeFactory.INSTANCE;
private final File input, output;
private final String password;
public CipherConfiguration(final String inputFile, final String outputFile, final String password) {
this.input = new File(inputFile);
this.output = new File(outputFile);
this.password = password;
InitialElementProducer<File> init = new InitialElementProducer<File>(this.input);
File2ByteArray f2b = new File2ByteArray();
CipherByteArray enc = new CipherByteArray(this.password, CipherMode.ENCRYPT);
ZipByteArray comp = new ZipByteArray(ZipMode.COMP);
ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT);
ByteArrayFileWriter writer = new ByteArrayFileWriter(output);
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(init.getOutputPort(), f2b.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(f2b.getOutputPort(), enc.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(enc.getOutputPort(), comp.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(comp.getOutputPort(), decomp.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(decomp.getOutputPort(), decrypt.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(decrypt.getOutputPort(), writer.getInputPort());
this.getFiniteProducerStages().add(init);
}
}
package teetime.examples.cipher;
import java.io.File;
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import teetime.framework.Analysis;
import teetime.framework.AnalysisConfiguration;
import com.google.common.io.Files;
public class CipherTest {
static String inputFile = "testdata/dependencies.html";
static String outputFile = "testdata/dependencies.html";
static String password = "Password";
static long start;
long stop;
static AnalysisConfiguration configuration = new CipherConfiguration(inputFile, outputFile, password);
final static Analysis analysis = new Analysis(configuration);
@BeforeClass
public static void beforeClass() {
analysis.init();
start = System.currentTimeMillis();
}
@Test
public void executeTest() {
analysis.start();
}
@AfterClass
public static void afterClass() {
System.out.println("It took " + (System.currentTimeMillis() - start) + " Milliseconds");
boolean bool = false;
try {
bool = Files.equal(new File(inputFile), new File(outputFile));
} catch (IOException e) {
e.printStackTrace();
}
Assert.assertTrue(bool);
}
}
......@@ -4,6 +4,8 @@ import teetime.framework.AnalysisConfiguration;
import teetime.framework.pipe.PipeFactory;
import teetime.framework.pipe.PipeFactory.ThreadCommunication;
import teetime.examples.loopStage.Countdown;
public class LoopStageAnalysisConfiguration extends AnalysisConfiguration {
public LoopStageAnalysisConfiguration() {
......
package teetime.examples.tokenizer;
import java.io.File;
import teetime.framework.AnalysisConfiguration;
import teetime.framework.pipe.PipeFactory;
import teetime.framework.pipe.PipeFactory.PipeOrdering;
import teetime.framework.pipe.PipeFactory.ThreadCommunication;
import teetime.stage.InitialElementProducer;
import teetime.stage.io.ByteArray2String;
import teetime.stage.io.CipherByteArray;
import teetime.stage.io.CipherByteArray.CipherMode;
import teetime.stage.io.File2ByteArray;
import teetime.stage.io.Printer;
import teetime.stage.io.Tokenizer;
import teetime.stage.io.ZipByteArray;
import teetime.stage.io.ZipByteArray.ZipMode;
public class TokenizerConfiguration extends AnalysisConfiguration {
private final PipeFactory pipeFactory = PipeFactory.INSTANCE;
private final File input;
private final String password;
public TokenizerConfiguration(final String inputFile, final String password) {
this.input = new File(inputFile);
this.password = password;
InitialElementProducer<File> init = new InitialElementProducer<File>(this.input);
File2ByteArray f2b = new File2ByteArray();
CipherByteArray enc = new CipherByteArray(this.password, CipherMode.ENCRYPT);
ZipByteArray comp = new ZipByteArray(ZipMode.COMP);
ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT);
ByteArray2String b2s = new ByteArray2String();
Tokenizer tokenizer = new Tokenizer(" ");
Printer<String> pt = new Printer<String>();
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(init.getOutputPort(), f2b.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(f2b.getOutputPort(), enc.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(enc.getOutputPort(), comp.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(comp.getOutputPort(), decomp.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(decomp.getOutputPort(), decrypt.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(decrypt.getOutputPort(), b2s.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(b2s.getOutputPort(), tokenizer.getInputPort());
this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
.create(tokenizer.getOutputPort(), pt.getInputPort());
this.getFiniteProducerStages().add(init);
}
}
package teetime.examples.tokenizer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import teetime.framework.Analysis;
import teetime.framework.AnalysisConfiguration;
public class TokenizerTest {
static String inputFile = "testdata/dependencies.html";
static String password = "Password";
static long start;
long stop;
static AnalysisConfiguration configuration = new TokenizerConfiguration(inputFile, password);
final static Analysis analysis = new Analysis(configuration);
@BeforeClass
public static void beforeClass() {
analysis.init();
start = System.currentTimeMillis();
}
@Test
public void executeTest() {
analysis.start();
}
@AfterClass
public static void afterClass() {
System.out.println("It took " + (System.currentTimeMillis() - start) + " Milliseconds");
// boolean bool = false;
// try {
// bool = Files.equal(new File(inputFile), new File(outputFile));
// } catch (IOException e) {
// e.printStackTrace();
// }
// Assert.assertTrue(bool);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment