Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
84d3478
Continue to StubNetwork
akucukoduk16 Mar 11, 2022
1ecfaf4
TestUnicastOneToAll_Sequentially implemented
akucukoduk16 Mar 11, 2022
29ec7da
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 11, 2022
c52b338
wip
thep2p Mar 11, 2022
27fae9a
Merge remote-tracking branch 'origin/master' into abdullah/stubnetwor…
thep2p Mar 11, 2022
e506da7
implements test for two engines
thep2p Mar 11, 2022
cc6109c
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 14, 2022
2160b51
adds concurrent test
thep2p Mar 14, 2022
6c438d9
adds lock
thep2p Mar 14, 2022
70b1da8
Some tests Implemented
akucukoduk16 Mar 15, 2022
6caf033
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 17, 2022
28a45a8
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 17, 2022
aacee37
Merge remote-tracking branch 'origin/abdullah/stubnetwork_extended_te…
akucukoduk16 Mar 17, 2022
02f3bc9
deletion of prints
akucukoduk16 Mar 17, 2022
e0e1750
deletion of prints
akucukoduk16 Mar 17, 2022
318326a
deletion of prints
akucukoduk16 Mar 17, 2022
7972de0
Continue to StubNetwork
akucukoduk16 Mar 11, 2022
7c7479a
TestUnicastOneToAll_Sequentially implemented
akucukoduk16 Mar 11, 2022
2013671
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 11, 2022
b6f48da
wip
thep2p Mar 11, 2022
18c4e78
implements test for two engines
thep2p Mar 11, 2022
b63acf9
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 14, 2022
0878ecd
adds concurrent test
thep2p Mar 14, 2022
7b098d6
adds lock
thep2p Mar 14, 2022
3485624
Some tests Implemented
akucukoduk16 Mar 15, 2022
7f2bc73
TestUnicastOneToAll_Concurrently Implemented
akucukoduk16 Mar 17, 2022
c9287a7
deletion of prints
akucukoduk16 Mar 17, 2022
47fa113
deletion of prints
akucukoduk16 Mar 17, 2022
d369b9d
deletion of prints
akucukoduk16 Mar 17, 2022
c4d0a2a
rebase
akucukoduk16 Mar 17, 2022
cd8b2a3
merge
akucukoduk16 Mar 17, 2022
6fdbb24
TestUnicastOneToAll_Sequentially implementations
akucukoduk16 Mar 17, 2022
be56fa8
TestUnicastOneToSome_Sequentially implementation
akucukoduk16 Mar 17, 2022
6fdb87c
TestUnicastOneToSome_Sequentially implementation
akucukoduk16 Mar 17, 2022
0b9d3ff
TestUnicastOneToSome_Concurrently implementation
akucukoduk16 Mar 18, 2022
8216a90
TestUnicastOneToAll_Sequentially_TwoEngines implementation
akucukoduk16 Mar 18, 2022
552d0a9
TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages implementation
akucukoduk16 Mar 18, 2022
08222a7
TestTwoStubNetworks_FourEngines_ConcurrentMessages implementation
akucukoduk16 Mar 18, 2022
bd63e34
TestRegisterToOccupiedChannel implementation
akucukoduk16 Mar 18, 2022
fd616fb
Javadoc modification
akucukoduk16 Mar 18, 2022
ee4eeb1
Merge remote-tracking branch 'origin/master' into abdullah/stubnetwor…
thep2p Mar 25, 2022
90f7985
Import reorganize
akucukoduk16 Mar 25, 2022
244b55b
Cl changes
Mar 26, 2022
8bd9069
Cl changes
Mar 26, 2022
e952cff
Cl changes
Mar 26, 2022
849a4b8
Merge branch 'master' into abdullah/stubnetwork_extended_test
thep2p Mar 28, 2022
98bfb5b
Merge branch 'master' into abdullah/stubnetwork_extended_test
thep2p Apr 2, 2022
dd0b643
applies revisions
thep2p Apr 4, 2022
3fc5bed
StubNetworkTest is fixed
Apr 5, 2022
67e84e4
Merge branch 'master' into abdullah/stubnetwork_extended_test
thep2p Apr 9, 2022
0cacff9
Merge remote-tracking branch 'origin/master' into abdullah/stubnetwor…
thep2p Apr 10, 2022
f7f1c97
StubNetworkTest Identifier fixed.
Apr 10, 2022
809ab18
applies revision
thep2p Apr 10, 2022
0d7b720
applies revisions
thep2p Apr 11, 2022
465d3ad
applies revisions
thep2p Apr 11, 2022
bd1c971
applies revisions
thep2p Apr 12, 2022
ef7a7da
fixes lint
thep2p Apr 12, 2022
196ebbd
applies revision
thep2p Apr 12, 2022
a9b9f5a
fixes lint
thep2p Apr 12, 2022
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,8 @@
/**
* Represents a runtime exception happens on the Networking layer of LightChain.
*/
public class LightChainNetworkingException extends Exception{ }
public class LightChainNetworkingException extends Exception {
public LightChainNetworkingException(String message, Throwable cause) {
super(message, cause);
}
}
41 changes: 41 additions & 0 deletions src/main/java/network/NetworkAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package network;

