diff --git a/build.xml b/build.xml index 78bcf8376..b22a61dfb 100644 --- a/build.xml +++ b/build.xml @@ -5,7 +5,7 @@ - + diff --git a/src/main/java/i5/las2peer/p2p/LocalNode.java b/src/main/java/i5/las2peer/p2p/LocalNode.java index 586e12e09..7db2ecf6f 100644 --- a/src/main/java/i5/las2peer/p2p/LocalNode.java +++ b/src/main/java/i5/las2peer/p2p/LocalNode.java @@ -21,9 +21,8 @@ import i5.las2peer.persistency.LocalStorage; import i5.las2peer.persistency.MalformedXMLException; import i5.las2peer.security.Agent; -import i5.las2peer.security.AgentException; -import i5.las2peer.security.BasicAgentStorage; import i5.las2peer.security.AgentContext; +import i5.las2peer.security.AgentException; import i5.las2peer.security.L2pSecurityException; import i5.las2peer.security.MessageReceiver; import i5.las2peer.security.UserAgent; @@ -39,8 +38,6 @@ */ public class LocalNode extends Node { - private BasicAgentStorage locallyKnownAgents; - /** * an id for this node */ @@ -55,8 +52,6 @@ private LocalNode() { Random r = new Random(); nodeId = r.nextLong(); - locallyKnownAgents = new BasicAgentStorage(this); - setStatus(NodeStatus.CONFIGURED); } @@ -71,8 +66,6 @@ private LocalNode(L2pClassManager classManager) { Random r = new Random(); nodeId = r.nextLong(); - locallyKnownAgents = new BasicAgentStorage(this); - setStatus(NodeStatus.CONFIGURED); } @@ -304,11 +297,6 @@ public void updateAgent(Agent agent) throws AgentException, L2pSecurityException } } - @Override - public boolean knowsAgentLocally(long agentId) { - return locallyKnownAgents.hasAgent(agentId); - } - @Override public Object[] getOtherKnownNodes() { return htLocalNodes.values().toArray(); diff --git a/src/main/java/i5/las2peer/p2p/Node.java b/src/main/java/i5/las2peer/p2p/Node.java index 95108fb01..39c4c40bf 100644 --- a/src/main/java/i5/las2peer/p2p/Node.java +++ b/src/main/java/i5/las2peer/p2p/Node.java @@ -1,5 +1,26 @@ 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; @@ -29,6 +50,7 @@ import i5.las2peer.security.AgentContext; import i5.las2peer.security.AgentException; import i5.las2peer.security.AgentStorage; +import i5.las2peer.security.BasicAgentStorage; import i5.las2peer.security.GroupAgent; import i5.las2peer.security.L2pSecurityException; import i5.las2peer.security.Mediator; @@ -43,32 +65,10 @@ 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. * @@ -158,6 +158,7 @@ public enum NodeStatus { private String sInformationFileName = DEFAULT_INFORMATION_FILE; private KeyPair nodeKeyPair; + protected BasicAgentStorage locallyKnownAgents; /** * maps names and emails to UserAgents @@ -225,6 +226,8 @@ public Node(L2pClassManager baseClassLoader, boolean standardObserver, boolean m nodeKeyPair = CryptoTools.generateKeyPair(); nodeServiceCache = new NodeServiceCache(this, nodeServiceCacheLifetime, nodeServiceCacheResultCount); + locallyKnownAgents = new BasicAgentStorage(this); + locallyKnownAgents.registerAgent(getAnonymous()); userManager = new UserAgentManager(this); aliasManager = new ServiceAliasManager(this); @@ -271,8 +274,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()); } /** @@ -556,8 +559,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 @@ -582,8 +585,8 @@ public void registerReceiver(MessageReceiver receiver) throws AgentAlreadyRegist 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 } } @@ -816,8 +819,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); } @@ -1021,7 +1024,9 @@ public boolean hasAgent(long id) { * @param agentId * @return true, if this agent is (already) known here at this node */ - public abstract boolean knowsAgentLocally(long agentId); + public boolean knowsAgentLocally(long agentId) { + return locallyKnownAgents.hasAgent(agentId); + } /** * Gets a local registered agent by its id. @@ -1088,8 +1093,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!"); } @@ -1114,8 +1119,8 @@ public Mediator createMediatorForAgent(Agent agent) throws AgentNotKnownExceptio * @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. @@ -1137,15 +1142,10 @@ public abstract void storeAgent(Agent agent) throws AgentAlreadyRegisteredExcept public Agent getAnonymous() { if (anonymousAgent == null) { try { - anonymousAgent = getAgent(MockAgentFactory.getAnonymous().getId()); - } catch (Exception e) { - try { - anonymousAgent = MockAgentFactory.getAnonymous(); - ((UserAgent) anonymousAgent).unlockPrivateKey("anonymous"); - storeAgent(anonymousAgent); - } catch (Exception e1) { - throw new RuntimeException("No anonymous agent could be initialized!?!", e1); - } + anonymousAgent = MockAgentFactory.getAnonymous(); + ((UserAgent) anonymousAgent).unlockPrivateKey("anonymous"); + } catch (Exception e1) { + throw new RuntimeException("No anonymous agent could be initialized!?!", e1); } } @@ -1274,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); } @@ -1296,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!"); @@ -1353,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!"); @@ -1494,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..) @@ -1554,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) { @@ -1630,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/main/java/i5/las2peer/p2p/PastryNodeImpl.java b/src/main/java/i5/las2peer/p2p/PastryNodeImpl.java index 644476c14..dc9fee173 100644 --- a/src/main/java/i5/las2peer/p2p/PastryNodeImpl.java +++ b/src/main/java/i5/las2peer/p2p/PastryNodeImpl.java @@ -38,7 +38,6 @@ import i5.las2peer.security.Agent; import i5.las2peer.security.AgentContext; import i5.las2peer.security.AgentException; -import i5.las2peer.security.BasicAgentStorage; import i5.las2peer.security.L2pSecurityException; import i5.las2peer.security.MessageReceiver; import i5.las2peer.security.UserAgent; @@ -82,7 +81,6 @@ public class PastryNodeImpl extends Node { private STORAGE_MODE mode = STORAGE_MODE.FILESYSTEM; private String storageDir; // null = default choosen by SharedStorage private Long nodeIdSeed; - private BasicAgentStorage locallyKnownAgents; /** * This is the regular constructor used by the {@link i5.las2peer.tools.L2pNodeLauncher}. Its parameters can be set @@ -106,7 +104,6 @@ public PastryNodeImpl(L2pClassManager classLoader, boolean useMonitoringObserver this.mode = storageMode; this.storageDir = null; // null = SharedStorage chooses directory this.nodeIdSeed = nodeIdSeed; - locallyKnownAgents = new BasicAgentStorage(this); setupPastryEnvironment(); this.setStatus(NodeStatus.CONFIGURED); } @@ -139,7 +136,6 @@ public PastryNodeImpl(String bootstrap, STORAGE_MODE storageMode, String storage this.mode = storageMode; this.storageDir = storageDir; this.nodeIdSeed = nodeIdSeed; - locallyKnownAgents = new BasicAgentStorage(this); setupPastryEnvironment(); this.setStatus(NodeStatus.CONFIGURED); } @@ -512,11 +508,6 @@ public Object[] findRegisteredAgent(long agentId, int hintOfExpectedCount) throw return application.searchAgent(agentId, hintOfExpectedCount).toArray(); } - @Override - public boolean knowsAgentLocally(long agentId) { - return locallyKnownAgents.hasAgent(agentId); - } - /** * provides access to the underlying pastry application mostly for testing purposes * @@ -528,19 +519,21 @@ public NodeApplication getApplication() { @Override public Agent getAgent(long id) throws AgentNotKnownException { - if (!locallyKnownAgents.hasAgent(id)) { + if (locallyKnownAgents.hasAgent(id)) { + return locallyKnownAgents.getAgent(id); + } else { observerNotice(Event.AGENT_GET_STARTED, pastryNode, id, null, (Long) null, ""); try { Envelope agentEnvelope = pastStorage.fetchEnvelope(Envelope.getAgentIdentifier(id), AGENT_GET_TIMEOUT); Agent agentFromNet = Agent.createFromXml((String) agentEnvelope.getContent()); observerNotice(Event.AGENT_GET_SUCCESS, pastryNode, id, null, (Long) null, ""); locallyKnownAgents.registerAgent(agentFromNet); + return agentFromNet; } catch (Exception e) { observerNotice(Event.AGENT_GET_FAILED, pastryNode, id, null, (Long) null, ""); throw new AgentNotKnownException("Unable to retrieve Agent " + id + " from past storage", e); } } - return locallyKnownAgents.getAgent(id); } @Override @@ -565,7 +558,7 @@ public void storeAgent(Agent agent) throws L2pSecurityException, AgentException try { agentEnvelope = pastStorage.fetchEnvelope(Envelope.getAgentIdentifier(agent.getId()), AGENT_GET_TIMEOUT); - agentEnvelope = pastStorage.createUnencryptedEnvelope(agentEnvelope, agentEnvelope.toXmlString()); + agentEnvelope = pastStorage.createUnencryptedEnvelope(agentEnvelope, agent.toXmlString()); } catch (ArtifactNotFoundException e) { agentEnvelope = pastStorage.createUnencryptedEnvelope(Envelope.getAgentIdentifier(agent.getId()), agent.toXmlString()); diff --git a/src/main/java/i5/las2peer/security/UserAgentManager.java b/src/main/java/i5/las2peer/security/UserAgentManager.java index 62c103c04..229c52b02 100644 --- a/src/main/java/i5/las2peer/security/UserAgentManager.java +++ b/src/main/java/i5/las2peer/security/UserAgentManager.java @@ -96,6 +96,9 @@ public void updateUserAgent(UserAgent agent) * @throws AgentNotKnownException */ public long getAgentIdByLogin(String name) throws AgentNotKnownException, L2pSecurityException { + if (name.equalsIgnoreCase("anonymous")) { + return node.getAnonymous().getId(); + } try { Envelope env = node.fetchEnvelope(PREFIX_USER_NAME + name.toLowerCase()); return (Long) env.getContent();