Skip to content

Commit

Permalink
cleaning up network a lot; fixed cc comps not being able to send wire…
Browse files Browse the repository at this point in the history
…less messages via access points; started wireless api
  • Loading branch information
fnuecke committed Mar 10, 2014
1 parent de00d88 commit 2ef86cf
Show file tree
Hide file tree
Showing 18 changed files with 353 additions and 173 deletions.
50 changes: 44 additions & 6 deletions src/main/java/li/cil/oc/api/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import li.cil.oc.api.detail.Builder;
import li.cil.oc.api.detail.NetworkAPI;
import li.cil.oc.api.network.Environment;
import li.cil.oc.api.network.Node;
import li.cil.oc.api.network.Visibility;
import li.cil.oc.api.network.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

/**
Expand Down Expand Up @@ -42,7 +41,8 @@ public final class Network {
* @param tileEntity the tile entity to initialize.
*/
public static void joinOrCreateNetwork(final TileEntity tileEntity) {
if (instance != null) instance.joinOrCreateNetwork(tileEntity);
if (instance != null)
instance.joinOrCreateNetwork(tileEntity);
}

/**
Expand All @@ -56,9 +56,34 @@ public static void joinOrCreateNetwork(final TileEntity tileEntity) {
* @throws IllegalArgumentException if the node already is in a network.
*/
public static void joinNewNetwork(final Node node) {
if (instance != null) instance.joinNewNetwork(node);
if (instance != null)
instance.joinNewNetwork(node);
}

public static void joinWirelessNetwork(final WirelessEndpoint endpoint) {
if (instance != null)
instance.joinWirelessNetwork(endpoint);
}

public static void updateWirelessNetwork(final WirelessEndpoint endpoint) {
if (instance != null)
instance.updateWirelessNetwork(endpoint);
}

public static void leaveWirelessNetwork(final WirelessEndpoint endpoint) {
if (instance != null)
instance.leaveWirelessNetwork(endpoint);
}

// ----------------------------------------------------------------------- //

public static void sendWirelessPacket(final WirelessEndpoint source, final double strength, final Packet packet) {
if (instance != null)
instance.sendWirelessPacket(source, strength, packet);
}

// ----------------------------------------------------------------------- //

/**
* Factory function for creating new nodes.
* <p/>
Expand Down Expand Up @@ -97,7 +122,20 @@ public static void joinNewNetwork(final Node node) {
* @return a new node builder.
*/
public static Builder.NodeBuilder newNode(final Environment host, final Visibility reachability) {
if (instance != null) return instance.newNode(host, reachability);
if (instance != null)
return instance.newNode(host, reachability);
return null;
}

public static Packet newPacket(final String source, final String destination, final int port, final Object[] data) {
if (instance != null)
return instance.newPacket(source, destination, port, data);
return null;
}

public static Packet newPacket(final NBTTagCompound nbt) {
if (instance != null)
return instance.newPacket(nbt);
return null;
}

Expand Down
21 changes: 18 additions & 3 deletions src/main/java/li/cil/oc/api/detail/NetworkAPI.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package li.cil.oc.api.detail;

