Package org.johnnei.javatorrent.network
Class BitTorrentSocket
- java.lang.Object
-
- org.johnnei.javatorrent.network.BitTorrentSocket
-
public class BitTorrentSocket extends Object
Handles the raw data on theByteChannelprovided byISocket.
-
-
Constructor Summary
Constructors Constructor Description BitTorrentSocket(MessageFactory messageFactory, ISocket socket)Creates a new bound BitTorrent socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanReadMessage()Buffers the next message for reading.voidclose()Closes the connection with the socketbooleanclosed()Checks if the socket is closedvoidenqueueMessage(IMessage message)Queues the message to be sendintgetDownloadRate()Returns the last polled download rate.LocalDateTimegetLastActivity()Gets the time at which the last byte has been read or written to the socket.StringgetSocketName()Gets the class simple name of the underlying socket.intgetUploadRate()Returns the last polled upload rate.booleanhasOutboundMessages()Checks if this socket has messages queued for sendingvoidpollRates()Polls all the transfer speeds.IMessagereadMessage()Converts the buffered message to anIMessage.voidsendMessages()Sends messages until the operation is no longer blocking.StringtoString()
-
-
-
Constructor Detail
-
BitTorrentSocket
public BitTorrentSocket(MessageFactory messageFactory, ISocket socket)
Creates a new bound BitTorrent socket.- Parameters:
messageFactory- The factory to createIMessageinstances.socket- The bound socket.
-
-
Method Detail
-
enqueueMessage
public void enqueueMessage(IMessage message)
Queues the message to be send- Parameters:
message- The message to be added to the queue
-
readMessage
public IMessage readMessage()
Converts the buffered message to anIMessage. must only be called whencanReadMessage()returnstrue.- Returns:
- The next message on the stream.
-
sendMessages
public void sendMessages() throws IOExceptionSends messages until the operation is no longer blocking.MessageBlockwill be send last.- Throws:
IOException- When write fails.
-
pollRates
public void pollRates()
Polls all the transfer speeds.
-
close
public void close()
Closes the connection with the socket
-
canReadMessage
public boolean canReadMessage() throws IOExceptionBuffers the next message for reading.- Returns:
- Returns
truewhen enough data is buffered to read the next message without blocking. - Throws:
IOException- When an IO error occurs during the buffering.
-
getDownloadRate
public int getDownloadRate()
Returns the last polled download rate.- Returns:
- The amount of downloaded bytes
- See Also:
pollRates()
-
getUploadRate
public int getUploadRate()
Returns the last polled upload rate.- Returns:
- The amount of uploaded bytes
- See Also:
pollRates()
-
closed
public boolean closed()
Checks if the socket is closed- Returns:
trueif the underlying socket is closed, otherwisefalse
-
hasOutboundMessages
public boolean hasOutboundMessages()
Checks if this socket has messages queued for sending- Returns:
trueif there is at least oneIMessagewaiting to be sent.
-
getSocketName
public String getSocketName()
Gets the class simple name of the underlying socket.- Returns:
- The name of the socket or an empty string when no socket is set.
-
getLastActivity
public LocalDateTime getLastActivity()
Gets the time at which the last byte has been read or written to the socket.- Returns:
- The most recent activity time
-
-