
12 changed files with 373 additions and 253 deletions
@ -0,0 +1,175 @@ |
|||
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager; |
|||
|
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.FsMsgQueue; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.stream.Collectors; |
|||
|
|||
public class FsMQIdentities { |
|||
public FsMQIdentity self = null; |
|||
private List<FsMQIdentity> identities = new ArrayList<>(); |
|||
private Map<String, FsMsgQueue> identityAddressQueues = new HashMap<String, FsMsgQueue>(); |
|||
|
|||
public FsMQIdentities(FsMQIdentity self) throws NullPointerException { |
|||
if (self != null) { |
|||
this.self = self; |
|||
addOrUpdate(self); |
|||
} else { |
|||
throw new NullPointerException("self cant be null"); |
|||
} |
|||
} |
|||
|
|||
// Identity address must be unique
|
|||
// Returns
|
|||
// - true for added
|
|||
// - false for updated or own ident (which cant be updated)
|
|||
public boolean addOrUpdate(FsMQIdentity ident) throws NullPointerException { |
|||
boolean ret = false; |
|||
if (ident != null) { |
|||
if (addIdent(ident)) { |
|||
// Good, add new ident
|
|||
ret = true; |
|||
} else { |
|||
// Ok, update ident
|
|||
update(ident); |
|||
ret = false; |
|||
} |
|||
} else { |
|||
throw new NullPointerException("ident cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// cant update own identity
|
|||
// True - Success
|
|||
// False - ident not existing or own identity
|
|||
public boolean update(FsMQIdentity ident) throws NullPointerException { |
|||
boolean ret = false; |
|||
if (ident != null) { |
|||
if (!isSelf(ident.getAddress()) && exists(ident.getAddress())) { |
|||
remove(ident.getAddress()); |
|||
addIdent(ident); |
|||
ret = true; |
|||
} |
|||
} else { |
|||
throw new NullPointerException("ident cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// Removes the identity from identities and identityQueues by address
|
|||
public boolean remove(String address) throws NullPointerException { |
|||
boolean ret = false; |
|||
if (address != null) { |
|||
if (exists(address) && !isSelf(address)) { |
|||
identities.removeIf(i -> i.getAddress().equals(address)); |
|||
identityAddressQueues.entrySet().removeIf(iq -> iq.getKey().equals(address)); |
|||
ret = true; |
|||
} |
|||
} else { |
|||
throw new NullPointerException("address cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// cant fail haha
|
|||
public void removeAll() { |
|||
for (FsMQIdentity i : getAll()) { |
|||
remove(i.getAddress()); |
|||
} |
|||
} |
|||
|
|||
// Returns number of identities added
|
|||
public int addAll(List<FsMQIdentity> idents) throws NullPointerException { |
|||
int ret = 0; |
|||
if (idents != null) { |
|||
for (FsMQIdentity i : idents) { |
|||
if (addOrUpdate(i)) { |
|||
ret++; |
|||
} |
|||
} |
|||
} else { |
|||
throw new NullPointerException("idents cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public boolean isSelf(String address) throws NullPointerException { |
|||
boolean ret = false; |
|||
if (address != null) { |
|||
ret = self.getAddress() == address; |
|||
} else { |
|||
throw new NullPointerException("address cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// True if existing
|
|||
// False if not
|
|||
// Exception on not unique
|
|||
public boolean exists(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()); |
|||
if (matches.size() > 1) { |
|||
throw new IllegalStateException("Internal Error: Identity address not unique: " + address); |
|||
} |
|||
if (matches.size() == 1) { |
|||
ret = true; |
|||
} |
|||
} else { |
|||
throw new NullPointerException("address cant be null"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// Returns null if not existing
|
|||
public FsMQIdentity getByAddress(String address) { |
|||
FsMQIdentity ret = null; |
|||
if (exists(address)) { |
|||
ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public List<FsMQIdentity> getAll() { |
|||
return new ArrayList<FsMQIdentity>(identities); |
|||
} |
|||
|
|||
public List<String> getAddresses() { |
|||
List<String> ret = new ArrayList<>(); |
|||
for (FsMQIdentity i : identities) { |
|||
ret.add(i.getAddress()); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
// True - success
|
|||
// False - already existing
|
|||
private boolean addIdent(FsMQIdentity ident) { |
|||
boolean ret = false; |
|||
if (!exists(ident.getAddress())) { |
|||
identities.add(ident); |
|||
createQueueForIdent(ident); |
|||
ret = true; |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
private void createQueueForIdent(FsMQIdentity ident) { |
|||
FsMsgQueue q = new FsMsgQueue(ident.getqDir()); |
|||
identityAddressQueues.put(ident.getAddress(), q); |
|||
} |
|||
|
|||
public FsMsgQueue getQueueForIdentity(String address) throws UnknownIdentityException { |
|||
FsMsgQueue ret = null; |
|||
ret = identityAddressQueues.get(address); |
|||
if (ret == null) { |
|||
throw new UnknownIdentityException("Unknown identity address: " + address); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,19 @@ |
|||
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.ctx; |
|||
|
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.FsMQManager; |
|||
|
|||
public class FsMQManagerTestContext extends FsMQManagerBaseTestContext { |
|||
public FsMQManager qm; |
|||
|
|||
public FsMQManagerTestContext(String selfAddress) { |
|||
super(selfAddress); |
|||
} |
|||
|
|||
@Override |
|||
public void init() throws Throwable { |
|||
super.init(); |
|||
qm = new FsMQManager(self); |
|||
qm.identities.addAll(peerList); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,61 @@ |
|||
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.stateless_rxtx_mp; |
|||
|
|||
import static foundation.pEp.jniadapter.test.framework.TestLogger.*; |
|||
|
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*; |
|||
import foundation.pEp.jniadapter.test.framework.*; |
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.ctx.*; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
class TestBob { |
|||
public static void main(String[] args) throws Exception { |
|||
TestSuite.getDefault().setVerbose(true); |
|||
TestSuite.getDefault().setTestColor(TestUtils.TermColor.YELLOW); |
|||
String myself = "Bob"; |
|||
FsMQManagerTestContext testCtx = new FsMQManagerTestContext(myself); |
|||
|
|||
new TestUnit<FsMQManagerTestContext>("I am: " + myself, testCtx, ctx -> { |
|||
log("I am: " + ctx.self.getAddress()); |
|||
assert ctx.self.getAddress().equals(myself); |
|||
}); |
|||
|
|||
new TestUnit<FsMQManagerTestContext>("I know Alice and Carol", testCtx, ctx -> { |
|||
log("I know:"); |
|||
log("QM"); |
|||
for (FsMQIdentity ident : ctx.qm.identities.getAll()) { |
|||
log(ident.toString()); |
|||
} |
|||
}); |
|||
|
|||
new TestUnit<FsMQManagerTestContext>("Clear own queue", testCtx, ctx -> { |
|||
ctx.qm.clearOwnQueue(); |
|||
}); |
|||
|
|||
new TestUnit<FsMQManagerTestContext>("PingPong", testCtx, ctx -> { |
|||
try { |
|||
FsMQMessage msg; |
|||
while((msg = ctx.qm.receiveMessage(5)) != null) { |
|||
// RX
|
|||
String fromStr = msg.getFrom().getAddress(); |
|||
String msgRx = msg.getMsg(); |
|||
log("RX From: " + fromStr); |
|||
log(msgRx); |
|||
|
|||
// TX
|
|||
String toStr = fromStr; |
|||
String msgTx = ctx.getMessages().get(0) + msgRx; |
|||
log("TX to:" + toStr); |
|||
log(msgTx); |
|||
ctx.qm.sendMessage(fromStr,msgTx); |
|||
} |
|||
} catch (IOException e) { |
|||
assert false :e.toString(); |
|||
} catch (ClassNotFoundException e) { |
|||
assert false : e.toString(); |
|||
} |
|||
}); |
|||
|
|||
TestSuite.getDefault().run(); |
|||
} |
|||
} |
Loading…
Reference in new issue