Class OutgoingMessageQueueImpl
- java.lang.Object
-
- com.biglybt.core.networkmanager.impl.OutgoingMessageQueueImpl
-
- All Implemented Interfaces:
OutgoingMessageQueue
public class OutgoingMessageQueueImpl extends java.lang.Object implements OutgoingMessageQueue
Priority-based outbound peer message queue.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
OutgoingMessageQueueImpl.NotificationItem
-
Nested classes/interfaces inherited from interface com.biglybt.core.networkmanager.OutgoingMessageQueue
OutgoingMessageQueue.MessageQueueListener
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.ArrayList
delayed_notifications
private AEMonitor
delayed_notifications_mon
private boolean
destroyed
private java.util.ArrayList
listeners
private AEMonitor
listeners_mon
private static int
MAX_HISTORY_TRACES
private java.lang.ref.WeakReference
origPositionsCache
private int
percent_complete
private java.util.LinkedList<RawMessage>
prev_sent
private boolean
priority_boost
private java.util.LinkedList<RawMessage>
queue
private AEMonitor
queue_mon
private java.lang.ref.WeakReference
rawBufferCache
private MessageStreamEncoder
stream_encoder
private int
total_data_size
private int
total_size
private boolean
trace
private static boolean
TRACE_HISTORY
private Transport
transport
private RawMessage
urgent_message
-
Constructor Summary
Constructors Constructor Description OutgoingMessageQueueImpl(MessageStreamEncoder stream_encoder)
Create a new outgoing message queue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addMessage(Message message, boolean manual_listener_notify)
Add a message to the message queue.void
cancelQueueListener(OutgoingMessageQueue.MessageQueueListener listener)
Cancel queue event notification listener.int[]
deliverToTransport(int max_bytes, boolean protocol_is_free, boolean manual_listener_notify)
Deliver (write) message(s) data to the underlying transport.void
destroy()
Destroy this queue; i.e.void
doListenerNotifications()
Manually send any unsent listener notifications.void
flush()
Force all pending messages to be deliveredint
getDataQueuedBytes()
MessageStreamEncoder
getEncoder()
int
getMssSize()
int
getPercentDoneOfCurrentMessage()
Get the percentage of the current message that has already been sent out.boolean
getPriorityBoost()
int
getProtocolQueuedBytes()
java.lang.String
getQueueTrace()
int
getTotalSize()
Get the total number of bytes ready to be transported.boolean
hasUrgentMessage()
Whether or not an urgent message (one that needs an immediate send, i.e.boolean
isBlocked()
boolean
isDestroyed()
void
notifyOfExternallySentMessage(Message message)
Notifty the queue (and its listeners) of a message sent externally on the queue's behalf.Message
peekFirstMessage()
void
registerQueueListener(OutgoingMessageQueue.MessageQueueListener listener)
Add a listener to be notified of queue events.boolean
removeMessage(Message message, boolean manual_listener_notify)
Remove a particular message from the queue.void
removeMessagesOfType(Message[] message_types, boolean manual_listener_notify)
Remove all messages of the given types from the queue.void
setEncoder(MessageStreamEncoder stream_encoder)
Set the message stream encoder that will be used to encode outgoing messages.void
setPriorityBoost(boolean boost)
void
setTrace(boolean on)
void
setTransport(Transport _transport)
-
-
-
Field Detail
-
queue
private final java.util.LinkedList<RawMessage> queue
-
queue_mon
private final AEMonitor queue_mon
-
delayed_notifications
private final java.util.ArrayList delayed_notifications
-
delayed_notifications_mon
private final AEMonitor delayed_notifications_mon
-
listeners
private volatile java.util.ArrayList listeners
-
listeners_mon
private final AEMonitor listeners_mon
-
total_size
private int total_size
-
total_data_size
private int total_data_size
-
priority_boost
private boolean priority_boost
-
urgent_message
private RawMessage urgent_message
-
destroyed
private boolean destroyed
-
stream_encoder
private MessageStreamEncoder stream_encoder
-
transport
private Transport transport
-
percent_complete
private int percent_complete
-
TRACE_HISTORY
private static final boolean TRACE_HISTORY
- See Also:
- Constant Field Values
-
MAX_HISTORY_TRACES
private static final int MAX_HISTORY_TRACES
- See Also:
- Constant Field Values
-
prev_sent
private final java.util.LinkedList<RawMessage> prev_sent
-
trace
private boolean trace
-
rawBufferCache
private java.lang.ref.WeakReference rawBufferCache
-
origPositionsCache
private java.lang.ref.WeakReference origPositionsCache
-
-
Constructor Detail
-
OutgoingMessageQueueImpl
public OutgoingMessageQueueImpl(MessageStreamEncoder stream_encoder)
Create a new outgoing message queue.- Parameters:
stream_encoder
- default message encoder
-
-
Method Detail
-
setTransport
public void setTransport(Transport _transport)
- Specified by:
setTransport
in interfaceOutgoingMessageQueue
-
getMssSize
public int getMssSize()
- Specified by:
getMssSize
in interfaceOutgoingMessageQueue
-
setEncoder
public void setEncoder(MessageStreamEncoder stream_encoder)
Set the message stream encoder that will be used to encode outgoing messages.- Specified by:
setEncoder
in interfaceOutgoingMessageQueue
- Parameters:
stream_encoder
- to use
-
getEncoder
public MessageStreamEncoder getEncoder()
- Specified by:
getEncoder
in interfaceOutgoingMessageQueue
-
getPercentDoneOfCurrentMessage
public int getPercentDoneOfCurrentMessage()
Get the percentage of the current message that has already been sent out.- Specified by:
getPercentDoneOfCurrentMessage
in interfaceOutgoingMessageQueue
- Returns:
- percentage complete (0-99), or -1 if no message is currently being sent
-
destroy
public void destroy()
Destroy this queue; i.e. perform cleanup actions.- Specified by:
destroy
in interfaceOutgoingMessageQueue
-
getTotalSize
public int getTotalSize()
Get the total number of bytes ready to be transported.- Specified by:
getTotalSize
in interfaceOutgoingMessageQueue
- Returns:
- total bytes remaining
-
getDataQueuedBytes
public int getDataQueuedBytes()
- Specified by:
getDataQueuedBytes
in interfaceOutgoingMessageQueue
-
getProtocolQueuedBytes
public int getProtocolQueuedBytes()
- Specified by:
getProtocolQueuedBytes
in interfaceOutgoingMessageQueue
-
getPriorityBoost
public boolean getPriorityBoost()
- Specified by:
getPriorityBoost
in interfaceOutgoingMessageQueue
-
setPriorityBoost
public void setPriorityBoost(boolean boost)
- Specified by:
setPriorityBoost
in interfaceOutgoingMessageQueue
-
isBlocked
public boolean isBlocked()
- Specified by:
isBlocked
in interfaceOutgoingMessageQueue
-
hasUrgentMessage
public boolean hasUrgentMessage()
Whether or not an urgent message (one that needs an immediate send, i.e. a no-delay message) is queued.- Specified by:
hasUrgentMessage
in interfaceOutgoingMessageQueue
- Returns:
- true if there's a message tagged for immediate write
-
peekFirstMessage
public Message peekFirstMessage()
- Specified by:
peekFirstMessage
in interfaceOutgoingMessageQueue
-
addMessage
public void addMessage(Message message, boolean manual_listener_notify)
Add a message to the message queue. NOTE: Allows for manual listener notification at some later time, using doListenerNotifications(), instead of notifying immediately from within this method. This is useful if you want to invoke listeners outside of some greater synchronised block to avoid deadlock.- Specified by:
addMessage
in interfaceOutgoingMessageQueue
- Parameters:
message
- message to addmanual_listener_notify
- true for manual notification, false for automatic
-
removeMessagesOfType
public void removeMessagesOfType(Message[] message_types, boolean manual_listener_notify)
Remove all messages of the given types from the queue. NOTE: Allows for manual listener notification at some later time, using doListenerNotifications(), instead of notifying immediately from within this method. This is useful if you want to invoke listeners outside of some greater synchronised block to avoid deadlock.- Specified by:
removeMessagesOfType
in interfaceOutgoingMessageQueue
- Parameters:
message_types
- type to removemanual_listener_notify
- true for manual notification, false for automatic
-
removeMessage
public boolean removeMessage(Message message, boolean manual_listener_notify)
Remove a particular message from the queue. NOTE: Only the original message found in the queue will be destroyed upon removal, which may not necessarily be the one passed as the method parameter, as some messages override equals() (i.e. BTRequest messages) instead of using reference equality, and could be a completely different object, and would need to be destroyed manually. If the message does not override equals, then any such method will likely *not* be found and removed, as internal queued object was a new allocation on insertion. NOTE: Allows for manual listener notification at some later time, using doListenerNotifications(), instead of notifying immediately from within this method. This is useful if you want to invoke listeners outside of some greater synchronised block to avoid deadlock.- Specified by:
removeMessage
in interfaceOutgoingMessageQueue
- Parameters:
message
- to removemanual_listener_notify
- true for manual notification, false for automatic- Returns:
- true if the message was removed, false otherwise
-
deliverToTransport
public int[] deliverToTransport(int max_bytes, boolean protocol_is_free, boolean manual_listener_notify) throws java.io.IOException
Deliver (write) message(s) data to the underlying transport. NOTE: Allows for manual listener notification at some later time, using doListenerNotifications(), instead of notifying immediately from within this method. This is useful if you want to invoke listeners outside of some greater synchronised block to avoid deadlock.- Specified by:
deliverToTransport
in interfaceOutgoingMessageQueue
- Parameters:
max_bytes
- maximum number of bytes to delivermanual_listener_notify
- true for manual notification, false for automatic- Returns:
- number of bytes delivered
- Throws:
java.io.IOException
- on delivery error
-
flush
public void flush()
Description copied from interface:OutgoingMessageQueue
Force all pending messages to be delivered- Specified by:
flush
in interfaceOutgoingMessageQueue
-
isDestroyed
public boolean isDestroyed()
- Specified by:
isDestroyed
in interfaceOutgoingMessageQueue
-
doListenerNotifications
public void doListenerNotifications()
Manually send any unsent listener notifications.- Specified by:
doListenerNotifications
in interfaceOutgoingMessageQueue
-
setTrace
public void setTrace(boolean on)
- Specified by:
setTrace
in interfaceOutgoingMessageQueue
-
getQueueTrace
public java.lang.String getQueueTrace()
- Specified by:
getQueueTrace
in interfaceOutgoingMessageQueue
-
registerQueueListener
public void registerQueueListener(OutgoingMessageQueue.MessageQueueListener listener)
Add a listener to be notified of queue events.- Specified by:
registerQueueListener
in interfaceOutgoingMessageQueue
- Parameters:
listener
-
-
cancelQueueListener
public void cancelQueueListener(OutgoingMessageQueue.MessageQueueListener listener)
Cancel queue event notification listener.- Specified by:
cancelQueueListener
in interfaceOutgoingMessageQueue
- Parameters:
listener
-
-
notifyOfExternallySentMessage
public void notifyOfExternallySentMessage(Message message)
Notifty the queue (and its listeners) of a message sent externally on the queue's behalf.- Specified by:
notifyOfExternallySentMessage
in interfaceOutgoingMessageQueue
- Parameters:
message
- sent externally
-
-