annotate src/wifibroadcast/WifiMulticastChannel.java @ 11:7912fd3af027

non select mode on DatagramChannel.
author one
date Sun, 29 Jul 2012 12:10:17 +0900
parents 95d58cc5bb0b
children e1f43b669cdb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 package wifibroadcast;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 import java.io.IOException;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import java.net.InetAddress;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 import java.net.InetSocketAddress;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 import java.net.NetworkInterface;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 import java.net.SocketAddress;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 import java.net.StandardProtocolFamily;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 import java.net.StandardSocketOptions;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 import java.nio.ByteBuffer;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 import java.nio.channels.DatagramChannel;
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
12 import java.nio.channels.SelectionKey;
9c99e2193277 find broadcast
one
parents: 2
diff changeset
13 import java.nio.channels.Selector;
9c99e2193277 find broadcast
one
parents: 2
diff changeset
14 import java.nio.channels.spi.SelectorProvider;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
16 public class WifiMulticastChannel implements WifiReceiver {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 private InetAddress mAddr;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 private DatagramChannel dc;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 private SocketAddress sAddr;
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
20 // select on DatagramChannel does not work now
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
21 private Selector selector;
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
22 private boolean selectMode = false;
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
23
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 3
diff changeset
24 public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException {
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
25 // join multicast group on this interface, and also use this
9c99e2193277 find broadcast
one
parents: 2
diff changeset
26 // interface for outgoing multicast datagrams
9c99e2193277 find broadcast
one
parents: 2
diff changeset
27 selector = SelectorProvider.provider().openSelector();
9c99e2193277 find broadcast
one
parents: 2
diff changeset
28 NetworkInterface ni = NetworkInterface.getByName("en1");
9c99e2193277 find broadcast
one
parents: 2
diff changeset
29 if (ni==null) {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
30 System.err.println("Can't open network interface "+"en1");
9c99e2193277 find broadcast
one
parents: 2
diff changeset
31 throw new IOException();
9c99e2193277 find broadcast
one
parents: 2
diff changeset
32 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
34 if (!ni.supportsMulticast()) {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
35 System.err.println("Network interface does not support multicast"+"en1");
9c99e2193277 find broadcast
one
parents: 2
diff changeset
36 throw new IOException();
9c99e2193277 find broadcast
one
parents: 2
diff changeset
37 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
39 dc = DatagramChannel.open(StandardProtocolFamily.INET)
9c99e2193277 find broadcast
one
parents: 2
diff changeset
40 .setOption(StandardSocketOptions.SO_REUSEADDR, true)
9c99e2193277 find broadcast
one
parents: 2
diff changeset
41 .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
9c99e2193277 find broadcast
one
parents: 2
diff changeset
42
9c99e2193277 find broadcast
one
parents: 2
diff changeset
43 mAddr = InetAddress.getByName(mCASTADDR);
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
44 sAddr = new InetSocketAddress(mAddr,port);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 dc.join(mAddr, ni);
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
46
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
47 if (sender == SocketType.Receiver) {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
48 dc.bind(new InetSocketAddress(port));
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
49 if (selectMode) {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
50 dc.configureBlocking(false);
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
51 dc.register(selector, SelectionKey.OP_READ);
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
52 }
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
53 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 @Override
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 public void recieve(ByteBuffer testData, long timeout) throws IOException {
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
58 if (selectMode && selector.select(timeout)==0) {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
59 System.out.println("bad select "+timeout);
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
60 testData.limit(0); testData.position(0);
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
61 return;
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
62 }
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
63 SocketAddress s = dc.receive(testData);
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
64 testData.flip();
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
65 System.out.println("From "+s+" "+testData.remaining()+" bytes.");
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 @Override
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 public void send(ByteBuffer testData) throws IOException {
11
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
70 while(testData.hasRemaining()) {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
71 dc.send(testData, sAddr);
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
72 }
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
73 try {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
74 Thread.sleep(100);
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
75 } catch (InterruptedException e) {
7912fd3af027 non select mode on DatagramChannel.
one
parents: 9
diff changeset
76 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }