Browse Source

Add FsMQManager (introduce pkg transport)

JNI-96
heck 5 years ago
parent
commit
05bed8968b
  1. 107
      test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java
  2. 23
      test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java
  3. 7
      test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/Makefile.conf
  4. 76
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  5. 8
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf
  6. 2
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile
  7. 67
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java
  8. 169
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java
  9. 7
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/Makefile.conf
  10. 24
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/Makefile
  11. 8
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java

107
test/java/foundation/pEp/jniadapter/test/framework/TestUtils.java

@ -1,8 +1,18 @@
package foundation.pEp.jniadapter.test.framework;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
@ -51,6 +61,7 @@ public class TestUtils {
}
/*
Time Utils
*/
@ -62,6 +73,8 @@ public class TestUtils {
}
}
/*
String Utils
*/
@ -192,5 +205,99 @@ public class TestUtils {
return text;
}
}
/*
FSUtils
*/
// possibly returns an empty list
public static List<File> filterbyFilename(List<File> files, String regex) {
List<File> ret = null;
Predicate<File> dotMsg = file -> file.getName().matches(regex);
ret = files.stream().filter(dotMsg).collect(Collectors.toList());
return ret;
}
// Possibly returns an empty ArrayList
public static List<File> listFilesByMtime(File dir) {
List<File> ret = new ArrayList<>();
File[] listOfFiles = dir.listFiles();
if (listOfFiles != null) {
Collections.addAll(ret, listOfFiles);
ret = sortFilesByMtime(ret);
}
return ret;
}
// null in null out
private static List<File> sortFilesByMtime(List<File> files) {
List<File> ret = null;
if (files != null) {
ret = new ArrayList(files);
Collections.sort(ret, (o1, o2) -> {
long ret1 = 0;
ret1 = o1.lastModified() - o2.lastModified();
return (int) clip(ret1, -1, 1);
});
}
return ret;
}
public static String readFile(Path path, Charset decoding) throws IOException {
String ret = null;
byte[] encoded = Files.readAllBytes(path);
ret = new String(encoded, decoding);
if (ret == null) {
throw new IOException("Error reading file: " + path);
}
return ret;
}
public static void writeFile(Path path, String msg, Charset encoding) throws IOException {
Files.write(path, msg.getBytes(encoding));
}
public static boolean deleteRecursively(File dir) {
deleteContentsRecursively(dir);
log("deleting: " + dir.getAbsolutePath());
return dir.delete();
}
public static boolean deleteContentsRecursively(File dir) {
boolean ret = false;
File[] allContents = dir.listFiles();
if (allContents != null) {
for (File file : allContents) {
ret = deleteRecursively(file);
}
}
return ret;
}
public static List<String> getAvailableCharsetNames() {
List<String> ret = new ArrayList<>();
for (String key : Charset.availableCharsets().keySet()) {
Charset val = Charset.forName(key);
ret.add(val.name());
}
return ret;
}
/*
Math Utils
*/
public static int clip(int val, int min, int max) {
return Math.max(min, Math.min(max, val));
}
public static long clip(long val, long min, long max) {
return Math.max(min, Math.min(max, val));
}
}

23
test/java/foundation/pEp/jniadapter/test/utils/AdapterBaseTestContext.java

