mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-13 01:10:19 -04:00
add wake message support to link cards
same api as network card closes #2753
This commit is contained in:
parent
cc0fddb914
commit
6d5dd948b5
@ -20,7 +20,7 @@ import net.minecraft.nbt.NBTTagCompound
|
|||||||
import scala.collection.convert.WrapAsJava._
|
import scala.collection.convert.WrapAsJava._
|
||||||
import scala.collection.convert.WrapAsScala._
|
import scala.collection.convert.WrapAsScala._
|
||||||
|
|
||||||
class LinkedCard extends prefab.ManagedEnvironment with QuantumNetwork.QuantumNode with DeviceInfo {
|
class LinkedCard extends prefab.ManagedEnvironment with QuantumNetwork.QuantumNode with DeviceInfo with traits.WakeMessageAware {
|
||||||
override val node = Network.newNode(this, Visibility.Network).
|
override val node = Network.newNode(this, Visibility.Network).
|
||||||
withComponent("tunnel", Visibility.Neighbors).
|
withComponent("tunnel", Visibility.Neighbors).
|
||||||
withConnector().
|
withConnector().
|
||||||
@ -60,10 +60,7 @@ class LinkedCard extends prefab.ManagedEnvironment with QuantumNetwork.QuantumNo
|
|||||||
@Callback(direct = true, doc = "function():number -- Gets the maximum packet size (config setting).")
|
@Callback(direct = true, doc = "function():number -- Gets the maximum packet size (config setting).")
|
||||||
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
||||||
|
|
||||||
def receivePacket(packet: Packet) {
|
def receivePacket(packet: Packet): Unit = receivePacket(packet, 0, null)
|
||||||
val distance = 0
|
|
||||||
node.sendToReachable("computer.signal", Seq("modem_message", packet.source, Int.box(packet.port), Double.box(distance)) ++ packet.data: _*)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Callback(direct = true, doc = "function():string -- Gets this link card's shared channel address")
|
@Callback(direct = true, doc = "function():string -- Gets this link card's shared channel address")
|
||||||
def getChannel(context: Context, args: Arguments): Array[AnyRef] = {
|
def getChannel(context: Context, args: Arguments): Array[AnyRef] = {
|
||||||
@ -93,10 +90,12 @@ class LinkedCard extends prefab.ManagedEnvironment with QuantumNetwork.QuantumNo
|
|||||||
if (nbt.hasKey(Settings.namespace + "tunnel")) {
|
if (nbt.hasKey(Settings.namespace + "tunnel")) {
|
||||||
tunnel = nbt.getString(Settings.namespace + "tunnel")
|
tunnel = nbt.getString(Settings.namespace + "tunnel")
|
||||||
}
|
}
|
||||||
|
loadWakeMessage(nbt)
|
||||||
}
|
}
|
||||||
|
|
||||||
override def save(nbt: NBTTagCompound) {
|
override def save(nbt: NBTTagCompound) {
|
||||||
super.save(nbt)
|
super.save(nbt)
|
||||||
nbt.setString(Settings.namespace + "tunnel", tunnel)
|
nbt.setString(Settings.namespace + "tunnel", tunnel)
|
||||||
|
saveWakeMessage(nbt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import scala.collection.convert.WrapAsJava._
|
|||||||
import scala.collection.convert.WrapAsScala._
|
import scala.collection.convert.WrapAsScala._
|
||||||
import scala.collection.mutable
|
import scala.collection.mutable
|
||||||
|
|
||||||
class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment with RackBusConnectable with DeviceInfo {
|
class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment with RackBusConnectable with DeviceInfo with traits.WakeMessageAware {
|
||||||
protected val visibility = host match {
|
protected val visibility = host match {
|
||||||
case _: Rack => Visibility.Neighbors
|
case _: Rack => Visibility.Neighbors
|
||||||
case _ => Visibility.Network
|
case _ => Visibility.Network
|
||||||
@ -41,10 +41,6 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment w
|
|||||||
// wired network card is the 1st in the max ports list (before both wireless cards)
|
// wired network card is the 1st in the max ports list (before both wireless cards)
|
||||||
protected def maxOpenPorts = Settings.get.maxOpenPorts(Tier.One)
|
protected def maxOpenPorts = Settings.get.maxOpenPorts(Tier.One)
|
||||||
|
|
||||||
protected var wakeMessage: Option[String] = None
|
|
||||||
|
|
||||||
protected var wakeMessageFuzzy = false
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
|
||||||
private final lazy val deviceInfo = Map(
|
private final lazy val deviceInfo = Map(
|
||||||
@ -120,23 +116,6 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment w
|
|||||||
@Callback(direct = true, doc = """function():number -- Gets the maximum packet size (config setting).""")
|
@Callback(direct = true, doc = """function():number -- Gets the maximum packet size (config setting).""")
|
||||||
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
||||||
|
|
||||||
@Callback(direct = true, doc = """function():string, boolean -- Get the current wake-up message.""")
|
|
||||||
def getWakeMessage(context: Context, args: Arguments): Array[AnyRef] = result(wakeMessage.orNull, wakeMessageFuzzy)
|
|
||||||
|
|
||||||
@Callback(doc = """function(message:string[, fuzzy:boolean]):string -- Set the wake-up message and whether to ignore additional data/parameters.""")
|
|
||||||
def setWakeMessage(context: Context, args: Arguments): Array[AnyRef] = {
|
|
||||||
val oldMessage = wakeMessage
|
|
||||||
val oldFuzzy = wakeMessageFuzzy
|
|
||||||
|
|
||||||
if (args.optAny(0, null) == null)
|
|
||||||
wakeMessage = None
|
|
||||||
else
|
|
||||||
wakeMessage = Option(args.checkString(0))
|
|
||||||
wakeMessageFuzzy = args.optBoolean(1, wakeMessageFuzzy)
|
|
||||||
|
|
||||||
result(oldMessage.orNull, oldFuzzy)
|
|
||||||
}
|
|
||||||
|
|
||||||
protected def doSend(packet: Packet) = visibility match {
|
protected def doSend(packet: Packet) = visibility match {
|
||||||
case Visibility.Neighbors => node.sendToNeighbors("network.message", packet)
|
case Visibility.Neighbors => node.sendToNeighbors("network.message", packet)
|
||||||
case Visibility.Network => node.sendToReachable("network.message", packet)
|
case Visibility.Network => node.sendToReachable("network.message", packet)
|
||||||
@ -163,58 +142,36 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment w
|
|||||||
if ((message.name == "computer.stopped" || message.name == "computer.started") && node.isNeighborOf(message.source))
|
if ((message.name == "computer.stopped" || message.name == "computer.started") && node.isNeighborOf(message.source))
|
||||||
openPorts.clear()
|
openPorts.clear()
|
||||||
if (message.name == "network.message") message.data match {
|
if (message.name == "network.message") message.data match {
|
||||||
case Array(packet: Packet) => receivePacket(packet, 0)
|
case Array(packet: Packet) => receivePacket(packet)
|
||||||
case _ =>
|
case _ =>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected def receivePacket(packet: Packet, distance: Double) {
|
override protected def isPacketAccepted(packet: Packet, distance: Double): Boolean = {
|
||||||
if (packet.source != node.address && Option(packet.destination).forall(_ == node.address)) {
|
if (super.isPacketAccepted(packet, distance)) {
|
||||||
if (openPorts.contains(packet.port)) {
|
if (openPorts.contains(packet.port)) {
|
||||||
node.sendToReachable("computer.signal", Seq("modem_message", packet.source, Int.box(packet.port), Double.box(distance)) ++ packet.data: _*)
|
|
||||||
networkActivity()
|
networkActivity()
|
||||||
}
|
return true
|
||||||
// Accept wake-up messages regardless of port because we close all ports
|
|
||||||
// when our computer shuts down.
|
|
||||||
val wakeup: Boolean = packet.data match {
|
|
||||||
case Array(message: Array[Byte]) if wakeMessage.contains(new String(message, Charsets.UTF_8)) => true
|
|
||||||
case Array(message: String) if wakeMessage.contains(message) => true
|
|
||||||
case Array(message: Array[Byte], _*) if wakeMessageFuzzy && wakeMessage.contains(new String(message, Charsets.UTF_8)) => true
|
|
||||||
case Array(message: String, _*) if wakeMessageFuzzy && wakeMessage.contains(message) => true
|
|
||||||
case _ => false
|
|
||||||
}
|
|
||||||
if (wakeup) {
|
|
||||||
host match {
|
|
||||||
case ctx: Context => ctx.start()
|
|
||||||
case _ => node.sendToNeighbors("computer.start")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
override def receivePacket(packet: Packet): Unit = {
|
override def receivePacket(packet: Packet): Unit = receivePacket(packet, 0, host)
|
||||||
receivePacket(packet, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
|
||||||
override def load(nbt: NBTTagCompound) {
|
override def load(nbt: NBTTagCompound) {
|
||||||
super.load(nbt)
|
super.load(nbt)
|
||||||
|
|
||||||
assert(openPorts.isEmpty)
|
assert(openPorts.isEmpty)
|
||||||
openPorts ++= nbt.getIntArray("openPorts")
|
openPorts ++= nbt.getIntArray("openPorts")
|
||||||
if (nbt.hasKey("wakeMessage")) {
|
loadWakeMessage(nbt)
|
||||||
wakeMessage = Option(nbt.getString("wakeMessage"))
|
|
||||||
}
|
|
||||||
wakeMessageFuzzy = nbt.getBoolean("wakeMessageFuzzy")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override def save(nbt: NBTTagCompound) {
|
override def save(nbt: NBTTagCompound) {
|
||||||
super.save(nbt)
|
super.save(nbt)
|
||||||
|
|
||||||
nbt.setIntArray("openPorts", openPorts.toArray)
|
nbt.setIntArray("openPorts", openPorts.toArray)
|
||||||
wakeMessage.foreach(nbt.setString("wakeMessage", _))
|
saveWakeMessage(nbt)
|
||||||
nbt.setBoolean("wakeMessageFuzzy", wakeMessageFuzzy)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
@ -46,7 +46,7 @@ abstract class WirelessNetworkCard(host: EnvironmentHost) extends NetworkCard(ho
|
|||||||
def receivePacket(packet: Packet, source: WirelessEndpoint) {
|
def receivePacket(packet: Packet, source: WirelessEndpoint) {
|
||||||
val (dx, dy, dz) = ((source.x + 0.5) - host.xPosition, (source.y + 0.5) - host.yPosition, (source.z + 0.5) - host.zPosition)
|
val (dx, dy, dz) = ((source.x + 0.5) - host.xPosition, (source.y + 0.5) - host.yPosition, (source.z + 0.5) - host.zPosition)
|
||||||
val distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
val distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
||||||
receivePacket(packet, distance)
|
receivePacket(packet, distance, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package li.cil.oc.server.component.traits
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets
|
||||||
|
import li.cil.oc.api.machine.Arguments
|
||||||
|
import li.cil.oc.api.machine.Callback
|
||||||
|
import li.cil.oc.api.machine.Context
|
||||||
|
import li.cil.oc.api.network.{EnvironmentHost, Packet}
|
||||||
|
import li.cil.oc.server.component._
|
||||||
|
import net.minecraft.nbt.NBTTagCompound
|
||||||
|
|
||||||
|
trait WakeMessageAware extends traits.NetworkAware {
|
||||||
|
protected var wakeMessage: Option[String] = None
|
||||||
|
|
||||||
|
protected var wakeMessageFuzzy: Boolean = false
|
||||||
|
|
||||||
|
@Callback(direct = true, doc = """function():string, boolean -- Get the current wake-up message.""")
|
||||||
|
def getWakeMessage(context: Context, args: Arguments): Array[AnyRef] = result(wakeMessage.orNull, wakeMessageFuzzy)
|
||||||
|
|
||||||
|
@Callback(doc = """function(message:string[, fuzzy:boolean]):string -- Set the wake-up message and whether to ignore additional data/parameters.""")
|
||||||
|
def setWakeMessage(context: Context, args: Arguments): Array[AnyRef] = {
|
||||||
|
val oldMessage = wakeMessage
|
||||||
|
val oldFuzzy = wakeMessageFuzzy
|
||||||
|
|
||||||
|
if (args.optAny(0, null) == null)
|
||||||
|
wakeMessage = None
|
||||||
|
else
|
||||||
|
wakeMessage = Option(args.checkString(0))
|
||||||
|
wakeMessageFuzzy = args.optBoolean(1, wakeMessageFuzzy)
|
||||||
|
|
||||||
|
result(oldMessage.orNull, oldFuzzy)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected def isPacketAccepted(packet: Packet, distance: Double): Boolean = true
|
||||||
|
|
||||||
|
protected def receivePacket(packet: Packet, distance: Double, host: EnvironmentHost) {
|
||||||
|
if (packet.source != node.address && Option(packet.destination).forall(_ == node.address)) {
|
||||||
|
if (isPacketAccepted(packet, distance)) {
|
||||||
|
node.sendToReachable("computer.signal", Seq("modem_message", packet.source, Int.box(packet.port), Double.box(distance)) ++ packet.data: _*)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept wake-up messages regardless of port because we close all ports
|
||||||
|
// when our computer shuts down.
|
||||||
|
val wakeup: Boolean = packet.data match {
|
||||||
|
case Array(message: Array[Byte]) if wakeMessage.contains(new String(message, Charsets.UTF_8)) => true
|
||||||
|
case Array(message: String) if wakeMessage.contains(message) => true
|
||||||
|
case Array(message: Array[Byte], _*) if wakeMessageFuzzy && wakeMessage.contains(new String(message, Charsets.UTF_8)) => true
|
||||||
|
case Array(message: String, _*) if wakeMessageFuzzy && wakeMessage.contains(message) => true
|
||||||
|
case _ => false
|
||||||
|
}
|
||||||
|
if (wakeup) {
|
||||||
|
host match {
|
||||||
|
case ctx: Context => ctx.start()
|
||||||
|
case _ => node.sendToNeighbors("computer.start")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def loadWakeMessage(nbt: NBTTagCompound): Unit = {
|
||||||
|
if (nbt.hasKey("wakeMessage")) {
|
||||||
|
wakeMessage = Option(nbt.getString("wakeMessage"))
|
||||||
|
}
|
||||||
|
wakeMessageFuzzy = nbt.getBoolean("wakeMessageFuzzy")
|
||||||
|
}
|
||||||
|
|
||||||
|
def saveWakeMessage(nbt: NBTTagCompound): Unit = {
|
||||||
|
wakeMessage.foreach(nbt.setString("wakeMessage", _))
|
||||||
|
nbt.setBoolean("wakeMessageFuzzy", wakeMessageFuzzy)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user