@Override public void update(Observable<UnitRegistryData> source, UnitRegistryData data) throws Exception { UNIT_REMOTE_REGISTRY_LOCK.writeLock().lock(); UNIT_DIFF.diff(Registries.getUnitRegistry().getUnitConfigs()); try { for (String unitId : UNIT_DIFF.getRemovedMessageMap().keySet()) { if (unitRemoteRegistry.contains(unitId)) { removeUnitRemote(unitRemoteRegistry.get(unitId)); } } } finally { UNIT_REMOTE_REGISTRY_LOCK.writeLock().unlock(); } } };
public AbstractHostUnitController(final Class unitClass, final DB builder) throws InstantiationException { super(unitClass, builder); this.unitMap = new HashMap<>(); this.hostedUnitDiff = new ProtobufListDiff<>(); }
protected Set<AbstractUnitController> getNewUnitController() { Set<AbstractUnitController> newUnitController = new HashSet<>(); hostedUnitDiff.getNewMessageMap().getMessages().forEach((newUnitConfig) -> { newUnitController.add(unitMap.get(newUnitConfig.getId())); }); return newUnitController; }
private void startUpdateObserver(final UnitRegistryData unitRegistryData) { registryDiff.diff(unitRegistryData.getUnitGroupUnitConfigList()); IdentifiableMessageMap<String, UnitConfig, UnitConfig.Builder> identifiableNewMessageMap = registryDiff.getNewMessageMap(); IdentifiableMessageMap<String, UnitConfig, UnitConfig.Builder> identifiableUpdatedMessageMap = registryDiff.getUpdatedMessageMap(); IdentifiableMessageMap<String, UnitConfig, UnitConfig.Builder> identifiableRemovedMessageMap = registryDiff.getRemovedMessageMap(); try { if (!identifiableNewMessageMap.isEmpty()) { final List<UnitConfig> unitConfigs = new ArrayList<>(identifiableNewMessageMap.getMessages()); NEW_UNIT_CONFIG_OBSERVABLE.notifyObservers(unitConfigs); } if (!identifiableUpdatedMessageMap.isEmpty()) { final List<UnitConfig> unitConfigs = new ArrayList<>(identifiableUpdatedMessageMap.getMessages()); UPDATED_UNIT_CONFIG_OBSERVABLE.notifyObservers(unitConfigs); } if (!identifiableRemovedMessageMap.isEmpty()) { final List<UnitConfig> unitConfigs = new ArrayList<>(identifiableRemovedMessageMap.getMessages()); REMOVED_UNIT_CONFIG_OBSERVABLE.notifyObservers(unitConfigs); } } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, LOGGER, LogLevel.ERROR); } } }
hostedUnitDiff.diff(getHostedUnits()); MultiException.ExceptionStack removeExceptionStack = null; hostedUnitDiff.getRemovedMessageMap().getMessages().forEach((removedUnitConfig) -> { unitMap.remove(removedUnitConfig.getId()).shutdown(); }); for (UnitConfig newUnitConfig : hostedUnitDiff.getNewMessageMap().getMessages()) { try { registerUnit(newUnitConfig);
protected Set<String> getRemovedUnitIds() { Set<String> removedUnitIds = new HashSet<>(); hostedUnitDiff.getRemovedMessageMap().getMessages().forEach((removedUnitConfig) -> { removedUnitIds.add(removedUnitConfig.getId()); }); return removedUnitIds; }
/** * Reset the unit registry observer by removing it reseting the unit diff and adding the observer again. * This is needed in unit tests because if multiple tests are run and in theses tests * the MockRegistry is restarted then the observer is registered on an old instance of the unit remote * and thus the effects of the observer cannot be tested. * * @throws InterruptedException thrown if Registries.getUnitRegistry is interrupted * @throws org.openbase.jul.exception.CouldNotPerformException thrown if the unit registry is not available */ private static void resetUnitRegistryObserver() throws InterruptedException, CouldNotPerformException { Registries.getUnitRegistry().removeDataObserver(UNIT_REGISTRY_OBSERVER); UNIT_DIFF.replaceOriginMap(new IdentifiableMessageMap<>(Registries.getUnitRegistry().getDalUnitConfigs())); Registries.getUnitRegistry().addDataObserver(UNIT_REGISTRY_OBSERVER); }
@Override public void activate() throws CouldNotPerformException, InterruptedException { try { this.registryDiff = new ProtobufListDiff<>(); if (JPService.getProperty(JPDebugMode.class).getValue()) { LOGGER.info("Debug Mode"); } //TEST // OffsetDateTime from = OffsetDateTime.parse("2017-04-21T19:00:00.000+02:00"); // OffsetDateTime until = OffsetDateTime.parse("2017-04-21T19:00:10.000+02:00"); // DataProviding dataProviding = new DataProviding(from, until); new HeartbeatPhase(); new UnitRegistrySynchronizer(); new UnitRemoteSynchronizer(); final List<UnitConfig> unitConfigs = getUnitConfigs(); NEW_UNIT_CONFIG_OBSERVABLE.notifyObservers(unitConfigs); this.unitRegistryObserver = (observable, unitRegistryData) -> startUpdateObserver(unitRegistryData); this.unitRegistryRemote.addDataObserver(unitRegistryObserver); } catch (JPServiceException ex) { ExceptionPrinter.printHistory(ex, LOGGER, LogLevel.ERROR); } }