import li.cil.oc.api.network.Environment;
import li.cil.oc.api.network.Node;
import li.cil.oc.api.network.Visibility;
import li.cil.oc.api.network.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public interface NetworkAPI {
Expand Down Expand Up @@ -35,6 +34,18 @@ public interface NetworkAPI {
*/
void joinNewNetwork(Node node);

void joinWirelessNetwork(WirelessEndpoint endpoint);

void updateWirelessNetwork(WirelessEndpoint endpoint);

void leaveWirelessNetwork(WirelessEndpoint endpoint);

// ----------------------------------------------------------------------- //

void sendWirelessPacket(WirelessEndpoint source, double strength, Packet packet);

// ----------------------------------------------------------------------- //

/**
* Factory function for creating new nodes.
* <p/>
Expand Down Expand Up @@ -73,4 +84,8 @@ public interface NetworkAPI {
* @return a new node builder.
*/
Builder.NodeBuilder newNode(Environment host, Visibility reachability);

Packet newPacket(String source, String destination, int port, Object[] data);

Packet newPacket(NBTTagCompound nbt);
}
60 changes: 60 additions & 0 deletions src/main/java/li/cil/oc/api/network/Packet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package li.cil.oc.api.network;

import net.minecraft.nbt.NBTTagCompound;

/**
* These packets represent messages sent using a network card or wireless
* network card, and can be relayed by the switch and access point blocks.
* <p/>
* These will be sent as the payload of <tt>network.message</tt> messages.
* <p/>
* <em>Important</em>: do <em>not</em> implement this interface. Use the factory
* methods in {@link li.cil.oc.api.Network} instead.
*/
public interface Packet {
/**
* The address of the <em>original</em> sender of this packet.
*/
String source();

/**
* The address of the destination of the packet. This is <tt>null</tt> for
* broadcast packets.
*/
String destination();

/**
* The port this packet is being sent to.
*/
int port();

/**
* The payload of the packet. This will usually only contain simple types,
* to allow persisting the packet.
*/
Object[] data();

/**
* The remaining 'time to live' for this packet. When a packet with a TTL of
* zero is received it will not be relayed by switches and access points. It
* will however still be received by a network card.
*/
int ttl();

/**
* Generates a copy of the packet, with a reduced time to live.
* <p/>
* This is called by switches and access points to generate relayed packets.
*
* @return a copy of this packet with a reduced TTL.
*/
Packet hop();

/**
* Saves the packet's data to the specified compound tag.
* <p/>
* Restore a packet saved like this using the factory method in the
* {@link li.cil.oc.api.Network} class.
*/
void save(NBTTagCompound nbt);
}
38 changes: 38 additions & 0 deletions src/main/java/li/cil/oc/api/network/WirelessEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package li.cil.oc.api.network;

import net.minecraft.world.World;

/**
* Interface for wireless endpoints that can be registered with the internal
* wireless network registry.
* <p/>
*/
public interface WirelessEndpoint {
/**
* The X coordinate of the endpoint in the world, in block coordinates.
*/
int x();

/**
* The Y coordinate of the endpoint in the world, in block coordinates.
*/
int y();

/**
* The Z coordinate of the endpoint in the world, in block coordinates.
*/
int z();

/**
* The world this endpoint lives in.
*/
World world();

/**
* Makes the endpoint receive a single packet.
*
* @param packet the packet to receive.
* @param distance the distance to the wireless endpoint that sent the packet.
*/
void receivePacket(Packet packet, double distance);
}
2 changes: 0 additions & 2 deletions src/main/resources/assets/opencomputers/robot.names
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# explicitly named using an anvil.
# If your name is on this list and you'd rather it not be, or you'd like a
# different alias, sorry! Please make a pull request with the changed list.
asie
crafteverywhere
LordFokas
Michiyo
Expand Down Expand Up @@ -30,4 +29,3 @@ R2-D2
Wheatley
Terminator T-1000
HAL 9000
Robot Devil
2 changes: 1 addition & 1 deletion src/main/scala/li/cil/oc/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Settings(config: Config) {
val textAntiAlias = config.getBoolean("client.textAntiAlias")
val pasteShortcut = config.getStringList("client.pasteShortcut").toSet
val robotLabels = config.getBoolean("client.robotLabels")
val rTreeDebugRenderer = false // *Not* to be configurable via config file.
val rTreeDebugRenderer = true // *Not* to be configurable via config file.

// ----------------------------------------------------------------------- //
// computer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package li.cil.oc.client.renderer

import li.cil.oc.util.{WirelessNetwork, RenderState}
import li.cil.oc.util.RenderState
import net.minecraft.client.Minecraft
import net.minecraftforge.client.event.RenderWorldLastEvent
import net.minecraftforge.event.ForgeSubscribe
import org.lwjgl.opengl.GL11
import li.cil.oc.server.network.WirelessNetwork

object WirelessNetworkDebugRenderer {
val colors = Array(0xFF0000, 0x00FFFF, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0xFFFFFF, 0x000000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ object CableRenderer extends TileEntitySpecialRenderer {
override def renderTileEntityAt(t: TileEntity, x: Double, y: Double, z: Double, f: Float) {
val cable = t.asInstanceOf[Cable]

GL11.glEnable(GL11.GL_LIGHTING)
GL11.glTranslated(x, y, z)
renderCable(cable.neighbors)
GL11.glTranslated(-x, -y, -z)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ object RobotRenderer extends TileEntitySpecialRenderer {
GL11.glEnable(GL11.GL_TEXTURE_2D) // For the font.
f.drawString(name, -halfWidth, 0, 0xFFFFFFFF)

GL11.glDepthMask(true)
GL11.glEnable(GL11.GL_LIGHTING)
GL11.glDisable(GL11.GL_BLEND)
GL11.glColor4f(1, 1, 1, 1)

GL11.glPopMatrix()
}

GL11.glDepthMask(true)
GL11.glEnable(GL11.GL_LIGHTING)
GL11.glDisable(GL11.GL_BLEND)
GL11.glColor4f(1, 1, 1, 1)

// If the move started while we were rendering and we have a reference to
// the *old* proxy the robot would be rendered at the wrong position, so we
// correct for the offset.
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/li/cil/oc/common/Proxy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import li.cil.oc.server.component.Keyboard
import li.cil.oc.server.component.machine
import li.cil.oc.server.component.machine.{LuaJLuaArchitecture, NativeLuaArchitecture}
import li.cil.oc.server.{TickHandler, driver, fs, network}
import li.cil.oc.util.{LuaStateFactory, WirelessNetwork}
import li.cil.oc.util.LuaStateFactory
import net.minecraftforge.common.MinecraftForge
import li.cil.oc.server.network.WirelessNetwork

class Proxy {
def preInit(e: FMLPreInitializationEvent): Unit = {
Expand Down
13 changes: 7 additions & 6 deletions src/main/scala/li/cil/oc/common/tileentity/Hub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package li.cil.oc.common.tileentity

import cpw.mods.fml.relauncher.{Side, SideOnly}
import li.cil.oc.api.network._
import li.cil.oc.server.component.NetworkCard
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.{api, Settings}
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.common.ForgeDirection
import scala.collection.mutable
import net.minecraft.entity.player.EntityPlayer

trait Hub extends Environment with SidedEnvironment with Analyzable {
val queueSize = 20

protected val plugs = ForgeDirection.VALID_DIRECTIONS.map(side => new Plug(side))

protected val queue = mutable.Queue.empty[(ForgeDirection, NetworkCard.Packet)]
protected val queue = mutable.Queue.empty[(ForgeDirection, Packet)]

// ----------------------------------------------------------------------- //

Expand All @@ -38,7 +39,7 @@ trait Hub extends Environment with SidedEnvironment with Analyzable {
}
}

protected def relayPacket(sourceSide: ForgeDirection, packet: NetworkCard.Packet) {
protected def relayPacket(sourceSide: ForgeDirection, packet: Packet) {
for (side <- ForgeDirection.VALID_DIRECTIONS if side != sourceSide) {
sidedNode(side).sendToReachable("network.message", packet)
}
Expand All @@ -51,7 +52,7 @@ trait Hub extends Environment with SidedEnvironment with Analyzable {
}
nbt.getTagList(Settings.namespace + "queue").foreach[NBTTagCompound](tag => {
val side = ForgeDirection.getOrientation(tag.getInteger("side"))
val packet = NetworkCard.loadPacket(tag)
val packet = api.Network.newPacket(tag)
queue += side -> packet
})
}
Expand Down Expand Up @@ -101,7 +102,7 @@ trait Hub extends Environment with SidedEnvironment with Analyzable {

protected def onPlugMessage(plug: Plug, message: Message) {
if (message.name == "network.message") message.data match {
case Array(packet: NetworkCard.Packet) if packet.ttl > 0 && queue.size < 20 => queue += plug.side -> packet.hop()
case Array(packet: Packet) if packet.ttl > 0 && queue.size < queueSize => queue += plug.side -> packet.hop()
case _ =>
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/li/cil/oc/common/tileentity/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package li.cil.oc.common.tileentity

import cpw.mods.fml.common.{Loader, Optional}
import dan200.computer.api.{ILuaContext, IComputerAccess, IPeripheral}
import li.cil.oc.api.network.Message
import li.cil.oc.api
import li.cil.oc.api.network.{Packet, Message}
import scala.collection.mutable
import li.cil.oc.server.component.NetworkCard.Packet

@Optional.Interface(iface = "dan200.computer.api.IPeripheral", modid = "ComputerCraft")
class Router extends Hub with IPeripheral {
Expand Down Expand Up @@ -53,9 +53,8 @@ class Router extends Hub with IPeripheral {
val sendPort = checkPort(arguments, 0)
val answerPort = checkPort(arguments, 1)
val data = Seq(Int.box(answerPort)) ++ arguments.drop(2)
plugs.foreach(plug => {
plug.node.sendToReachable("network.message", new Packet(plug.node.address, None, sendPort, data))
})
val packet = api.Network.newPacket(s"cc${computer.getID}_${computer.getAttachmentName}", null, sendPort, data.toArray)
relayPacket(null, packet)
null
case "isWireless" => Array(java.lang.Boolean.FALSE)
case _ => null
Expand All @@ -73,9 +72,10 @@ class Router extends Hub with IPeripheral {
port
}

protected def queueMessage(port: Int, answerPort: Int, args: Seq[AnyRef]) {
protected def queueMessage(source: String, destination: String, port: Int, answerPort: Int, args: Array[AnyRef]) {
for (computer <- computers.map(_.asInstanceOf[IComputerAccess])) {
if (openPorts(computer).contains(port))
val address = s"cc${computer.getID}_${computer.getAttachmentName}"
if (source != address && Option(destination).forall(_ == address) && openPorts(computer).contains(port))
computer.queueEvent("modem_message", Array(Seq(computer.getAttachmentName, Int.box(port), Int.box(answerPort)) ++ args.map {
case x: Array[Byte] => new String(x, "UTF-8")
case x => x
Expand All @@ -90,9 +90,9 @@ class Router extends Hub with IPeripheral {
case Array(packet: Packet) =>
packet.data.headOption match {
case Some(answerPort: java.lang.Double) =>
queueMessage(packet.port, answerPort.toInt, packet.data.drop(1).toSeq)
queueMessage(packet.source, packet.destination, packet.port, answerPort.toInt, packet.data.drop(1))
case _ =>
queueMessage(packet.port, -1, packet.data.toSeq)
queueMessage(packet.source, packet.destination, packet.port, -1, packet.data)
}
case _ =>
}
Expand Down
Loading

0 comments on commit 2ef86cf

Please sign in to comment.