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

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

@ -66,10 +66,6 @@ class TestMain {
assert ctx.qm != null : "null";
}).add();
new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
assert ctx.qm.identityExists(ctx.ownAddress);
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) {
@ -79,7 +75,7 @@ class TestMain {
}).add();
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();
new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> {
@ -134,11 +130,11 @@ class TestMain {
new TestUnit<FsMQManagerTestContext>("Update ident " + testCtx.carolAddress, testCtx, ctx -> {
ctx.carol.setqDir(ctx.carolQDir);
assert ctx.qm.updateIdentity(ctx.carol): "Error updating ident";
assert ctx.qm.updateIdentity(ctx.carol) : "Error updating ident";
}).add();
new TestUnit<FsMQManagerTestContext>("Update ownIdent Fails " + testCtx.carolAddress, testCtx, ctx -> {
assert !ctx.qm.updateIdentity(ctx.self): "upadted own ident";
assert !ctx.qm.updateIdentity(ctx.self) : "upadted own ident";
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
@ -204,13 +200,27 @@ class TestMain {
assert idents.size() == 2 : "identity count wrong";
}).add();
new TestUnit<FsMQManagerTestContext>("cant remove own ident", testCtx, ctx -> {
ctx.qm.removeIdentity(ctx.self.getAddress());
assert ctx.qm.getIdentities().size() == 2 : "identity count wrong";
assert ctx.qm.identityExists(ctx.self.getAddress()) : "removed own identity";
}).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 -> {
ctx.qm.clearOwnQueue();
@ -219,7 +229,7 @@ class TestMain {
new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> {
log("waitForMessage with timeout...");
try {
ctx.qm.receiveMessage(3);
ctx.qm.receiveMessage(1);
} catch (IOException e) {
throw new RuntimeException(e.toString());
} catch (ClassNotFoundException e) {
@ -240,14 +250,14 @@ class TestMain {
}).add();
new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> {
String msg = null;
FsMQMessage msg = null;
try {
msg = ctx.qm.receiveMessage(10);
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
log("RX MSG: " + msg);
assert msg.equals(ctx.messages.get(0)) : "message content mismatch";
log("RX MSG: \n" + msg.toString());
assert msg.getMsg().equals(ctx.messages.get(0)) : "message content mismatch";
}).add();

Loading…
Cancel
Save