summaryrefslogtreecommitdiff
path: root/src/main/java/net/cshift/transit/network/Channel.java
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2023-11-18 14:30:50 -0500
committerKyle Gunger <kgunger12@gmail.com>2023-11-18 14:30:50 -0500
commit0d147b3a184fc311bd3dae6bf1c974722753b3bf (patch)
tree33d3ef5f8b37831f4704613e98f76a5f11f1f3d4 /src/main/java/net/cshift/transit/network/Channel.java
parent5c5e1b3dd0985986a941a65726f8f2d7bff7a188 (diff)
Change namespace againHEADmain
Diffstat (limited to 'src/main/java/net/cshift/transit/network/Channel.java')
-rw-r--r--src/main/java/net/cshift/transit/network/Channel.java146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/main/java/net/cshift/transit/network/Channel.java b/src/main/java/net/cshift/transit/network/Channel.java
new file mode 100644
index 0000000..311ed4b
--- /dev/null
+++ b/src/main/java/net/cshift/transit/network/Channel.java
@@ -0,0 +1,146 @@
+/*
+ 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.transit.network;
+
+import net.cshift.transit.network.packet.IStaticPacket;
+import net.cshift.transit.type.group.GroupRegistry;
+import net.cshift.transit.type.group.TypeGroup;
+
+/**
+ * @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;
+ private Class<D> baseClass;
+
+ /** 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, Class<D> baseClass)
+ {
+ to = node;
+ this.id = id;
+ this.group = group;
+ this.baseClass = baseClass;
+ }
+
+
+
+ // ####################
+ // # 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 TypeGroup<D> getGroup() {
+ return GroupRegistry.<D>groupByID(group, baseClass);
+ }
+
+ /** 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.<D>accept(packet, this);
+ }
+}