Skip to content

Commit

Permalink
changed time for gridnode package for testing purpuses
Browse files Browse the repository at this point in the history
  • Loading branch information
Fim-84 committed Mar 18, 2024
1 parent 79e19a4 commit 3f3dfea
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ UGD Software AB (org. nr: 559339-5824)

package org.unigrid.hedgehog.model.gridnode;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -31,7 +32,7 @@ UGD Software AB (org. nr: 559339-5824)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Gridnode {
public class GridnodeData {

@AllArgsConstructor
public enum Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void getHeartbeatData() {
CDIUtil.resolveAndRun(Topology.class, topology -> {

Collateral collateralCalculator = new Collateral();
final Set<Gridnode> gridnodes = topology.cloneGridnode();
final Set<GridnodeData> gridnodes = topology.cloneGridnode();
int numNodes = gridnodes.size();
log.atDebug().log("Heartbeat");
log.atDebug().log("number of node on the network " + numNodes);
Expand All @@ -96,13 +96,12 @@ public void getHeartbeatData() {
pubKeys.add(k.getPublicKeyAsHex());
});

gridnodes.removeIf(gridnode -> gridnode.getStatus() == Gridnode.Status.ACTIVE
gridnodes.removeIf(gridnode -> gridnode.getStatus() == GridnodeData.Status.ACTIVE
&& pubKeys.contains(gridnode.getId()));
});

gridnodes.forEach(gridnode -> gridnode.setStatus(Gridnode.Status.INACTIVE));
gridnodes.forEach(gridnode -> gridnode.setStatus(GridnodeData.Status.INACTIVE));
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ UGD Software AB (org. nr: 559339-5824)
import org.unigrid.hedgehog.model.Network;
import org.unigrid.hedgehog.model.cdi.Lock;
import org.unigrid.hedgehog.model.cdi.Protected;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.packet.Packet;

@Slf4j
@ApplicationScoped
public class Topology {
private HashSet<Node> nodes;
private HashSet<Gridnode> gridnodes;
private HashSet<GridnodeData> gridnodes;

@Inject
@Getter private ChannelMap channels;
Expand All @@ -55,7 +55,7 @@ private void init() {
repopulate();
gridnodes = new HashSet<>();
if (!StringUtils.isEmpty(GridnodeOptions.getGridnodeKey())) {
gridnodes.add(Gridnode.builder().id(GridnodeOptions.getGridnodeKey())
gridnodes.add(GridnodeData.builder().id(GridnodeOptions.getGridnodeKey())
.hostName(NetOptions.getHost() + ":" + NetOptions.getPort()).build());
}
}
Expand Down Expand Up @@ -136,23 +136,23 @@ public static void sendAll(Packet packet, Topology topology, Optional<BiConsumer
}

@Protected @Lock(LockMode.WRITE)
public void modifyGridnode(Gridnode gridnode, Consumer<Gridnode> consumer) {
public void modifyGridnode(GridnodeData gridnode, Consumer<GridnodeData> consumer) {
gridnodes.forEach(g -> {
if (gridnode.equals(g)) {
consumer.accept(g);
}
});
}

public void changeGridnodeStatus(String gridnodeId, Gridnode.Status status) {
Gridnode gridnode = Gridnode.builder().id(gridnodeId).status(status).build();
public void changeGridnodeStatus(String gridnodeId, GridnodeData.Status status) {
GridnodeData gridnode = GridnodeData.builder().id(gridnodeId).status(status).build();
modifyGridnode(gridnode, (g) -> {
g.setStatus(gridnode.getStatus());
});
}

@Protected @Lock(LockMode.WRITE)
public boolean addGridnode(Gridnode gridnode) {
public boolean addGridnode(GridnodeData gridnode) {
if (!gridnodes.contains(gridnode)) {
return gridnodes.add(gridnode);
}
Expand All @@ -161,7 +161,7 @@ public boolean addGridnode(Gridnode gridnode) {
}

@Protected @Lock(LockMode.WRITE)
public boolean removeGridnode(Gridnode gridnode) {
public boolean removeGridnode(GridnodeData gridnode) {
if (!gridnodes.contains(gridnode)) {
return gridnodes.remove(gridnode);
}
Expand All @@ -170,7 +170,7 @@ public boolean removeGridnode(Gridnode gridnode) {
}

@Protected @Lock(LockMode.READ)
public Set<Gridnode> cloneGridnode() {
public Set<GridnodeData> cloneGridnode() {
return new HashSet(gridnodes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ UGD Software AB (org. nr: 559339-5824)
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.unigrid.hedgehog.model.crypto.NetworkIdentifier;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.Topology;
import org.unigrid.hedgehog.model.network.codec.api.PacketDecoder;
import org.unigrid.hedgehog.model.network.packet.PublishGridnode;
Expand Down Expand Up @@ -54,8 +54,8 @@ public Optional<PublishGridnode> typedDecode(ChannelHandlerContext ctx, ByteBuf
final String hostName = ByteBufUtils.readNullTerminatedString(in);
log.atDebug().log("decode gridnode");

gridnodePacket.setGridnode(Gridnode.builder().hostName(hostName)
.id(gridnodeId).status(Gridnode.Status.get(gridnodeStatus)).build());
gridnodePacket.setGridnode(GridnodeData.builder().hostName(hostName)
.id(gridnodeId).status(GridnodeData.Status.get(gridnodeStatus)).build());

return Optional.of(gridnodePacket);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ UGD Software AB (org. nr: 559339-5824)
import lombok.extern.slf4j.Slf4j;
import org.unigrid.hedgehog.command.option.GridnodeOptions;
import org.unigrid.hedgehog.model.cdi.CDIUtil;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.Topology;
import static org.unigrid.hedgehog.model.network.handler.ConnectionHandler.SOCKET_ADDRESS_KEY;
import org.unigrid.hedgehog.model.network.packet.PublishGridnode;
Expand All @@ -45,7 +45,7 @@ public PublishGridnodeChannelHandler() {
@Override
public void typedChannelRead(ChannelHandlerContext ctx, PublishGridnode obj) throws Exception {
CDIUtil.resolveAndRun(Topology.class, topology -> {
Set<Gridnode> gridnodes = topology.cloneGridnode();
Set<GridnodeData> gridnodes = topology.cloneGridnode();
boolean isEmpty = true;
//System.out.println(gridnodes.size());

Expand All @@ -62,14 +62,14 @@ public void typedChannelRead(ChannelHandlerContext ctx, PublishGridnode obj) thr
}
}

for (Gridnode g: gridnodes) {
for (GridnodeData g: gridnodes) {
if (g.getId().equals(obj.getGridnode().getId())) {
isEmpty = false;
}
}

if (isEmpty) {
topology.addGridnode(Gridnode.builder().id(obj.getGridnode().getId())
topology.addGridnode(GridnodeData.builder().id(obj.getGridnode().getId())
.status(obj.getGridnode().getStatus())
.hostName(obj.getGridnode().getHostName()).build());
Topology.sendAll(PublishGridnode.builder().gridnode(obj.getGridnode()).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ UGD Software AB (org. nr: 559339-5824)
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;

@Data
@Builder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PublishGridnode extends Packet implements Serializable {

public static final int DISTRIBUTION_FREQUENCY_MINUTES = 5;
public static final int DISTRIBUTION_FREQUENCY_MINUTES = 2;

@Builder.Default private Gridnode gridnode = Gridnode.builder().build();
@Builder.Default private GridnodeData gridnode = GridnodeData.builder().build();

public PublishGridnode() {
setType(Packet.Type.GRIDNODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ UGD Software AB (org. nr: 559339-5824)
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.unigrid.hedgehog.model.cdi.CDIUtil;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.Topology;
import org.unigrid.hedgehog.model.network.packet.PublishGridnode;

Expand All @@ -44,7 +44,7 @@ public PublishGridnodeSchedule() {
public Consumer<Channel> getConsumer() {
return channel -> {
CDIUtil.resolveAndRun(Topology.class, topology -> {
final Set<Gridnode> gridnodesToSend = topology.cloneGridnode();
final Set<GridnodeData> gridnodesToSend = topology.cloneGridnode();

gridnodesToSend.forEach((g) -> {
log.atTrace().log("Sending gridnode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ UGD Software AB (org. nr: 559339-5824)
import org.unigrid.hedgehog.model.network.Topology;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.gridnode.HeartbeatData;
import org.unigrid.hedgehog.model.network.ActivateGridnode;
import org.unigrid.hedgehog.model.network.packet.PublishGridnode;
Expand All @@ -67,11 +67,11 @@ public class GridnodeResource extends CDIBridgeResource {
@GET @Path("/collateral")
public Response get() {
// get number of gridnodes and calculate colleteral
Set<Gridnode> gridnodes = topology.cloneGridnode();
Set<GridnodeData> gridnodes = topology.cloneGridnode();
int count = 0;

for (Gridnode g : gridnodes) {
if (g.getStatus() == Gridnode.Status.ACTIVE) {
for (GridnodeData g : gridnodes) {
if (g.getStatus() == GridnodeData.Status.ACTIVE) {
count++;
}
}
Expand All @@ -83,7 +83,7 @@ public Response get() {
@GET
@SneakyThrows
public Response list() {
Set<Gridnode> gridnodes = topology.cloneGridnode();
Set<GridnodeData> gridnodes = topology.cloneGridnode();

String json = "";
ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -113,12 +113,12 @@ public Response put(@NotNull ActivateGridnode gridnode,
byte[] messageBytes = gridnode.getGridnodeId().getBytes();
byte[] signBytes = Base64.getDecoder().decode(sign);
if (GridnodeKey.verifySignature(messageBytes, signBytes, pubKey)) {
final Set<Gridnode> gridnodes = topology.cloneGridnode();
final Set<GridnodeData> gridnodes = topology.cloneGridnode();
gridnodes.forEach(g -> {
if (g.getId().equals(gridnode.getGridnodeId())) {
log.atTrace().log("Activating node with id {}", g.getId());
topology.modifyGridnode(g, n -> {
n.setStatus(Gridnode.Status.ACTIVE);
n.setStatus(GridnodeData.Status.ACTIVE);
});
Topology.sendAll(PublishGridnode.builder().gridnode(g).build(),
topology, Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ UGD Software AB (org. nr: 559339-5824)
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.unigrid.hedgehog.jqwik.ArbitraryGenerator;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;

public class GridnodeIntegrityTest extends BaseCodecTest<PublishGridnode>{
@Mocked
Expand All @@ -52,7 +52,7 @@ public Arbitrary<PublishGridnode> provideGridnodePacket(@ForAll @AlphaChars @Str

final PublishGridnode gp = PublishGridnode.builder().build();
final String ip = ArbitraryGenerator.ip4();
final Gridnode gridnode = Gridnode.builder().id(gridnodeKey).hostName(ip + ":" + port).build();
final GridnodeData gridnode = GridnodeData.builder().id(gridnodeKey).hostName(ip + ":" + port).build();
gp.setGridnode(gridnode);

return Arbitraries.of(gp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ UGD Software AB (org. nr: 559339-5824)
import static org.hamcrest.Matchers.*;
import org.unigrid.hedgehog.client.P2PClient;
import org.unigrid.hedgehog.command.option.GridnodeOptions;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.Connection;
import org.unigrid.hedgehog.model.network.Node;
import org.unigrid.hedgehog.model.network.Topology;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void shoulPropagateGridnodeToNetwork(@ForAll("provideTestServers") List<T
final int port = server.getP2p().getPort();
final Connection connection = new P2PClient(host, port);
final ECKey key = new ECKey();
Gridnode gridnode = Gridnode.builder().hostName(host + ":" + port).id(key.getPublicKeyAsHex())
GridnodeData gridnode = GridnodeData.builder().hostName(host + ":" + port).id(key.getPublicKeyAsHex())
.build();
topology.addNode(Node.builder().address(new InetSocketAddress(host, port))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ UGD Software AB (org. nr: 559339-5824)
import static org.hamcrest.Matchers.*;
import org.unigrid.hedgehog.client.P2PClient;
import org.unigrid.hedgehog.jqwik.ArbitraryGenerator;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.network.Connection;
import org.unigrid.hedgehog.model.network.Node;
import org.unigrid.hedgehog.model.network.Topology;
Expand Down Expand Up @@ -85,8 +85,8 @@ public void provideActiveGridnode(@ForAll Family family,
node = Node.fromAddress((family == Family.IP4 ? "%s:%d" : "[%s]:%d").formatted(address, port));
}
topology.addNode(node);
Gridnode gridnode = Gridnode.builder().hostName(node.getAddress().getHostName()).id(key)
.status(Gridnode.Status.ACTIVE).build();
GridnodeData gridnode = GridnodeData.builder().hostName(node.getAddress().getHostName()).id(key)
.status(GridnodeData.Status.ACTIVE).build();
topology.addGridnode(gridnode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ UGD Software AB (org. nr: 559339-5824)
import org.unigrid.hedgehog.model.crypto.GridnodeKey;
import org.unigrid.hedgehog.model.crypto.Signature;
import org.unigrid.hedgehog.model.gridnode.Delegations;
import org.unigrid.hedgehog.model.gridnode.Gridnode;
import org.unigrid.hedgehog.model.gridnode.GridnodeData;
import org.unigrid.hedgehog.model.gridnode.HeartbeatData;
import org.unigrid.hedgehog.model.network.Topology;
import org.unigrid.hedgehog.model.network.schedule.PublishGridnodeScheduleTest;
Expand Down Expand Up @@ -89,7 +89,7 @@ private enum Family {

@Provide
@SneakyThrows
public Arbitrary<Gridnode> provideGridnode(@ForAll Family family,
public Arbitrary<GridnodeData> provideGridnode(@ForAll Family family,
@ForAll @IntRange(min = 1024, max = 65535) int port) {

String address = switch (family) {
Expand All @@ -105,7 +105,7 @@ public Arbitrary<Gridnode> provideGridnode(@ForAll Family family,
node = Node.fromAddress((family == Family.IP4 ? "%s:%d" : "[%s]:%d").formatted(address, port));
}
ECKey key = new ECKey();
Gridnode gridnode = Gridnode.builder().hostName(node.getAddress().getHostName())
GridnodeData gridnode = GridnodeData.builder().hostName(node.getAddress().getHostName())
.id(key.getPublicKeyAsHex()).build();
topology.addNode(node);
topology.addGridnode(gridnode);
Expand All @@ -129,8 +129,8 @@ public void provideActiveGridnode(@ForAll Family family,
node = Node.fromAddress((family == Family.IP4 ? "%s:%d" : "[%s]:%d").formatted(address, port));
}

Gridnode gridnode = Gridnode.builder().hostName(node.getAddress().getHostName()).id(key)
.status(Gridnode.Status.ACTIVE).build();
GridnodeData gridnode = GridnodeData.builder().hostName(node.getAddress().getHostName()).id(key)
.status(GridnodeData.Status.ACTIVE).build();
topology.addNode(node);
topology.addGridnode(gridnode);
}
Expand All @@ -152,7 +152,7 @@ public void provideInactiveGridnode(@ForAll Family family,
node = Node.fromAddress((family == Family.IP4 ? "%s:%d" : "[%s]:%d").formatted(address, port));
}

Gridnode gridnode = Gridnode.builder().hostName(node.getAddress().getHostName()).id(key)
GridnodeData gridnode = GridnodeData.builder().hostName(node.getAddress().getHostName()).id(key)
.build();
topology.addNode(node);
topology.addGridnode(gridnode);
Expand Down Expand Up @@ -295,10 +295,10 @@ public void shouldRemoveIllegalNodes(@ForAll("provideSignature") Signature signa

@Property(tries = 10, shrinking = ShrinkingMode.OFF)
public void shouldReturnBaseCost() {
Set<Gridnode> gridnodes = topology.cloneGridnode();
Set<GridnodeData> gridnodes = topology.cloneGridnode();
int count = 0;
for(Gridnode g : gridnodes){
if(g.getStatus() == Gridnode.Status.ACTIVE) {
for(GridnodeData g : gridnodes){
if(g.getStatus() == GridnodeData.Status.ACTIVE) {
count++;
}
}
Expand All @@ -321,7 +321,7 @@ public void shouldReturnBaseCost() {
}

@Example
public void shouldActivateGridnode(@ForAll("provideGridnode") Gridnode node) {
public void shouldActivateGridnode(@ForAll("provideGridnode") GridnodeData node) {
try {
//ECKey key = keys.get(0);
String s = "start gridnode";
Expand Down

0 comments on commit 3f3dfea

Please sign in to comment.