@ -1,14 +1,33 @@
package foundation.pEp.jniadapter.test.utils;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Vector;
import java.util.*;
//public class ABAliceTestContext extends AdapterBaseTestContext {
// FsMQManager transport;
//
// @Override
// public void init() throws Throwable {
// super.init();
// transport = new FsMQManager(alice.address, "../resources/per-user-dir/alice/inbox");
// transport.clearOwnQueue();
// transport.addPeer(bob.address, "../resources/per-user-dir/bob/inbox");
//
// transport.broadcastSigOnline();
// transport.waitForPeerOnline(bob.address);
// }
//
//}
public class AdapterBaseTestContext extends AbstractTestContext {
// Basic

7
test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/Makefile.conf

@ -1,7 +0,0 @@
JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.fsmsgqueue.test
JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/fsmsgqueue/test
JAVA_CWD=../../../../../../../../
JAVA=java -enableassertions
JAVA_CLASSES_FSMSGQUEUE= \
../../FsMsgQueue.class

76
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java

@ -0,0 +1,76 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager;
import foundation.pEp.jniadapter.test.framework.TestUtils;
import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.FsMsgQueue;
import java.util.HashMap;
import java.util.Map;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class FsMQManager {
private String ownAddress;
private FsMsgQueue ownQueue;
private Map<String, FsMsgQueue> peerQueues = new HashMap<>();
private static String SIGNALONLINEMSG = "SIGONLINE";
public FsMQManager(String ownAddr, String ownQueueDir) {
ownAddress = ownAddr;
ownQueue = new FsMsgQueue(ownQueueDir);
}
public void addPeer(String address, String queueDir) {
FsMsgQueue q = new FsMsgQueue(queueDir);
peerQueues.put(address, q);
}
public void sendMsgToPeer(String address, String msg) throws UnknownPeerException {
getQueueForPeer(address).add(msg);
}
public void waitForPeerOnline(String address) {
String msg = "";
while (msg != "startup from " + address) {
log("Waiting for " + address);
msg = waitForMsg();
}
}
public void clearOwnQueue() {
ownQueue.clear();
}
public String waitForMsg() {
while (ownQueue.isEmpty()) {
TestUtils.sleep(100);
}
return ownQueue.remove();
}
public void sendSigOnlineToPeer(String address) {
String msg = SIGNALONLINEMSG + " " + ownAddress;
log("Sending SIGONLINE to: " + address);
sendMsgToPeer(address, msg);
}
public void broadcastSigOnline() {
for (String k : peerQueues.keySet()) {
sendSigOnlineToPeer(k);
}
}
private FsMsgQueue getQueueForPeer(String address) throws UnknownPeerException {
FsMsgQueue ret = peerQueues.get(address);
if (ret == null) {
throw new UnknownPeerException("No peer with address:" + address);
}
return ret;
}
}
class UnknownPeerException extends RuntimeException {
UnknownPeerException(String message) {
super(message);
}
}

8
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf

@ -0,0 +1,8 @@
JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test
JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test
JAVA_CWD=../../../../../../../../../
JAVA=java -enableassertions
JAVA_CLASSES_FSMSGQUEUE= \
../../FsMQManager.class \
../../../fsmsgqueue/FsMsgQueue.class

2
test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/Makefile → test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile

@ -1,6 +1,6 @@
include ../Makefile.conf
TEST_UNIT_NAME=readwrite
TEST_UNIT_NAME=regression
JAVA_CLASSES = \
TestMain.class \

67
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

@ -0,0 +1,67 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.regression;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import foundation.pEp.jniadapter.test.framework.*;
import java.util.HashMap;
import java.util.Map;
class FsMsgQueueTestContext extends AbstractTestContext {
Map<String, String> peers;
String ownAddress = "Alice";
String ownQDir = "../resources/fsmsgqueue-test/alice";
String addressBob = "Bob";
String addressCarol = "Carol";
FsMQManager qm;
@Override
public void init() throws Throwable {
peers = new HashMap<>();
peers.put(addressBob, "../resources/fsmsgqueue-test/bob");
peers.put(addressCarol, "../resources/fsmsgqueue-test/carol");
}
}
class TestMain {
public static void main(String[] args) throws Exception {
TestSuite.setVerbose(true);
FsMsgQueueTestContext testCtx = new FsMsgQueueTestContext();
new TestUnit<FsMsgQueueTestContext>("Constructor", testCtx, ctx -> {
log("Creating QM for: " + ctx.ownAddress);
ctx.qm = new FsMQManager(ctx.ownAddress, ctx.ownQDir);
}).add();
new TestUnit<FsMsgQueueTestContext>("Clear own queue", testCtx, ctx -> {
ctx.qm.clearOwnQueue();
}).add();
new TestUnit<FsMsgQueueTestContext>("Add peer bob", testCtx, ctx -> {
for(String k : ctx.peers.keySet()) {
log("Adding peer: " + k);
ctx.qm.addPeer(k, ctx.peers.get(k));
}
}).add();
new TestUnit<FsMsgQueueTestContext>("Broadcast online", testCtx, ctx -> {
ctx.qm.broadcastSigOnline();
}).add();
new TestUnit<FsMsgQueueTestContext>("Wait for bob", testCtx, ctx -> {
log("Waiting for Bob to signal online");
ctx.qm.waitForPeerOnline(ctx.addressBob);
log("Bob is online");
}).add();
TestSuite.run();
}
}

169
test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/FsMsgQueue.java → test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java

@ -1,24 +1,13 @@
package foundation.pEp.jniadapter.test.utils.fsmsgqueue;
package foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.test.framework.TestUtils.*;
import foundation.pEp.jniadapter.test.utils.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Parameter;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javafx.util.Pair;
@ -85,21 +74,6 @@ public class FsMsgQueue implements Queue<String> {
return ret;
}
private String getNewFilename() {
String ret = "";
List<File> msgFiles = dotMsgFilesSorted(qDir, true);
int newNumber = 0;
if(msgFiles.size() > 0) {
File latest = msgFiles.get(msgFiles.size() - 1);
newNumber = Integer.parseInt(latest.getName().replace(".msg", "")) + 1;
}
ret = TestUtils.padOrClipString(String.valueOf(newNumber),"0",12,Alignment.Right,"");
ret += ".msg";
return ret;
}
@Override
public boolean offer(String msg) {
boolean ret = true;
@ -111,30 +85,6 @@ public class FsMsgQueue implements Queue<String> {
return ret;
}
private File getOldestMsgFilename() {
File ret = null;
List<File> msgFiles = dotMsgFilesSorted(qDir, false);
if(msgFiles.size() > 0) {
File oldest = msgFiles.get(msgFiles.size() - 1);
ret = oldest;
}
return ret;
}
private Pair<File, String> get() throws Exception {
Pair<File, String> ret = null;
File oldestFile = getOldestMsgFilename();
log("reading file:" + oldestFile.getName());
if (oldestFile == null) {
throw new NoSuchElementException("MNo .msg file in dir: " + qDir);
} else {
String fContent = null;
fContent = readFile(oldestFile.toPath(), fileEncoding);
ret = new Pair<>(oldestFile, fContent);
}
return ret;
}
@Override
public String remove() throws NoSuchElementException {
String ret = null;
@ -199,7 +149,6 @@ public class FsMsgQueue implements Queue<String> {
return ret;
}
@Override
public void clear() {
deleteContentsRecursively(qDir);
@ -260,20 +209,47 @@ public class FsMsgQueue implements Queue<String> {
}
//Math Utils
public static int clip(int val, int min, int max) {
return Math.max(min, Math.min(max, val));
}
public static long clip(long val, long min, long max) {
return Math.max(min, Math.min(max, val));
private String getNewFilename() {
String ret = "";
List<File> msgFiles = dotMsgFilesSorted(qDir, true);
int newNumber = 0;
if(msgFiles.size() > 0) {
File latest = msgFiles.get(msgFiles.size() - 1);
newNumber = Integer.parseInt(latest.getName().replace(".msg", "")) + 1;
}
ret = padOrClipString(String.valueOf(newNumber),"0",12,Alignment.Right,"");
ret += ".msg";
return ret;
}
private File getOldestMsgFilename() {
File ret = null;
List<File> msgFiles = dotMsgFilesSorted(qDir, false);
if(msgFiles.size() > 0) {
File oldest = msgFiles.get(msgFiles.size() - 1);
ret = oldest;
}
return ret;
}
// File Utils
private Pair<File, String> get() throws Exception {
Pair<File, String> ret = null;
File oldestFile = getOldestMsgFilename();
log("reading file:" + oldestFile.getName());
if (oldestFile == null) {
throw new NoSuchElementException("MNo .msg file in dir: " + qDir);
} else {
String fContent = null;
fContent = readFile(oldestFile.toPath(), fileEncoding);
ret = new Pair<>(oldestFile, fContent);
}
return ret;
}
// Possibly returns an empty List
private static List<File> dotMsgFilesSorted(File dir, boolean ascending) {
private List<File> dotMsgFilesSorted(File dir, boolean ascending) {
List<File> ret = new ArrayList<>();
File[] listOfFiles = dir.listFiles();
if (listOfFiles != null) {
@ -295,78 +271,7 @@ public class FsMsgQueue implements Queue<String> {
return ret;
}
// possibly returns an empty list
private static List<File> filterbyFilename(List<File> files, String regex) {
List<File> ret = null;
Predicate<File> dotMsg = file -> file.getName().matches(regex);
ret = files.stream().filter(dotMsg).collect(Collectors.toList());
return ret;
}
// Possibly returns an empty ArrayList
private static List<File> listFilesByMtime(File dir) {
List<File> ret = new ArrayList<>();
File[] listOfFiles = dir.listFiles();
if (listOfFiles != null) {
Collections.addAll(ret, listOfFiles);
ret = sortFilesByMtime(ret);
}
return ret;
}
// null in null out
private static List<File> sortFilesByMtime(List<File> files) {
List<File> ret = null;
if (files != null) {
ret = new ArrayList(files);
Collections.sort(ret, (o1, o2) -> {
long ret1 = 0;
ret1 = o1.lastModified() - o2.lastModified();
return (int) clip(ret1, -1, 1);
});
}
return ret;
}
public static String readFile(Path path, Charset decoding) throws IOException {
String ret = null;
byte[] encoded = Files.readAllBytes(path);
ret = new String(encoded, decoding);
if (ret == null) {
throw new IOException("Error reading file: " + path);
}
return ret;
}
public static void writeFile(Path path, String msg, Charset encoding) throws IOException {
Files.write(path, msg.getBytes(encoding));
}
public static boolean deleteRecursively(File dir) {
deleteContentsRecursively(dir);
log("deleting: " + dir.getAbsolutePath());
return dir.delete();
}
public static boolean deleteContentsRecursively(File dir) {
boolean ret = false;
File[] allContents = dir.listFiles();
if (allContents != null) {
for (File file : allContents) {
ret = deleteRecursively(file);
}
}
return ret;
}
private static List<String> getAvailableCharsetNames() {
List<String> ret = new ArrayList<>();
for (String key : Charset.availableCharsets().keySet()) {
Charset val = Charset.forName(key);
ret.add(val.name());
}
return ret;
}
}

7
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/Makefile.conf

@ -0,0 +1,7 @@
JAVA_PKG_BASENAME=foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.test
JAVA_PKG_BASEPATH=foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test
JAVA_CWD=../../../../../../../../../
JAVA=java -enableassertions
JAVA_CLASSES_FSMSGQUEUE= \
../../FsMsgQueue.class

24
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/Makefile

@ -0,0 +1,24 @@
include ../Makefile.conf
TEST_UNIT_NAME=regression
JAVA_CLASSES = \
TestMain.class \
JAVA_CLASSES += $(JAVA_CLASSES_FSMSGQUEUE)
.PHONY: compile run test clean
all: compile
$(MAKE) run
run: compile
cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain
compile: $(JAVA_CLASSES)
%.class: %.java
cd $(JAVA_CWD);pwd;javac $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$<
clean:
rm -f $(JAVA_CLASSES)

8
test/java/foundation/pEp/jniadapter/test/utils/fsmsgqueue/test/readwrite/TestMain.java → test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/test/regression/TestMain.java

@ -1,11 +1,11 @@
package foundation.pEp.jniadapter.test.utils.fsmsgqueue.test.readwrite;
package foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.test.regression;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import static foundation.pEp.jniadapter.test.utils.fsmsgqueue.FsMsgQueue.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.*;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.test.utils.*;
import foundation.pEp.jniadapter.test.utils.fsmsgqueue.*;
import java.io.File;
import java.util.ArrayList;
Loading…
Cancel
Save