mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-17 02:57:35 -05:00
Compare commits
3 Commits
315-now-pl
...
2672-dispa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0689359d5 | ||
|
|
6dd06288ac | ||
|
|
5fd469c402 |
@@ -68,7 +68,12 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
|
|||||||
private final static Logger mLog = LoggerFactory.getLogger(PolyphaseChannelManager.class);
|
private final static Logger mLog = LoggerFactory.getLogger(PolyphaseChannelManager.class);
|
||||||
private static final double CHANNEL_OVERSAMPLING = 2.0;
|
private static final double CHANNEL_OVERSAMPLING = 2.0;
|
||||||
private static final int POLYPHASE_CHANNELIZER_TAPS_PER_CHANNEL = 9;
|
private static final int POLYPHASE_CHANNELIZER_TAPS_PER_CHANNEL = 9;
|
||||||
private static final int MAXIMUM_QUEUED_NATIVE_BUFFERS = 500;
|
/**
|
||||||
|
* Maximum duration of queued native buffers awaiting the channelizer. The queue is bounded by sample count instead
|
||||||
|
* of buffer count because tuner buffer sizes vary widely, for example about 1,000 samples per RSP buffer and 32,768
|
||||||
|
* samples per RTL-SDR buffer, so that each tuner can absorb a processing delay of the same duration.
|
||||||
|
*/
|
||||||
|
private static final double MAXIMUM_QUEUED_SECONDS = 1.0;
|
||||||
|
|
||||||
private Broadcaster<SourceEvent> mSourceEventBroadcaster = new Broadcaster<>();
|
private Broadcaster<SourceEvent> mSourceEventBroadcaster = new Broadcaster<>();
|
||||||
private TunerController mTunerController;
|
private TunerController mTunerController;
|
||||||
@@ -78,7 +83,7 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
|
|||||||
private ComplexPolyphaseChannelizerM2 mPolyphaseChannelizer;
|
private ComplexPolyphaseChannelizerM2 mPolyphaseChannelizer;
|
||||||
private ChannelSourceEventListener mChannelSourceEventListener = new ChannelSourceEventListener();
|
private ChannelSourceEventListener mChannelSourceEventListener = new ChannelSourceEventListener();
|
||||||
private NativeBufferReceiver mNativeBufferReceiver = new NativeBufferReceiver();
|
private NativeBufferReceiver mNativeBufferReceiver = new NativeBufferReceiver();
|
||||||
private Dispatcher mBufferDispatcher;
|
private Dispatcher<INativeBuffer> mBufferDispatcher;
|
||||||
private boolean mRunning = true;
|
private boolean mRunning = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,10 +104,21 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
|
|||||||
|
|
||||||
mChannelCalculator = new ChannelCalculator(tunerController.getSampleRate(), channelCount,
|
mChannelCalculator = new ChannelCalculator(tunerController.getSampleRate(), channelCount,
|
||||||
tunerController.getFrequency(), CHANNEL_OVERSAMPLING);
|
tunerController.getFrequency(), CHANNEL_OVERSAMPLING);
|
||||||
mBufferDispatcher = new Dispatcher("sdrtrunk polyphase buffer processor", 10, MAXIMUM_QUEUED_NATIVE_BUFFERS);
|
mBufferDispatcher = new Dispatcher<>("sdrtrunk polyphase buffer processor", 10, INativeBuffer::sampleCount,
|
||||||
|
getMaximumQueuedSamples(tunerController.getSampleRate()));
|
||||||
mBufferDispatcher.setListener(mNativeBufferReceiver);
|
mBufferDispatcher.setListener(mNativeBufferReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the maximum number of queued samples for the buffer dispatcher.
|
||||||
|
* @param sampleRate of the tuner
|
||||||
|
* @return maximum queued samples, at least one
|
||||||
|
*/
|
||||||
|
private static long getMaximumQueuedSamples(double sampleRate)
|
||||||
|
{
|
||||||
|
return Math.max(1, (long)(sampleRate * MAXIMUM_QUEUED_SECONDS));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a description of the state of this manager.
|
* Provides a description of the state of this manager.
|
||||||
*/
|
*/
|
||||||
@@ -278,6 +294,7 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
|
|||||||
double sampleRate = sourceEvent.getValue().doubleValue();
|
double sampleRate = sourceEvent.getValue().doubleValue();
|
||||||
int channelCount = ComplexPolyphaseChannelizerM2.getChannelCount(sampleRate);
|
int channelCount = ComplexPolyphaseChannelizerM2.getChannelCount(sampleRate);
|
||||||
mChannelCalculator.setRates(sampleRate, channelCount);
|
mChannelCalculator.setRates(sampleRate, channelCount);
|
||||||
|
mBufferDispatcher.setMaximumQueuedWeight(getMaximumQueuedSamples(sampleRate));
|
||||||
break;
|
break;
|
||||||
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_LOCKED:
|
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_LOCKED:
|
||||||
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_UNLOCKED:
|
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_UNLOCKED:
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import io.github.dsheirer.preference.UserPreferences;
|
|||||||
import io.github.dsheirer.preference.identifier.TalkgroupFormatPreference;
|
import io.github.dsheirer.preference.identifier.TalkgroupFormatPreference;
|
||||||
import io.github.dsheirer.sample.Broadcaster;
|
import io.github.dsheirer.sample.Broadcaster;
|
||||||
import io.github.dsheirer.sample.Listener;
|
import io.github.dsheirer.sample.Listener;
|
||||||
|
import io.github.dsheirer.source.tuner.channel.TunerChannelSource;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -356,6 +357,21 @@ public class ChannelMetadataView extends VBox implements ChannelMetadataModel.Li
|
|||||||
frequency.setCellFactory(NowPlayingCells.formatted(value -> frequencyFormat.format(value / 1e6d)));
|
frequency.setCellFactory(NowPlayingCells.formatted(value -> frequencyFormat.format(value / 1e6d)));
|
||||||
columns.add(frequency);
|
columns.add(frequency);
|
||||||
|
|
||||||
|
//Tuner that sources the channel
|
||||||
|
columns.add(NowPlayingCells.column("tuner", "Tuner", null, Pos.CENTER,
|
||||||
|
row -> getTunerName(row.getChannelMetadata()), Row::revisionProperty));
|
||||||
|
|
||||||
|
//System and site labels from the channel configuration, hidden by default
|
||||||
|
TableColumn<Row,String> system = NowPlayingCells.column("system", "System", null, Pos.CENTER,
|
||||||
|
row -> toText(row.getChannelMetadata().getSystemConfigurationIdentifier()), Row::revisionProperty);
|
||||||
|
system.setVisible(false);
|
||||||
|
columns.add(system);
|
||||||
|
|
||||||
|
TableColumn<Row,String> site = NowPlayingCells.column("site", "Site", null, Pos.CENTER,
|
||||||
|
row -> toText(row.getChannelMetadata().getSiteConfigurationIdentifier()), Row::revisionProperty);
|
||||||
|
site.setVisible(false);
|
||||||
|
columns.add(site);
|
||||||
|
|
||||||
columns.add(NowPlayingCells.column("channel.name", "Channel Name", null, Pos.CENTER,
|
columns.add(NowPlayingCells.column("channel.name", "Channel Name", null, Pos.CENTER,
|
||||||
row -> toText(row.getChannelMetadata().getChannelNameConfigurationIdentifier()), Row::revisionProperty));
|
row -> toText(row.getChannelMetadata().getChannelNameConfigurationIdentifier()), Row::revisionProperty));
|
||||||
|
|
||||||
@@ -365,7 +381,8 @@ public class ChannelMetadataView extends VBox implements ChannelMetadataModel.Li
|
|||||||
column.setSortable(false);
|
column.setSortable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Column numbers in the Swing version of the table, for its stored column widths. Talker is a new column.
|
//Column numbers in the Swing version of the table, for its stored column widths. Talker, Tuner, System and Site are new
|
||||||
|
//columns.
|
||||||
String[] swingColumns = {"status", "decoder", "from", "from.alias", "to", "to.alias", "channel", "frequency",
|
String[] swingColumns = {"status", "decoder", "from", "from.alias", "to", "to.alias", "channel", "frequency",
|
||||||
"channel.name"};
|
"channel.name"};
|
||||||
|
|
||||||
@@ -382,6 +399,22 @@ public class ChannelMetadataView extends VBox implements ChannelMetadataModel.Li
|
|||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the tuner that sources the channel, or null if the channel isn't sourced by a tuner.
|
||||||
|
*/
|
||||||
|
private String getTunerName(ChannelMetadata channelMetadata)
|
||||||
|
{
|
||||||
|
Channel channel = mChannelMetadataModel.getChannelFromMetadata(channelMetadata);
|
||||||
|
ProcessingChain processingChain = channel != null ? mChannelProcessingManager.getProcessingChain(channel) : null;
|
||||||
|
|
||||||
|
if(processingChain != null && processingChain.getSource() instanceof TunerChannelSource tunerChannelSource)
|
||||||
|
{
|
||||||
|
return tunerChannelSource.getTunerName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decoder logical channel name with the timeslot, if any
|
* Decoder logical channel name with the timeslot, if any
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ public class TableColumnMonitor<S>
|
|||||||
public static final String LEGACY_INDEX = "column.legacy.index";
|
public static final String LEGACY_INDEX = "column.legacy.index";
|
||||||
private static final String ORDER = ".column.order";
|
private static final String ORDER = ".column.order";
|
||||||
private static final String HIDDEN = ".column.hidden";
|
private static final String HIDDEN = ".column.hidden";
|
||||||
|
private static final String KNOWN = ".column.known";
|
||||||
private static final String WIDTH = ".column.width.";
|
private static final String WIDTH = ".column.width.";
|
||||||
private static final String SEPARATOR = ",";
|
private static final String SEPARATOR = ",";
|
||||||
|
|
||||||
@@ -230,6 +231,7 @@ public class TableColumnMonitor<S>
|
|||||||
|
|
||||||
mPreferences.remove(mKey + ORDER);
|
mPreferences.remove(mKey + ORDER);
|
||||||
mPreferences.remove(mKey + HIDDEN);
|
mPreferences.remove(mKey + HIDDEN);
|
||||||
|
mPreferences.remove(mKey + KNOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -328,10 +330,13 @@ public class TableColumnMonitor<S>
|
|||||||
if(hidden != null)
|
if(hidden != null)
|
||||||
{
|
{
|
||||||
Set<String> hiddenIds = Set.of(hidden.isEmpty() ? new String[0] : hidden.split(SEPARATOR));
|
Set<String> hiddenIds = Set.of(hidden.isEmpty() ? new String[0] : hidden.split(SEPARATOR));
|
||||||
|
Set<String> knownIds = getKnownIds();
|
||||||
|
|
||||||
for(TableColumn<S,?> column: mDefaultOrder)
|
for(TableColumn<S,?> column: mDefaultOrder)
|
||||||
{
|
{
|
||||||
column.setVisible(!hiddenIds.contains(column.getId()));
|
//Columns that weren't in the table when the visibility was stored are new, so use the default
|
||||||
|
column.setVisible(knownIds.contains(column.getId()) ? !hiddenIds.contains(column.getId()) :
|
||||||
|
mDefaultVisibility.get(column));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(getVisibleColumnCount() == 0)
|
if(getVisibleColumnCount() == 0)
|
||||||
@@ -375,6 +380,32 @@ public class TableColumnMonitor<S>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IDs of the columns that were in the table when the visibility was stored. Settings stored before the known
|
||||||
|
* columns were stored only had columns that are shown by default.
|
||||||
|
*/
|
||||||
|
private Set<String> getKnownIds()
|
||||||
|
{
|
||||||
|
String known = mPreferences.get(mKey + KNOWN, null);
|
||||||
|
|
||||||
|
if(known != null)
|
||||||
|
{
|
||||||
|
return Set.of(known.isEmpty() ? new String[0] : known.split(SEPARATOR));
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> ids = new HashSet<>();
|
||||||
|
|
||||||
|
for(TableColumn<S,?> column: mDefaultOrder)
|
||||||
|
{
|
||||||
|
if(mDefaultVisibility.get(column))
|
||||||
|
{
|
||||||
|
ids.add(column.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
private int getVisibleColumnCount()
|
private int getVisibleColumnCount()
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -405,9 +436,12 @@ public class TableColumnMonitor<S>
|
|||||||
private void storeVisibility()
|
private void storeVisibility()
|
||||||
{
|
{
|
||||||
List<String> ids = new ArrayList<>();
|
List<String> ids = new ArrayList<>();
|
||||||
|
List<String> known = new ArrayList<>();
|
||||||
|
|
||||||
for(TableColumn<S,?> column: mDefaultOrder)
|
for(TableColumn<S,?> column: mDefaultOrder)
|
||||||
{
|
{
|
||||||
|
known.add(column.getId());
|
||||||
|
|
||||||
if(!column.isVisible())
|
if(!column.isVisible())
|
||||||
{
|
{
|
||||||
ids.add(column.getId());
|
ids.add(column.getId());
|
||||||
@@ -415,6 +449,7 @@ public class TableColumnMonitor<S>
|
|||||||
}
|
}
|
||||||
|
|
||||||
mPreferences.put(mKey + HIDDEN, String.join(SEPARATOR, ids));
|
mPreferences.put(mKey + HIDDEN, String.join(SEPARATOR, ids));
|
||||||
|
mPreferences.put(mKey + KNOWN, String.join(SEPARATOR, known));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getWidthKey(TableColumn<S,?> column)
|
private String getWidthKey(TableColumn<S,?> column)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class MultiFrequencyTunerChannelSource extends TunerChannelSource
|
|||||||
private final static Logger mLog = LoggerFactory.getLogger(MultiFrequencyTunerChannelSource.class);
|
private final static Logger mLog = LoggerFactory.getLogger(MultiFrequencyTunerChannelSource.class);
|
||||||
|
|
||||||
private TunerManager mTunerManager;
|
private TunerManager mTunerManager;
|
||||||
private TunerChannelSource mTunerChannelSource;
|
private volatile TunerChannelSource mTunerChannelSource;
|
||||||
private List<Long> mFrequencies;
|
private List<Long> mFrequencies;
|
||||||
private List<Long> mLockedFrequencies = new ArrayList<>();
|
private List<Long> mLockedFrequencies = new ArrayList<>();
|
||||||
private int mFrequencyListPointer = 0;
|
private int mFrequencyListPointer = 0;
|
||||||
@@ -73,6 +73,17 @@ public class MultiFrequencyTunerChannelSource extends TunerChannelSource
|
|||||||
mPreferredTuner = preferredTuner;
|
mPreferredTuner = preferredTuner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the tuner that provides the current frequency's tuner channel source, which can change as the
|
||||||
|
* frequencies rotate.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getTunerName()
|
||||||
|
{
|
||||||
|
TunerChannelSource source = mTunerChannelSource;
|
||||||
|
return source != null ? source.getTunerName() : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the channel decoder requested frequency correction.
|
* Applies the channel decoder requested frequency correction.
|
||||||
* @param correction requested by the downstream processors.
|
* @param correction requested by the downstream processors.
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public abstract class TunerChannelSource extends ComplexSource implements ISourc
|
|||||||
protected String mThreadName;
|
protected String mThreadName;
|
||||||
private final ChannelFrequencyErrorManager mChannelFrequencyErrorManager;
|
private final ChannelFrequencyErrorManager mChannelFrequencyErrorManager;
|
||||||
private volatile long mStartTimestamp;
|
private volatile long mStartTimestamp;
|
||||||
|
private volatile String mTunerName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tuner Channel Source is a Digital Drop Channel (DDC) abstract class that defines the minimum functionality
|
* Tuner Channel Source is a Digital Drop Channel (DDC) abstract class that defines the minimum functionality
|
||||||
@@ -112,6 +113,24 @@ public abstract class TunerChannelSource extends ComplexSource implements ISourc
|
|||||||
return mTunerChannel;
|
return mTunerChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the tuner that provides this channel source.
|
||||||
|
* @return preferred name of the tuner or null if not set
|
||||||
|
*/
|
||||||
|
public String getTunerName()
|
||||||
|
{
|
||||||
|
return mTunerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the name of the tuner that provides this channel source.
|
||||||
|
* @param tunerName preferred name of the tuner
|
||||||
|
*/
|
||||||
|
public void setTunerName(String tunerName)
|
||||||
|
{
|
||||||
|
mTunerName = tunerName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the timestamp for the oldest samples to provide to this channel when it starts. This only applies to
|
* Sets the timestamp for the oldest samples to provide to this channel when it starts. This only applies to
|
||||||
* sources where the producer incorporates a sample delay buffer.
|
* sources where the producer incorporates a sample delay buffer.
|
||||||
|
|||||||
@@ -655,6 +655,7 @@ public class TunerManager implements IDiscoveredTunerStatusListener
|
|||||||
|
|
||||||
if(source != null)
|
if(source != null)
|
||||||
{
|
{
|
||||||
|
source.setTunerName(discoveredTuner.getTuner().getPreferredName());
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -680,6 +681,11 @@ public class TunerManager implements IDiscoveredTunerStatusListener
|
|||||||
{
|
{
|
||||||
source = discoveredTuner.getTuner().getChannelSourceManager().getSource(tunerChannel,
|
source = discoveredTuner.getTuner().getChannelSourceManager().getSource(tunerChannel,
|
||||||
channelSpecification, threadName);
|
channelSpecification, threadName);
|
||||||
|
|
||||||
|
if(source != null)
|
||||||
|
{
|
||||||
|
source.setTunerName(discoveredTuner.getTuner().getPreferredName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* *****************************************************************************
|
* *****************************************************************************
|
||||||
* Copyright (C) 2014-2023 Dennis Sheirer
|
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -70,7 +70,7 @@ public class RspNativeBuffer extends AbstractNativeBuffer
|
|||||||
@Override
|
@Override
|
||||||
public int sampleCount()
|
public int sampleCount()
|
||||||
{
|
{
|
||||||
return mISamples.length * 2;
|
return mISamples.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* *****************************************************************************
|
* *****************************************************************************
|
||||||
* Copyright (C) 2014-2025 Dennis Sheirer
|
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -31,6 +31,8 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.function.ToLongFunction;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -54,6 +56,9 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
private HeartbeatManager mHeartbeatManager;
|
private HeartbeatManager mHeartbeatManager;
|
||||||
private long mDroppedCount = 0;
|
private long mDroppedCount = 0;
|
||||||
private long mLastOverflowLogTimestamp = 0;
|
private long mLastOverflowLogTimestamp = 0;
|
||||||
|
private final ToLongFunction<E> mWeigher;
|
||||||
|
private volatile long mMaximumQueuedWeight = Long.MAX_VALUE;
|
||||||
|
private final AtomicLong mQueuedWeight = new AtomicLong();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance of a Dispatcher with integrated heartbeat support.
|
* Constructs an instance of a Dispatcher with integrated heartbeat support.
|
||||||
@@ -90,6 +95,7 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
mThreadName = threadName;
|
mThreadName = threadName;
|
||||||
mInterval = interval;
|
mInterval = interval;
|
||||||
mQueue = new LinkedTransferQueue<>();
|
mQueue = new LinkedTransferQueue<>();
|
||||||
|
mWeigher = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,6 +109,46 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
mThreadName = threadName;
|
mThreadName = threadName;
|
||||||
mInterval = interval;
|
mInterval = interval;
|
||||||
mQueue = new LinkedBlockingQueue<>(maximumQueueSize);
|
mQueue = new LinkedBlockingQueue<>(maximumQueueSize);
|
||||||
|
mWeigher = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an instance that is bounded by the total weight of the queued elements instead of the element count,
|
||||||
|
* for when element sizes vary widely, for example sample buffers from different tuners. An element is dropped
|
||||||
|
* when adding it would exceed the maximum weight, except when nothing is queued, so that an element heavier than
|
||||||
|
* the maximum is still delivered. Elements count toward the weight until the listener has processed them.
|
||||||
|
*
|
||||||
|
* @param threadName to name the dispatcher thread
|
||||||
|
* @param interval for processing each batch in milliseconds.
|
||||||
|
* @param weigher to calculate the weight of each element, for example a sample count.
|
||||||
|
* @param maximumQueuedWeight for the queued elements.
|
||||||
|
*/
|
||||||
|
public Dispatcher(String threadName, long interval, ToLongFunction<E> weigher, long maximumQueuedWeight)
|
||||||
|
{
|
||||||
|
if(weigher == null)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("Weigher cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
mThreadName = threadName;
|
||||||
|
mInterval = interval;
|
||||||
|
mQueue = new LinkedTransferQueue<>();
|
||||||
|
mWeigher = weigher;
|
||||||
|
setMaximumQueuedWeight(maximumQueuedWeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the maximum total weight of the queued elements, for a dispatcher that was constructed with a weigher.
|
||||||
|
* @param maximumQueuedWeight for the queued elements.
|
||||||
|
*/
|
||||||
|
public void setMaximumQueuedWeight(long maximumQueuedWeight)
|
||||||
|
{
|
||||||
|
if(maximumQueuedWeight <= 0)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("Maximum queued weight must be positive: " + maximumQueuedWeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
mMaximumQueuedWeight = maximumQueuedWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,7 +195,27 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean queued = mQueue.offer(e);
|
boolean queued;
|
||||||
|
|
||||||
|
if(mWeigher != null)
|
||||||
|
{
|
||||||
|
long weight = mWeigher.applyAsLong(e);
|
||||||
|
long queuedWeight = mQueuedWeight.addAndGet(weight);
|
||||||
|
|
||||||
|
if(queuedWeight > mMaximumQueuedWeight && queuedWeight > weight)
|
||||||
|
{
|
||||||
|
mQueuedWeight.addAndGet(-weight);
|
||||||
|
queued = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
queued = mQueue.offer(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
queued = mQueue.offer(e);
|
||||||
|
}
|
||||||
|
|
||||||
if(!queued)
|
if(!queued)
|
||||||
{
|
{
|
||||||
@@ -189,6 +255,7 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
mQueue.clear();
|
mQueue.clear();
|
||||||
|
mQueuedWeight.set(0);
|
||||||
mExecutorService = Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory(mThreadName));
|
mExecutorService = Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory(mThreadName));
|
||||||
|
|
||||||
Runnable r = (mHeartbeatManager != null ? new ProcessorWithHeartbeat() : new Processor());
|
Runnable r = (mHeartbeatManager != null ? new ProcessorWithHeartbeat() : new Processor());
|
||||||
@@ -210,6 +277,7 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
mScheduledFuture.cancel(false);
|
mScheduledFuture.cancel(false);
|
||||||
mScheduledFuture = null;
|
mScheduledFuture = null;
|
||||||
mQueue.clear();
|
mQueue.clear();
|
||||||
|
mQueuedWeight.set(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mExecutorService != null)
|
if(mExecutorService != null)
|
||||||
@@ -259,6 +327,8 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
mListener.getClass() + "]", t);
|
mListener.getClass() + "]", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
release(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,6 +364,19 @@ public class Dispatcher<E> implements Listener<E>
|
|||||||
mListener.getClass() + "]", t);
|
mListener.getClass() + "]", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
release(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the weight of a dequeued element from the queued weight, once the element is processed.
|
||||||
|
*/
|
||||||
|
private void release(E element)
|
||||||
|
{
|
||||||
|
if(mWeigher != null)
|
||||||
|
{
|
||||||
|
mQueuedWeight.addAndGet(-mWeigher.applyAsLong(element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -212,6 +212,27 @@ public class TableColumnMonitorTest
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void newHiddenByDefaultColumnStaysHidden() throws Exception
|
||||||
|
{
|
||||||
|
onFx(() -> {
|
||||||
|
//Stored by a version without the hidden by default 'site' column and without the known columns
|
||||||
|
mPreferences.put(KEY + ".column.hidden", "a");
|
||||||
|
TableView<String> table = createTable("a", "b", "site");
|
||||||
|
table.getColumns().get(2).setVisible(false);
|
||||||
|
monitor(table);
|
||||||
|
assertEquals(List.of("b"), visibleIds(table));
|
||||||
|
|
||||||
|
//The user shows the column, which is kept in the next session
|
||||||
|
table.getColumns().get(2).setVisible(true);
|
||||||
|
TableView<String> restored = createTable("a", "b", "site", "system");
|
||||||
|
restored.getColumns().get(2).setVisible(false);
|
||||||
|
restored.getColumns().get(3).setVisible(false);
|
||||||
|
monitor(restored);
|
||||||
|
assertEquals(List.of("b", "site"), visibleIds(restored));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void lastVisibleColumnCantBeHidden() throws Exception
|
public void lastVisibleColumnCantBeHidden() throws Exception
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -125,4 +125,60 @@ public class DispatcherTest
|
|||||||
|
|
||||||
assertEquals(101, received.size());
|
assertEquals(101, received.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test: a weight-bounded queue drops elements that would exceed the maximum weight and keeps lighter ones.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void weightBoundedQueueDropsElementsOverMaximumWeight() throws Exception
|
||||||
|
{
|
||||||
|
//Each element weighs its own value: 1 + 2 + 3 = 6 is queued, then 4 would exceed 6 and is dropped, then 0 fits
|
||||||
|
Dispatcher<Integer> dispatcher = new Dispatcher<>("test", 1, Integer::longValue, 6);
|
||||||
|
List<Integer> received = dispatchWhileBlocked(dispatcher, 4);
|
||||||
|
dispatcher.receive(0);
|
||||||
|
awaitSize(received, 5);
|
||||||
|
Thread.sleep(50);
|
||||||
|
dispatcher.stop();
|
||||||
|
|
||||||
|
assertEquals(List.of(0, 1, 2, 3, 0), received);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test: an element heavier than the maximum weight is delivered when nothing else is queued.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void weightBoundedQueueAcceptsHeavyElementWhenEmpty() throws Exception
|
||||||
|
{
|
||||||
|
Dispatcher<Integer> dispatcher = new Dispatcher<>("test", 1, Integer::longValue, 6);
|
||||||
|
List<Integer> received = dispatchWhileBlocked(dispatcher, 0);
|
||||||
|
dispatcher.receive(100);
|
||||||
|
dispatcher.receive(1);
|
||||||
|
awaitSize(received, 2);
|
||||||
|
Thread.sleep(50);
|
||||||
|
dispatcher.stop();
|
||||||
|
|
||||||
|
assertEquals(List.of(0, 100), received);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test: processed elements release their weight so that the queue accepts new elements after an overflow.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void weightBoundedQueueRecoversAfterOverflow() throws Exception
|
||||||
|
{
|
||||||
|
Dispatcher<Integer> dispatcher = new Dispatcher<>("test", 1, Integer::longValue, 6);
|
||||||
|
List<Integer> received = dispatchWhileBlocked(dispatcher, 10);
|
||||||
|
awaitSize(received, 4);
|
||||||
|
Thread.sleep(50);
|
||||||
|
|
||||||
|
for(int x = 0; x < 3; x++)
|
||||||
|
{
|
||||||
|
dispatcher.receive(5);
|
||||||
|
awaitSize(received, 5 + x);
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatcher.stop();
|
||||||
|
|
||||||
|
assertEquals(List.of(0, 1, 2, 3, 5, 5, 5), received);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user