Crossfire JXClient, Trunk
Reader.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of Oracle nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 import java.nio.channels.*;
42 import java.nio.charset.*;
43 import java.nio.ByteBuffer;
44 import java.net.*;
45 import java.io.IOException;
46 import java.util.*;
47 
48 public class Reader {
49 
50  static void usage() {
51  System.err.println("usage: java Reader group:port@interf [-only source...] [-block source...]");
52  System.exit(-1);
53  }
54 
55  static void printDatagram(SocketAddress sa, ByteBuffer buf) {
56  System.out.format("-- datagram from %s --\n",
57  ((InetSocketAddress)sa).getAddress().getHostAddress());
58  System.out.println(Charset.defaultCharset().decode(buf));
59  }
60 
61  static void parseAddessList(String s, List<InetAddress> list)
62  throws UnknownHostException
63  {
64  String[] sources = s.split(",");
65  for (int i=0; i<sources.length; i++) {
66  list.add(InetAddress.getByName(sources[i]));
67  }
68  }
69 
70  public static void main(String[] args) throws IOException {
71  if (args.length == 0)
72  usage();
73 
74  // first parameter is the multicast address (interface required)
75  MulticastAddress target = MulticastAddress.parse(args[0]);
76  if (target.interf() == null)
77  usage();
78 
79  // addition arguments are source addresses to include or exclude
80  List<InetAddress> includeList = new ArrayList<InetAddress>();
81  List<InetAddress> excludeList = new ArrayList<InetAddress>();
82  int argc = 1;
83  while (argc < args.length) {
84  String option = args[argc++];
85  if (argc >= args.length)
86  usage();
87  String value = args[argc++];
88  if (option.equals("-only")) {
89  parseAddessList(value, includeList);
90  continue;
91  }
92  if (option.equals("-block")) {
93  parseAddessList(value, excludeList);
94  continue;
95  }
96  usage();
97  }
98  if (!includeList.isEmpty() && !excludeList.isEmpty()) {
99  usage();
100  }
101 
102  // create and bind socket
103  ProtocolFamily family = StandardProtocolFamily.INET;
104  if (target.group() instanceof Inet6Address) {
105  family = StandardProtocolFamily.INET6;
106  }
107  DatagramChannel dc = DatagramChannel.open(family)
108  .setOption(StandardSocketOptions.SO_REUSEADDR, true)
109  .bind(new InetSocketAddress(target.port()));
110 
111  if (includeList.isEmpty()) {
112  // join group and block addresses on the exclude list
113  MembershipKey key = dc.join(target.group(), target.interf());
114  for (InetAddress source: excludeList) {
115  key.block(source);
116  }
117  } else {
118  // join with source-specific membership for each source
119  for (InetAddress source: includeList) {
120  dc.join(target.group(), target.interf(), source);
121  }
122  }
123 
124  // register socket with Selector
125  Selector sel = Selector.open();
126  dc.configureBlocking(false);
127  dc.register(sel, SelectionKey.OP_READ);
128 
129  // print out each datagram that we receive
130  ByteBuffer buf = ByteBuffer.allocateDirect(4096);
131  for (;;) {
132  int updated = sel.select();
133  if (updated > 0) {
134  Iterator<SelectionKey> iter = sel.selectedKeys().iterator();
135  while (iter.hasNext()) {
136  SelectionKey sk = iter.next();
137  iter.remove();
138 
139  DatagramChannel ch = (DatagramChannel)sk.channel();
140  SocketAddress sa = ch.receive(buf);
141  if (sa != null) {
142  buf.flip();
143  printDatagram(sa, buf);
144  buf.rewind();
145  buf.limit(buf.capacity());
146  }
147  }
148  }
149  }
150  }
151 }
Reader.usage
static void usage()
Definition: Reader.java:50
Reader.printDatagram
static void printDatagram(SocketAddress sa, ByteBuffer buf)
Definition: Reader.java:55
Reader.parseAddessList
static void parseAddessList(String s, List< InetAddress > list)
Definition: Reader.java:61
MulticastAddress.interf
final NetworkInterface interf
Definition: MulticastAddress.java:53
MulticastAddress.parse
static MulticastAddress parse(String s)
Definition: MulticastAddress.java:80
Reader.main
static void main(String[] args)
Definition: Reader.java:70
MulticastAddress
Definition: MulticastAddress.java:50
MulticastAddress.group
final InetAddress group
Definition: MulticastAddress.java:51
MulticastAddress.port
final int port
Definition: MulticastAddress.java:52