summaryrefslogtreecommitdiff
path: root/src/main/java/net/cshift/api/transit/network
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2023-11-14 17:09:23 -0500
committerKyle Gunger <kgunger12@gmail.com>2023-11-14 17:09:23 -0500
commitd1680864edfbc7cec0e6decf1e3a2b30f120aa0e (patch)
treeb47154132e9f3212cc8ed4457e5d4dafee6f1124 /src/main/java/net/cshift/api/transit/network
parent80fe25bcb811508a5fe826c37513a8a63239cb55 (diff)
Update to 1.20.2
Diffstat (limited to 'src/main/java/net/cshift/api/transit/network')
-rw-r--r--src/main/java/net/cshift/api/transit/network/Channel.java142
-rw-r--r--src/main/java/net/cshift/api/transit/network/INode.java70
-rw-r--r--src/main/java/net/cshift/api/transit/network/ISystem.java15
-rw-r--r--src/main/java/net/cshift/api/transit/network/PoolManifest.java40
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java43
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java14
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java23
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java44
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java35
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java36
10 files changed, 462 insertions, 0 deletions
diff --git a/src/main/java/net/cshift/api/transit/network/Channel.java b/src/main/java/net/cshift/api/transit/network/Channel.java
new file mode 100644
index 0000000..bd4cdf2
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/Channel.java
@@ -0,0 +1,142 @@
+/*
+ MIT License
+
+ Copyright (c) 2023 Kyle Gunger
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+package net.cshift.api.transit.network;
+
+import net.cshift.api.transit.network.packet.IStaticPacket;
+
+/**
+ * @author Kyle Gunger
+ * @apiNote A channel represents a connection between two nodes. It is able to send data in packets, and serves as a way to organize incoming traffic.
+ * @param <D> The type of data the packets will be transfering
+ */
+public final class Channel<D> {
+ private INode to;
+ private int id;
+ private String group;
+
+ /** This constructor should be called by a node approving a connection. The approving node can give the connection an ID and group.
+ * Negative IDs indicate a terminated connection, so do not initialize the class with a negative ID.
+ *
+ * @param node The recieving node
+ * @param id The channel's id, as assigned by the recieving node. In most cases, this will match the pool ID as a way to match channels to pools.
+ * @param group
+ */
+ public Channel(INode node, int id, String group)
+ {
+ to = node;
+ this.id = id;
+ this.group = group;
+ }
+
+
+
+ // ####################
+ // # Channel specific #
+ // ####################
+
+ /** The recieving INode
+ *
+ * @return
+ */
+ public INode getReciever()
+ {
+ return to;
+ }
+
+ /** The ID of the connection, assigned by the recieving INode
+ *
+ * @return
+ */
+ public int getID()
+ {
+ return id;
+ }
+
+ /** The group that the channel operates on
+ *
+ * @return
+ */
+ public String getGroup() {
+ return group;
+ }
+
+ /** Returns true if the connection has been terminated
+ *
+ * @return
+ */
+ public boolean isTerminated()
+ {
+ return id < 0;
+ }
+
+
+
+ // ################################
+ // # Info from the recieving node #
+ // ################################
+
+ /** Pressure
+ *
+ * @apiNote This part of the api is not properly documented yet, and it's use is not reccommended for cross-mod communications.
+ * @return A Number representing the pressure from the channel (in base group units).
+ */
+ public Number pressure()
+ {
+ return to.getPressure(this);
+ }
+
+ /** Max transfer rate
+ *
+ * @return A Number representing the max transfer rate from the channel (in base group units per tick).
+ */
+ public Number rate()
+ {
+ return to.getRate(this);
+ }
+
+
+
+ // ################################
+ // # Interact with the other node #
+ // ################################
+
+ /** Terminates the connection and relays the termination to the recieving node
+ */
+ public void terminate()
+ {
+ id = -1;
+ to.onTerminate(this);
+ }
+
+ /** Send a packet to the recieving node
+ *
+ * @param packet the packet to send
+ * @return The overflow data if the packet is only partially accepted. {@code null} otherwise.
+ */
+ public IStaticPacket<D> send(IStaticPacket<D> packet)
+ {
+ return to.accept(packet, this);
+ }
+}
diff --git a/src/main/java/net/cshift/api/transit/network/INode.java b/src/main/java/net/cshift/api/transit/network/INode.java
new file mode 100644
index 0000000..54701e7
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/INode.java
@@ -0,0 +1,70 @@
+package net.cshift.api.transit.network;
+
+import net.cshift.api.transit.network.packet.*;
+
+/**
+ * @author Kyle Gunger
+ * @apiNote A node inside or outside a system.
+ */
+public interface INode
+{
+ /** Returns a pool manifest for the INode
+ *
+ */
+ public PoolManifest getManifest();
+
+ /** Get the system managing the node or {@code null} if there isn't one
+ *
+ */
+ public ISystem getSystem();
+
+
+
+ // ###############
+ // # Connections #
+ // ###############
+
+ /** Call this function to establish a connection with a node.
+ *
+ * @param <T> The type of connection being asked for
+ * @param poolID The ID of the pool the channel will interface with (see PoolManifest)
+ * @param asker The asking node
+ * @return A channel if the node accepts the request, {@code null} otherwise
+ */
+ public <T> Channel<T> connect(int poolID, INode asker);
+
+ /** Accept a packet from a channel (or not).
+ *
+ * @apiNote Do not call this function, use Channel.send(packet) instead.
+ * @param <T> The type of the packet and channel
+ * @param packet The packet to be vetted
+ * @param channel The channel which the packet is coming through
+ * @return The overflow data if the packet is only partially accepted. {@code null} otherwise.
+ */
+ public <T> IStaticPacket<T> accept(IStaticPacket<T> packet, Channel<T> channel);
+
+ /** Pressure
+ *
+ * @apiNote Do not call this function, use Channel.pressure() instead.
+ * @param channel The channel asking for the pressure
+ * @return A Number representing the pressure from the channel (in base group units).
+ */
+ public <T> Number getPressure(Channel<T> channel);
+
+ /** Transfer rate
+ *
+ * @apiNote Do not call this function, use Channel.rate() instead.
+ * @param channel The channel asking for the transfer rate
+ * @return A Number representing the transfer rate from the channel (in base group units per tick).
+ */
+ public <T> Number getRate(Channel<T> channel);
+
+ /** Called when a channel is terminated
+ *
+ * @apiNote Do not call this function, use Channel.terminate() instead.
+ * @param <T> The type of the channel
+ * @param channel The channel being terminated
+ */
+ public <T> void onTerminate(Channel<T> channel);
+
+}
diff --git a/src/main/java/net/cshift/api/transit/network/ISystem.java b/src/main/java/net/cshift/api/transit/network/ISystem.java
new file mode 100644
index 0000000..b7ab9bf
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/ISystem.java
@@ -0,0 +1,15 @@
+package net.cshift.api.transit.network;
+
+
+/**
+ * @author Kyle Gunger
+ * @apiNote An ISystem represents a system of nodes which has been optimized for performance. A node can exist on its own, but an ISystem can't exist without a node.
+ */
+public interface ISystem
+{
+ /**The nodes stored by the system
+ *
+ * @return INode[]
+ */
+ public INode[] getNodes();
+}
diff --git a/src/main/java/net/cshift/api/transit/network/PoolManifest.java b/src/main/java/net/cshift/api/transit/network/PoolManifest.java
new file mode 100644
index 0000000..26cbece
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/PoolManifest.java
@@ -0,0 +1,40 @@
+package net.cshift.api.transit.network;
+
+/**
+ * @author Kyle Gunger
+ * @apiNote A pool manifest represents a set of possible data/resource pools that another node can request a channel to.
+ */
+public abstract class PoolManifest {
+
+ /** Represents the number of pools that the node has access to for the specified resource.
+ * @apiNote A "pool" in this context represents an independant network of resources.
+ * Pool zero should be the default group that simple nodes will attempt to connect to.
+ * @param group The TypeGroup that the pool belongs to
+ */
+ public abstract int poolCount(String group);
+
+ /** The ID of the pool. The INode will use this in a connection attempt with the other INode.
+ *
+ * @param group The TypeGroup the pool belongs to
+ * @param pool Array-like index for pool (gotten from poolCount)
+ */
+ public abstract int poolID(String group, int pool);
+
+ /** If the mod supports named pools, the names can be querried through this function.
+ *
+ * @param group The TypeGroup the pool belongs to
+ * @param pool Array-like index for pool (gotten from poolCount)
+ */
+ public String poolName(String group, int pool) {
+ return "";
+ }
+
+ /** If the mod supports pool descriptions, they can be accessed by this method.
+ *
+ * @param group The TypeGroup the pool belongs to
+ * @param pool Array-like index for pool (gotten from poolCount)
+ */
+ public String poolDescription(String group, int pool) {
+ return "";
+ }
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java
new file mode 100644
index 0000000..f3a6c47
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java
@@ -0,0 +1,43 @@
+package net.cshift.api.transit.network.packet;
+
+import net.cshift.api.transit.type.Type;
+
+/**
+ * Simple packet which stores a fluid value.
+ *
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers
+ */
+public class DynamicPacket<D> implements IDynamicPacket<D> {
+ private D data;
+ private Type<D> type;
+
+ /** Constructor. Stores the given data and uses the given type.
+ *
+ * @param dat The packet's data
+ * @param t The packet's type
+ */
+ public DynamicPacket(D dat, Type<D> t)
+ {
+ data = dat;
+ type = t;
+ }
+
+ @Override
+ public D getData()
+ {
+ return data;
+ }
+
+ @Override
+ public void setData(D dat)
+ {
+ data = dat;
+ }
+
+ @Override
+ public Type<D> getType() {
+ return type;
+ }
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java
new file mode 100644
index 0000000..334d5f6
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java
@@ -0,0 +1,14 @@
+package net.cshift.api.transit.network.packet;
+
+/** Interface describing a fluid packet.
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers
+ */
+public interface IDynamicPacket<D> extends IStaticPacket<D>{
+ /**Set the packet's data.
+ *
+ * @return <D> The packet's data
+ */
+ public void setData(D dat);
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java
new file mode 100644
index 0000000..ba1b17f
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java
@@ -0,0 +1,23 @@
+package net.cshift.api.transit.network.packet;
+
+import net.cshift.api.transit.type.*;
+
+/** Interface describing an unchanging packet.
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers.
+ */
+public interface IStaticPacket<D>
+{
+ /**Get the packet's data.
+ *
+ * @return <D> The packet's data
+ */
+ public D getData();
+
+ /**Get the packet's type.
+ *
+ * @return IType<<D>> The type of the packet
+ */
+ public Type<D> getType();
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java
new file mode 100644
index 0000000..d382645
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java
@@ -0,0 +1,44 @@
+package net.cshift.api.transit.network.packet;
+
+import net.cshift.api.transit.type.Type;
+
+/** Static packet with extra data attached.
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers
+ * @param <M> The type of metadata
+*/
+public class MetaDynamicPacket<D, M> extends DynamicPacket<D>
+{
+ private M metaData;
+
+ /** Constructor
+ *
+ * @param dat The data to store
+ * @param t The Type of the data
+ * @param meta The metadata to store
+ */
+ public MetaDynamicPacket(D dat, Type<D> t, M meta)
+ {
+ super(dat, t);
+ metaData = meta;
+ }
+
+ /** Get the metadata of the packet.
+ *
+ * @return The packet's metadata
+ */
+ public M getMetaData()
+ {
+ return metaData;
+ }
+
+ /** Set the metadata of the packet.
+ *
+ * @param meta The packet's new metadata
+ */
+ public void setMetaData(M meta)
+ {
+ metaData = meta;
+ }
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java
new file mode 100644
index 0000000..aea1b83
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java
@@ -0,0 +1,35 @@
+package net.cshift.api.transit.network.packet;
+
+import net.cshift.api.transit.type.*;
+
+/** Static packet with extra data attached.
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers
+ * @param <M> The type of metadata
+*/
+public class MetaPacket<D, M> extends StaticPacket<D>
+{
+ private M metaData;
+
+ /** Constructor
+ *
+ * @param dat The data to store
+ * @param t The Type of the data
+ * @param meta The metadata to store
+ */
+ public MetaPacket(D dat, Type<D> t, M meta)
+ {
+ super(dat, t);
+ metaData = meta;
+ }
+
+ /** Get the metadata of the packet.
+ *
+ * @return The packet's metadata
+ */
+ public M getMetaData()
+ {
+ return metaData;
+ }
+}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java
new file mode 100644
index 0000000..2c6a16a
--- /dev/null
+++ b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java
@@ -0,0 +1,36 @@
+package net.cshift.api.transit.network.packet;
+
+import net.cshift.api.transit.type.*;
+
+/** Simple packet which stores an unchanging value.
+ * @author Kyle Gunger
+ *
+ * @param <D> The data type (Object) that the packet transfers.
+ */
+public class StaticPacket<D> implements IStaticPacket<D>
+{
+ private D data;
+ private Type<D> type;
+
+ /** Constructor. Stores the given data and uses the given type.
+ *
+ * @param dat The packet's data
+ * @param t The packet's type
+ */
+ public StaticPacket(D dat, Type<D> t)
+ {
+ data = dat;
+ type = t;
+ }
+
+ @Override
+ public D getData()
+ {
+ return data;
+ }
+
+ @Override
+ public Type<D> getType() {
+ return type;
+ }
+}