diff --git a/src/main/java/network/p2p/MessageClient.java b/src/main/java/network/p2p/MessageClient.java index 14f22360..deaee6e6 100644 --- a/src/main/java/network/p2p/MessageClient.java +++ b/src/main/java/network/p2p/MessageClient.java @@ -28,20 +28,28 @@ import model.codec.EncodedEntity; import model.lightchain.Identifier; import modules.codec.JsonEncoder; +import network.p2p.proto.GetReply; +import network.p2p.proto.GetRequest; import network.p2p.proto.Message; import network.p2p.proto.MessengerGrpc; +import network.p2p.proto.PutMessage; +import network.p2p.proto.StorageGrpc; /** * Client side of gRPC that is responsible for sending messages from this node. */ public class MessageClient { private final MessengerGrpc.MessengerStub asyncStub; + private final StorageGrpc.StorageStub storageAsyncStub; /** * Constructor. */ public MessageClient(Channel channel) { + asyncStub = MessengerGrpc.newStub(channel); + storageAsyncStub = StorageGrpc.newStub(channel); + } /** @@ -73,11 +81,11 @@ public void onCompleted() { EncodedEntity encodedEntity = encoder.encode(entity); Message message = Message.newBuilder() - .setChannel(channel) - .setPayload(ByteString.copyFrom(encodedEntity.getBytes())) - .setType(encodedEntity.getType()) - .addTargetIds(ByteString.copyFrom(target.getBytes())) - .build(); + .setChannel(channel) + .setPayload(ByteString.copyFrom(encodedEntity.getBytes())) + .setType(encodedEntity.getType()) + .addTargetIds(ByteString.copyFrom(target.getBytes())) + .build(); requestObserver.onNext(message); if (finishLatch.getCount() == 0) { @@ -100,4 +108,127 @@ public void onCompleted() { System.err.println("deliver can not finish within 1 minutes"); } } + + /** + * Implements logic to asynchronously put entity to the target. + */ + public void put(Entity entity, String channel) throws InterruptedException { + final CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver responseObserver = new StreamObserver() { + @Override + public void onNext(Empty value) { + + } + + @Override + public void onError(Throwable t) { + System.err.println("put failed: " + Status.fromThrowable(t)); + finishLatch.countDown(); + } + + @Override + public void onCompleted() { + finishLatch.countDown(); + } + }; + + StreamObserver requestObserver = storageAsyncStub.put(responseObserver); + + try { + JsonEncoder encoder = new JsonEncoder(); + + EncodedEntity encodedEntity = encoder.encode(entity); + PutMessage putMessage = PutMessage.newBuilder() + .setChannel(channel) + .setPayload(ByteString.copyFrom(encodedEntity.getBytes())) + .setType(encodedEntity.getType()) + .build(); + requestObserver.onNext(putMessage); + + if (finishLatch.getCount() == 0) { + // RPC completed or errored before we finished sending. + // Sending further requests won't error, but they will just be thrown away. + return; + } + + } catch (RuntimeException e) { + // Cancel RPC + requestObserver.onError(e); + throw e; + } + + // Mark the end of requests + requestObserver.onCompleted(); + + // Receiving happens asynchronously + if (!finishLatch.await(1, TimeUnit.MINUTES)) { + System.err.println("put can not finish within 1 minutes"); + } + } + + /** + * Implements logic to asynchronously get entity from the target. + */ + public Entity get(Identifier identifier, String channel) throws InterruptedException { + + final CountDownLatch finishLatch = new CountDownLatch(1); + final Entity[] entity = {null}; + StreamObserver requestObserver = + storageAsyncStub.get(new StreamObserver() { + + @Override + public void onNext(GetReply response) { + + JsonEncoder encoder = new JsonEncoder(); + EncodedEntity e = new EncodedEntity(response.getPayload().toByteArray(), response.getType()); + try { + // puts the incoming entity onto the distributedStorageComponent + entity[0] = encoder.decode(e); + } catch (ClassNotFoundException ex) { + // TODO: replace with fatal log + System.err.println("could not decode incoming GetReply response"); + ex.printStackTrace(); + } + + } + + @Override + public void onError(Throwable t) { + finishLatch.countDown(); + } + + @Override + public void onCompleted() { + finishLatch.countDown(); + } + + }); + + try { + + GetRequest request = GetRequest + .newBuilder() + .setIdentifier(ByteString.copyFrom(identifier.getBytes())) + .setChannel(channel) + .build(); + + requestObserver.onNext(request); + + } catch (RuntimeException e) { + // Cancel RPC + requestObserver.onError(e); + throw e; + } + // Mark the end of requests + requestObserver.onCompleted(); + + // Receiving happens asynchronously + if (!finishLatch.await(1, TimeUnit.MINUTES)) { + System.err.println("get can not finish within 1 minutes"); + } + + return entity[0]; + + } + } diff --git a/src/main/java/network/p2p/MessageServer.java b/src/main/java/network/p2p/MessageServer.java index 279a94a2..79cf7d7c 100644 --- a/src/main/java/network/p2p/MessageServer.java +++ b/src/main/java/network/p2p/MessageServer.java @@ -18,17 +18,28 @@ import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; +import com.google.protobuf.ByteString; import com.google.protobuf.Empty; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.grpc.Server; import io.grpc.ServerBuilder; import io.grpc.stub.StreamObserver; +import model.Entity; import model.codec.EncodedEntity; +import model.lightchain.Identifier; import modules.codec.JsonEncoder; +import network.p2p.proto.GetReply; +import network.p2p.proto.GetRequest; import network.p2p.proto.Message; import network.p2p.proto.MessengerGrpc; +import network.p2p.proto.PutMessage; +import network.p2p.proto.StorageGrpc; import protocol.Engine; /** @@ -37,6 +48,7 @@ public class MessageServer { private final Server server; private final HashMap engineChannelTable; + public ConcurrentMap> distributedStorageComponent; /** * Create a MessageServer using ServerBuilder as a base. @@ -45,10 +57,12 @@ public class MessageServer { */ public MessageServer(int port) { server = ServerBuilder.forPort(port) - .addService(new MessengerImpl()) - .build(); + .addService(new MessengerImpl()) + .addService(new StorageImpl()) + .build(); this.engineChannelTable = new HashMap<>(); + this.distributedStorageComponent = new ConcurrentHashMap<>(); } /** @@ -93,7 +107,7 @@ public void stop() throws InterruptedException { } /** - * Concrete implementation of the gRPC Serverside response methods. + * Concrete implementation of the gRPC Serverside response methods for Message functionality. */ @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "meant to be externally mutable") public class MessengerImpl extends MessengerGrpc.MessengerImplBase { @@ -149,4 +163,115 @@ public void onCompleted() { } } + /** + * Concrete implementation of the gRPC Serverside response methods for Storage functionality. + */ + @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "meant to be externally mutable") + public class StorageImpl extends StorageGrpc.StorageImplBase { + + @Override + public StreamObserver put(StreamObserver responseObserver) { + return new StreamObserver() { + @Override + @SuppressFBWarnings(value = "DM_EXIT", justification = "meant to fail VM safely upon error") + public void onNext(PutMessage putMessage) { + + JsonEncoder encoder = new JsonEncoder(); + EncodedEntity e = new EncodedEntity(putMessage.getPayload().toByteArray(), putMessage.getType()); + + if (engineChannelTable.containsKey(putMessage.getChannel())) { + try { + + // TODO: replace with info log + System.out.println("Putting Entity"); + System.out.println("ID: " + encoder.decode(e).id()); + System.out.println("Channel: " + putMessage.getChannel()); + System.out.println("Type: " + putMessage.getType()); + + // puts the incoming entity onto the distributedStorageComponent + + if (distributedStorageComponent.containsKey(putMessage.getChannel())) { + distributedStorageComponent.get(putMessage.getChannel()).add(encoder.decode(e)); + } else { + HashSet s = new HashSet<>(); + s.add(encoder.decode(e)); + distributedStorageComponent.put(putMessage.getChannel(), s); + } + + } catch (ClassNotFoundException ex) { + // TODO: replace with fatal log + System.err.println("could not decode incoming put message"); + ex.printStackTrace(); + System.exit(1); + } + } else { + // TODO: replace with error log + System.err.println("no channel found for incoming put message: " + putMessage.getChannel()); + } + } + + @Override + public void onError(Throwable t) { + // TODO: replace with error log + System.err.println("encountered error in deliver: " + t); + } + + @Override + public void onCompleted() { + responseObserver.onNext(com.google.protobuf.Empty.newBuilder().build()); + responseObserver.onCompleted(); + } + }; + } + + @Override + public StreamObserver get(StreamObserver responseObserver) { + return new StreamObserver() { + @Override + public void onNext(GetRequest request) { + + Identifier id = new Identifier(request.getIdentifier().toByteArray()); + JsonEncoder encoder = new JsonEncoder(); + + System.out.println("Getting Entity"); + System.out.println("ID: " + id); + + Entity entity = null; + + if (distributedStorageComponent.containsKey(request.getChannel())) { + + for (Entity e : distributedStorageComponent.get(request.getChannel())) { + if (e.id().comparedTo(id) == 0) { + entity = e; + EncodedEntity encodedEntity = encoder.encode(entity); + + GetReply reply = GetReply + .newBuilder() + .setPayload(ByteString.copyFrom(encodedEntity.getBytes())) + .setType(encodedEntity.getType()) + .build(); + + responseObserver.onNext(reply); + } + } + + } else { + System.out.println("CHANNEL NOT FOUND"); + } + + } + + @Override + public void onError(Throwable t) { + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + }; + } + + } + } diff --git a/src/main/java/network/p2p/P2pConduit.java b/src/main/java/network/p2p/P2pConduit.java index 6f5fb7a9..3e86b7fc 100644 --- a/src/main/java/network/p2p/P2pConduit.java +++ b/src/main/java/network/p2p/P2pConduit.java @@ -47,6 +47,12 @@ public void unicast(Entity e, Identifier target) throws LightChainNetworkingExce @Override public void put(Entity e) throws LightChainDistributedStorageException { + try { + network.putEntity(e, this.channel); + } catch (InterruptedException | IllegalArgumentException ex) { + throw new LightChainDistributedStorageException(); + } + } /** @@ -59,11 +65,18 @@ public void put(Entity e) throws LightChainDistributedStorageException { */ @Override public Entity get(Identifier identifier) throws LightChainDistributedStorageException { - return null; + + try { + return network.getEntity(identifier, this.channel); + } catch (InterruptedException | IllegalArgumentException ex) { + throw new LightChainDistributedStorageException(); + } + } @Override public ArrayList allEntities() throws LightChainDistributedStorageException { - return null; + return network.getAllEntities(channel); } + } diff --git a/src/main/java/network/p2p/P2pNetwork.java b/src/main/java/network/p2p/P2pNetwork.java index 7e7d5927..643d36c8 100644 --- a/src/main/java/network/p2p/P2pNetwork.java +++ b/src/main/java/network/p2p/P2pNetwork.java @@ -1,6 +1,7 @@ package network.p2p; import java.io.IOException; +import java.util.ArrayList; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -102,7 +103,7 @@ public String getAddress() { * @throws IllegalArgumentException if target identifier does not correspond to a valid address. */ public void sendUnicast(Entity e, Identifier target, String channel) throws InterruptedException, - IOException, IllegalArgumentException { + IOException, IllegalArgumentException { String targetAddress = this.idToAddressMap.get(target); if (targetAddress == null) { @@ -117,4 +118,114 @@ public void sendUnicast(Entity e, Identifier target, String channel) throws Inte } } + /** + * Puts the provided Entity into the correct storage element. + * + * @param e the entity to be put. + * @param channel the network channel on which this entity is put. + * @throws InterruptedException if the transmission of Entity relay is interrupted. + */ + public void putEntity(Entity e, String channel) throws InterruptedException { + + Identifier currentId = e.id(); + Identifier smallestId = (Identifier) idToAddressMap.keySet().toArray()[0]; + Identifier targetId; + + for (Identifier id : idToAddressMap.keySet()) { + + if (currentId.comparedTo(id) == -1 && e.id().comparedTo(id) == 1) { + currentId = id; + } + + if (smallestId.comparedTo(id) == 1) { + smallestId = id; + } + + } + + if (currentId.comparedTo(e.id()) == 0) { + targetId = smallestId; + } else { + targetId = currentId; + } + + String targetAddress = this.idToAddressMap.get(targetId); + if (targetAddress == null) { + throw new IllegalArgumentException("target identifier does not exist: " + targetId.toString()); + } + ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(targetAddress).usePlaintext().build(); + try { + MessageClient client = new MessageClient(managedChannel); + client.put(e, channel); + } finally { + managedChannel.shutdownNow(); + } + + } + + /** + * Implements logic to asynchronously put entity from the target. + */ + public Entity getEntity(Identifier identifier, String channel) throws InterruptedException { + + Entity e = null; + + Identifier currentId = identifier; + Identifier smallestId = (Identifier) idToAddressMap.keySet().toArray()[0]; + Identifier targetId; + + for (Identifier id : idToAddressMap.keySet()) { + + if (currentId.comparedTo(id) == -1 && identifier.comparedTo(id) == 1) { + currentId = id; + } + + if (smallestId.comparedTo(id) == 1) { + smallestId = id; + } + + } + + if (currentId.comparedTo(identifier) == 0) { + targetId = smallestId; + } else { + targetId = currentId; + } + + String targetAddress = this.idToAddressMap.get(targetId); + if (targetAddress == null) { + throw new IllegalArgumentException("target identifier does not exist: " + targetId.toString()); + } + ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(targetAddress).usePlaintext().build(); + try { + MessageClient client = new MessageClient(managedChannel); + e = client.get(identifier, channel); + } finally { + managedChannel.shutdownNow(); + } + + return e; + + } + + /** + * Gathers all the Entities requested by the caller. + * + * @param channel the channel of the requesting Engine + * @return an ArrayList containing all the Entities + */ + public ArrayList getAllEntities(String channel) { + + ArrayList entities = new ArrayList<>(); + + if (!(server.distributedStorageComponent.get(channel) == null)) { + for (Entity e : server.distributedStorageComponent.get(channel)) { + entities.add(e); + } + } + + return entities; + + } + } diff --git a/src/main/java/network/p2p/proto/GetReply.java b/src/main/java/network/p2p/proto/GetReply.java new file mode 100644 index 00000000..11e69597 --- /dev/null +++ b/src/main/java/network/p2p/proto/GetReply.java @@ -0,0 +1,625 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +/** + * Protobuf type {@code network.p2p.proto.GetReply} + */ +public final class GetReply extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:network.p2p.proto.GetReply) + GetReplyOrBuilder { +private static final long serialVersionUID = 0L; + // Use GetReply.newBuilder() to construct. + private GetReply(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private GetReply() { + payload_ = com.google.protobuf.ByteString.EMPTY; + type_ = ""; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new GetReply(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private GetReply( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + + payload_ = input.readBytes(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + type_ = s; + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetReply_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetReply_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.GetReply.class, network.p2p.proto.GetReply.Builder.class); + } + + public static final int PAYLOAD_FIELD_NUMBER = 1; + private com.google.protobuf.ByteString payload_; + /** + * bytes Payload = 1; + * @return The payload. + */ + @java.lang.Override + public com.google.protobuf.ByteString getPayload() { + return payload_; + } + + public static final int TYPE_FIELD_NUMBER = 2; + private volatile java.lang.Object type_; + /** + * string Type = 2; + * @return The type. + */ + @java.lang.Override + public java.lang.String getType() { + java.lang.Object ref = type_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + type_ = s; + return s; + } + } + /** + * string Type = 2; + * @return The bytes for type. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getTypeBytes() { + java.lang.Object ref = type_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + type_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!payload_.isEmpty()) { + output.writeBytes(1, payload_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(type_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, type_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!payload_.isEmpty()) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(1, payload_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(type_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, type_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof network.p2p.proto.GetReply)) { + return super.equals(obj); + } + network.p2p.proto.GetReply other = (network.p2p.proto.GetReply) obj; + + if (!getPayload() + .equals(other.getPayload())) return false; + if (!getType() + .equals(other.getType())) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + PAYLOAD_FIELD_NUMBER; + hash = (53 * hash) + getPayload().hashCode(); + hash = (37 * hash) + TYPE_FIELD_NUMBER; + hash = (53 * hash) + getType().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static network.p2p.proto.GetReply parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetReply parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetReply parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetReply parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetReply parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetReply parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetReply parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.GetReply parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.GetReply parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static network.p2p.proto.GetReply parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.GetReply parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.GetReply parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(network.p2p.proto.GetReply prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code network.p2p.proto.GetReply} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:network.p2p.proto.GetReply) + network.p2p.proto.GetReplyOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetReply_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetReply_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.GetReply.class, network.p2p.proto.GetReply.Builder.class); + } + + // Construct using network.p2p.proto.GetReply.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + payload_ = com.google.protobuf.ByteString.EMPTY; + + type_ = ""; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetReply_descriptor; + } + + @java.lang.Override + public network.p2p.proto.GetReply getDefaultInstanceForType() { + return network.p2p.proto.GetReply.getDefaultInstance(); + } + + @java.lang.Override + public network.p2p.proto.GetReply build() { + network.p2p.proto.GetReply result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public network.p2p.proto.GetReply buildPartial() { + network.p2p.proto.GetReply result = new network.p2p.proto.GetReply(this); + result.payload_ = payload_; + result.type_ = type_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof network.p2p.proto.GetReply) { + return mergeFrom((network.p2p.proto.GetReply)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(network.p2p.proto.GetReply other) { + if (other == network.p2p.proto.GetReply.getDefaultInstance()) return this; + if (other.getPayload() != com.google.protobuf.ByteString.EMPTY) { + setPayload(other.getPayload()); + } + if (!other.getType().isEmpty()) { + type_ = other.type_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + network.p2p.proto.GetReply parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (network.p2p.proto.GetReply) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private com.google.protobuf.ByteString payload_ = com.google.protobuf.ByteString.EMPTY; + /** + * bytes Payload = 1; + * @return The payload. + */ + @java.lang.Override + public com.google.protobuf.ByteString getPayload() { + return payload_; + } + /** + * bytes Payload = 1; + * @param value The payload to set. + * @return This builder for chaining. + */ + public Builder setPayload(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + + payload_ = value; + onChanged(); + return this; + } + /** + * bytes Payload = 1; + * @return This builder for chaining. + */ + public Builder clearPayload() { + + payload_ = getDefaultInstance().getPayload(); + onChanged(); + return this; + } + + private java.lang.Object type_ = ""; + /** + * string Type = 2; + * @return The type. + */ + public java.lang.String getType() { + java.lang.Object ref = type_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + type_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string Type = 2; + * @return The bytes for type. + */ + public com.google.protobuf.ByteString + getTypeBytes() { + java.lang.Object ref = type_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + type_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string Type = 2; + * @param value The type to set. + * @return This builder for chaining. + */ + public Builder setType( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + type_ = value; + onChanged(); + return this; + } + /** + * string Type = 2; + * @return This builder for chaining. + */ + public Builder clearType() { + + type_ = getDefaultInstance().getType(); + onChanged(); + return this; + } + /** + * string Type = 2; + * @param value The bytes for type to set. + * @return This builder for chaining. + */ + public Builder setTypeBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + type_ = value; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:network.p2p.proto.GetReply) + } + + // @@protoc_insertion_point(class_scope:network.p2p.proto.GetReply) + private static final network.p2p.proto.GetReply DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new network.p2p.proto.GetReply(); + } + + public static network.p2p.proto.GetReply getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public GetReply parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new GetReply(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public network.p2p.proto.GetReply getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/main/java/network/p2p/proto/GetReplyOrBuilder.java b/src/main/java/network/p2p/proto/GetReplyOrBuilder.java new file mode 100644 index 00000000..0b3bfbb1 --- /dev/null +++ b/src/main/java/network/p2p/proto/GetReplyOrBuilder.java @@ -0,0 +1,27 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +public interface GetReplyOrBuilder extends + // @@protoc_insertion_point(interface_extends:network.p2p.proto.GetReply) + com.google.protobuf.MessageOrBuilder { + + /** + * bytes Payload = 1; + * @return The payload. + */ + com.google.protobuf.ByteString getPayload(); + + /** + * string Type = 2; + * @return The type. + */ + java.lang.String getType(); + /** + * string Type = 2; + * @return The bytes for type. + */ + com.google.protobuf.ByteString + getTypeBytes(); +} diff --git a/src/main/java/network/p2p/proto/GetRequest.java b/src/main/java/network/p2p/proto/GetRequest.java new file mode 100644 index 00000000..7cb1dde1 --- /dev/null +++ b/src/main/java/network/p2p/proto/GetRequest.java @@ -0,0 +1,625 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +/** + * Protobuf type {@code network.p2p.proto.GetRequest} + */ +public final class GetRequest extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:network.p2p.proto.GetRequest) + GetRequestOrBuilder { +private static final long serialVersionUID = 0L; + // Use GetRequest.newBuilder() to construct. + private GetRequest(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private GetRequest() { + identifier_ = com.google.protobuf.ByteString.EMPTY; + channel_ = ""; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new GetRequest(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private GetRequest( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + + identifier_ = input.readBytes(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + channel_ = s; + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetRequest_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetRequest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.GetRequest.class, network.p2p.proto.GetRequest.Builder.class); + } + + public static final int IDENTIFIER_FIELD_NUMBER = 1; + private com.google.protobuf.ByteString identifier_; + /** + * bytes Identifier = 1; + * @return The identifier. + */ + @java.lang.Override + public com.google.protobuf.ByteString getIdentifier() { + return identifier_; + } + + public static final int CHANNEL_FIELD_NUMBER = 2; + private volatile java.lang.Object channel_; + /** + * string Channel = 2; + * @return The channel. + */ + @java.lang.Override + public java.lang.String getChannel() { + java.lang.Object ref = channel_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + channel_ = s; + return s; + } + } + /** + * string Channel = 2; + * @return The bytes for channel. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getChannelBytes() { + java.lang.Object ref = channel_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + channel_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!identifier_.isEmpty()) { + output.writeBytes(1, identifier_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(channel_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, channel_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!identifier_.isEmpty()) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(1, identifier_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(channel_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, channel_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof network.p2p.proto.GetRequest)) { + return super.equals(obj); + } + network.p2p.proto.GetRequest other = (network.p2p.proto.GetRequest) obj; + + if (!getIdentifier() + .equals(other.getIdentifier())) return false; + if (!getChannel() + .equals(other.getChannel())) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + IDENTIFIER_FIELD_NUMBER; + hash = (53 * hash) + getIdentifier().hashCode(); + hash = (37 * hash) + CHANNEL_FIELD_NUMBER; + hash = (53 * hash) + getChannel().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static network.p2p.proto.GetRequest parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetRequest parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetRequest parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetRequest parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetRequest parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.GetRequest parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.GetRequest parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.GetRequest parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.GetRequest parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static network.p2p.proto.GetRequest parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.GetRequest parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.GetRequest parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(network.p2p.proto.GetRequest prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code network.p2p.proto.GetRequest} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:network.p2p.proto.GetRequest) + network.p2p.proto.GetRequestOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetRequest_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetRequest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.GetRequest.class, network.p2p.proto.GetRequest.Builder.class); + } + + // Construct using network.p2p.proto.GetRequest.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + identifier_ = com.google.protobuf.ByteString.EMPTY; + + channel_ = ""; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_GetRequest_descriptor; + } + + @java.lang.Override + public network.p2p.proto.GetRequest getDefaultInstanceForType() { + return network.p2p.proto.GetRequest.getDefaultInstance(); + } + + @java.lang.Override + public network.p2p.proto.GetRequest build() { + network.p2p.proto.GetRequest result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public network.p2p.proto.GetRequest buildPartial() { + network.p2p.proto.GetRequest result = new network.p2p.proto.GetRequest(this); + result.identifier_ = identifier_; + result.channel_ = channel_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof network.p2p.proto.GetRequest) { + return mergeFrom((network.p2p.proto.GetRequest)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(network.p2p.proto.GetRequest other) { + if (other == network.p2p.proto.GetRequest.getDefaultInstance()) return this; + if (other.getIdentifier() != com.google.protobuf.ByteString.EMPTY) { + setIdentifier(other.getIdentifier()); + } + if (!other.getChannel().isEmpty()) { + channel_ = other.channel_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + network.p2p.proto.GetRequest parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (network.p2p.proto.GetRequest) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private com.google.protobuf.ByteString identifier_ = com.google.protobuf.ByteString.EMPTY; + /** + * bytes Identifier = 1; + * @return The identifier. + */ + @java.lang.Override + public com.google.protobuf.ByteString getIdentifier() { + return identifier_; + } + /** + * bytes Identifier = 1; + * @param value The identifier to set. + * @return This builder for chaining. + */ + public Builder setIdentifier(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + + identifier_ = value; + onChanged(); + return this; + } + /** + * bytes Identifier = 1; + * @return This builder for chaining. + */ + public Builder clearIdentifier() { + + identifier_ = getDefaultInstance().getIdentifier(); + onChanged(); + return this; + } + + private java.lang.Object channel_ = ""; + /** + * string Channel = 2; + * @return The channel. + */ + public java.lang.String getChannel() { + java.lang.Object ref = channel_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + channel_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string Channel = 2; + * @return The bytes for channel. + */ + public com.google.protobuf.ByteString + getChannelBytes() { + java.lang.Object ref = channel_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + channel_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string Channel = 2; + * @param value The channel to set. + * @return This builder for chaining. + */ + public Builder setChannel( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + channel_ = value; + onChanged(); + return this; + } + /** + * string Channel = 2; + * @return This builder for chaining. + */ + public Builder clearChannel() { + + channel_ = getDefaultInstance().getChannel(); + onChanged(); + return this; + } + /** + * string Channel = 2; + * @param value The bytes for channel to set. + * @return This builder for chaining. + */ + public Builder setChannelBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + channel_ = value; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:network.p2p.proto.GetRequest) + } + + // @@protoc_insertion_point(class_scope:network.p2p.proto.GetRequest) + private static final network.p2p.proto.GetRequest DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new network.p2p.proto.GetRequest(); + } + + public static network.p2p.proto.GetRequest getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public GetRequest parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new GetRequest(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public network.p2p.proto.GetRequest getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/main/java/network/p2p/proto/GetRequestOrBuilder.java b/src/main/java/network/p2p/proto/GetRequestOrBuilder.java new file mode 100644 index 00000000..7c363626 --- /dev/null +++ b/src/main/java/network/p2p/proto/GetRequestOrBuilder.java @@ -0,0 +1,27 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +public interface GetRequestOrBuilder extends + // @@protoc_insertion_point(interface_extends:network.p2p.proto.GetRequest) + com.google.protobuf.MessageOrBuilder { + + /** + * bytes Identifier = 1; + * @return The identifier. + */ + com.google.protobuf.ByteString getIdentifier(); + + /** + * string Channel = 2; + * @return The channel. + */ + java.lang.String getChannel(); + /** + * string Channel = 2; + * @return The bytes for channel. + */ + com.google.protobuf.ByteString + getChannelBytes(); +} diff --git a/src/main/java/network/p2p/proto/PutMessage.java b/src/main/java/network/p2p/proto/PutMessage.java new file mode 100644 index 00000000..0e58a792 --- /dev/null +++ b/src/main/java/network/p2p/proto/PutMessage.java @@ -0,0 +1,831 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +/** + * Protobuf type {@code network.p2p.proto.PutMessage} + */ +public final class PutMessage extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:network.p2p.proto.PutMessage) + PutMessageOrBuilder { +private static final long serialVersionUID = 0L; + // Use PutMessage.newBuilder() to construct. + private PutMessage(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private PutMessage() { + originId_ = com.google.protobuf.ByteString.EMPTY; + channel_ = ""; + payload_ = com.google.protobuf.ByteString.EMPTY; + type_ = ""; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new PutMessage(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private PutMessage( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + + originId_ = input.readBytes(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + channel_ = s; + break; + } + case 26: { + + payload_ = input.readBytes(); + break; + } + case 34: { + java.lang.String s = input.readStringRequireUtf8(); + + type_ = s; + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_PutMessage_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_PutMessage_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.PutMessage.class, network.p2p.proto.PutMessage.Builder.class); + } + + public static final int ORIGINID_FIELD_NUMBER = 1; + private com.google.protobuf.ByteString originId_; + /** + * bytes OriginId = 1; + * @return The originId. + */ + @java.lang.Override + public com.google.protobuf.ByteString getOriginId() { + return originId_; + } + + public static final int CHANNEL_FIELD_NUMBER = 2; + private volatile java.lang.Object channel_; + /** + * string Channel = 2; + * @return The channel. + */ + @java.lang.Override + public java.lang.String getChannel() { + java.lang.Object ref = channel_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + channel_ = s; + return s; + } + } + /** + * string Channel = 2; + * @return The bytes for channel. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getChannelBytes() { + java.lang.Object ref = channel_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + channel_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int PAYLOAD_FIELD_NUMBER = 3; + private com.google.protobuf.ByteString payload_; + /** + * bytes Payload = 3; + * @return The payload. + */ + @java.lang.Override + public com.google.protobuf.ByteString getPayload() { + return payload_; + } + + public static final int TYPE_FIELD_NUMBER = 4; + private volatile java.lang.Object type_; + /** + * string Type = 4; + * @return The type. + */ + @java.lang.Override + public java.lang.String getType() { + java.lang.Object ref = type_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + type_ = s; + return s; + } + } + /** + * string Type = 4; + * @return The bytes for type. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getTypeBytes() { + java.lang.Object ref = type_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + type_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!originId_.isEmpty()) { + output.writeBytes(1, originId_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(channel_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, channel_); + } + if (!payload_.isEmpty()) { + output.writeBytes(3, payload_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(type_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 4, type_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!originId_.isEmpty()) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(1, originId_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(channel_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, channel_); + } + if (!payload_.isEmpty()) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(3, payload_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(type_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(4, type_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof network.p2p.proto.PutMessage)) { + return super.equals(obj); + } + network.p2p.proto.PutMessage other = (network.p2p.proto.PutMessage) obj; + + if (!getOriginId() + .equals(other.getOriginId())) return false; + if (!getChannel() + .equals(other.getChannel())) return false; + if (!getPayload() + .equals(other.getPayload())) return false; + if (!getType() + .equals(other.getType())) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + ORIGINID_FIELD_NUMBER; + hash = (53 * hash) + getOriginId().hashCode(); + hash = (37 * hash) + CHANNEL_FIELD_NUMBER; + hash = (53 * hash) + getChannel().hashCode(); + hash = (37 * hash) + PAYLOAD_FIELD_NUMBER; + hash = (53 * hash) + getPayload().hashCode(); + hash = (37 * hash) + TYPE_FIELD_NUMBER; + hash = (53 * hash) + getType().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static network.p2p.proto.PutMessage parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.PutMessage parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.PutMessage parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.PutMessage parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.PutMessage parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static network.p2p.proto.PutMessage parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static network.p2p.proto.PutMessage parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.PutMessage parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.PutMessage parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static network.p2p.proto.PutMessage parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static network.p2p.proto.PutMessage parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static network.p2p.proto.PutMessage parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(network.p2p.proto.PutMessage prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code network.p2p.proto.PutMessage} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:network.p2p.proto.PutMessage) + network.p2p.proto.PutMessageOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_PutMessage_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_PutMessage_fieldAccessorTable + .ensureFieldAccessorsInitialized( + network.p2p.proto.PutMessage.class, network.p2p.proto.PutMessage.Builder.class); + } + + // Construct using network.p2p.proto.PutMessage.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + originId_ = com.google.protobuf.ByteString.EMPTY; + + channel_ = ""; + + payload_ = com.google.protobuf.ByteString.EMPTY; + + type_ = ""; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return network.p2p.proto.StorageOuterClass.internal_static_network_p2p_proto_PutMessage_descriptor; + } + + @java.lang.Override + public network.p2p.proto.PutMessage getDefaultInstanceForType() { + return network.p2p.proto.PutMessage.getDefaultInstance(); + } + + @java.lang.Override + public network.p2p.proto.PutMessage build() { + network.p2p.proto.PutMessage result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public network.p2p.proto.PutMessage buildPartial() { + network.p2p.proto.PutMessage result = new network.p2p.proto.PutMessage(this); + result.originId_ = originId_; + result.channel_ = channel_; + result.payload_ = payload_; + result.type_ = type_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof network.p2p.proto.PutMessage) { + return mergeFrom((network.p2p.proto.PutMessage)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(network.p2p.proto.PutMessage other) { + if (other == network.p2p.proto.PutMessage.getDefaultInstance()) return this; + if (other.getOriginId() != com.google.protobuf.ByteString.EMPTY) { + setOriginId(other.getOriginId()); + } + if (!other.getChannel().isEmpty()) { + channel_ = other.channel_; + onChanged(); + } + if (other.getPayload() != com.google.protobuf.ByteString.EMPTY) { + setPayload(other.getPayload()); + } + if (!other.getType().isEmpty()) { + type_ = other.type_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + network.p2p.proto.PutMessage parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (network.p2p.proto.PutMessage) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private com.google.protobuf.ByteString originId_ = com.google.protobuf.ByteString.EMPTY; + /** + * bytes OriginId = 1; + * @return The originId. + */ + @java.lang.Override + public com.google.protobuf.ByteString getOriginId() { + return originId_; + } + /** + * bytes OriginId = 1; + * @param value The originId to set. + * @return This builder for chaining. + */ + public Builder setOriginId(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + + originId_ = value; + onChanged(); + return this; + } + /** + * bytes OriginId = 1; + * @return This builder for chaining. + */ + public Builder clearOriginId() { + + originId_ = getDefaultInstance().getOriginId(); + onChanged(); + return this; + } + + private java.lang.Object channel_ = ""; + /** + * string Channel = 2; + * @return The channel. + */ + public java.lang.String getChannel() { + java.lang.Object ref = channel_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + channel_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string Channel = 2; + * @return The bytes for channel. + */ + public com.google.protobuf.ByteString + getChannelBytes() { + java.lang.Object ref = channel_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + channel_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string Channel = 2; + * @param value The channel to set. + * @return This builder for chaining. + */ + public Builder setChannel( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + channel_ = value; + onChanged(); + return this; + } + /** + * string Channel = 2; + * @return This builder for chaining. + */ + public Builder clearChannel() { + + channel_ = getDefaultInstance().getChannel(); + onChanged(); + return this; + } + /** + * string Channel = 2; + * @param value The bytes for channel to set. + * @return This builder for chaining. + */ + public Builder setChannelBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + channel_ = value; + onChanged(); + return this; + } + + private com.google.protobuf.ByteString payload_ = com.google.protobuf.ByteString.EMPTY; + /** + * bytes Payload = 3; + * @return The payload. + */ + @java.lang.Override + public com.google.protobuf.ByteString getPayload() { + return payload_; + } + /** + * bytes Payload = 3; + * @param value The payload to set. + * @return This builder for chaining. + */ + public Builder setPayload(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + + payload_ = value; + onChanged(); + return this; + } + /** + * bytes Payload = 3; + * @return This builder for chaining. + */ + public Builder clearPayload() { + + payload_ = getDefaultInstance().getPayload(); + onChanged(); + return this; + } + + private java.lang.Object type_ = ""; + /** + * string Type = 4; + * @return The type. + */ + public java.lang.String getType() { + java.lang.Object ref = type_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + type_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string Type = 4; + * @return The bytes for type. + */ + public com.google.protobuf.ByteString + getTypeBytes() { + java.lang.Object ref = type_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + type_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string Type = 4; + * @param value The type to set. + * @return This builder for chaining. + */ + public Builder setType( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + type_ = value; + onChanged(); + return this; + } + /** + * string Type = 4; + * @return This builder for chaining. + */ + public Builder clearType() { + + type_ = getDefaultInstance().getType(); + onChanged(); + return this; + } + /** + * string Type = 4; + * @param value The bytes for type to set. + * @return This builder for chaining. + */ + public Builder setTypeBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + type_ = value; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:network.p2p.proto.PutMessage) + } + + // @@protoc_insertion_point(class_scope:network.p2p.proto.PutMessage) + private static final network.p2p.proto.PutMessage DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new network.p2p.proto.PutMessage(); + } + + public static network.p2p.proto.PutMessage getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public PutMessage parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new PutMessage(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public network.p2p.proto.PutMessage getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/main/java/network/p2p/proto/PutMessageOrBuilder.java b/src/main/java/network/p2p/proto/PutMessageOrBuilder.java new file mode 100644 index 00000000..df63fd3f --- /dev/null +++ b/src/main/java/network/p2p/proto/PutMessageOrBuilder.java @@ -0,0 +1,45 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +public interface PutMessageOrBuilder extends + // @@protoc_insertion_point(interface_extends:network.p2p.proto.PutMessage) + com.google.protobuf.MessageOrBuilder { + + /** + * bytes OriginId = 1; + * @return The originId. + */ + com.google.protobuf.ByteString getOriginId(); + + /** + * string Channel = 2; + * @return The channel. + */ + java.lang.String getChannel(); + /** + * string Channel = 2; + * @return The bytes for channel. + */ + com.google.protobuf.ByteString + getChannelBytes(); + + /** + * bytes Payload = 3; + * @return The payload. + */ + com.google.protobuf.ByteString getPayload(); + + /** + * string Type = 4; + * @return The type. + */ + java.lang.String getType(); + /** + * string Type = 4; + * @return The bytes for type. + */ + com.google.protobuf.ByteString + getTypeBytes(); +} diff --git a/src/main/java/network/p2p/proto/StorageGrpc.java b/src/main/java/network/p2p/proto/StorageGrpc.java new file mode 100644 index 00000000..1a746397 --- /dev/null +++ b/src/main/java/network/p2p/proto/StorageGrpc.java @@ -0,0 +1,318 @@ +package network.p2p.proto; + +import static io.grpc.MethodDescriptor.generateFullMethodName; + +/** + */ +@javax.annotation.Generated( + value = "by gRPC proto compiler (version 1.45.1)", + comments = "Source: storage.proto") +@io.grpc.stub.annotations.GrpcGenerated +public final class StorageGrpc { + + private StorageGrpc() {} + + public static final String SERVICE_NAME = "network.p2p.proto.Storage"; + + // Static method descriptors that strictly reflect the proto. + private static volatile io.grpc.MethodDescriptor getPutMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "Put", + requestType = PutMessage.class, + responseType = com.google.protobuf.Empty.class, + methodType = io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + public static io.grpc.MethodDescriptor getPutMethod() { + io.grpc.MethodDescriptor getPutMethod; + if ((getPutMethod = StorageGrpc.getPutMethod) == null) { + synchronized (StorageGrpc.class) { + if ((getPutMethod = StorageGrpc.getPutMethod) == null) { + StorageGrpc.getPutMethod = getPutMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "Put")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + PutMessage.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + com.google.protobuf.Empty.getDefaultInstance())) + .setSchemaDescriptor(new StorageMethodDescriptorSupplier("Put")) + .build(); + } + } + } + return getPutMethod; + } + + private static volatile io.grpc.MethodDescriptor getGetMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "Get", + requestType = GetRequest.class, + responseType = GetReply.class, + methodType = io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + public static io.grpc.MethodDescriptor getGetMethod() { + io.grpc.MethodDescriptor getGetMethod; + if ((getGetMethod = StorageGrpc.getGetMethod) == null) { + synchronized (StorageGrpc.class) { + if ((getGetMethod = StorageGrpc.getGetMethod) == null) { + StorageGrpc.getGetMethod = getGetMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "Get")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + GetRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + GetReply.getDefaultInstance())) + .setSchemaDescriptor(new StorageMethodDescriptorSupplier("Get")) + .build(); + } + } + } + return getGetMethod; + } + + /** + * Creates a new async stub that supports all call types for the service + */ + public static StorageStub newStub(io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StorageStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageStub(channel, callOptions); + } + }; + return StorageStub.newStub(factory, channel); + } + + /** + * Creates a new blocking-style stub that supports unary and streaming output calls on the service + */ + public static StorageBlockingStub newBlockingStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StorageBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageBlockingStub(channel, callOptions); + } + }; + return StorageBlockingStub.newStub(factory, channel); + } + + /** + * Creates a new ListenableFuture-style stub that supports unary calls on the service + */ + public static StorageFutureStub newFutureStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StorageFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageFutureStub(channel, callOptions); + } + }; + return StorageFutureStub.newStub(factory, channel); + } + + /** + */ + public static abstract class StorageImplBase implements io.grpc.BindableService { + + /** + */ + public io.grpc.stub.StreamObserver put( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getPutMethod(), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver get( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getGetMethod(), responseObserver); + } + + @Override public final io.grpc.ServerServiceDefinition bindService() { + return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + .addMethod( + getPutMethod(), + io.grpc.stub.ServerCalls.asyncClientStreamingCall( + new MethodHandlers< + PutMessage, + com.google.protobuf.Empty>( + this, METHODID_PUT))) + .addMethod( + getGetMethod(), + io.grpc.stub.ServerCalls.asyncBidiStreamingCall( + new MethodHandlers< + GetRequest, + GetReply>( + this, METHODID_GET))) + .build(); + } + } + + /** + */ + public static final class StorageStub extends io.grpc.stub.AbstractAsyncStub { + private StorageStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StorageStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageStub(channel, callOptions); + } + + /** + */ + public io.grpc.stub.StreamObserver put( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncClientStreamingCall( + getChannel().newCall(getPutMethod(), getCallOptions()), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver get( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncBidiStreamingCall( + getChannel().newCall(getGetMethod(), getCallOptions()), responseObserver); + } + } + + /** + */ + public static final class StorageBlockingStub extends io.grpc.stub.AbstractBlockingStub { + private StorageBlockingStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StorageBlockingStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageBlockingStub(channel, callOptions); + } + } + + /** + */ + public static final class StorageFutureStub extends io.grpc.stub.AbstractFutureStub { + private StorageFutureStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StorageFutureStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StorageFutureStub(channel, callOptions); + } + } + + private static final int METHODID_PUT = 0; + private static final int METHODID_GET = 1; + + private static final class MethodHandlers implements + io.grpc.stub.ServerCalls.UnaryMethod, + io.grpc.stub.ServerCalls.ServerStreamingMethod, + io.grpc.stub.ServerCalls.ClientStreamingMethod, + io.grpc.stub.ServerCalls.BidiStreamingMethod { + private final StorageImplBase serviceImpl; + private final int methodId; + + MethodHandlers(StorageImplBase serviceImpl, int methodId) { + this.serviceImpl = serviceImpl; + this.methodId = methodId; + } + + @Override + @SuppressWarnings("unchecked") + public void invoke(Req request, io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + default: + throw new AssertionError(); + } + } + + @Override + @SuppressWarnings("unchecked") + public io.grpc.stub.StreamObserver invoke( + io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + case METHODID_PUT: + return (io.grpc.stub.StreamObserver) serviceImpl.put( + (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_GET: + return (io.grpc.stub.StreamObserver) serviceImpl.get( + (io.grpc.stub.StreamObserver) responseObserver); + default: + throw new AssertionError(); + } + } + } + + private static abstract class StorageBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier { + StorageBaseDescriptorSupplier() {} + + @Override + public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() { + return StorageOuterClass.getDescriptor(); + } + + @Override + public com.google.protobuf.Descriptors.ServiceDescriptor getServiceDescriptor() { + return getFileDescriptor().findServiceByName("Storage"); + } + } + + private static final class StorageFileDescriptorSupplier + extends StorageBaseDescriptorSupplier { + StorageFileDescriptorSupplier() {} + } + + private static final class StorageMethodDescriptorSupplier + extends StorageBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoMethodDescriptorSupplier { + private final String methodName; + + StorageMethodDescriptorSupplier(String methodName) { + this.methodName = methodName; + } + + @Override + public com.google.protobuf.Descriptors.MethodDescriptor getMethodDescriptor() { + return getServiceDescriptor().findMethodByName(methodName); + } + } + + private static volatile io.grpc.ServiceDescriptor serviceDescriptor; + + public static io.grpc.ServiceDescriptor getServiceDescriptor() { + io.grpc.ServiceDescriptor result = serviceDescriptor; + if (result == null) { + synchronized (StorageGrpc.class) { + result = serviceDescriptor; + if (result == null) { + serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME) + .setSchemaDescriptor(new StorageFileDescriptorSupplier()) + .addMethod(getPutMethod()) + .addMethod(getGetMethod()) + .build(); + } + } + } + return result; + } +} diff --git a/src/main/java/network/p2p/proto/StorageOuterClass.java b/src/main/java/network/p2p/proto/StorageOuterClass.java new file mode 100644 index 00000000..39cf4006 --- /dev/null +++ b/src/main/java/network/p2p/proto/StorageOuterClass.java @@ -0,0 +1,79 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: storage.proto + +package network.p2p.proto; + +public final class StorageOuterClass { + private StorageOuterClass() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + static final com.google.protobuf.Descriptors.Descriptor + internal_static_network_p2p_proto_PutMessage_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_network_p2p_proto_PutMessage_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_network_p2p_proto_GetRequest_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_network_p2p_proto_GetRequest_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_network_p2p_proto_GetReply_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_network_p2p_proto_GetReply_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\rstorage.proto\022\021network.p2p.proto\032\033goog" + + "le/protobuf/empty.proto\"N\n\nPutMessage\022\020\n" + + "\010OriginId\030\001 \001(\014\022\017\n\007Channel\030\002 \001(\t\022\017\n\007Payl" + + "oad\030\003 \001(\014\022\014\n\004Type\030\004 \001(\t\"1\n\nGetRequest\022\022\n" + + "\nIdentifier\030\001 \001(\014\022\017\n\007Channel\030\002 \001(\t\")\n\010Ge" + + "tReply\022\017\n\007Payload\030\001 \001(\014\022\014\n\004Type\030\002 \001(\t2\224\001" + + "\n\007Storage\022@\n\003Put\022\035.network.p2p.proto.Put" + + "Message\032\026.google.protobuf.Empty\"\000(\001\022G\n\003G" + + "et\022\035.network.p2p.proto.GetRequest\032\033.netw" + + "ork.p2p.proto.GetReply\"\000(\0010\001B\002P\001b\006proto3" + }; + descriptor = com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + com.google.protobuf.EmptyProto.getDescriptor(), + }); + internal_static_network_p2p_proto_PutMessage_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_network_p2p_proto_PutMessage_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_network_p2p_proto_PutMessage_descriptor, + new java.lang.String[] { "OriginId", "Channel", "Payload", "Type", }); + internal_static_network_p2p_proto_GetRequest_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_network_p2p_proto_GetRequest_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_network_p2p_proto_GetRequest_descriptor, + new java.lang.String[] { "Identifier", "Channel", }); + internal_static_network_p2p_proto_GetReply_descriptor = + getDescriptor().getMessageTypes().get(2); + internal_static_network_p2p_proto_GetReply_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_network_p2p_proto_GetReply_descriptor, + new java.lang.String[] { "Payload", "Type", }); + com.google.protobuf.EmptyProto.getDescriptor(); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/src/main/proto/storage.proto b/src/main/proto/storage.proto index 003a8b4f..97e7dbad 100644 --- a/src/main/proto/storage.proto +++ b/src/main/proto/storage.proto @@ -14,6 +14,7 @@ message PutMessage { message GetRequest { bytes Identifier = 1; + string Channel = 2; } message GetReply { diff --git a/src/test/java/networking/p2p/StorageTest.java b/src/test/java/networking/p2p/StorageTest.java index 1178df3b..f3f873b6 100644 --- a/src/test/java/networking/p2p/StorageTest.java +++ b/src/test/java/networking/p2p/StorageTest.java @@ -1,5 +1,27 @@ package networking.p2p; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.lightchain.Identifier; +import network.Conduit; +import network.p2p.P2pConduit; +import network.p2p.P2pNetwork; +import networking.MockEngine; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import unittest.fixtures.EntityFixture; +import unittest.fixtures.IdentifierFixture; + /** * Encapsulates tests for p2p storage. */ @@ -26,4 +48,407 @@ public class StorageTest { // So a total of 10 unique entities are stored. // Then check the union of all stored entities across all storage components // should be exactly the 10 unique original entities that are stored. + + private static final int PORT_ZERO = 0; + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + + /** + * Creates 10 Networks each with 2 Engines on 2 different channels. Each Engine on each Network puts 100 Entities + * on their Channels, totaling 1000 Entities on each Channel. Tests that each Engine on each Channel can get all + * Entities on that Channel but none from the other Channel. + */ + @Test + void testOne() throws InterruptedException { + + int concurrencyDegree = 10; + + P2pNetwork[] networks = new P2pNetwork[10]; + Conduit[] conduits1 = new P2pConduit[100]; + Conduit[] conduits2 = new P2pConduit[100]; + + Entity[] entitiesForChannel1 = new Entity[1000]; + Entity[] entitiesForChannel2 = new Entity[1000]; + + for (int i = 0; i < networks.length; i++) { + + networks[i] = new P2pNetwork(IdentifierFixture.newIdentifier(), PORT_ZERO); + + MockEngine engineA1 = new MockEngine(); + conduits1[i] = networks[i].register(engineA1, channel1); + + MockEngine engineA2 = new MockEngine(); + conduits2[i] = networks[i].register(engineA2, channel2); + + } + + startNetworks(networks); + + Thread[] threads = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads[i] = new Thread(() -> { + + for (int j = 0; j < 100; j++) { + entitiesForChannel1[finalI * 100 + j] = new EntityFixture(); + entitiesForChannel2[finalI * 100 + j] = new EntityFixture(); + } + + }); + } + + for (Thread t : threads) { + t.start(); + t.join(); + } + + Thread[] threads2 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads2[i] = new Thread(() -> { + + for (int j = 0; j < 100; j++) { + entitiesForChannel1[finalI * 100 + j] = new EntityFixture(); + entitiesForChannel2[finalI * 100 + j] = new EntityFixture(); + try { + conduits1[finalI].put(entitiesForChannel1[finalI * 10 + j]); + conduits2[finalI].put(entitiesForChannel2[finalI * 10 + j]); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + }); + } + + for (Thread t : threads2) { + t.start(); + t.join(); + } + + Thread[] threads3 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads3[i] = new Thread(() -> { + + Set set1 = new HashSet(); + Set set2 = new HashSet(); + + for (int j = 0; j < 1000; j++) { + try { + set1.add(conduits1[finalI].get(entitiesForChannel1[j].id())); + set2.add(conduits2[finalI].get(entitiesForChannel2[j].id())); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + // Tests that every Entity for each Channel was retrieved by every Engine on that Channel + Assertions.assertEquals(1000, set1.size()); + Assertions.assertEquals(1000, set2.size()); + + }); + } + + for (Thread t : threads3) { + t.start(); + } + + Thread[] threads4 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads4[i] = new Thread(() -> { + + Set set1 = new HashSet(); + Set set2 = new HashSet(); + + for (int j = 0; j < 1000; j++) { + try { + set1.add(conduits2[finalI].get(entitiesForChannel1[j].id())); + set2.add(conduits1[finalI].get(entitiesForChannel2[j].id())); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + // Tests that none of the Entities for specific Channels were accessible by Engines on other Channels + Assertions.assertTrue(set1.contains(null)); + Assertions.assertTrue(set2.contains(null)); + Assertions.assertEquals(set1.size(), 1); + Assertions.assertEquals(set2.size(), 1); + + }); + } + + for (Thread t : threads4) { + t.start(); + } + + } + + /** + * Creates 10 Networks each with 2 Engines on 2 different channels. Each Engine on each Network puts 100 Entities + * on their Channels, totaling 1000 Entities on each Channel. Tests that the Distributed Storage Components + * cumulatively contain all the Entities, nothing else and no duplicates. + */ + @Test + void testTwo() throws InterruptedException { + + int concurrencyDegree = 10; + + P2pNetwork[] networks = new P2pNetwork[10]; + Conduit[] conduits1 = new P2pConduit[100]; + Conduit[] conduits2 = new P2pConduit[100]; + + Entity[] entitiesForChannel1 = new Entity[1000]; + Entity[] entitiesForChannel2 = new Entity[1000]; + + for (int i = 0; i < networks.length; i++) { + + networks[i] = new P2pNetwork(IdentifierFixture.newIdentifier(), PORT_ZERO); + + MockEngine engineA1 = new MockEngine(); + conduits1[i] = networks[i].register(engineA1, channel1); + + MockEngine engineA2 = new MockEngine(); + conduits2[i] = networks[i].register(engineA2, channel2); + + } + + startNetworks(networks); + + Thread[] threads = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads[i] = new Thread(() -> { + + for (int j = 0; j < 100; j++) { + entitiesForChannel1[finalI * 100 + j] = new EntityFixture(); + entitiesForChannel2[finalI * 100 + j] = new EntityFixture(); + } + + }); + } + + for (Thread t : threads) { + t.start(); + t.join(); + } + + Thread[] threads2 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads2[i] = new Thread(() -> { + + for (int j = 0; j < 100; j++) { + entitiesForChannel1[finalI * 100 + j] = new EntityFixture(); + entitiesForChannel2[finalI * 100 + j] = new EntityFixture(); + try { + conduits1[finalI].put(entitiesForChannel1[finalI * 10 + j]); + conduits2[finalI].put(entitiesForChannel2[finalI * 10 + j]); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + }); + } + + for (Thread t : threads2) { + t.start(); + t.join(); + } + + Thread[] threads3 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads3[i] = new Thread(() -> { + + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + + for (int j = 0; j < 1000; j++) { + try { + list1.add(conduits1[finalI].get(entitiesForChannel1[j].id())); + list2.add(conduits2[finalI].get(entitiesForChannel2[j].id())); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + // Tests that nothing but every unique Entity for each Channel was retrieved by every Engine on that Channel + Assertions.assertEquals(1000, list1.size()); + Assertions.assertEquals(1000, list2.size()); + + }); + } + + for (Thread t : threads3) { + t.start(); + } + + Thread[] threads4 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads4[i] = new Thread(() -> { + + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + + Set set1 = new HashSet(); + Set set2 = new HashSet(); + + boolean duplicatesFound = false; + + for (int j = 0; j < 1000; j++) { + try { + + if (set1.contains(conduits1[finalI].get(entitiesForChannel1[j].id())) + || set2.contains(conduits2[finalI].get(entitiesForChannel2[j].id()))) { + duplicatesFound = true; + } + + set1.add(conduits1[finalI].get(entitiesForChannel1[j].id())); + set2.add(conduits2[finalI].get(entitiesForChannel2[j].id())); + + list1.add(conduits2[finalI].get(entitiesForChannel1[j].id())); + list2.add(conduits1[finalI].get(entitiesForChannel2[j].id())); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + // Tests that none of the Entities for specific Channels were accessible by Engines on other Channels + Assertions.assertTrue(list1.contains(null)); + Assertions.assertTrue(list2.contains(null)); + Assertions.assertEquals(list1.size(), 1); + Assertions.assertEquals(list2.size(), 1); + + // Tests no storage elements contained duplicates + Assertions.assertTrue(!duplicatesFound); + + }); + } + + for (Thread t : threads4) { + t.start(); + } + + } + + /** + * Creates 10 Networks each with 2 Engines on 2 different channels. Each Engine on each Network puts 1 unique Entity + * on their Channels, totaling 10 Entities on each Channel. Tests that each Engine on each Channel can get all + * Entities on that Channel but with no duplicates. + */ + @Test + void testThree() throws InterruptedException { + + int concurrencyDegree = 10; + + P2pNetwork[] networks = new P2pNetwork[10]; + Conduit[] conduits1 = new P2pConduit[100]; + Conduit[] conduits2 = new P2pConduit[100]; + + Entity[] entitiesForChannel1 = new Entity[1000]; + Entity[] entitiesForChannel2 = new Entity[1000]; + + for (int i = 0; i < networks.length; i++) { + + networks[i] = new P2pNetwork(IdentifierFixture.newIdentifier(), PORT_ZERO); + + MockEngine engineA1 = new MockEngine(); + conduits1[i] = networks[i].register(engineA1, channel1); + + MockEngine engineA2 = new MockEngine(); + conduits2[i] = networks[i].register(engineA2, channel2); + + } + + startNetworks(networks); + + Thread[] threads2 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + threads2[i] = new Thread(() -> { + + Entity entityForChannel1 = new EntityFixture(); + Entity entityForChannel2 = new EntityFixture(); + + for (int j = 0; j < 100; j++) { + try { + conduits1[finalI].put(entityForChannel1); + conduits2[finalI].put(entityForChannel2); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + } + + }); + } + + for (Thread t : threads2) { + t.start(); + t.join(); + } + + Set set1 = new HashSet(); + Set set2 = new HashSet(); + + Thread[] threads3 = new Thread[concurrencyDegree]; + for (int i = 0; i < concurrencyDegree; i++) { + + try { + set1.addAll(conduits1[i].allEntities()); + set2.addAll(conduits2[i].allEntities()); + } catch (LightChainDistributedStorageException e) { + Assertions.fail(); + } + + } + + // Tests that when every Engine on 10 different Nodes registers a single unique Entity, the entire data across + // all Nodes for that Channel contains 10 unique Entities in total + Assertions.assertEquals(10, set1.size()); + Assertions.assertEquals(10, set2.size()); + + } + + private void startNetworks(P2pNetwork[] networks) { + Thread[] networkThreads = new Thread[networks.length]; + AtomicInteger threadErrorCount = new AtomicInteger(); + CountDownLatch done = new CountDownLatch(networks.length); + + for (int i = 0; i < networks.length; i++) { + int finalI = i; + networkThreads[i] = new Thread(() -> { + try { + networks[finalI].start(); + } catch (IOException e) { + threadErrorCount.incrementAndGet(); + } + done.countDown(); + }); + } + + for (int i = 0; i < networks.length; i++) { + networkThreads[i].start(); + } + try { + boolean doneOneTime = done.await(10, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + ConcurrentMap idToAddressMap = new ConcurrentHashMap<>(); + for (P2pNetwork network : networks) { + idToAddressMap.put(network.getId(), network.getAddress()); + } + + for (P2pNetwork network : networks) { + network.setIdToAddressMap(idToAddressMap); + } + } + }