From 4a8af3e1616c461cf291711a3bdc391cf1f1b867 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Mon, 18 Mar 2019 13:25:54 +0000 Subject: [PATCH] ref: Moves some utility classes to jitsi-java-utils. --- pom.xml | 5 + src/main/java/org/ice4j/ice/Agent.java | 1 + .../ice/harvest/AbstractUdpListener.java | 2 +- .../org/ice4j/stack/NetAccessManager.java | 2 +- .../ice4j/stack/StunClientTransaction.java | 1 + src/main/java/org/ice4j/stack/StunStack.java | 2 +- .../org/ice4j/util/AsyncQueueHandler.java | 285 --------- .../ice4j/util/CustomizableThreadFactory.java | 56 -- .../java/org/ice4j/util/ExecutorFactory.java | 156 ----- src/main/java/org/ice4j/util/PacketQueue.java | 549 ------------------ .../java/org/ice4j/util/QueueStatistics.java | 131 ----- .../java/org/ice4j/util/RateStatistics.java | 146 ----- 12 files changed, 10 insertions(+), 1326 deletions(-) delete mode 100644 src/main/java/org/ice4j/util/AsyncQueueHandler.java delete mode 100644 src/main/java/org/ice4j/util/CustomizableThreadFactory.java delete mode 100644 src/main/java/org/ice4j/util/ExecutorFactory.java delete mode 100644 src/main/java/org/ice4j/util/PacketQueue.java delete mode 100644 src/main/java/org/ice4j/util/QueueStatistics.java delete mode 100644 src/main/java/org/ice4j/util/RateStatistics.java diff --git a/pom.xml b/pom.xml index d9463af..c837404 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,11 @@ + + org.jitsi + jitsi-java-utils + 1.0.0-SNAPSHOT + org.opentelecoms.sdp java-sdp-nist-bridge diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 02f83ea..88b1fae 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -32,6 +32,7 @@ import org.ice4j.ice.harvest.*; import org.ice4j.stack.*; import org.ice4j.util.*; import org.ice4j.util.Logger; // Disambiguation. +import org.jitsi.javautils.*; /** * An Agent could be described as the main class (i.e. the chef diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java index 07de0d1..06306ad 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java @@ -20,7 +20,7 @@ package org.ice4j.ice.harvest; import org.ice4j.*; import org.ice4j.attribute.*; import org.ice4j.message.*; -import org.ice4j.util.*; +import org.jitsi.javautils.*; import java.io.*; import java.net.*; diff --git a/src/main/java/org/ice4j/stack/NetAccessManager.java b/src/main/java/org/ice4j/stack/NetAccessManager.java index 5747a6f..1efe2e5 100644 --- a/src/main/java/org/ice4j/stack/NetAccessManager.java +++ b/src/main/java/org/ice4j/stack/NetAccessManager.java @@ -29,7 +29,7 @@ import java.util.logging.Logger; // Disambiguation import org.ice4j.*; import org.ice4j.message.*; import org.ice4j.socket.*; -import org.ice4j.util.*; +import org.jitsi.javautils.*; /** * Manages Connectors and MessageProcessingTask execution and diff --git a/src/main/java/org/ice4j/stack/StunClientTransaction.java b/src/main/java/org/ice4j/stack/StunClientTransaction.java index 553947f..e1fe73d 100644 --- a/src/main/java/org/ice4j/stack/StunClientTransaction.java +++ b/src/main/java/org/ice4j/stack/StunClientTransaction.java @@ -26,6 +26,7 @@ import java.util.logging.*; import org.ice4j.*; import org.ice4j.message.*; import org.ice4j.util.*; +import org.jitsi.javautils.*; /** * The {@code StunClientTransaction} class retransmits requests as specified by diff --git a/src/main/java/org/ice4j/stack/StunStack.java b/src/main/java/org/ice4j/stack/StunStack.java index cd29741..4273484 100644 --- a/src/main/java/org/ice4j/stack/StunStack.java +++ b/src/main/java/org/ice4j/stack/StunStack.java @@ -31,7 +31,7 @@ import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.security.*; import org.ice4j.socket.*; -import org.ice4j.util.*; +import org.jitsi.javautils.*; /** * The entry point to the Stun4J stack. The class is used to start, stop and diff --git a/src/main/java/org/ice4j/util/AsyncQueueHandler.java b/src/main/java/org/ice4j/util/AsyncQueueHandler.java deleted file mode 100644 index 95ff248..0000000 --- a/src/main/java/org/ice4j/util/AsyncQueueHandler.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright @ 2018 8x8, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ice4j.util; - -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; -import java.util.logging.Logger; // Disambiguation - -/** - * Asynchronously reads items from provided {@link #queue} on separate thread - * borrowed from {@link #executor} and process items with specified handler. - * Thread is not blocked when queue is empty and returned back to - * {@link #executor} pool. New or existing thread is borrowed from - * {@link #executor} when queue is non empty and {@link #reader} is not - * running - * - * @author Yura Yaroshevich - */ -public final class AsyncQueueHandler -{ - /** - * The {@link java.util.logging.Logger} used by the - * {@link AsyncQueueHandler} class and its instances for logging output. - */ - private static final java.util.logging.Logger logger - = Logger.getLogger(AsyncQueueHandler.class.getName()); - - /** - * The default ExecutorService to run AsyncQueueHandler - * when there is no executor injected in constructor. - */ - private final static ExecutorService sharedExecutor - = Executors.newCachedThreadPool( - new CustomizableThreadFactory( - AsyncQueueHandler.class.getName() + "-executor-", true)); - - /** - * Executor service to run {@link #reader}, which asynchronously - * invokes specified {@link #handler} on queued items. - */ - private final ExecutorService executor; - - /** - * An {@link BlockingQueue } whose items read on separate thread and - * processed by provided {@link #handler}. - */ - private final BlockingQueue queue; - - /** - * The {@link Handler} used to handle items read from - * {@link #queue} by {@link #reader}. - */ - private final Handler handler; - - /** - * An identifier of current reader which is used for debugging purpose. - */ - private final String id; - - /** - * Specifies the number of items allowed to be handled sequentially - * without yielding control to executor's thread. Specifying positive - * number allows implementation of cooperative multi-tasking - * between different {@link AsyncQueueHandler} sharing - * same {@link ExecutorService}. - */ - private final long maxSequentiallyHandledItems; - - /** - * A flag which indicates if reading of {@link #queue} is allowed - * to continue. - */ - private final AtomicBoolean running = new AtomicBoolean(); - - /** - * Synchronization object of current instance state, in particular - * used to resolve races between {@link #handleQueueItemsUntilEmpty()} - * and {@link #reader} exit. In particular synchronization object used to - * access to field {@link #readerFuture}. - */ - private final Object syncRoot = new Object(); - - /** - * Stores Future of currently executing {@link #reader} - */ - private Future readerFuture; - - /** - * Perpetually reads item from {@link #queue} and uses - * {@link #handler} on each of them. - */ - private final Runnable reader = new Runnable() - { - @Override - public void run() - { - long sequentiallyHandledItems = 0; - - while (running.get()) - { - if (maxSequentiallyHandledItems > 0 && - sequentiallyHandledItems >= maxSequentiallyHandledItems) - { - onYield(); - return; - } - - T item; - - synchronized (syncRoot) - { - item = queue.poll(); - - if (item == null) - { - cancel(false); - return; - } - } - - sequentiallyHandledItems++; - - try - { - handler.handleItem(item); - } - catch (Throwable e) - { - logger.warning("Failed to handle item: " + e); - } - } - } - }; - - /** - * Constructs instance of {@link AsyncQueueHandler} which is capable of - * asynchronous reading provided queue from thread borrowed from executor to - * process items with provided handler. - * @param queue thread-safe queue which holds items to process - * @param handler an implementation of handler routine which will be - * invoked per each item placed in the queue. - * @param id optional identifier of current handler for debug purpose - * @param executor optional executor service to borrow threads from - */ - public AsyncQueueHandler( - BlockingQueue queue, - Handler handler, - String id, - ExecutorService executor) - { - this(queue, handler, id, executor, -1); - } - - /** - * Constructs instance of {@link AsyncQueueHandler} which is capable of - * asynchronous reading provided queue from thread borrowed from executor to - * process items with provided handler. - * @param queue thread-safe queue which holds items to process - * @param handler an implementation of handler routine which will be - * invoked per each item placed in the queue. - * @param id optional identifier of current handler for debug purpose - * @param executor optional executor service to borrow threads from - * @param maxSequentiallyHandledItems maximum number of items sequentially - * handled on thread borrowed from {@link #executor} before temporary - * releasing thread and re-acquiring it from {@link #executor}. - */ - public AsyncQueueHandler( - BlockingQueue queue, - Handler handler, - String id, - ExecutorService executor, - long maxSequentiallyHandledItems) - { - if (queue == null) - { - throw new IllegalArgumentException("queue is null"); - } - - if (handler == null) - { - throw new IllegalArgumentException("handler is null"); - } - - this.executor = executor != null ? executor : sharedExecutor; - this.queue = queue; - this.handler = handler; - this.id = id; - this.maxSequentiallyHandledItems = maxSequentiallyHandledItems; - } - - /** - * Attempts to stop execution of {@link #reader} if running - */ - public void cancel() - { - cancel(true); - } - - /** - * Checks if {@link #reader} is running on one of {@link #executor} - * thread and if no submits execution of {@link #reader} on executor. - */ - public void handleQueueItemsUntilEmpty() - { - synchronized (syncRoot) - { - if (readerFuture == null || readerFuture.isDone()) - { - rescheduleReader(); - } - } - } - - /** - * Invoked when execution of {@link #reader} is about to temporary - * cancel and further execution need to be re-scheduled. - */ - private void onYield() - { - if (logger.isLoggable(java.util.logging.Level.FINE)) - { - logger.fine("Yielding AsyncQueueHandler with ID = " + id); - } - - rescheduleReader(); - } - - /** - * Attempts to cancel currently running reader. - * @param mayInterruptIfRunning indicates if {@link #reader} allowed - * to be interrupted if running - */ - private void cancel(boolean mayInterruptIfRunning) - { - synchronized (syncRoot) - { - running.set(false); - - if (readerFuture != null) - { - readerFuture.cancel(mayInterruptIfRunning); - readerFuture = null; - } - } - } - - /** - * Reschedules execution of {@link #reader} on {@link #executor}'s thread. - */ - private void rescheduleReader() - { - synchronized (syncRoot) - { - running.set(true); - readerFuture = executor.submit(reader); - } - } - - /** - * A simple interface to handle enqueued {@link T} items. - * @param the type of the item. - */ - public interface Handler - { - /** - * Does something with an item. - * @param item the item to do something with. - */ - void handleItem(T item); - } -} diff --git a/src/main/java/org/ice4j/util/CustomizableThreadFactory.java b/src/main/java/org/ice4j/util/CustomizableThreadFactory.java deleted file mode 100644 index 3d2b913..0000000 --- a/src/main/java/org/ice4j/util/CustomizableThreadFactory.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. - * - * Copyright @ 2018 Jitsi.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ice4j.util; - -import java.util.concurrent.*; - -/** - * A thread factory which supports customizing name prefix of created threads - * and if produced threads are daemons or not. - */ -public final class CustomizableThreadFactory implements ThreadFactory -{ - private final ThreadFactory defaultThreadFactory - = Executors.defaultThreadFactory(); - - private final String threadNamePrefix; - - private final boolean isDaemon; - - public CustomizableThreadFactory(String threadNamePrefix, boolean isDaemon) - { - this.threadNamePrefix = threadNamePrefix; - this.isDaemon = isDaemon; - } - - @Override - public Thread newThread(Runnable r) - { - Thread thread = this.defaultThreadFactory.newThread(r); - if (this.threadNamePrefix != null && !threadNamePrefix.isEmpty()) - { - thread.setName(this.threadNamePrefix + thread.getName()); - } - if (thread.isDaemon() != this.isDaemon) - { - thread.setDaemon(this.isDaemon); - } - return thread; - } -} diff --git a/src/main/java/org/ice4j/util/ExecutorFactory.java b/src/main/java/org/ice4j/util/ExecutorFactory.java deleted file mode 100644 index 41528a9..0000000 --- a/src/main/java/org/ice4j/util/ExecutorFactory.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. - * - * Copyright @ 2018 Jitsi.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ice4j.util; - -import java.util.concurrent.*; - -/** - * Helper class which contains functions to create pre-configured executors - * - * @author Yura Yaroshevich - */ -public class ExecutorFactory -{ - /** - * Create {@link ScheduledExecutorService} with single executor thread - * @param threadNamePrefix - name prefix for threads created by pool - * @param threadKeepAliveTime - keep alive time before idle thread is freed - * @param timeUnit - time unit of threadKeepAliveTime - * @return pre-configured {@link ScheduledExecutorService} - */ - public static ScheduledExecutorService createSingleThreadScheduledExecutor( - String threadNamePrefix, - int threadKeepAliveTime, - TimeUnit timeUnit) - { - return createScheduledExecutor( - 1, - threadNamePrefix, - threadKeepAliveTime, - timeUnit); - } - - /** - * Create {@link ScheduledExecutorService} with number of threads up to - * number of CPU cores on machine - * @param threadNamePrefix - name prefix for threads created by pool - * @param threadKeepAliveTime - keep alive time before idle thread is freed - * @param timeUnit - time unit of threadKeepAliveTime - * @return pre-configured {@link ScheduledExecutorService} - */ - public static ScheduledExecutorService createCPUBoundScheduledExecutor( - String threadNamePrefix, - int threadKeepAliveTime, - TimeUnit timeUnit) - { - return createScheduledExecutor( - Runtime.getRuntime().availableProcessors(), - threadNamePrefix, - threadKeepAliveTime, - timeUnit); - } - /** - * Creates pre-configured {@link ScheduledExecutorService} instance with - * defaults suitable for ice4j - * @param threadNamePrefix - name prefix for threads created by pool - * @param poolSize - max number of threads to keep in pool. - * @param threadKeepAliveTime - keep alive time before idle thread is freed - * @param timeUnit - time unit of threadKeepAliveTime - * @return pre-configured {@link ScheduledExecutorService} - */ - private static ScheduledExecutorService createScheduledExecutor( - int poolSize, - String threadNamePrefix, - int threadKeepAliveTime, - TimeUnit timeUnit) - { - CustomizableThreadFactory threadFactory - = new CustomizableThreadFactory(threadNamePrefix, true); - - // Motivation: - // The desired behaviour from pool is that it creates limited number - // of threads based on current load, as well as releasing threads when - // there is no work to execute. - // Based on these requirements the following default configuration is - // chosen. - // corePoolSize for {@link ScheduledThreadPoolExecutor} is - // behaved both as corePoolSize and maxPoolSize, so - // it is actually fixed-size pool. - // Even so spec says that corePoolSize is number of - // threads to keep in pool they are actually created on demand, so if - // there is no load, then threads are not created. But until pool has - // less than corePoolSize threads, pool will create new thread, - // to execute scheduled task, even though already created threads are - // idle and have no tasks to execute. - // keepAliveTime is configurable and specifies timeout before - // idle core thread deleted from pool - // removeOnCancelPolicy is set to true, to immediately remove - // queued task from pool queue, because some task might be scheduled - // with very big delay causing having reference to creator from pool - // - // Having corePoolSize is set to 0 with unlimited pool size - // maximumPoolSize is observed to create only 1 thread in pool - // no matter how many task are queued and become eligible to execute. - final ScheduledThreadPoolExecutor executor - = new ScheduledThreadPoolExecutor(poolSize, threadFactory); - executor.setKeepAliveTime(threadKeepAliveTime, timeUnit); - executor.allowCoreThreadTimeOut(true); - executor.setRemoveOnCancelPolicy(true); - return executor; - } - - /** - * Creates a {@link ExecutorService} with limited number of threads which - * are released after idle timeout. - * - * @param threadsLimit - numbers of threads in pool - * @param threadNamePrefix - name prefix for threads created by pool - * @return pre-configured {@link ExecutorService} - */ - public static ExecutorService createFixedThreadPool( - int threadsLimit, - String threadNamePrefix) - { - final CustomizableThreadFactory threadFactory - = new CustomizableThreadFactory(threadNamePrefix, true); - - final ThreadPoolExecutor executor = new ThreadPoolExecutor( - threadsLimit, threadsLimit, 60L, TimeUnit.SECONDS, - new LinkedBlockingDeque<>(), threadFactory); - executor.allowCoreThreadTimeOut(true); - - return executor; - } - - /** - * Creates an {@link ExecutorService} with an unlimited number of threads - * which are released after idle timeout. - * - * @param threadNamePrefix - name prefix for threads created by pool - * @return pre-configured {@link ExecutorService} - */ - public static ExecutorService createCachedThreadPool( - String threadNamePrefix) - { - final CustomizableThreadFactory threadFactory - = new CustomizableThreadFactory(threadNamePrefix, true); - - return Executors.newCachedThreadPool(threadFactory); - } -} diff --git a/src/main/java/org/ice4j/util/PacketQueue.java b/src/main/java/org/ice4j/util/PacketQueue.java deleted file mode 100644 index 3b7e3fc..0000000 --- a/src/main/java/org/ice4j/util/PacketQueue.java +++ /dev/null @@ -1,549 +0,0 @@ -/* - * Copyright @ 2015 Atlassian Pty Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.ice4j.util; - -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; -import java.util.logging.Logger; // Disambiguation. - -/** - * An abstract queue of packets. This is meant to eventually be able to be used - * in the following classes (in ice4j and libjitsi) in place of their ad-hoc - * queue implementations (which is the reason the class is parameterized): - *
RTPConnectorOutputStream.Queue - *
PushSourceStreamImpl#readQ - *
OutputDataStreamImpl#writeQ - *
SinglePortHarvester.MySocket#queue - *
MultiplexingSocket#received (and the rest of Multiplex* classes). - * - * @author Boris Grozev - * @author Yura Yaroshevich - */ -public abstract class PacketQueue -{ - /** - * The {@link Logger} used by the {@link PacketQueue} class and its - * instances for logging output. - */ - private static final Logger logger - = Logger.getLogger(PacketQueue.class.getName()); - - /** - * The default capacity of a {@link PacketQueue}. - */ - private final static int DEFAULT_CAPACITY = 256; - - /** - * The capacity of the {@code byte[]} cache, if it is enabled. - */ - private final static int CACHE_CAPACITY = 100; - - /** - * Returns true if a warning should be logged after a queue has dropped - * {@code numDroppedPackets} packets. - * @param numDroppedPackets the number of dropped packets. - * @return {@code true} if a warning should be logged. - */ - public static boolean logDroppedPacket(int numDroppedPackets) - { - return - numDroppedPackets == 1 || - (numDroppedPackets <= 1000 && numDroppedPackets % 100 == 0) || - numDroppedPackets % 1000 == 0; - } - - /** - * The underlying {@link BlockingQueue} which holds packets. - * Used as synchronization object between {@link #close()}, {@link #get()} - * and {@link #doAdd(Object)}. - */ - private final BlockingQueue queue; - - /** - * Whether this {@link PacketQueue} should store the {@code byte[]} or - * {@code T} instances added to it via one of the {@code add} methods (if - * {@code false}), or create and store a copy (if {@code true}). - */ - private final boolean copy; - - /** - * The {@link QueueStatistics} instance optionally used to collect and print - * detailed statistics about this queue. - */ - private final QueueStatistics queueStatistics; - - /** - * The optionally used {@link AsyncQueueHandler} to perpetually read packets - * from {@link #queue} on separate thread and handle them with provided - * packet handler. - */ - private final AsyncQueueHandler asyncQueueHandler; - - /** - * A string used to identify this {@link PacketQueue} for logging purposes. - */ - private final String id; - - /** - * Whether this queue has been closed. Field is denoted as volatile, - * because it is set in one thread and could be read in while loop in other. - */ - private volatile boolean closed = false; - - /** - * The number of packets which were dropped from this {@link PacketQueue} as - * a result of a packet being added while the queue is at full capacity. - */ - private final AtomicInteger numDroppedPackets = new AtomicInteger(); - - /** - * Initializes a new {@link PacketQueue} instance. - */ - public PacketQueue() - { - this(false, "PacketQueue", null); - } - - /** - * Initializes a new {@link PacketQueue} instance. - * @param enableStatistics whether detailed statistics should be calculated - * and printed. WARNING: this will produce copious output (one line per - * packet added or removed). - * @param id the ID of the packet queue, to be used for logging. - * @param packetHandler An optional handler to be used by the queue for - * packets read from it. If a non-null value is passed the queue will - * start its own thread, which will read packets from the queue and execute - * {@code handler.handlePacket} on them. If set to null, no thread will be - * created, and the queue will provide access to the head element via - * {@link #get()} and {@link #poll()}. - */ - public PacketQueue( - boolean enableStatistics, String id, PacketHandler packetHandler) - { - this(DEFAULT_CAPACITY, true, enableStatistics, id, packetHandler); - } - - /** - * Initializes a new {@link PacketQueue} instance. - * @param capacity the capacity of the queue. - * @param copy whether the queue is to store the instances it is given via - * the various {@code add} methods, or create a copy. - * @param enableStatistics whether detailed statistics should be calculated - * and printed. WARNING: this will produce copious output (one line per - * packet added or removed). - * @param id the ID of the packet queue, to be used for logging. - * @param packetHandler An optional handler to be used by the queue for - * packets read from it. If a non-null value is passed the queue will - * start its own thread, which will read packets from the queue and execute - * {@code handler.handlePacket} on them. If set to null, no thread will be - * created, and the queue will provide access to the head element via - * {@link #get()} and {@link #poll()}. - */ - public PacketQueue(int capacity, boolean copy, - boolean enableStatistics, String id, - PacketHandler packetHandler) - { - this(capacity, copy, enableStatistics, id, packetHandler, null); - } - - /** - * Initializes a new {@link PacketQueue} instance. - * @param capacity the capacity of the queue. - * @param copy whether the queue is to store the instances it is given via - * the various {@code add} methods, or create a copy. - * @param enableStatistics whether detailed statistics should be calculated - * and printed. WARNING: this will produce copious output (one line per - * packet added or removed). - * @param id the ID of the packet queue, to be used for logging. - * @param packetHandler An optional handler to be used by the queue for - * packets read from it. If a non-null value is passed the queue will - * start its own thread, which will read packets from the queue and execute - * {@code handler.handlePacket} on them. If set to null, no thread will be - * created, and the queue will provide access to the head element via - * {@link #get()} and {@link #poll()}. - * @param executor An optional executor service to use to execute - * packetHandler for items added to queue. - */ - public PacketQueue( - int capacity, - boolean copy, - boolean enableStatistics, - String id, - PacketHandler packetHandler, - ExecutorService executor) - { - this.copy = copy; - this.id = id; - queue = new ArrayBlockingQueue<>(capacity); - - queueStatistics - = enableStatistics ? new QueueStatistics(id) : null; - - if (packetHandler != null) - { - asyncQueueHandler = new AsyncQueueHandler( - queue, - new HandlerAdapter(packetHandler), - id, - executor, - packetHandler.maxSequentiallyProcessedPackets()); - } - else - { - asyncQueueHandler = null; - } - - logger.fine("Initialized a PacketQueue instance with ID " + id); - } - - /** - * Adds a packet represented by a {@code byte[]} with a corresponding - * offset and length to this queue. - * @param buf the {@code byte[]} to add. - * @param off the offset into {@code byte[]} where data begins. - * @param len the length of the data. - */ - public void add(byte[] buf, int off, int len) - { - add(buf, off, len, null); - } - - /** - * Adds a packet represented by a {@code byte[]} with a corresponding - * offset and length, and a context object to this queue. - * @param buf the {@code byte[]} to add. - * @param off the offset into {@code byte[]} where data begins. - * @param len the length of the data. - * @param context an object which will be added to the queue as part of the - * packet. - */ - public void add(byte[] buf, int off, int len, Object context) - { - if (copy) - { - byte[] newBuf = getByteArray(len); - System.arraycopy(buf, off, newBuf, 0, len); - doAdd(createPacket(newBuf, 0, len, context)); - } - else - { - doAdd(createPacket(buf, off, len, context)); - } - } - - /** - * Adds a specific packet ({@code T}) instance to the queue. - * @param pkt the packet to add. - */ - public void add(T pkt) - { - if (copy) - { - // create a new instance - add(getBuffer(pkt), getOffset(pkt), getLength(pkt), getContext(pkt)); - } - else - { - doAdd(pkt); - } - } - - /** - * Get an unused {@link byte[]} instance with length at least {@code len}. - * @param len the minimum length of the returned instance. - * @return a {@link byte[]} instance with length at least {@code len}. - */ - private byte[] getByteArray(int len) - { - return new byte[len]; - } - - /** - * Adds a specific packet ({@code T}) instance to the queue. - * @param pkt the packet to add. - */ - private void doAdd(T pkt) - { - if (closed) - return; - - while (!queue.offer(pkt)) - { - // Drop from the head of the queue. - T p = queue.poll(); - if (p != null) - { - if (queueStatistics != null) - { - queueStatistics.remove(System.currentTimeMillis()); - } - final int numDroppedPackets = - this.numDroppedPackets.incrementAndGet(); - if (logDroppedPacket(numDroppedPackets)) - { - logger.warning( - "Packets dropped (id=" + id + "): " + numDroppedPackets); - } - - // Call release on dropped packet to allow proper implementation - // of object pooling by PacketQueue users - releasePacket(p); - } - } - - if (queueStatistics != null) - { - queueStatistics.add(System.currentTimeMillis()); - } - - synchronized (queue) - { - // notify single thread because only 1 item was added into queue - queue.notify(); - } - - if (asyncQueueHandler != null) - { - asyncQueueHandler.handleQueueItemsUntilEmpty(); - } - } - - /** - * Removes and returns the packet ({@code T}) at the head of this queue. - * Blocks until there is a packet in the queue. Returns {@code null} if - * the queue is closed or gets closed while waiting for a packet to be added. - * @return the packet at the head of this queue. - */ - public T get() - { - if (asyncQueueHandler != null) - { - // If the queue was configured with a handler, it is running its - // own reading thread, and reading from it via this interface would - // not provide consistent results. - throw new IllegalStateException( - "Trying to read from a queue with a configured handler."); - } - - while (true) - { - if (closed) - return null; - synchronized (queue) - { - T pkt = queue.poll(); - if (pkt != null) - { - if (queueStatistics != null) - { - queueStatistics.remove(System.currentTimeMillis()); - } - return pkt; - } - - try - { - queue.wait(); - } - catch (InterruptedException ie) - {} - } - } - } - - /** - * Removes and returns the packet ({@code T}) at the head of this queue, if - * the queue is non-empty. If the queue is closed or empty, returns null - * without blocking. - * @return the packet at the head of this queue, or null if the queue is - * empty. - */ - public T poll() - { - if (closed) - return null; - - if (asyncQueueHandler != null) - { - // If the queue was configured with a handler, it is running its - // own reading thread, and reading from it via this interface would - // not provide consistent results. - throw new IllegalStateException( - "Trying to read from a queue with a configured handler."); - } - - synchronized (queue) - { - T pkt = queue.poll(); - if (pkt != null && queueStatistics != null) - { - queueStatistics.remove(System.currentTimeMillis()); - } - - return pkt; - } - } - - /** - * Closes current PacketQueue instance. No items will be added - * to queue when it's closed. Threads which were blocked in {@link #get()} - * will receive null. Asynchronous queue processing by - * {@link #asyncQueueHandler} is stopped. - */ - public void close() - { - if (!closed) - { - closed = true; - - if (asyncQueueHandler != null) - { - asyncQueueHandler.cancel(); - } - - synchronized (queue) - { - // notify all threads because PacketQueue is closed and all - // threads waiting on queue must stop reading it. - queue.notifyAll(); - } - } - } - - /** - * Extracts the underlying {@code byte[]} from a packet. - * @param pkt the packet to get the {@code byte[]} from. - * @return the underlying {@code byte[]} of {@code pkt}. - */ - public abstract byte[] getBuffer(T pkt); - - /** - * Extracts the offset of a packet. - * @param pkt the packet to get the offset of. - * @return the offset of {@code pkt}. - */ - public abstract int getOffset(T pkt); - - /** - * Extracts the length of a packet. - * @param pkt the packet to get the length of. - * @return the length of {@code pkt}. - */ - public abstract int getLength(T pkt); - - /** - * Extracts the context of a packet. - * @param pkt the packet to get the context of. - * @return the context of {@code pkt}. - */ - public abstract Object getContext(T pkt); - - /** - * Creates a new packet ({@link T} instance) with the given {@code byte[]}, - * offset, length and context. - * @param buf the {@code byte[]} of the new instance. - * @param off the offset of the new instance. - * @param len the length of the new instance. - * @param context the context of the new instance. - * @return a new packet ({@link T} instance). - */ - protected abstract T createPacket( - byte[] buf, int off, int len, Object context); - - /** - * Releases packet when it is handled by provided packet handler. - * This method is not called when PacketQueue was created without - * handler and hence no automatic queue processing is done. - * Default implementation is empty, but it might be used to implement - * packet pooling to re-use them. - * @param pkt packet to release - */ - protected void releasePacket(T pkt) - { - } - - /** - * A simple interface to handle packets. - * @param the type of the packets. - */ - public interface PacketHandler - { - /** - * Does something with a packet. - * @param pkt the packet to do something with. - * @return {@code true} if the operation was successful, and - * {@code false} otherwise. - */ - boolean handlePacket(T pkt); - - /** - * Specifies the number of packets allowed to be processed sequentially - * without yielding control to executor's thread. Specifying positive - * number will allow other possible queues sharing same - * {@link ExecutorService} to process their packets. - * @return positive value to specify max number of packets which allows - * implementation of cooperative multi-tasking between different - * {@link PacketQueue} sharing same {@link ExecutorService}. - */ - default long maxSequentiallyProcessedPackets() - { - return -1; - } - } - - /** - * An adapter class implementing {@link AsyncQueueHandler.Handler} - * to wrap {@link PacketHandler}. - */ - private final class HandlerAdapter implements AsyncQueueHandler.Handler - { - /** - * An actual handler of packets. - */ - private final PacketHandler handler; - - /** - * Constructs adapter of {@link PacketHandler} to - * {@link AsyncQueueHandler.Handler} interface. - * @param handler an handler instance to adapt - */ - HandlerAdapter(PacketHandler handler) - { - this.handler = handler; - } - - /** - * {@inheritDoc} - */ - @Override - public void handleItem(T item) - { - if (queueStatistics != null) - { - queueStatistics.remove(System.currentTimeMillis()); - } - - try - { - handler.handlePacket(item); - } - finally - { - releasePacket(item); - } - } - } -} diff --git a/src/main/java/org/ice4j/util/QueueStatistics.java b/src/main/java/org/ice4j/util/QueueStatistics.java deleted file mode 100644 index d110541..0000000 --- a/src/main/java/org/ice4j/util/QueueStatistics.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright @ 2015 Atlassian Pty Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.ice4j.util; - -import java.util.logging.*; -import java.util.logging.Logger; // Disambiguation. - -public class QueueStatistics -{ - /** - * The scale to use for {@link RateStatistics}. This makes their output in - * units (e.g. packets) per second. - */ - private static final int SCALE = 1000; - - /** - * The interval (in number of calls to {@link #add(long)} or - * {@link #remove(long)}) at which the gathered statistics will be printed. - */ - private static final int DEFAULT_PRINT_INTERVAL = 500; - - /** - * Calculate the average rate of addition of packets in a 200ms window. - */ - private final RateStatistics addRateStatistics = new RateStatistics(200, SCALE); - - /** - * Calculate the average rate of removal of packets in a 200ms window. - */ - private final RateStatistics removeRateStatistics = new RateStatistics(200, SCALE); - - /** - * The {@link Logger} instance used for logging output. - */ - private final Logger logger; - - private int head = 0; - private int size = 0; - private int[] sizes = new int[DEFAULT_PRINT_INTERVAL]; - private long[] timestamps = new long[DEFAULT_PRINT_INTERVAL]; - private long[] addRates = new long[DEFAULT_PRINT_INTERVAL]; - private long[] removeRates = new long[DEFAULT_PRINT_INTERVAL]; - private int[] totalPacketsAddedHistory = new int[DEFAULT_PRINT_INTERVAL]; - private int totalPacketsAdded = 0; - private String logHeader; - - /** - * Initializes a new {@link QueueStatistics} instance. - * - * @param id Identifier to distinguish the log output of multiple - * {@link QueueStatistics} instances. - */ - public QueueStatistics(String id) - { - logger = Logger.getLogger("QueueStatistics-" + id); - logHeader = "QueueStatistics-" + id + ": "; - - // We let the users of this class decide whether to enable logging (by - // creating a QueueStatistic instance) or not. - logger.setLevel(Level.ALL); - } - - /** - * Registers the addition of a packet to the corresponding queue. - * @param now the time (in milliseconds since the epoch) at which the - * packet was added. - */ - public synchronized void add(long now) - { - addRateStatistics.update(1, now); - size++; - totalPacketsAdded++; - update(now); - } - - /** - * Registers the removal of a packet from the corresponding queue. - * @param now the time (in milliseconds since the epoch) at which the - * packet was removed. - */ - public synchronized void remove(long now) - { - removeRateStatistics.update(1, now); - size--; - update(now); - } - - private synchronized void update(long now) - { - if (head == sizes.length) - { - print(); - head = 0; - } - - sizes[head] = size; - timestamps[head] = now; - addRates[head] = addRateStatistics.getRate(now); - removeRates[head] = removeRateStatistics.getRate(now); - totalPacketsAddedHistory[head] = totalPacketsAdded; - head++; - } - - private void print() - { - StringBuilder s = new StringBuilder(); - for (int i =0; i8*1000 to go to bits/s. - */ - public RateStatistics(int windowSizeMs, float scale) - { - buckets = new long[windowSizeMs + 1]; // N ms in (N+1) buckets. - this.scale = scale / (buckets.length - 1); - } - - private synchronized void eraseOld(long nowMs) - { - long newOldestTime = nowMs - buckets.length + 1; - - if (newOldestTime <= oldestTime) - return; - - while (oldestTime < newOldestTime) - { - long countInOldestBucket = buckets[oldestIndex]; - - accumulatedCount -= countInOldestBucket; - buckets[oldestIndex] = 0L; - if (++oldestIndex >= buckets.length) - { - oldestIndex = 0; - } - ++oldestTime; - if (accumulatedCount == 0L) - { - // This guarantees we go through all the buckets at most once, - // even if newOldestTime is far greater than oldestTime. - break; - } - } - oldestTime = newOldestTime; - } - - public long getRate() - { - return getRate(System.currentTimeMillis()); - } - - public long getRate(long nowMs) - { - eraseOld(nowMs); - return (long) (accumulatedCount * scale + 0.5F); - } - - public long getAccumulatedCount() - { - return getAccumulatedCount(System.currentTimeMillis()); - } - - public long getAccumulatedCount(long nowMs) - { - eraseOld(nowMs); - return accumulatedCount; - } - - - public void update(int count, long nowMs) - { - if (nowMs < oldestTime) // Too old data is ignored. - return; - - synchronized (this) - { - eraseOld(nowMs); - - int nowOffset = (int) (nowMs - oldestTime); - int index = oldestIndex + nowOffset; - - if (index >= buckets.length) - index -= buckets.length; - buckets[index] += count; - accumulatedCount += count; - } - } -}