import model.Entity;
import model.exceptions.LightChainDistributedStorageException;
import model.exceptions.LightChainNetworkingException;
import model.lightchain.Identifier;

/**
* NetworkAdapter models the interface that is exposed to the conduits from the networking layer.
*/
public interface NetworkAdapter {
/**
* Sends the Entity through the Network to the remote target.
*
* @param e the Entity to be sent over the network.
* @param target Identifier of the receiver.
* @param channel channel on which this entity is sent.
* @throws LightChainNetworkingException any unhappy path taken on sending the Entity.
*/
void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException;

/**
* Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes.
*
* @param e the Entity to be stored over the network.
* @param namespace namespace on which this entity is stored.
* @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity.
*/
void put(Entity e, String namespace) throws LightChainDistributedStorageException;

/**
* Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table
* (DHT) of nodes.
*
* @param identifier identifier of the entity to be retrieved.
* @param namespace the namespace on which this query is resolved.
* @return the retrieved entity or null if it does not exist.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity.
*/
Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException;
}
51 changes: 48 additions & 3 deletions src/test/java/networking/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,51 @@
* Models the core communication part of the networking layer that allows stub network instances to talk to each other.
*/
public class Hub {
private ConcurrentHashMap<Identifier, Network> networks;
private ConcurrentHashMap<Identifier, Entity> entities;
}
private final ConcurrentHashMap<Identifier, Network> networks;
private final ConcurrentHashMap<Identifier, Entity> entities;

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

/**
* Registeration of a network to the Hub.
*
* @param identifier identifier of network.
* @param network to be registered.
*/
public void registerNetwork(Identifier identifier, Network network) {
networks.put(identifier, network);
}

/**
* Transfer entity from to another network on the same channel.
*
* @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 {
StubNetwork net = this.getNetwork(target);
try {
net.receiveUnicast(entity, channel);
} catch (IllegalArgumentException ex) {
throw new IllegalStateException("target network failed on receiving unicast: " + ex.getMessage());
}

}

/**
* Get the network with identifier.
*
* @param identifier identity of network.
* @return network corresponding to identifier.
*/
private StubNetwork getNetwork(Identifier identifier) {
return (StubNetwork) networks.get(identifier);
}
}
59 changes: 59 additions & 0 deletions src/test/java/networking/MockConduit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package networking;

import model.Entity;
import model.exceptions.LightChainDistributedStorageException;
import model.exceptions.LightChainNetworkingException;
import model.lightchain.Identifier;
import network.Conduit;
import network.NetworkAdapter;

/**
* MockConduit represents the Networking interface that is exposed to an Engine.
*/
public class MockConduit implements Conduit {

private final String channel;
private final NetworkAdapter networkAdapter;

public MockConduit(String channel, NetworkAdapter adapter) {
this.channel = channel;
this.networkAdapter = adapter;
}

/**
* Sends the Entity through the Network to the remote target.
*
* @param e the Entity to be sent over the network.
* @param target Identifier of the receiver.
* @throws LightChainNetworkingException any unhappy path taken on sending the Entity.
*/
@Override
public void unicast(Entity e, Identifier target) throws LightChainNetworkingException {
this.networkAdapter.unicast(e, target, channel);
}

/**
* Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes.
*
* @param e the Entity to be stored over the network.
* @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity.
*/
@Override
public void put(Entity e) throws LightChainDistributedStorageException {

}

/**
* Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table
* (DHT) of nodes.
*
* @param identifier identifier of the entity to be retrieved.
* @return the retrieved entity or null if it does not exist.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity.
*/
@Override
public Entity get(Identifier identifier) throws LightChainDistributedStorageException {
return null;
}

}
54 changes: 51 additions & 3 deletions src/test/java/networking/MockEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package networking;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import model.Entity;
import model.lightchain.Identifier;
Expand All @@ -10,10 +12,56 @@
* Represents a mock implementation of Engine interface for testing.
*/
public class MockEngine implements Engine {
private Set<Identifier> receivedEntityIds;
private final ReentrantReadWriteLock lock;
private final Set<Identifier> receivedEntityIds;

public MockEngine() {
this.receivedEntityIds = new HashSet<>();
this.lock = new ReentrantReadWriteLock();
}

/**
* Called by Network whenever an Entity is arrived for this engine.
*
* @param e the arrived Entity from the network.
* @throws IllegalArgumentException any unhappy path taken on processing the Entity.
*/

@Override
public void process(Entity e) throws IllegalArgumentException {
// TODO: put e.Id() in the set.
lock.writeLock().lock();

receivedEntityIds.add(e.id());

lock.writeLock().unlock();
}

/**
* Check whether an entity is received.
*
* @param e the entity.
* @return true if the entity received, otherwise false.
*/
public boolean hasReceived(Entity e) {
lock.readLock().lock();

boolean ok = this.receivedEntityIds.contains(e.id());

lock.readLock().unlock();
return ok;
}

/**
* Total distinct entities this engine received.
*
* @return total messages it received.
*/
public int totalReceived() {
lock.readLock().lock();

int size = receivedEntityIds.size();

lock.readLock().unlock();
return size;
}
}
}
109 changes: 106 additions & 3 deletions src/test/java/networking/StubNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,127 @@

