This commit is contained in:
Mark Fisher
2010-01-18 18:17:53 +00:00
parent b00361ae91
commit cb563cef22
3 changed files with 74 additions and 5 deletions

View File

@@ -24,6 +24,9 @@ import java.net.MulticastSocket;
import org.springframework.integration.core.MessagingException;
/**
* Channel adapter that joins a multicast group and receives incoming packets and
* sends them to an output channel.
*
* @author Gary Russell
* @since 2.0
*/
@@ -33,7 +36,10 @@ public class MulticastReceivingChannelAdapter extends UnicastReceivingChannelAda
/**
* @param port
* Constructs a MulticastReceivingChannelAdapter that listens for packets on the
* specified multichannel address (group) and port.
* @param group The multichannel address.
* @param port The port.
*/
public MulticastReceivingChannelAdapter(String group, int port) {
super(port);
@@ -41,8 +47,12 @@ public class MulticastReceivingChannelAdapter extends UnicastReceivingChannelAda
}
/**
* @param port
* @param lengthCheck
* Constructs a MulticastReceivingChannelAdapter that listens for packets on the
* specified multichannel address (group) and port. Enables setting the lengthCheck
* option, which expects a length to precede the incoming packets.
* @param group The multichannel address.
* @param port The port.
* @param lengthCheck If true, enables the lengthCheck Option.
*/
public MulticastReceivingChannelAdapter(String group, int port, boolean lengthCheck) {
super(port, lengthCheck);

View File

@@ -39,14 +39,54 @@ public class MulticastSendingMessageHandler extends UnicastSendingMessageHandler
protected int timeToLive = -1;
/**
* Constructs a MulticastSendingMessageHandler to send data to the multicast address/port.
* @param address The multicast address.
* @param port The port.
*/
public MulticastSendingMessageHandler(String address, int port) {
super(address, port);
}
/**
* Constructs a MulticastSendingMessageHandler to send data to the multicast address/port
* and enables setting the lengthCheck option (if set, a length is prepended to the packet and checked
* at the destination).
* @param address The multicast address.
* @param port The port.
* @param lengthCheck Enable the lengthCheck option.
*/
public MulticastSendingMessageHandler(String address, int port, boolean lengthCheck) {
super(address, port, lengthCheck);
}
/**
* Constructs a MulticastSendingMessageHandler to send data to the multicast address/port
* and enables setting the acknowledge option, where the destination sends a receipt acknowledgment.
* @param address The multicast address.
* @param port The port.
* @param acknowledge Whether or not acknowledgments are required.
* @param ackHost The host to which acknowledgments should be sent; required if acknowledge is true.
* @param ackPort The port to which acknowledgments should be sent; required if acknowledge is true.
* @param ackTimeout How long to wait (milliseconds) for an acknowledgment.
*/
public MulticastSendingMessageHandler(String address, int port,
boolean acknowledge, String ackHost, int ackPort, int ackTimeout) {
super(address, port, acknowledge, ackHost, ackPort, ackTimeout);
}
/**
* Constructs a MulticastSendingMessageHandler to send data to the multicast address/port
* and enables setting the acknowledge option, where the destination sends a receipt acknowledgment.
* @param address The multicast address.
* @param port The port.
* @param lengthCheck Enable the lengthCheck option.
* @param acknowledge Whether or not acknowledgments are required.
* @param ackHost The host to which acknowledgments should be sent; required if acknowledge is true.
* @param ackPort The port to which acknowledgments should be sent; required if acknowledge is true.
* @param ackTimeout How long to wait (milliseconds) for an acknowledgment.
*/
public MulticastSendingMessageHandler(String address, int port,
boolean lengthCheck, boolean acknowledge, String ackHost,
int ackPort, int ackTimeout) {
@@ -62,6 +102,10 @@ public class MulticastSendingMessageHandler extends UnicastSendingMessageHandler
this.ackCounter = minAcksForSuccess;
}
/**
* Set the underlying {@link MulticastSocket} time to live property.
* @param timeToLive {@see MulticastSocket#setTimeToLive(int)}
*/
public void setTimeToLive(int timeToLive) {
this.timeToLive = timeToLive;
}

View File

@@ -34,6 +34,10 @@ import org.springframework.integration.ip.IpHeaders;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* A channel adapter to receive incoming UDP packets. Packets can optionally be preceded by a
* 4 byte length field, used to validate that all data was received. Packets may also contain
* information indicating an acknowledgment needs to be sent.
*
* @author Gary Russell
* @since 2.0
*/
@@ -50,11 +54,22 @@ public class UnicastReceivingChannelAdapter extends AbstractInternetProtocolRece
private static Pattern addressPattern = Pattern.compile("([^:]*):([0-9]*)");
/**
* Constructs a UnicastReceivingChannelAdapter that listens on the specified port.
* @param port
*/
public UnicastReceivingChannelAdapter(int port) {
super(port);
mapper.setLengthCheck(false);
}
/**
* Constructs a UnicastReceivingChannelAdapter that listens for packets on
* the specified port. Enables setting the lengthCheck option, which expects
* a length to precede the incoming packets.
* @param port The port.
* @param lengthCheck If true, enables the lengthCheck Option.
*/
public UnicastReceivingChannelAdapter(int port, boolean lengthCheck) {
super(port);
mapper.setLengthCheck(lengthCheck);
@@ -106,7 +121,7 @@ public class UnicastReceivingChannelAdapter extends AbstractInternetProtocolRece
}
}
private void sendAck(Message<byte[]> message) {
protected void sendAck(Message<byte[]> message) {
MessageHeaders headers = message.getHeaders();
Object id = headers.getId();
byte[] ack = id.toString().getBytes();
@@ -155,7 +170,7 @@ public class UnicastReceivingChannelAdapter extends AbstractInternetProtocolRece
return true;
}
public DatagramPacket receive() throws Exception {
protected DatagramPacket receive() throws Exception {
DatagramSocket socket = this.getSocket();
final byte[] buffer = new byte[this.receiveBufferSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);