public class UDPConnector extends Object implements Connector
Connector employing UDP as the transport protocol for exchanging
data between networked clients and a server application. It implements the
network stage in the Californium architecture.
In order to process data received from the network via UDP, client code can
register a RawDataChannel instance by means of the
setRawDataReceiver(RawDataChannel) method. Sending data out to
connected clients can be achieved by means of the send(RawData)
method.
UDP broadcast is allowed.
The number of threads can be set through setReceiverThreadCount(int)
and setSenderThreadCount(int) before the connector is started.| Modifier and Type | Field and Description |
|---|---|
protected InetSocketAddress |
localAddr |
static org.slf4j.Logger |
LOGGER |
protected boolean |
running |
static int |
UNDEFINED |
| Constructor and Description |
|---|
UDPConnector()
Creates a connector on the wildcard address listening on an ephemeral
port, i.e.
|
UDPConnector(InetSocketAddress address)
Creates a connector bound to a given IP address and port.
|
| Modifier and Type | Method and Description |
|---|---|
void |
destroy()
Stops the connector and cleans up any leftovers.
|
InetSocketAddress |
getAddress()
Gets the address of the socket this connector is bound to.
|
String |
getProtocol()
Returns the protocol of the connector.
|
int |
getReceiveBufferSize() |
int |
getReceiverPacketSize() |
int |
getReceiverThreadCount() |
int |
getSendBufferSize() |
int |
getSenderThreadCount() |
protected void |
init(DatagramSocket socket)
Initialize connector using the provided socket.
|
void |
send(RawData msg)
Sends a raw message to a client via the network.
|
void |
setEndpointContextMatcher(EndpointContextMatcher matcher)
Set endpoint context matcher to be used for sending messages.
|
void |
setRawDataReceiver(RawDataChannel receiver)
Sets the handler for incoming messages.
|
void |
setReceiveBufferSize(int size) |
void |
setReceiverPacketSize(int size) |
void |
setReceiverThreadCount(int count) |
void |
setSendBufferSize(int size) |
void |
setSenderThreadCount(int count) |
void |
start()
Starts the connector.
|
void |
stop()
Stops the connector.
|
String |
toString() |
public static final org.slf4j.Logger LOGGER
public static final int UNDEFINED
protected volatile boolean running
protected final InetSocketAddress localAddr
public UDPConnector()
UDPConnector(null).public UDPConnector(InetSocketAddress address)
address - the IP address and port, if null the
connector is bound to an ephemeral port on the wildcard
addresspublic void start()
throws IOException
Connectorstart in interface ConnectorIOException - Signals that an I/O exception has occurred.protected void init(DatagramSocket socket) throws IOException
socket - datagram socket for communicationIOException - if there is an error in the datagram socket calls.public void stop()
ConnectorConnector.start() method.public void destroy()
Connectorpublic void send(RawData msg)
ConnectorThis should be a non-blocking operation.
The MessageCallback registered on the message will be
notified about the different stages that the message is going
through as part of being sent.
public void setRawDataReceiver(RawDataChannel receiver)
ConnectorRawDataChannel.receiveData(RawData) method
will be called whenever a new message from a client has been received
via the network.setRawDataReceiver in interface Connectorreceiver - the message handlerpublic void setEndpointContextMatcher(EndpointContextMatcher matcher)
ConnectorsetEndpointContextMatcher in interface Connectormatcher - endpoint context matcherEndpointContextMatcher.isToBeSent(EndpointContext, EndpointContext)public InetSocketAddress getAddress()
ConnectorNote that the IP address returned might be a wildcard address, indicating that this connector is listening on all network interface's IP addresses.
The connector may have been configured to bind to an ephemeral port. In such cases the concrete port that the connector is bound to will only be known after it has been started.
If the connector is not running, the semantics of the address returned is undefined. It may be a default (wildcard) address or the address the connector has been configured to bind to.
getAddress in interface Connectorpublic void setReceiveBufferSize(int size)
public int getReceiveBufferSize()
public void setSendBufferSize(int size)
public int getSendBufferSize()
public void setReceiverThreadCount(int count)
public int getReceiverThreadCount()
public void setSenderThreadCount(int count)
public int getSenderThreadCount()
public void setReceiverPacketSize(int size)
public int getReceiverPacketSize()
public String getProtocol()
ConnectorgetProtocol in interface Connector"UDP", "DTLS", "TCP", or "TLS"Copyright © 2019 Eclipse Foundation. All rights reserved.