Skip to content

Commit

Permalink
restart after changing lat/lon
Browse files Browse the repository at this point in the history
+ config listeners are no longer userd. remove them.
  • Loading branch information
dernasherbrezon committed Mar 2, 2024
1 parent 07088c3 commit a7400eb
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 102 deletions.
17 changes: 1 addition & 16 deletions src/main/java/ru/r2cloud/device/DeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import ru.r2cloud.satellite.ObservationRequestComparator;
import ru.r2cloud.satellite.SatelliteDao;
import ru.r2cloud.util.Clock;
import ru.r2cloud.util.ConfigListener;
import ru.r2cloud.util.Configuration;
import ru.r2cloud.util.NamingThreadFactory;
import ru.r2cloud.util.SafeRunnable;
import ru.r2cloud.util.ThreadPoolFactory;
import ru.r2cloud.util.Util;

public class DeviceManager implements Lifecycle, ConfigListener {
public class DeviceManager implements Lifecycle {

private static final Logger LOG = LoggerFactory.getLogger(DeviceManager.class);

Expand All @@ -45,8 +44,6 @@ public DeviceManager(Configuration config, SatelliteDao dao, ThreadPoolFactory t
this.config = config;
this.threadpoolFactory = threadpoolFactory;
this.clock = clock;
this.config.subscribe(this, "locaiton.lat");
this.config.subscribe(this, "locaiton.lon");
}

public void addDevice(Device device) {
Expand Down Expand Up @@ -177,18 +174,6 @@ public void stop() {
}
}

@Override
public void onConfigUpdated() {
if (config.getProperty("locaiton.lat") != null && config.getProperty("locaiton.lon") != null) {
LOG.info("base station location changed. reschedule");
stop();
start();
} else {
LOG.info("missing location. cancelling all observations");
stop();
}
}

public List<DeviceStatus> getStatus() {
List<DeviceStatus> result = new ArrayList<>(devices.size());
for (Device cur : devices) {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/ru/r2cloud/util/ConfigListener.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/ru/r2cloud/util/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand All @@ -45,7 +44,6 @@ public class Configuration {
private static final Set<PosixFilePermission> MODE600 = new HashSet<>();

private final Properties systemSettings = new Properties();
private final Map<String, List<ConfigListener>> listeners = new ConcurrentHashMap<>();
private final Set<String> changedProperties = new HashSet<>();

static {
Expand Down Expand Up @@ -160,25 +158,6 @@ public void update() {
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
Set<ConfigListener> toNotify = new HashSet<>();
synchronized (changedProperties) {
for (String cur : changedProperties) {
List<ConfigListener> curListener = listeners.get(cur);
if (curListener == null) {
continue;
}
toNotify.addAll(curListener);
}
changedProperties.clear();
}

for (ConfigListener cur : toNotify) {
try {
cur.onConfigUpdated();
} catch (Exception e) {
LOG.error("unable to notify listener: {}", cur, e);
}
}
}

public long getThreadPoolShutdownMillis() {
Expand Down Expand Up @@ -846,17 +825,6 @@ public void remove(String name) {
userSettings.remove(name);
}

public void subscribe(ConfigListener listener, String... names) {
for (String cur : names) {
List<ConfigListener> previous = this.listeners.get(cur);
if (previous == null) {
previous = new ArrayList<>();
this.listeners.put(cur, previous);
}
previous.add(listener);
}
}

public File getTempDirectory() {
String tmpDirectory = getProperty("server.tmp.directory");
if (tmpDirectory != null) {
Expand Down
47 changes: 0 additions & 47 deletions src/test/java/ru/r2cloud/util/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -39,49 +35,6 @@ public class ConfigurationTest {
private TestConfiguration config;
private MockFileSystem fs;

@Test
public void notCalled() {
ConfigListener listener = mock(ConfigListener.class);
Integer value = new Random().nextInt();
config.subscribe(listener, UUID.randomUUID().toString());
config.setProperty(UUID.randomUUID().toString(), value);
config.setProperty(UUID.randomUUID().toString(), value);
config.update();

verify(listener, never()).onConfigUpdated();
}

@Test
public void listenToUpdate() {
ConfigListener listener = mock(ConfigListener.class);
String propName = UUID.randomUUID().toString();
Integer value = new Random().nextInt();

config.subscribe(listener, propName);
config.setProperty(propName, value);
config.setProperty(UUID.randomUUID().toString(), value);
config.update();

verify(listener, atLeast(1)).onConfigUpdated();
}

@Test
public void twoListenersUpdated() {
ConfigListener listener1 = mock(ConfigListener.class);
ConfigListener listener2 = mock(ConfigListener.class);
String propName = UUID.randomUUID().toString();
Integer value = new Random().nextInt();

config.subscribe(listener1, propName);
config.subscribe(listener2, propName);
config.setProperty(propName, value);
config.setProperty(UUID.randomUUID().toString(), value);
config.update();

verify(listener1, atLeast(1)).onConfigUpdated();
verify(listener2, atLeast(1)).onConfigUpdated();
}

@Test
public void update() {
String propName = UUID.randomUUID().toString();
Expand Down

0 comments on commit a7400eb

Please sign in to comment.