Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
/**
* Represents a runtime exception happens on distributed storage layer of LightChain.
*/
public class LightChainDistributedStorageException extends Exception{}
public class LightChainDistributedStorageException extends Exception {
public LightChainDistributedStorageException(String s) {
}
}
11 changes: 11 additions & 0 deletions src/main/java/network/NetworkAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package network;

import java.util.ArrayList;

import model.Entity;
import model.exceptions.LightChainDistributedStorageException;
import model.exceptions.LightChainNetworkingException;
Expand Down Expand Up @@ -38,4 +40,13 @@ public interface NetworkAdapter {
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity.
*/
Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException;

/**
* Retrieves all entities stored on the underlying DHT of nodes that stored on this channel.
*
* @param namespace the namespace on which this query is resolved.
* @return list of all entities stored on this channel from underlying DHT.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entities.
*/
ArrayList<Entity> allEntities(String namespace) throws LightChainDistributedStorageException;
}
6 changes: 3 additions & 3 deletions src/test/java/networking/MockConduit.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void unicast(Entity e, Identifier target) throws LightChainNetworkingExce
*/
@Override
public void put(Entity e) throws LightChainDistributedStorageException {

this.networkAdapter.put(e, channel);
}

/**
Expand All @@ -66,12 +66,12 @@ public void put(Entity e) throws LightChainDistributedStorageException {
*/
@Override
public Entity get(Identifier identifier) throws LightChainDistributedStorageException {
return null;
return this.networkAdapter.get(identifier, channel);
}

@Override
public ArrayList<Entity> allEntities() throws LightChainDistributedStorageException {
return null;
return this.networkAdapter.allEntities(channel);
}

public boolean hasSent(Identifier entityId) {
Expand Down
80 changes: 77 additions & 3 deletions src/test/java/networking/stub/Hub.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,103 @@
package networking.stub;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import model.Entity;
import model.exceptions.LightChainDistributedStorageException;
import model.lightchain.Identifier;
import network.Network;

/**
* Models the core communication part of the networking layer that allows stub network instances to talk to each other.
*/
public class Hub {
private final ReentrantReadWriteLock lock;
private final String channel1 = "test-network-channel-1";
private final String channel2 = "test-network-channel-2";
private final ConcurrentHashMap<Identifier, Network> networks;
private final ConcurrentHashMap<Identifier, Entity> channelMap1;
private final ConcurrentHashMap<Identifier, Entity> channelMap2;

/**
* Create a hub.
*/
public Hub() {
this.networks = new ConcurrentHashMap<>();
this.channelMap1 = new ConcurrentHashMap<>();
this.channelMap2 = new ConcurrentHashMap<>();
this.lock = new ReentrantReadWriteLock();
}

/**
* Put Entity to channel.
*
* @param e entitiy.
* @param namespace channel name.
* @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity.
*/
public void putEntityToChannel(Entity e, String namespace) throws LightChainDistributedStorageException {
try {
lock.writeLock().lock();
if (namespace.equals(channel1)) {
channelMap1.put(e.id(), e);
} else if (namespace.equals(channel2)) {
channelMap2.put(e.id(), e);
} else {
throw new LightChainDistributedStorageException("entity could not be put the given channel");
}
} finally {
lock.writeLock().unlock();
}
}

/**
* Get entity from channel.
*
* @param identifier of entity.
* @param namespace channel name.
* @return entity.
* @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity.
*/
public Entity getEntityFromChannel(Identifier identifier, String namespace)
throws LightChainDistributedStorageException {
try {
lock.readLock().lock();
if (namespace.equals(channel1)) {
return channelMap1.get(identifier);
} else if (namespace.equals(channel2)) {
return channelMap2.get(identifier);
} else {
throw new LightChainDistributedStorageException("could not get the entity");
}
} finally {
lock.readLock().unlock();
}
}

/**
* Retrieves all entities stored on the underlying DHT of nodes that stored on this channel.
*
* @param namespace the namespace on which this query is resolved.
* @return list of all entities stored on this channel from underlying DHT.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entities.
*/
public ArrayList<Entity> getAllEntities(String namespace) throws LightChainDistributedStorageException {
if (namespace.equals(channel1)) {
return new ArrayList<>(channelMap1.values());
} else if (namespace.equals(channel2)) {
return new ArrayList<>(channelMap2.values());
} else {
throw new LightChainDistributedStorageException("could not get the entities");
}
}

/**
* Registeration of a network to the Hub.
*
* @param identifier identifier of network.
* @param network to be registered.
* @param network to be registered.
*/
public void registerNetwork(Identifier identifier, Network network) {
networks.put(identifier, network);
Expand All @@ -32,8 +106,8 @@ public void registerNetwork(Identifier identifier, Network network) {
/**
* Transfer entity from to another network on the same channel.
*
* @param entity entity to be transferred.
* @param target identifier of target.
* @param entity entity to be transferred.
* @param target identifier of target.
* @param channel channel on which the entity is delivered to target.
*/
public void transferEntity(Entity entity, Identifier target, String channel) throws IllegalStateException {
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/networking/stub/StubNetwork.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package networking.stub;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

import model.Entity;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void unicast(Entity e, Identifier target, String channel) throws LightCha
*/
@Override
public void put(Entity e, String namespace) throws LightChainDistributedStorageException {

this.hub.putEntityToChannel(e, namespace);
}

/**
Expand All @@ -124,6 +125,18 @@ public void put(Entity e, String namespace) throws LightChainDistributedStorageE
*/
@Override
public Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException {
return null;
return this.hub.getEntityFromChannel(identifier, namespace);
}

/**
* Retrieves all entities stored on the underlying DHT of nodes that stored on this channel.
*
* @param namespace the namespace on which this query is resolved.
* @return list of all entities stored on this channel from underlying DHT.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entities.
*/
@Override
public ArrayList<Entity> allEntities(String namespace) throws LightChainDistributedStorageException {
return this.hub.getAllEntities(namespace);
}
}
Loading