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

Commit

Permalink
Fix peripheral access not being detached
Browse files Browse the repository at this point in the history
When the modem is detached (broken) the associated peripheral accesses
were not detached, leaving phantom IComputerAccess instances.

See #95
  • Loading branch information
SquidDev committed Sep 20, 2016
1 parent 11fb618 commit 15ba0db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.squiddev.cctweaks.core.network.modem;

import com.google.common.collect.Lists;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.SetMultimap;
import dan200.computercraft.api.peripheral.IPeripheral;
Expand All @@ -11,10 +12,7 @@
import org.squiddev.cctweaks.api.network.Packet;
import org.squiddev.cctweaks.core.network.AbstractNode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Basic wired modem that handles peripherals and
Expand All @@ -27,12 +25,12 @@ public abstract class BasicModem extends AbstractNode implements INetwork, IWorl
/**
* Set of receivers to use
*/
protected final SetMultimap<Integer, IReceiver> receivers = MultimapBuilder.hashKeys().hashSetValues().build();
private final SetMultimap<Integer, IReceiver> receivers = MultimapBuilder.hashKeys().hashSetValues().build();

/**
* List of wrappers for peripherals on the remote network
*/
public final Map<String, PeripheralAccess> peripheralWrappersByName = new HashMap<String, PeripheralAccess>();
private final Map<String, PeripheralAccess> peripheralWrappersByName = new HashMap<String, PeripheralAccess>();

private boolean peripheralEnabled = false;

Expand Down Expand Up @@ -96,11 +94,29 @@ public void detachPeripheral(String name) {
}
}

/**
* Detach all peripherals
*/
public void detachPeripherals() {
synchronized (peripheralWrappersByName) {
List<String> names = Lists.newArrayList(peripheralWrappersByName.keySet());
for (String name : names) {
detachPeripheralUnsync(name);
}
}
}

private void detachPeripheralUnsync(String name) {
PeripheralAccess wrapper = peripheralWrappersByName.remove(name);
if (wrapper != null) wrapper.detach();
}

public PeripheralAccess getPeripheral(String name) {
synchronized (peripheralWrappersByName) {
return peripheralWrappersByName.get(name);
}
}

@Override
public void receivePacket(Packet packet, double distanceTravelled) {
synchronized (receivers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ public Object[] callMethod(IComputerAccess computer, ILuaContext context, int me
return new Object[]{table};
}
case 1: { // isPresentRemote
PeripheralAccess access = modem.peripheralWrappersByName.get(parseString(arguments, 0));
PeripheralAccess access = modem.getPeripheral(parseString(arguments, 0));
return new Object[]{access != null && access.getType() != null};
}
case 2: { // getTypeRemote
PeripheralAccess access = modem.peripheralWrappersByName.get(parseString(arguments, 0));
PeripheralAccess access = modem.getPeripheral(parseString(arguments, 0));
String type = null;
return access == null || (type = access.getType()) != null ? new Object[]{type} : null;
}
case 3: {// getMethodsRemote
PeripheralAccess access = modem.peripheralWrappersByName.get(parseString(arguments, 0));
PeripheralAccess access = modem.getPeripheral(parseString(arguments, 0));
String[] names;
if (access != null && (names = access.getMethodNames()) != null) {
Map<Object, Object> table = new HashMap<Object, Object>();
Expand All @@ -111,7 +111,7 @@ public Object[] callMethod(IComputerAccess computer, ILuaContext context, int me
System.arraycopy(arguments, 2, methodArgs, 0, arguments.length - 2);

// Get the peripheral and call it
PeripheralAccess access = modem.peripheralWrappersByName.get(remoteName);
PeripheralAccess access = modem.getPeripheral(remoteName);
if (access == null) throw new LuaException("No peripheral: " + remoteName);
return access.callMethod(context, methodName, methodArgs);
}
Expand All @@ -128,7 +128,7 @@ public Object[] callMethod(IComputerAccess computer, ILuaContext context, int me
String methodName = arguments.getString(1);

// Get the peripheral and call it
PeripheralAccess access = modem.peripheralWrappersByName.get(remoteName);
PeripheralAccess access = modem.getPeripheral(remoteName);
if (access == null) throw new LuaException("No peripheral: " + remoteName);
return access.callMethod(context, methodName, arguments.subArgs(2));
} else {
Expand All @@ -150,16 +150,11 @@ public void attach(IComputerAccess computer) {

@Override
public void detach(IComputerAccess computer) {
modem.detachPeripherals();
super.detach(computer);
INetworkController controller = modem.getAttachedNetwork();
if (controller != null) {
for (String name : controller.getPeripheralsOnNetwork().keySet()) {
modem.detachPeripheral(name);
}
}
}

public static String parseString(Object[] arguments, int index) throws LuaException {
private static String parseString(Object[] arguments, int index) throws LuaException {
if (arguments.length > index) {
if (arguments[index] instanceof byte[]) {
return BinaryConverter.decodeString((byte[]) arguments[index]);
Expand Down

0 comments on commit 15ba0db

Please sign in to comment.