41 import java.nio.channels.*;
42 import java.nio.charset.*;
43 import java.nio.ByteBuffer;
45 import java.io.IOException;
51 System.err.println(
"usage: java Reader group:port@interf [-only source...] [-block source...]");
56 System.out.format(
"-- datagram from %s --\n",
57 ((InetSocketAddress)sa).getAddress().getHostAddress());
58 System.out.println(Charset.defaultCharset().decode(buf));
62 throws UnknownHostException
64 String[] sources = s.split(
",");
65 for (
int i=0; i<sources.length; i++) {
66 list.add(InetAddress.getByName(sources[i]));
70 public static void main(String[] args)
throws IOException {
76 if (target.
interf() ==
null)
80 List<InetAddress> includeList =
new ArrayList<InetAddress>();
81 List<InetAddress> excludeList =
new ArrayList<InetAddress>();
83 while (argc < args.length) {
84 String option = args[argc++];
85 if (argc >= args.length)
87 String value = args[argc++];
88 if (option.equals(
"-only")) {
92 if (option.equals(
"-block")) {
98 if (!includeList.isEmpty() && !excludeList.isEmpty()) {
103 ProtocolFamily family = StandardProtocolFamily.INET;
104 if (target.
group() instanceof Inet6Address) {
105 family = StandardProtocolFamily.INET6;
107 DatagramChannel dc = DatagramChannel.open(family)
108 .setOption(StandardSocketOptions.SO_REUSEADDR,
true)
109 .bind(
new InetSocketAddress(target.
port()));
111 if (includeList.isEmpty()) {
113 MembershipKey key = dc.join(target.
group(), target.
interf());
114 for (InetAddress source: excludeList) {
119 for (InetAddress source: includeList) {
125 Selector sel = Selector.open();
126 dc.configureBlocking(
false);
127 dc.register(sel, SelectionKey.OP_READ);
130 ByteBuffer buf = ByteBuffer.allocateDirect(4096);
132 int updated = sel.select();
134 Iterator<SelectionKey> iter = sel.selectedKeys().iterator();
135 while (iter.hasNext()) {
136 SelectionKey sk = iter.next();
139 DatagramChannel ch = (DatagramChannel)sk.channel();
140 SocketAddress sa = ch.receive(buf);
145 buf.limit(buf.capacity());