Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Commit

Permalink
Remove peripherals when node is detached
Browse files Browse the repository at this point in the history
See #95 and #108
  • Loading branch information
SquidDev committed Aug 20, 2016
1 parent cfc11de commit f214e1e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.squiddev.cctweaks.core.network.controller;

import com.google.common.collect.Sets;
import dan200.computercraft.api.peripheral.IPeripheral;
import joptsimple.internal.Strings;
import org.squiddev.cctweaks.api.network.INetworkNode;
Expand All @@ -9,16 +10,18 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Validates {@link NetworkController} instances.
*
* This shouldn't be used in production, but is helpful when testing.
* This is helpful when testing.
*/
public class ControllerValidator {
public static void validate(NetworkController controller) {
List<String> errors = new ArrayList<String>();

Set<IPeripheral> foundPeripherals = Sets.newHashSet();
for (Map.Entry<INetworkNode, Point> entry : controller.points.entrySet()) {
Point point = entry.getValue();

Expand All @@ -36,6 +39,7 @@ public static void validate(NetworkController controller) {

for (Map.Entry<String, IPeripheral> peripheral : point.peripherals.entrySet()) {
IPeripheral other = controller.peripheralsOnNetwork.get(peripheral.getKey());
foundPeripherals.add(peripheral.getValue());

if (other == null || !peripheral.getValue().equals(other)) {
String error = String.format("Peripherals for node %s (%s): %s != %s", point.node, peripheral.getKey(), peripheral.getValue(), other);
Expand Down Expand Up @@ -63,6 +67,13 @@ public static void validate(NetworkController controller) {
}
}

for (Map.Entry<String, IPeripheral> peripheral : controller.getPeripheralsOnNetwork().entrySet()) {
if (!foundPeripherals.contains(peripheral.getValue())) {
errors.add(String.format("Peripheral not in any node %s => %s", peripheral.getKey(), peripheral.getValue()));
}
}


if (errors.size() > 0) {
trace("Controller is invalid:\n - " + Strings.join(errors, "\n - "));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public NetworkController(Map<INetworkNode, Point> points, Map<String, IPeriphera
*
* @param point The point to add
*/
protected void addPoint(Point point) {
private void addPoint(Point point) {
INetworkNode node = point.node;
if (node.getAttachedNetwork() != null) {
point.detachFromNetwork();
Expand All @@ -78,7 +78,7 @@ protected void addPoint(Point point) {
* @return The point
* @throws NullPointerException If the point cannot be found.
*/
public Point getPoint(INetworkNode node) {
private Point getPoint(INetworkNode node) {
Preconditions.checkNotNull(node, "Node cannot be null");
return Preconditions.checkNotNull(points.get(node), "Cannot find point for node %s", node);
}
Expand All @@ -90,7 +90,7 @@ public Point getPoint(INetworkNode node) {
*
* @param newNode The node to merge
*/
protected void assimilateNode(INetworkNode newNode) {
private void assimilateNode(INetworkNode newNode) {
INetworkController controller = newNode.getAttachedNetwork();

if (controller == null) {
Expand Down Expand Up @@ -132,7 +132,7 @@ protected void assimilateNode(INetworkNode newNode) {
*
* @param networks The networks to split into.
*/
protected void handleSplit(Collection<Map<INetworkNode, Point>> networks) {
private void handleSplit(Collection<Map<INetworkNode, Point>> networks) {
// If there are no changes, then we just ignore it.
if (networks.size() <= 1) return;

Expand All @@ -154,15 +154,22 @@ protected void handleSplit(Collection<Map<INetworkNode, Point>> networks) {
*
* @param difference The result of {@link Maps#difference(Map, Map)} on oldPeripherals, newPeripherals
*/
protected void handleInvalidation(MapDifference<String, IPeripheral> difference) {
private void handleInvalidation(MapDifference<String, IPeripheral> difference) {
handleInvalidation(difference.entriesOnlyOnLeft(), difference.entriesOnlyOnRight());
}

protected void handleInvalidation(Map<String, IPeripheral> removed, Map<String, IPeripheral> added) {
private void handleInvalidation(Map<String, IPeripheral> removed, Map<String, IPeripheral> added) {
for (Point point : points.values()) {
point.networkInvalidated(removed, added);
}
}

private void removePeripherals(Map<String, IPeripheral> peripherals) {
for (String name : peripherals.keySet()) {
peripheralsOnNetwork.remove(name);
}
handleInvalidation(Collections.unmodifiableMap(peripherals), Collections.<String, IPeripheral>emptyMap());
}
//endregion

//region INetworkController node access
Expand Down Expand Up @@ -198,6 +205,7 @@ public void removeNode(INetworkNode removedNode) {

points.remove(removedNode);
point.detachFromNetwork();
removePeripherals(point.peripherals);
handleSplit(point.breakConnections());

ControllerValidator.validate(this);
Expand Down Expand Up @@ -270,10 +278,10 @@ public void transmitPacket(INetworkNode start, Packet packet) {
Point startPoint = getPoint(start);

Set<Point> received = new HashSet<Point>(points.size());
Queue<NodeScanner.TransmitPoint> transmitTo = new PriorityQueue<NodeScanner.TransmitPoint>();
transmitTo.offer(new NodeScanner.TransmitPoint(startPoint, 0));
Queue<TransmitPoint> transmitTo = new PriorityQueue<TransmitPoint>();
transmitTo.offer(new TransmitPoint(startPoint, 0));

NodeScanner.TransmitPoint nodePair;
TransmitPoint nodePair;
while ((nodePair = transmitTo.poll()) != null) {
Point point = nodePair.point;
if (!received.add(point)) continue;
Expand All @@ -296,8 +304,23 @@ public void transmitPacket(INetworkNode start, Packet packet) {

distance += Math.sqrt(dx * dx + dy * dy + dz * dz);
}
transmitTo.offer(new NodeScanner.TransmitPoint(otherPoint, distance));
transmitTo.offer(new TransmitPoint(otherPoint, distance));
}
}
}

public static class TransmitPoint implements Comparable<TransmitPoint> {
public final Point point;
public final double distance;

public TransmitPoint(Point point, double distance) {
this.point = point;
this.distance = distance;
}

@Override
public int compareTo(TransmitPoint other) {
return Double.compare(this.distance, other.distance);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.*;

/**
* Various helper node functions
* Scans a list of points, putting each group of connected nodes in an individual network.
*/
public class NodeScanner {
/**
Expand Down Expand Up @@ -50,19 +50,4 @@ public static Collection<Map<INetworkNode, Point>> scanNetwork(Iterable<Point> p

return networks;
}

public static class TransmitPoint implements Comparable<TransmitPoint> {
public final Point point;
public final double distance;

public TransmitPoint(Point point, double distance) {
this.point = point;
this.distance = distance;
}

@Override
public int compareTo(TransmitPoint other) {
return Double.compare(this.distance, other.distance);
}
}
}

0 comments on commit f214e1e

Please sign in to comment.