Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtzook committed Nov 8, 2023
2 parents c3057e5 + fcd75c5 commit 4be7966
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.flash3388.flashlib.scheduling.impl.triggers.GenericTrigger;
import com.flash3388.flashlib.scheduling.impl.triggers.ManualBasedTrigger;
import com.flash3388.flashlib.scheduling.impl.triggers.TriggerActionController;
import com.flash3388.flashlib.scheduling.impl.triggers.TriggerActionControllerImpl;
import com.flash3388.flashlib.scheduling.impl.triggers.TriggerBaseImpl;
import com.flash3388.flashlib.scheduling.triggers.ManualTrigger;
import com.flash3388.flashlib.scheduling.triggers.Trigger;
Expand Down Expand Up @@ -263,7 +264,7 @@ public ActionGroup newActionGroup(ActionGroupType type) {
}

private void executeTriggers() {
TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
for (GenericTrigger trigger : mTriggers) {
trigger.update(controller);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,9 @@

import com.flash3388.flashlib.scheduling.actions.Action;

import java.util.ArrayList;
import java.util.List;
public interface TriggerActionController {

public class TriggerActionController {

private final List<Action> mActionsToStartIfRunning;
private final List<Action> mActionsToStopIfRunning;
private final List<Action> mActionsToToggle;

public TriggerActionController() {
mActionsToStartIfRunning = new ArrayList<>(2);
mActionsToStopIfRunning = new ArrayList<>(2);
mActionsToToggle = new ArrayList<>(2);
}

public List<Action> getActionsToStartIfRunning() {
return mActionsToStartIfRunning;
}

public List<Action> getActionsToStopIfRunning() {
return mActionsToStopIfRunning;
}

public List<Action> getActionsToToggle() {
return mActionsToToggle;
}

public void addActionToStartIfRunning(Action action) {
mActionsToStartIfRunning.add(action);
}

public void addActionToStopIfRunning(Action action) {
mActionsToStopIfRunning.add(action);
}

public void addActionToToggle(Action action) {
mActionsToToggle.add(action);
}
void addActionToStartIfRunning(Action action);
void addActionToStopIfRunning(Action action);
void addActionToToggle(Action action);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.flash3388.flashlib.scheduling.impl.triggers;

import com.flash3388.flashlib.scheduling.actions.Action;

import java.util.ArrayList;
import java.util.List;

public class TriggerActionControllerImpl implements TriggerActionController {

private final List<Action> mActionsToStartIfRunning;
private final List<Action> mActionsToStopIfRunning;
private final List<Action> mActionsToToggle;

public TriggerActionControllerImpl() {
mActionsToStartIfRunning = new ArrayList<>(2);
mActionsToStopIfRunning = new ArrayList<>(2);
mActionsToToggle = new ArrayList<>(2);
}

public List<Action> getActionsToStartIfRunning() {
return mActionsToStartIfRunning;
}

public List<Action> getActionsToStopIfRunning() {
return mActionsToStopIfRunning;
}

public List<Action> getActionsToToggle() {
return mActionsToToggle;
}

@Override
public void addActionToStartIfRunning(Action action) {
mActionsToStartIfRunning.add(action);
}

@Override
public void addActionToStopIfRunning(Action action) {
mActionsToStopIfRunning.add(action);
}

@Override
public void addActionToToggle(Action action) {
mActionsToToggle.add(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import com.flash3388.flashlib.scheduling.actions.Action;
import com.flash3388.flashlib.scheduling.actions.ActionBase;
import com.flash3388.flashlib.scheduling.actions.ActionsMock;
import com.flash3388.flashlib.scheduling.impl.triggers.GenericTrigger;
import com.flash3388.flashlib.scheduling.impl.triggers.TriggerActionController;
import com.flash3388.flashlib.scheduling.triggers.Trigger;
import com.flash3388.flashlib.time.ClockMock;
import com.flash3388.flashlib.util.FlashLibMainThread;
import org.hamcrest.collection.IsMapContaining;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -29,6 +33,7 @@ class SingleThreadedSchedulerTest {

private Map<Action, RunningActionContext> mPendingActions;
private Map<Action, RunningActionContext> mRunningActions;
private Collection<GenericTrigger> mTriggers;
private Map<Requirement, Action> mRequirementsUsage;
private Map<Subsystem, Action> mDefaultActions;

Expand All @@ -38,6 +43,7 @@ class SingleThreadedSchedulerTest {
public void setUp() throws Exception {
mPendingActions = new HashMap<>();
mRunningActions = new HashMap<>();
mTriggers = new ArrayList<>();
mRequirementsUsage = new HashMap<>();
mDefaultActions = new HashMap<>();

Expand All @@ -48,7 +54,7 @@ public void setUp() throws Exception {
mPendingActions,
mRunningActions,
new ArrayList<>(),
new ArrayList<>(),
mTriggers,
mRequirementsUsage,
mDefaultActions);
}
Expand Down Expand Up @@ -384,4 +390,115 @@ public void end(FinishReason reason) {}

assertThat(mRunningActions, IsMapContaining.hasKey(actionToStart));
}

@Test
public void run_withTriggersWithActionNotRunning_startsActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToStartIfRunning(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, IsMapContaining.hasKey(action));
}

@Test
public void run_withTriggersWithActionRunning_notStartsActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();
RunningActionContext context = mock(RunningActionContext.class);
when(context.iterate()).thenReturn(false);
mRunningActions.put(action, context);

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToStartIfRunning(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, IsMapContaining.hasKey(action));
}

@Test
public void run_withTriggersAndActionRunning_stopsActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();
RunningActionContext context = mock(RunningActionContext.class);
when(context.iterate()).thenReturn(false);
mRunningActions.put(action, context);

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToStopIfRunning(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, not(IsMapContaining.hasKey(action)));
}

@Test
public void run_withTriggersAndActionNotRunning_notStopsActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToStopIfRunning(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, not(IsMapContaining.hasKey(action)));
}

@Test
public void run_withTriggersAndActionNotRunning_toggleToStartActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToToggle(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, IsMapContaining.hasKey(action));
}

@Test
public void run_withTriggersAndActionRunning_toggleToStopActionFromTrigger() throws Exception {
Action action = ActionsMock.actionMocker().build();
RunningActionContext context = mock(RunningActionContext.class);
when(context.iterate()).thenReturn(false);
mRunningActions.put(action, context);

GenericTrigger trigger = new GenericTrigger() {
@Override
public void update(TriggerActionController controller) {
controller.addActionToToggle(action);
}
};
mTriggers.add(trigger);

mScheduler.run(SchedulerModeMock.mockNotDisabledMode());

assertThat(mRunningActions, not(IsMapContaining.hasKey(action)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public void setState_stateChanges_updatesListenersWithChange() throws Exception
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.INACTIVE);
trigger.addStateListener(listener);

trigger.setState(TriggerState.ACTIVE, mock(TriggerActionController.class));
trigger.setState(TriggerState.ACTIVE, mock(TriggerActionControllerImpl.class));

verify(listener, times(1)).onStateChange(
eq(TriggerState.ACTIVE),
eq(TriggerState.INACTIVE),
any(TriggerActionController.class));
any(TriggerActionControllerImpl.class));
}

@Test
Expand All @@ -33,7 +33,7 @@ public void whenActive_triggerActivates_startAction() throws Exception {
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.INACTIVE);
trigger.whenActive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.ACTIVE, controller);

assertThat(controller.getActionsToStartIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand All @@ -46,7 +46,7 @@ public void whenInactive_triggerDeactivates_startAction() throws Exception {
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.ACTIVE);
trigger.whenInactive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.INACTIVE, controller);

assertThat(controller.getActionsToStartIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand All @@ -61,7 +61,7 @@ public void cancelWhenActive_triggerActivatesAndActionRunning_cancelsAction() th
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.INACTIVE);
trigger.cancelWhenActive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.ACTIVE, controller);

assertThat(controller.getActionsToStopIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand All @@ -76,7 +76,7 @@ public void cancelWhenInactive_triggerDeactivatesAndActionRunning_cancelsAction(
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.ACTIVE);
trigger.cancelWhenInactive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.INACTIVE, controller);

assertThat(controller.getActionsToStopIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand All @@ -89,7 +89,7 @@ public void whileActive_triggerActivates_startAction() throws Exception {
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.INACTIVE);
trigger.whileActive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.ACTIVE, controller);

assertThat(controller.getActionsToStartIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand All @@ -104,7 +104,7 @@ public void whileActive_triggerDeactivatesAndActionRunning_cancelsAction() throw
TriggerBaseImpl trigger = new TriggerBaseImpl(TriggerState.ACTIVE);
trigger.whileActive(mockAction);

TriggerActionController controller = new TriggerActionController();
TriggerActionControllerImpl controller = new TriggerActionControllerImpl();
trigger.setState(TriggerState.INACTIVE, controller);

assertThat(controller.getActionsToStopIfRunning(), IsIterableContainingInOrder.contains(mockAction));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.flash3388.flashlib.net.util;
package com.flash3388.flashlib.util.net;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.NoSuchElementException;

public class NetInterfaces {
Expand Down Expand Up @@ -35,4 +36,21 @@ public static InterfaceAddress getIpv4AddressByInterface(NetworkInterface networ

throw new NoSuchElementException("address not found");
}

public static byte[] getMacAddress() throws IOException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (!interfaces.hasMoreElements()) {
throw new IOException("no network interfaces available");
}

while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
byte[] hardwareAddress = networkInterface.getHardwareAddress();
if (hardwareAddress != null) {
return hardwareAddress;
}
}

throw new IOException("unable to obtain hardware address");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

public class InstanceId {
private static final int PROCESS_ID_SIZE = Long.BYTES;
Expand All @@ -14,6 +15,9 @@ public class InstanceId {
private final byte[] mProcessId;

public InstanceId(byte[] machineId, byte[] processId) {
Objects.requireNonNull(machineId, "machineId");
Objects.requireNonNull(processId, "processId");

assert machineId.length > 1;
assert PROCESS_ID_SIZE == processId.length;

Expand Down
Loading

0 comments on commit 4be7966

Please sign in to comment.