From 6004e9b704ffdee226d4d4e0f095846ebb5f1c4d Mon Sep 17 00:00:00 2001 From: Jasper Nalbach Date: Sun, 16 Oct 2016 10:41:57 +0200 Subject: [PATCH 1/5] implemented multi level service paths --- .../las2peer/p2p/AliasConflictException.java | 15 ++ .../las2peer/p2p/AliasNotFoundException.java | 9 +- .../p2p/DuplicateServiceAliasException.java | 10 - .../i5/las2peer/p2p/ServiceAliasManager.java | 184 +++++++++++++++--- .../i5/las2peer/security/ServiceAgent.java | 4 +- .../las2peer/p2p/ServiceAliasManagerTest.java | 96 ++++++++- 6 files changed, 264 insertions(+), 54 deletions(-) create mode 100644 src/main/java/i5/las2peer/p2p/AliasConflictException.java delete mode 100644 src/main/java/i5/las2peer/p2p/DuplicateServiceAliasException.java diff --git a/src/main/java/i5/las2peer/p2p/AliasConflictException.java b/src/main/java/i5/las2peer/p2p/AliasConflictException.java new file mode 100644 index 000000000..972b9a8a5 --- /dev/null +++ b/src/main/java/i5/las2peer/p2p/AliasConflictException.java @@ -0,0 +1,15 @@ +package i5.las2peer.p2p; + +public class AliasConflictException extends Exception { + + private static final long serialVersionUID = 1L; + + public AliasConflictException(String message) { + super(message); + } + + public AliasConflictException(String message, Throwable reason) { + super(message, reason); + } + +} diff --git a/src/main/java/i5/las2peer/p2p/AliasNotFoundException.java b/src/main/java/i5/las2peer/p2p/AliasNotFoundException.java index 4d345a289..36ebbeefc 100644 --- a/src/main/java/i5/las2peer/p2p/AliasNotFoundException.java +++ b/src/main/java/i5/las2peer/p2p/AliasNotFoundException.java @@ -2,12 +2,13 @@ public class AliasNotFoundException extends Exception { - /** - * - */ private static final long serialVersionUID = 1L; - public AliasNotFoundException(String message, Exception reason) { + public AliasNotFoundException(String message) { + super(message); + } + + public AliasNotFoundException(String message, Throwable reason) { super(message, reason); } diff --git a/src/main/java/i5/las2peer/p2p/DuplicateServiceAliasException.java b/src/main/java/i5/las2peer/p2p/DuplicateServiceAliasException.java deleted file mode 100644 index 8e0468c50..000000000 --- a/src/main/java/i5/las2peer/p2p/DuplicateServiceAliasException.java +++ /dev/null @@ -1,10 +0,0 @@ -package i5.las2peer.p2p; - -public class DuplicateServiceAliasException extends Exception { - - /** - * - */ - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java index d94639550..26c4505fc 100644 --- a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java +++ b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java @@ -1,8 +1,9 @@ package i5.las2peer.p2p; +import i5.las2peer.api.exceptions.ArtifactNotFoundException; import i5.las2peer.api.exceptions.StorageException; -import i5.las2peer.logging.NodeObserver.Event; import i5.las2peer.persistency.Envelope; +import i5.las2peer.security.Agent; import i5.las2peer.security.AgentLockedException; import i5.las2peer.security.L2pSecurityException; import i5.las2peer.security.ServiceAgent; @@ -10,12 +11,36 @@ import i5.las2peer.tools.SerializationException; /** - * Responsible for mapping service aliases to service names + * Responsible for mapping service aliases to service names and resolving paths to service names. * */ public class ServiceAliasManager { + // TODO WEBCONNECTOR + // TODO DOCS + private static final String PREFIX = "SERVICE_ALIAS-"; + private static final int MAX_PATH_LEVEL = 10; + private static final String SEPERATOR = "/"; + private static final String BLANK = "BLANK"; + + public class AliasResolveResponse { + String serviceName; + int numMatchedParts; + + public AliasResolveResponse(String serviceName, int numMatchedParts) { + this.serviceName = serviceName; + this.numMatchedParts = numMatchedParts; + } + + public String getServiceName() { + return this.serviceName; + } + + public int getNumMatchedParts() { + return this.numMatchedParts; + } + } private Node node; @@ -24,54 +49,149 @@ public ServiceAliasManager(Node node) { } /** - * Stores service alias of a service + * Registers the service alias of the given service. * - * @param agent an unlocked UserAgent - * @param alias - * @throws AgentLockedException - * @throws DuplicateServiceAliasException + * @param agent an unlocked service agent + * @param alias an alias, optionally seperated by {@link #SEPERATOR} and not deeper than {@link #MAX_PATH_LEVEL}. + * @throws AgentLockedException if the service agent is locked + * @throws AliasConflictException if a conflict occurs (a prefix or whole alias is already registered) */ - public void registerServiceAlias(ServiceAgent agent, String alias) - throws AgentLockedException, DuplicateServiceAliasException { + public void registerServiceAlias(ServiceAgent agent, String alias) throws AgentLockedException, + AliasConflictException { if (agent.isLocked()) { throw new AgentLockedException("Only unlocked Agents can be registered!"); } - String content = agent.getServiceNameVersion().getName(); + // if no alias exists, simply return + if (alias == null) { + return; + } + + String serviceName = agent.getServiceNameVersion().getName(); + + // preprocess path + alias = preprocessPath(alias); + String[] split = alias.split(SEPERATOR); + if (split.length > MAX_PATH_LEVEL) { + new AliasConflictException("Alias is too long."); + } - try { // check if alias already exists - String currentContent = getServiceNameByAlias(alias); - if (!currentContent.equals(content)) { // if service name is not the same, it's an error - throw new DuplicateServiceAliasException(); + // check for conflicts + try { + String currentEntry = getEntry(alias); + if (!currentEntry.equals(serviceName)) { // if service name is not the same, it's an error + throw new AliasConflictException("Alias has already been taken."); } else { return; // otherwise we're done } - } catch (AliasNotFoundException e) { - // if no mapping exists, ignore and continue with creating a new mapping + } catch (StorageException | CryptoException | L2pSecurityException | SerializationException e) { + // alias can be registered + } + + // register prefixes as BLANK + int level = 0; + String currentKey = null; + while (level < split.length - 1) { + // construct key + if (currentKey == null) { + currentKey = split[level]; + } else { + currentKey += SEPERATOR + split[level]; + } + + String currentEntry = null; try { - Envelope envName = node.createEnvelope(PREFIX + alias.toLowerCase(), content, agent, - node.getAnonymous()); - node.storeEnvelope(envName, agent); - } catch (SerializationException | StorageException | IllegalArgumentException | CryptoException e2) { - node.observerNotice(Event.NODE_ERROR, "Envelope error while updating user list: " + e2); + currentEntry = getEntry(currentKey); + } catch (StorageException | CryptoException | L2pSecurityException | SerializationException e) { + } + + if (currentEntry != null && !currentEntry.equals(BLANK)) { + throw new AliasConflictException("A prefix of the given alias is already registered."); + } else if (currentEntry == null) { + try { + createEntry(agent, currentKey, BLANK); + } catch (IllegalArgumentException | StorageException | SerializationException | CryptoException e) { + throw new AliasConflictException("Storage error.", e); + } } + // else: there is already a BLANK, nothing to do + + // in the case of BLANK go one level deeper + level++; + } + + // register alias + try { + createEntry(agent, alias, serviceName); + } catch (IllegalArgumentException | StorageException | SerializationException | CryptoException e) { + throw new AliasConflictException("Storage error.", e); } } /** - * get a service name id by alias + * Resolves a path to a service alias. * - * @param alias - * @return - * @throws AliasNotFoundException + * @param path the path + * @return the service name + * @throws AliasNotFoundException if the path cannot be resolves to a service name */ - public String getServiceNameByAlias(String alias) throws AliasNotFoundException { - try { - Envelope env = node.fetchEnvelope(PREFIX + alias.toLowerCase()); - String content = (String) env.getContent(node.getAnonymous()); - return content; - } catch (StorageException | CryptoException | L2pSecurityException | SerializationException e) { - throw new AliasNotFoundException("Alias not found!", e); + public AliasResolveResponse resolvePathToServiceName(String path) throws AliasNotFoundException { + path = preprocessPath(path); + String[] split = path.split(SEPERATOR); + + int level = 0; + String currentKey = null; + while (level < split.length && level < MAX_PATH_LEVEL) { + // construct key + if (currentKey == null) { + currentKey = split[level]; + } else { + currentKey += SEPERATOR + split[level]; + } + + String currentEntry = null; + try { + currentEntry = getEntry(currentKey); + } catch (StorageException | CryptoException | L2pSecurityException | SerializationException e) { + throw new AliasNotFoundException("Path does not exist.", e); + } + + if (!currentEntry.equals(BLANK)) { + return new AliasResolveResponse(currentEntry, level + 1); + } + + // in the case of BLANK go one level deeper + level++; + } + + if (level == MAX_PATH_LEVEL) { + throw new AliasNotFoundException("Given path is too long."); + } + + throw new AliasNotFoundException("Given path does not fit any alias."); + } + + private String preprocessPath(String path) { + path = path.toLowerCase().trim(); + while (path.startsWith(SEPERATOR)) { + path = path.substring(1); + } + while (path.endsWith(SEPERATOR)) { + path = path.substring(0, path.length() - 2); } + return path; + } + + private String getEntry(String key) throws ArtifactNotFoundException, StorageException, CryptoException, + L2pSecurityException, SerializationException { + Envelope env = node.fetchEnvelope(PREFIX + key); + String content = (String) env.getContent(node.getAnonymous()); + return content; + } + + private void createEntry(Agent agent, String key, String value) throws StorageException, IllegalArgumentException, + SerializationException, CryptoException { + Envelope envName = node.createEnvelope(PREFIX + key.toLowerCase(), value, agent, node.getAnonymous()); + node.storeEnvelope(envName, agent); } } diff --git a/src/main/java/i5/las2peer/security/ServiceAgent.java b/src/main/java/i5/las2peer/security/ServiceAgent.java index eb79f34f0..3ea3dd5e9 100644 --- a/src/main/java/i5/las2peer/security/ServiceAgent.java +++ b/src/main/java/i5/las2peer/security/ServiceAgent.java @@ -27,7 +27,7 @@ import i5.las2peer.execution.ServiceInvocationException; import i5.las2peer.logging.NodeObserver.Event; import i5.las2peer.p2p.AgentNotKnownException; -import i5.las2peer.p2p.DuplicateServiceAliasException; +import i5.las2peer.p2p.AliasConflictException; import i5.las2peer.p2p.Node; import i5.las2peer.p2p.NodeNotFoundException; import i5.las2peer.p2p.ServiceNameVersion; @@ -431,7 +431,7 @@ public void notifyRegistrationTo(Node node) throws L2pServiceException { node.getServiceAliasManager().registerServiceAlias(this, alias); } catch (AgentLockedException e) { throw new L2pServiceException("Service alias could not be registered!", e); - } catch (DuplicateServiceAliasException e) { + } catch (AliasConflictException e) { throw new L2pServiceException("Service alias is already used by anther service!", e); } } diff --git a/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java b/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java index 006d421e5..0b09514b9 100644 --- a/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java +++ b/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java @@ -17,7 +17,7 @@ public void reset() { } @Test - public void test() throws CryptoException, L2pSecurityException, DuplicateServiceAliasException, + public void testDuplication() throws CryptoException, L2pSecurityException, AliasConflictException, AliasNotFoundException { LocalNode node = LocalNode.launchNode(); ServiceAgent agentA = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceA@1.0"), "asdf"); @@ -31,11 +31,11 @@ public void test() throws CryptoException, L2pSecurityException, DuplicateServic // regular creation node.getServiceAliasManager().registerServiceAlias(agentA, "aliasA"); - assertEquals(node.getServiceAliasManager().getServiceNameByAlias("aliasA"), "serviceA"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("aliasA").getServiceName(), "serviceA"); // second alias node.getServiceAliasManager().registerServiceAlias(agentB, "aliasB"); - assertEquals(node.getServiceAliasManager().getServiceNameByAlias("aliasB"), "serviceB"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("aliasB").getServiceName(), "serviceB"); // register second time, no conflict node.getServiceAliasManager().registerServiceAlias(agentA2, "aliasA"); @@ -43,11 +43,94 @@ public void test() throws CryptoException, L2pSecurityException, DuplicateServic // duplicate try { node.getServiceAliasManager().registerServiceAlias(agentC, "aliasA"); - fail("DuplicateServiceAliasException expected"); - } catch (DuplicateServiceAliasException e) { + fail("AliasConflictException expected"); + } catch (AliasConflictException e) { } } + @Test + public void testRegistering() throws CryptoException, L2pSecurityException, AliasConflictException, + AliasNotFoundException { + LocalNode node = LocalNode.launchNode(); + ServiceAgent agentA = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceA@1.0"), "asdf"); + agentA.unlockPrivateKey("asdf"); + ServiceAgent agentB = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceB@1.0"), "asdf"); + agentB.unlockPrivateKey("asdf"); + ServiceAgent agentC = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceC@1.0"), "asdf"); + agentC.unlockPrivateKey("asdf"); + ServiceAgent agentD = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceD@1.0"), "asdf"); + agentD.unlockPrivateKey("asdf"); + + // regular creation + node.getServiceAliasManager().registerServiceAlias(agentA, "prefix/prefix/aliasA"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasA").getServiceName(), + "serviceA"); + + // second alias + node.getServiceAliasManager().registerServiceAlias(agentB, "prefix/prefix/aliasB"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasB").getServiceName(), + "serviceB"); + + // existing alias is prefix of new one + try { + node.getServiceAliasManager().registerServiceAlias(agentC, "prefix/prefix/aliasA/aliasC"); + fail("AliasConflictException expected"); + } catch (AliasConflictException e) { + } + + // alias is prefix of existing ones + try { + node.getServiceAliasManager().registerServiceAlias(agentD, "prefix/prefix"); + fail("AliasConflictException expected"); + } catch (AliasConflictException e) { + } + } + + @Test + public void testResolve() throws CryptoException, L2pSecurityException, AliasConflictException, + AliasNotFoundException { + LocalNode node = LocalNode.launchNode(); + ServiceAgent agentA = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceA@1.0"), "asdf"); + agentA.unlockPrivateKey("asdf"); + ServiceAgent agentB = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceB@1.0"), "asdf"); + agentB.unlockPrivateKey("asdf"); + ServiceAgent agentC = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceC@1.0"), "asdf"); + agentC.unlockPrivateKey("asdf"); + ServiceAgent agentD = ServiceAgent.createServiceAgent(ServiceNameVersion.fromString("serviceD@1.0"), "asdf"); + agentD.unlockPrivateKey("asdf"); + + // register + node.getServiceAliasManager().registerServiceAlias(agentA, "prefix/prefix/aliasA"); + node.getServiceAliasManager().registerServiceAlias(agentB, "prefix/prefix/aliasB"); + node.getServiceAliasManager().registerServiceAlias(agentC, "prefix/aliasC"); + node.getServiceAliasManager().registerServiceAlias(agentD, "prefix/aliasD"); + + // resolve + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasA").getServiceName(), + "serviceA"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasA") + .getNumMatchedParts(), 3); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasB").getServiceName(), + "serviceB"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasB") + .getNumMatchedParts(), 3); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasC").getServiceName(), + "serviceC"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasC").getNumMatchedParts(), 2); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasD").getServiceName(), + "serviceD"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasD").getNumMatchedParts(), 2); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasA/asdf") + .getServiceName(), "serviceA"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/prefix/aliasA/asdf") + .getNumMatchedParts(), 3); + assertEquals( + node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasC/asdf/rtzh").getServiceName(), + "serviceC"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasC/asdf/rtzh") + .getNumMatchedParts(), 2); + } + @Test public void testIntegration() throws CryptoException, L2pSecurityException, AgentAlreadyRegisteredException, AgentException, AliasNotFoundException { @@ -58,6 +141,7 @@ public void testIntegration() throws CryptoException, L2pSecurityException, Agen node.registerReceiver(agentA); // regular creation - assertEquals(node.getServiceAliasManager().getServiceNameByAlias("test"), "i5.las2peer.api.TestService"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("test").getServiceName(), + "i5.las2peer.api.TestService"); } } From 217ddc37d8b8b062f8a0371a355b9f06a20dee1a Mon Sep 17 00:00:00 2001 From: Jasper Nalbach Date: Mon, 24 Oct 2016 16:29:11 +0200 Subject: [PATCH 2/5] fix superfluous logs --- src/main/java/i5/las2peer/p2p/ServiceAliasManager.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java index d94639550..29a8a390b 100644 --- a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java +++ b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java @@ -31,8 +31,8 @@ public ServiceAliasManager(Node node) { * @throws AgentLockedException * @throws DuplicateServiceAliasException */ - public void registerServiceAlias(ServiceAgent agent, String alias) - throws AgentLockedException, DuplicateServiceAliasException { + public void registerServiceAlias(ServiceAgent agent, String alias) throws AgentLockedException, + DuplicateServiceAliasException { if (agent.isLocked()) { throw new AgentLockedException("Only unlocked Agents can be registered!"); } @@ -68,7 +68,7 @@ public void registerServiceAlias(ServiceAgent agent, String alias) public String getServiceNameByAlias(String alias) throws AliasNotFoundException { try { Envelope env = node.fetchEnvelope(PREFIX + alias.toLowerCase()); - String content = (String) env.getContent(node.getAnonymous()); + String content = (String) env.getContent(node.getAnonymous(), node); return content; } catch (StorageException | CryptoException | L2pSecurityException | SerializationException e) { throw new AliasNotFoundException("Alias not found!", e); From 5a163d70df4b60f6c2cbc8e6404818626f363fab Mon Sep 17 00:00:00 2001 From: Jasper Nalbach Date: Mon, 31 Oct 2016 11:36:51 +0100 Subject: [PATCH 3/5] make service path implementation more robust --- .../i5/las2peer/p2p/ServiceAliasManager.java | 44 +++++++++---------- .../las2peer/p2p/ServiceAliasManagerTest.java | 6 +++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java index 114aee43b..9b088d056 100644 --- a/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java +++ b/src/main/java/i5/las2peer/p2p/ServiceAliasManager.java @@ -10,15 +10,16 @@ import i5.las2peer.tools.CryptoException; import i5.las2peer.tools.SerializationException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * Responsible for mapping service aliases to service names and resolving paths to service names. * */ public class ServiceAliasManager { - // TODO WEBCONNECTOR - // TODO DOCS - private static final String PREFIX = "SERVICE_ALIAS-"; private static final int MAX_PATH_LEVEL = 10; private static final String SEPERATOR = "/"; @@ -51,6 +52,8 @@ public ServiceAliasManager(Node node) { /** * Registers the service alias of the given service. * + * Note that a service alias cannot be a prefix of another service alias. + * * @param agent an unlocked service agent * @param alias an alias, optionally seperated by {@link #SEPERATOR} and not deeper than {@link #MAX_PATH_LEVEL}. * @throws AgentLockedException if the service agent is locked @@ -71,9 +74,9 @@ public void registerServiceAlias(ServiceAgent agent, String alias) throws AgentL String serviceName = agent.getServiceNameVersion().getName(); // preprocess path - alias = preprocessPath(alias); - String[] split = alias.split(SEPERATOR); - if (split.length > MAX_PATH_LEVEL) { + List split = splitPath(alias); + alias = String.join(SEPERATOR, split); + if (split.size() > MAX_PATH_LEVEL) { new AliasConflictException("Alias is too long."); } @@ -92,12 +95,12 @@ public void registerServiceAlias(ServiceAgent agent, String alias) throws AgentL // register prefixes as BLANK int level = 0; String currentKey = null; - while (level < split.length - 1) { + while (level < split.size() - 1) { // construct key if (currentKey == null) { - currentKey = split[level]; + currentKey = split.get(level); } else { - currentKey += SEPERATOR + split[level]; + currentKey += SEPERATOR + split.get(level); } String currentEntry = null; @@ -137,17 +140,16 @@ public void registerServiceAlias(ServiceAgent agent, String alias) throws AgentL * @throws AliasNotFoundException if the path cannot be resolves to a service name */ public AliasResolveResponse resolvePathToServiceName(String path) throws AliasNotFoundException { - path = preprocessPath(path); - String[] split = path.split(SEPERATOR); + List split = splitPath(path); int level = 0; String currentKey = null; - while (level < split.length && level < MAX_PATH_LEVEL) { + while (level < split.size() && level < MAX_PATH_LEVEL) { // construct key if (currentKey == null) { - currentKey = split[level]; + currentKey = split.get(level); } else { - currentKey += SEPERATOR + split[level]; + currentKey += SEPERATOR + split.get(level); } String currentEntry = null; @@ -172,15 +174,13 @@ public AliasResolveResponse resolvePathToServiceName(String path) throws AliasNo throw new AliasNotFoundException("Given path does not fit any alias."); } - private String preprocessPath(String path) { + private List splitPath(String path) { path = path.toLowerCase().trim(); - while (path.startsWith(SEPERATOR)) { - path = path.substring(1); - } - while (path.endsWith(SEPERATOR)) { - path = path.substring(0, path.length() - 2); - } - return path; + + ArrayList pathSplit = new ArrayList(Arrays.asList(path.split(SEPERATOR))); + pathSplit.removeIf(item -> item == null || "".equals(item)); + + return pathSplit; } private String getEntry(String key) throws ArtifactNotFoundException, StorageException, CryptoException, diff --git a/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java b/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java index 0b09514b9..60e98acea 100644 --- a/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java +++ b/src/test/java/i5/las2peer/p2p/ServiceAliasManagerTest.java @@ -129,6 +129,12 @@ public void testResolve() throws CryptoException, L2pSecurityException, AliasCon "serviceC"); assertEquals(node.getServiceAliasManager().resolvePathToServiceName("prefix/aliasC/asdf/rtzh") .getNumMatchedParts(), 2); + + // resolve with empty path parts + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("//prefix//aliasC//asdf//rtzh//") + .getServiceName(), "serviceC"); + assertEquals(node.getServiceAliasManager().resolvePathToServiceName("//prefix//aliasC//asdf//rtzh//") + .getNumMatchedParts(), 2); } @Test From a9e22ba9e83aea79bbc6c7c64e728921e3fac687 Mon Sep 17 00:00:00 2001 From: Jasper Nalbach Date: Wed, 2 Nov 2016 10:08:07 +0100 Subject: [PATCH 4/5] todo update --- src/main/java/i5/las2peer/p2p/Node.java | 107 +++++++++--------- .../i5/las2peer/p2p/PastryNodeImplTest.java | 8 +- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/main/java/i5/las2peer/p2p/Node.java b/src/main/java/i5/las2peer/p2p/Node.java index bbe7495a3..95108fb01 100644 --- a/src/main/java/i5/las2peer/p2p/Node.java +++ b/src/main/java/i5/las2peer/p2p/Node.java @@ -1,26 +1,5 @@ package i5.las2peer.p2p; -import java.io.File; -import java.io.Serializable; -import java.lang.management.ManagementFactory; -import java.lang.reflect.InvocationTargetException; -import java.security.KeyPair; -import java.security.PublicKey; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map.Entry; -import java.util.Set; -import java.util.Timer; -import java.util.TimerTask; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.Vector; - -import com.sun.management.OperatingSystemMXBean; - import i5.las2peer.api.exceptions.ArtifactNotFoundException; import i5.las2peer.api.exceptions.EnvelopeAlreadyExistsException; import i5.las2peer.api.exceptions.StorageException; @@ -47,9 +26,9 @@ import i5.las2peer.persistency.Envelope; import i5.las2peer.persistency.NodeStorageInterface; import i5.las2peer.security.Agent; +import i5.las2peer.security.AgentContext; import i5.las2peer.security.AgentException; import i5.las2peer.security.AgentStorage; -import i5.las2peer.security.AgentContext; import i5.las2peer.security.GroupAgent; import i5.las2peer.security.L2pSecurityException; import i5.las2peer.security.Mediator; @@ -64,10 +43,32 @@ import i5.las2peer.tools.CryptoException; import i5.las2peer.tools.CryptoTools; import i5.las2peer.tools.SerializationException; + +import java.io.File; +import java.io.Serializable; +import java.lang.management.ManagementFactory; +import java.lang.reflect.InvocationTargetException; +import java.security.KeyPair; +import java.security.PublicKey; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; + import rice.pastry.NodeHandle; import rice.pastry.PastryNode; import rice.pastry.socket.SocketNodeHandle; +import com.sun.management.OperatingSystemMXBean; + /** * Base class for nodes in the las2peer environment. * @@ -102,11 +103,13 @@ public enum NodeStatus { */ private OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory .getOperatingSystemMXBean(); - public static final double DEFAULT_CPU_LOAD_TRESHOLD = 0.5; - private double cpuLoadThreshold = DEFAULT_CPU_LOAD_TRESHOLD; // TODO: make it configurable + private NodeServiceCache nodeServiceCache; - // TODO make time as setting - // should be lowered in future, currently services don't change often + + // TODO make node parameters configurable: + public static final double DEFAULT_CPU_LOAD_TRESHOLD = 0.5; + private double cpuLoadThreshold = DEFAULT_CPU_LOAD_TRESHOLD; + // should be lowered in future, currently services don't change often: private int nodeServiceCacheLifetime = 60; // time before cached node info becomes invalidated private int nodeServiceCacheResultCount = 3; // number of service instances to be collected from the network private int tidyUpTimerInterval = 60; @@ -268,8 +271,8 @@ public void removeObserver(NodeObserver observer) { * @param service The service that should be monitored. */ public void setServiceMonitoring(ServiceAgent service) { - observerNotice(Event.SERVICE_ADD_TO_MONITORING, this.getNodeId(), service.getId(), null, null, - service.getServiceNameVersion().toString()); + observerNotice(Event.SERVICE_ADD_TO_MONITORING, this.getNodeId(), service.getId(), null, null, service + .getServiceNameVersion().toString()); } /** @@ -553,8 +556,8 @@ public synchronized void shutDown() { * @throws AgentException any problem with the agent itself (probably on calling * {@link i5.las2peer.security.Agent#notifyRegistrationTo} */ - public void registerReceiver(MessageReceiver receiver) - throws AgentAlreadyRegisteredException, L2pSecurityException, AgentException { + public void registerReceiver(MessageReceiver receiver) throws AgentAlreadyRegisteredException, + L2pSecurityException, AgentException { // TODO allow multiple mediators registered at the same time for one agent to avoid conflicts between connectors @@ -579,8 +582,8 @@ public void registerReceiver(MessageReceiver receiver) try { storeAgent(agent); } catch (AgentAlreadyRegisteredException e) { - System.out.println( - "Just for notice - not an error: tried to store an already known agent before registering"); + System.out + .println("Just for notice - not an error: tried to store an already known agent before registering"); // nothing to do } } @@ -813,8 +816,8 @@ public abstract void sendMessage(Message message, Object atNodeId, MessageResult * @throws NodeNotFoundException * @throws L2pSecurityException */ - public void sendResponse(Message message, Object atNodeId) - throws AgentNotKnownException, NodeNotFoundException, L2pSecurityException { + public void sendResponse(Message message, Object atNodeId) throws AgentNotKnownException, NodeNotFoundException, + L2pSecurityException { sendMessage(message, atNodeId, null); } @@ -1085,8 +1088,8 @@ public ServiceAgent[] getRegisteredServices() { * @throws L2pSecurityException * @throws AgentAlreadyRegisteredException */ - public Mediator createMediatorForAgent(Agent agent) - throws AgentNotKnownException, L2pSecurityException, AgentAlreadyRegisteredException { + public Mediator createMediatorForAgent(Agent agent) throws AgentNotKnownException, L2pSecurityException, + AgentAlreadyRegisteredException { if (agent.isLocked()) { throw new L2pSecurityException("You need to unlock the agent for mediation!"); } @@ -1111,8 +1114,8 @@ public Mediator createMediatorForAgent(Agent agent) * @throws L2pSecurityException * @throws AgentException */ - public abstract void storeAgent(Agent agent) - throws AgentAlreadyRegisteredException, L2pSecurityException, AgentException; + public abstract void storeAgent(Agent agent) throws AgentAlreadyRegisteredException, L2pSecurityException, + AgentException; /** * Updates an existing agent of the network. @@ -1271,8 +1274,8 @@ public Serializable invoke(Agent executing, ServiceNameVersion service, String m * @throws InterruptedException */ public Serializable invoke(Agent executing, ServiceNameVersion service, String method, Serializable[] parameters, - boolean exactVersion) - throws L2pSecurityException, AgentNotKnownException, L2pServiceException, InterruptedException { + boolean exactVersion) throws L2pSecurityException, AgentNotKnownException, L2pServiceException, + InterruptedException { return invoke(executing, service, method, parameters, exactVersion, false); } @@ -1293,8 +1296,8 @@ public Serializable invoke(Agent executing, ServiceNameVersion service, String m * @throws InterruptedException */ public Serializable invoke(Agent executing, ServiceNameVersion service, String method, Serializable[] parameters, - boolean exactVersion, boolean localOnly) - throws L2pSecurityException, AgentNotKnownException, L2pServiceException, InterruptedException { + boolean exactVersion, boolean localOnly) throws L2pSecurityException, AgentNotKnownException, + L2pServiceException, InterruptedException { if (getStatus() != NodeStatus.RUNNING) { throw new IllegalStateException("You can invoke methods only on a running node!"); @@ -1350,8 +1353,8 @@ public Serializable invoke(Agent executing, ServiceNameVersion service, String m * @throws L2pServiceException */ public Serializable invokeLocally(Agent executing, ServiceAgent serviceAgent, String method, - Serializable[] parameters) - throws L2pSecurityException, AgentNotKnownException, InterruptedException, L2pServiceException { + Serializable[] parameters) throws L2pSecurityException, AgentNotKnownException, InterruptedException, + L2pServiceException { if (getStatus() != NodeStatus.RUNNING) { throw new IllegalStateException("You can invoke methods only on a running node!"); @@ -1491,10 +1494,10 @@ public Serializable invokeGlobally(Agent executing, long serviceAgentId, Object return ((RMIResultContent) resultContent).getContent(); } else { // Do not log service class name (privacy..) - this.observerNotice(Event.RMI_FAILED, this.getNodeId(), executing, - "Unknown RMI response type: " + resultContent.getClass().getCanonicalName()); - throw new ServiceInvocationException( - "Unknown RMI response type: " + resultContent.getClass().getCanonicalName()); + this.observerNotice(Event.RMI_FAILED, this.getNodeId(), executing, "Unknown RMI response type: " + + resultContent.getClass().getCanonicalName()); + throw new ServiceInvocationException("Unknown RMI response type: " + + resultContent.getClass().getCanonicalName()); } } catch (AgentNotKnownException e) { // Do not log service class name (privacy..) @@ -1551,8 +1554,8 @@ public boolean handoverAnswer(Message answer) { return false; } - observerNotice(Event.MESSAGE_RECEIVED_ANSWER, answer.getSendingNodeId(), answer.getSenderId(), this.getNodeId(), - answer.getRecipientId(), "" + answer.getResponseToId()); + observerNotice(Event.MESSAGE_RECEIVED_ANSWER, answer.getSendingNodeId(), answer.getSenderId(), + this.getNodeId(), answer.getRecipientId(), "" + answer.getResponseToId()); MessageResultListener listener = htAnswerListeners.get(answer.getResponseToId()); if (listener == null) { @@ -1627,8 +1630,8 @@ public Message sendMessageAndWaitForAnswer(Message m, Object atNodeId) throws Ag * @throws InterruptedException * @throws TimeoutException */ - public Message[] sendMessageAndCollectAnswers(Message m, int recipientCount) - throws InterruptedException, TimeoutException { + public Message[] sendMessageAndCollectAnswers(Message m, int recipientCount) throws InterruptedException, + TimeoutException { long timeout = m.getTimeoutTs() - new Date().getTime(); MessageResultListener listener = new MessageResultListener(timeout, timeout / 4); listener.addRecipients(recipientCount); diff --git a/src/test/java/i5/las2peer/p2p/PastryNodeImplTest.java b/src/test/java/i5/las2peer/p2p/PastryNodeImplTest.java index d1e6a9a40..c7afc6b49 100755 --- a/src/test/java/i5/las2peer/p2p/PastryNodeImplTest.java +++ b/src/test/java/i5/las2peer/p2p/PastryNodeImplTest.java @@ -1,5 +1,8 @@ package i5.las2peer.p2p; +import i5.las2peer.security.UserAgent; +import i5.las2peer.testing.TestSuite; + import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; @@ -10,9 +13,6 @@ import org.junit.Assert; import org.junit.Test; -import i5.las2peer.security.UserAgent; -import i5.las2peer.testing.TestSuite; - public class PastryNodeImplTest { public final int NODE_COUNT = 5; public final int AGENTS_PER_NODE_COUNT = 5; @@ -53,7 +53,7 @@ public void doNothing() { } - // disabled, because it does not work in Jenkins... + // TODO disabled, because it does not work in Jenkins... /* @Before From 4ac9e51875d6705b36cea9c4626099def74fa59e Mon Sep 17 00:00:00 2001 From: Jasper Nalbach Date: Fri, 4 Nov 2016 11:55:19 +0100 Subject: [PATCH 5/5] increase build number --- build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.xml b/build.xml index e57aba85a..31c16c4db 100644 --- a/build.xml +++ b/build.xml @@ -5,7 +5,7 @@ - +