Browse Source

Add FsMQMessage

JNI-96
heck 5 years ago
parent
commit
520a35d8ad
  1. 67
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  2. 30
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQMessage.java
  3. 1
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf
  4. 30
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

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

@ -116,7 +116,7 @@ public class FsMQManager {
public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException, NullPointerException { public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException, NullPointerException {
if (address != null) { if (address != null) {
if (msg != null) { if (msg != null) {
FsMQMessage mqMsg = new FsMQMessage(self, msg); FsMQMessageInternal mqMsg = new FsMQMessageInternal(self, msg);
String serializedStr = mqMsg.serialize(); String serializedStr = mqMsg.serialize();
getQueueForIdentity(address).add(serializedStr); getQueueForIdentity(address).add(serializedStr);
} else { } else {
@ -131,15 +131,15 @@ public class FsMQManager {
getQueueForIdentity(self.getAddress()).clear(); getQueueForIdentity(self.getAddress()).clear();
} }
public String receiveMessage() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { public FsMQMessage receiveMessage() throws IOException, ClassNotFoundException, TimeoutException {
return receiveMessage(0); return receiveMessage(0);
} }
// Blocks until timeout // Blocks until timeout
public String receiveMessage(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { public FsMQMessage receiveMessage(int timeoutSec) throws IOException, ClassNotFoundException, TimeoutException {
String ret = null; FsMQMessage ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress()); FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress());
FsMQMessage mqMsg = null; FsMQMessageInternal mqMsg = null;
int pollInterval = 100; int pollInterval = 100;
int pollRepeats = timeoutSec * 1000 / pollInterval; int pollRepeats = timeoutSec * 1000 / pollInterval;
int pollCounter = 0; int pollCounter = 0;
@ -152,16 +152,16 @@ public class FsMQManager {
} }
} }
String serializedMsg = onwQueue.remove(); String serializedMsg = onwQueue.remove();
mqMsg = FsMQMessage.deserialize(serializedMsg); mqMsg = FsMQMessageInternal.deserialize(serializedMsg);
} while (doHandshakeProtocol(mqMsg)); } while (doHandshakeProtocol(mqMsg));
ret = mqMsg.msg; ret = mqMsg.toFsMQMessage();
return ret; return ret;
} }
// True if existing // True if existing
// False if not // False if not
// Exception on not unique // Exception on not unique
public boolean identityExists(String address) { public boolean identityExists(String address) throws IllegalStateException, NullPointerException{
boolean ret = false; boolean ret = false;
if (address != null) { if (address != null) {
List<FsMQIdentity> matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()); List<FsMQIdentity> matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList());
@ -178,14 +178,14 @@ public class FsMQManager {
} }
// // Return null if not existing // Returns null if not existing
// public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException { public FsMQIdentity getIdentityForAddress(String address) {
// FsMQIdentity ret = null; FsMQIdentity ret = null;
// if (identityExists(address)) { if (identityExists(address)) {
// ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0); ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0);
// } }
// return ret; return ret;
// } }
public List<FsMQIdentity> getIdentities() { public List<FsMQIdentity> getIdentities() {
return new ArrayList<FsMQIdentity>(identities); return new ArrayList<FsMQIdentity>(identities);
@ -225,7 +225,6 @@ public class FsMQManager {
return ret; return ret;
} }
// public void handshake(FsMQIdentity ident) { // public void handshake(FsMQIdentity ident) {
// String msg = ""; // String msg = "";
// sendSYN(ident); // sendSYN(ident);
@ -236,7 +235,7 @@ public class FsMQManager {
// sendACK(ident); // sendACK(ident);
// } // }
private boolean doHandshakeProtocol(FsMQMessage msg) { private boolean doHandshakeProtocol(FsMQMessageInternal msg) {
boolean ret = false; boolean ret = false;
// //
// if(msg.matches(SYNMSG)) { // if(msg.matches(SYNMSG)) {
@ -263,21 +262,11 @@ public class FsMQManager {
} }
class FsMQMessage implements java.io.Serializable { class FsMQMessageInternal extends FsMQMessage implements java.io.Serializable {
FsMQIdentity from = null;
FsMQHandshakeHeader header = null; FsMQHandshakeHeader header = null;
String msg = null;
FsMQMessage(FsMQIdentity from, String msg) throws IllegalStateException { FsMQMessageInternal(FsMQIdentity from, String msg) throws IllegalStateException {
if (from == null || msg == null) { super(from,msg);
throw new IllegalStateException("from and msg cant be null");
}
this.from = from;
this.msg = msg;
}
public String getMsg() {
return msg;
} }
public FsMQHandshakeHeader getHeader() { public FsMQHandshakeHeader getHeader() {
@ -288,16 +277,22 @@ class FsMQMessage implements java.io.Serializable {
this.header = header; this.header = header;
} }
public static FsMQMessage deserialize(String serializedMsg) throws IOException, ClassNotFoundException { public FsMQMessage toFsMQMessage() throws NullPointerException {
FsMQMessage ret = null; FsMQMessage ret = new FsMQMessage(this.getFrom(),this.getMsg());
return ret;
}
public static FsMQMessageInternal deserialize(String serializedMsg) throws IOException, ClassNotFoundException {
FsMQMessageInternal ret = null;
byte[] data = Base64.getDecoder().decode(serializedMsg); byte[] data = Base64.getDecoder().decode(serializedMsg);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object obj = ois.readObject(); Object obj = ois.readObject();
ois.close(); ois.close();
if (!(obj instanceof FsMQMessage)) { if (!(obj instanceof FsMQMessageInternal)) {
throw new ClassNotFoundException("Unvalid serialized string"); throw new ClassNotFoundException("Invalid serialized string");
} else { } else {
ret = (FsMQMessage) obj; ret = (FsMQMessageInternal) obj;
} }
return ret; return ret;
} }

30
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQMessage.java

@ -0,0 +1,30 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager;
public class FsMQMessage implements java.io.Serializable {
private FsMQIdentity from = null;
private String msg = null;
public FsMQMessage(FsMQIdentity from, String msg) throws NullPointerException {
if (from == null || msg == null) {
throw new IllegalStateException("from and msg cant be null");
}
this.from = from;
this.msg = msg;
}
public FsMQIdentity getFrom() {
return from;
}
public String getMsg() {
return msg;
}
@Override
public String toString() {
String ret = "";
ret += "from: '" + from.getAddress() + "'\n";
ret += "msg : '" + msg + "'";
return ret;
}
}

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

@ -6,5 +6,6 @@ JAVA=java -enableassertions
JAVA_CLASSES_FSMSGQUEUE= \ JAVA_CLASSES_FSMSGQUEUE= \
../../FsMQManager.class \ ../../FsMQManager.class \
../../FsMQIdentity.class \ ../../FsMQIdentity.class \
../../FsMQMessage.class \
../../UnknownIdentityException.class \ ../../UnknownIdentityException.class \
../../../fsmsgqueue/FsMsgQueue.class ../../../fsmsgqueue/FsMsgQueue.class

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

@ -66,10 +66,6 @@ class TestMain {
assert ctx.qm != null : "null"; assert ctx.qm != null : "null";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
assert ctx.qm.identityExists(ctx.ownAddress);
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities(); List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) { for (FsMQIdentity i : idents) {
@ -79,7 +75,7 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
assert ctx.qm.identityExists(ctx.self.getAddress()) : "Own identity uknown"; assert ctx.qm.identityExists(ctx.self.getAddress()) : "Own identity unknown";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> {
@ -204,13 +200,27 @@ class TestMain {
assert idents.size() == 2 : "identity count wrong"; assert idents.size() == 2 : "identity count wrong";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("cant remove own ident", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("cant remove own ident", testCtx, ctx -> {
ctx.qm.removeIdentity(ctx.self.getAddress()); ctx.qm.removeIdentity(ctx.self.getAddress());
assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; assert ctx.qm.getIdentities().size() == 2 : "identity count wrong";
assert ctx.qm.identityExists(ctx.self.getAddress()) : "removed own identity"; assert ctx.qm.identityExists(ctx.self.getAddress()) : "removed own identity";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("getIdentForAddr" + testCtx.bobAddress, testCtx, ctx -> {
FsMQIdentity found = ctx.qm.getIdentityForAddress(ctx.bob.getAddress());
assert found != null :"failed to find known address";
assert found.getAddress().equals(ctx.bob.getAddress()) :"found wrong ident";
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentForAdd" + testCtx.ownAddress, testCtx, ctx -> {
FsMQIdentity found = ctx.qm.getIdentityForAddress(ctx.self.getAddress());
assert found != null :"failed to find known address";
assert found.getAddress().equals(ctx.self.getAddress()) :"found wrong ident";
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentityForAddress not existing", testCtx, ctx -> {
assert ctx.qm.getIdentityForAddress("UNKNOWN") == null : "Found an unknown address";
}).add();
new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {
ctx.qm.clearOwnQueue(); ctx.qm.clearOwnQueue();
@ -219,7 +229,7 @@ class TestMain {
new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> {
log("waitForMessage with timeout..."); log("waitForMessage with timeout...");
try { try {
ctx.qm.receiveMessage(3); ctx.qm.receiveMessage(1);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.toString()); throw new RuntimeException(e.toString());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -240,14 +250,14 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> {
String msg = null; FsMQMessage msg = null;
try { try {
msg = ctx.qm.receiveMessage(10); msg = ctx.qm.receiveMessage(10);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e.toString()); throw new RuntimeException(e.toString());
} }
log("RX MSG: " + msg); log("RX MSG: \n" + msg.toString());
assert msg.equals(ctx.messages.get(0)) : "message content mismatch"; assert msg.getMsg().equals(ctx.messages.get(0)) : "message content mismatch";
}).add(); }).add();

Loading…
Cancel
Save