Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #23

Merged
merged 5 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<property name="ivy.organisation" value="i5" />
<property name="ivy.module" value="las2peer" />
<property name="ivy.revision" value="0.6" />
<property name="ivy.build.number" value="3" />
<property name="ivy.build.number" value="4" />
<property name="ivy.deliver.revision" value="${ivy.revision}" />
<property name="ivy.pom.version" value="${ivy.revision}" />

Expand Down
14 changes: 1 addition & 13 deletions src/main/java/i5/las2peer/p2p/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,8 +38,6 @@
*/
public class LocalNode extends Node {

private BasicAgentStorage locallyKnownAgents;

/**
* an id for this node
*/
Expand All @@ -55,8 +52,6 @@ private LocalNode() {
Random r = new Random();
nodeId = r.nextLong();

locallyKnownAgents = new BasicAgentStorage(this);

setStatus(NodeStatus.CONFIGURED);
}

Expand All @@ -71,8 +66,6 @@ private LocalNode(L2pClassManager classManager) {
Random r = new Random();
nodeId = r.nextLong();

locallyKnownAgents = new BasicAgentStorage(this);

setStatus(NodeStatus.CONFIGURED);
}

Expand Down Expand Up @@ -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();
Expand Down
116 changes: 58 additions & 58 deletions src/main/java/i5/las2peer/p2p/Node.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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

Expand All @@ -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
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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!");
}
Expand All @@ -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.
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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!");
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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..)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/i5/las2peer/p2p/PastryNodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/i5/las2peer/security/UserAgentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down