import java.util.concurrent.ConcurrentHashMap;

import model.Entity;
import model.exceptions.LightChainDistributedStorageException;
import model.exceptions.LightChainNetworkingException;
import model.lightchain.Identifier;
import network.Conduit;
import network.Network;
import network.NetworkAdapter;
import protocol.Engine;
import unittest.fixtures.IdentifierFixture;

/**
* A mock implementation of networking layer as a test util.
*/
public class StubNetwork implements Network {
public class StubNetwork implements Network, NetworkAdapter {
private final ConcurrentHashMap<String, Engine> engines;
private final Hub hub;
private final Identifier identifier;

/**
* Create stubNetwork.
*
* @param hub the hub which stubnetwork registered is.
*/
public StubNetwork(Hub hub) {
this.engines = new ConcurrentHashMap<>();
this.hub = hub;
this.identifier = IdentifierFixture.newIdentifier();
this.hub.registerNetwork(identifier, this);
}

/**
* Get the identifier of the stubnet.
*
* @return identifier.
*/
public Identifier id() {
return this.identifier;
}

/**
* Forward the incoming entity to the engine whose channel is given.
*
* @param entity received entity
* @param channel the channel through which the received entity is sent
*/
public void receiveUnicast(Entity entity, String channel) throws IllegalArgumentException {
Engine engine = getEngine(channel);
try {
engine.process(entity);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("could not process the entity", e);
}
}

/**
* Registers an Engine to the Network by providing it with a Conduit.
*
* @param en the Engine to be registered.
* @param channel the unique channel corresponding to the Engine.
* @return unique Conduit object created to connect the Network to the Engine.
* @throws IllegalStateException if the channel is already taken by another Engine.
*/
@Override
public Conduit register(Engine en, String channel) throws IllegalStateException {
Conduit conduit = new MockConduit(channel, this);
try {
if (engines.containsKey(channel)) {
throw new IllegalStateException();
}
engines.put(channel, en);
} catch (IllegalArgumentException ex) {
throw new IllegalStateException("could not register the engine");
}
return conduit;
}

public Engine getEngine(String ch) {
return engines.get(ch);
}

/**
* Sends the Entity through the Network to the remote target.
*
* @param e the Entity to be sent over the network.
* @param target Identifier of the receiver.
* @param channel channel on which this entity is sent.
* @throws LightChainNetworkingException any unhappy path taken on sending the Entity.
*/
@Override
public void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException {
try {
this.hub.transferEntity(e, target, channel);
} catch (IllegalStateException ex) {
throw new LightChainNetworkingException("stub network could not transfer entity", ex);
}

}

/**
* Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes.
*
* @param e the Entity to be stored over the network.
* @param namespace namespace on which this entity is stored.
* @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity.
*/
@Override
public void put(Entity e, String namespace) throws LightChainDistributedStorageException {

}

/**
* Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table
* (DHT) of nodes.
*
* @param identifier identifier of the entity to be retrieved.
* @param namespace the namespace on which this query is resolved.
* @return the retrieved entity or null if it does not exist.
* @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity.
*/
@Override
public Conduit register(Engine e, String channel) throws IllegalStateException {
public Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException {
return null;
}
}
}
Loading