Drop MessageQueue, use a LinkedBlockingQueue instead (#63)
LinkedBlockingQueue are available since java 5
This is a bit more efficient as we were calling notifyAll()
instead of notify() in add(), thus waking up too much
This also remove 'logger.finest("Adding raw message to queue.");'
Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
This commit is contained in:
parent
a363a973eb
commit
164ca04236
@ -20,6 +20,7 @@ package org.ice4j.stack;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
@ -45,7 +46,7 @@ class Connector
|
||||
/**
|
||||
* The message queue is where incoming messages are added.
|
||||
*/
|
||||
private final MessageQueue messageQueue;
|
||||
private final BlockingQueue<RawMessage> messageQueue;
|
||||
|
||||
/**
|
||||
* The socket object that used by this access point to access the network.
|
||||
@ -86,12 +87,12 @@ class Connector
|
||||
* communication.
|
||||
* @param remoteAddress the remote address of the socket of this
|
||||
* {@link Connector} if it is a TCP socket, or null if it is UDP.
|
||||
* @param messageQueue the FIFO list where incoming messages should be queued
|
||||
* @param messageQueue the Queue where incoming messages should be queued
|
||||
* @param errorHandler the instance to notify when errors occur.
|
||||
*/
|
||||
protected Connector(IceSocketWrapper socket,
|
||||
TransportAddress remoteAddress,
|
||||
MessageQueue messageQueue,
|
||||
BlockingQueue<RawMessage> messageQueue,
|
||||
ErrorHandler errorHandler)
|
||||
{
|
||||
this.sock = socket;
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
*/
|
||||
package org.ice4j.stack;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
@ -45,7 +46,7 @@ class MessageProcessor
|
||||
/**
|
||||
* The queue where we store incoming messages until they are collected.
|
||||
*/
|
||||
private final MessageQueue messageQueue;
|
||||
private final BlockingQueue<RawMessage> messageQueue;
|
||||
|
||||
/**
|
||||
* The listener that will be retrieving <tt>MessageEvent</tt>s
|
||||
@ -73,7 +74,7 @@ class MessageProcessor
|
||||
*
|
||||
* @param netAccessManager the <tt>NetAccessManager</tt> which is creating
|
||||
* the new instance, is going to be its owner, specifies the
|
||||
* <tt>MessageQueue</tt> which is to store incoming messages, specifies the
|
||||
* <tt>BlockingQueue</tt> which is to store incoming messages, specifies the
|
||||
* <tt>MessageEventHandler</tt> and represents the <tt>ErrorHandler</tt> to
|
||||
* handle exceptions in the new instance
|
||||
* @throws IllegalArgumentException if any of the mentioned properties of
|
||||
@ -85,7 +86,7 @@ class MessageProcessor
|
||||
if (netAccessManager == null)
|
||||
throw new NullPointerException("netAccessManager");
|
||||
|
||||
MessageQueue messageQueue = netAccessManager.getMessageQueue();
|
||||
BlockingQueue<RawMessage> messageQueue = netAccessManager.getMessageQueue();
|
||||
|
||||
if (messageQueue == null)
|
||||
{
|
||||
@ -125,7 +126,7 @@ class MessageProcessor
|
||||
|
||||
try
|
||||
{
|
||||
rawMessage = messageQueue.remove();
|
||||
rawMessage = messageQueue.take();
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
|
||||
@ -1,118 +0,0 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
*
|
||||
* 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.stack;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The class is used as a part of the stack's thread pooling strategy. Method
|
||||
* access is synchronized and message delivery ( remove() ) is blocking in the
|
||||
* case of an empty queue.
|
||||
*
|
||||
* @author Emil Ivov
|
||||
*/
|
||||
class MessageQueue
|
||||
{
|
||||
/**
|
||||
* The <tt>Logger</tt> used by the <tt>MessageQueue</tt> and its instances
|
||||
* for logging output.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(MessageQueue.class.getName());
|
||||
|
||||
// not sure whether Vector is the best choice here since we explicitly
|
||||
//sync all methods ... review later
|
||||
private final Vector<RawMessage> queue = new Vector<>();
|
||||
|
||||
//keep a copy of the fifo size and make it accessible for concurrent
|
||||
// queries.
|
||||
private int size = 0;
|
||||
|
||||
/**
|
||||
* Create an empty MessageFIFO
|
||||
*/
|
||||
MessageQueue()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of messages currently in the queue.
|
||||
* @return the number of messages in the queue.
|
||||
*/
|
||||
public int getSize()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the FIFO is currently empty.
|
||||
* @return true if the FIFO is currently empty and false otherwise.
|
||||
*/
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return (size == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified message to the queue.
|
||||
*
|
||||
* @param rawMessage the message to add.
|
||||
*/
|
||||
public synchronized void add(RawMessage rawMessage)
|
||||
{
|
||||
logger.finest("Adding raw message to queue.");
|
||||
queue.add(rawMessage);
|
||||
size++;
|
||||
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns the oldest message from the fifo. If there are
|
||||
* currently no messages in the queue the method block until there is at
|
||||
* least one message.
|
||||
*
|
||||
*
|
||||
* @return the oldest message in the fifo.
|
||||
* @throws java.lang.InterruptedException if an InterruptedException is
|
||||
* thrown wail waiting for a new message to be added.
|
||||
*/
|
||||
public synchronized RawMessage remove()
|
||||
throws InterruptedException
|
||||
{
|
||||
waitWhileEmpty();
|
||||
RawMessage rawMessage = queue.remove(0);
|
||||
|
||||
size--;
|
||||
return rawMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks until there is at least one message in the queue.
|
||||
*
|
||||
* @throws java.lang.InterruptedException if an InterruptedException is
|
||||
* thrown wail waiting for a new message to be added.
|
||||
*/
|
||||
public synchronized void waitWhileEmpty()
|
||||
throws InterruptedException
|
||||
{
|
||||
while (isEmpty())
|
||||
wait();
|
||||
}
|
||||
}
|
||||
@ -20,6 +20,7 @@ package org.ice4j.stack;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
@ -79,7 +80,7 @@ class NetAccessManager
|
||||
/**
|
||||
* A synchronized FIFO where incoming messages are stocked for processing.
|
||||
*/
|
||||
private final MessageQueue messageQueue = new MessageQueue();
|
||||
private final BlockingQueue<RawMessage> messageQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
/**
|
||||
* A thread pool of message processors.
|
||||
@ -196,13 +197,13 @@ class NetAccessManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>MessageQueue</tt> of this <tt>NetAccessManager</tt> in which
|
||||
* Gets the <tt>BlockingQueue</tt> of this <tt>NetAccessManager</tt> in which
|
||||
* incoming messages are stocked for processing.
|
||||
*
|
||||
* @return the <tt>MessageQueue</tt> of this <tt>NetAccessManager</tt> in
|
||||
* @return the <tt>BlockingQueue</tt> of this <tt>NetAccessManager</tt> in
|
||||
* which incoming messages are stocked for processing
|
||||
*/
|
||||
MessageQueue getMessageQueue()
|
||||
BlockingQueue<RawMessage> getMessageQueue()
|
||||
{
|
||||
return messageQueue;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user