Mercurial > hg > Members > kono > WifiBroadcast
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 |
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 | 12 import java.nio.channels.SelectionKey; |
13 import java.nio.channels.Selector; | |
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 | 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 | 20 // select on DatagramChannel does not work now |
3 | 21 private Selector selector; |
11 | 22 private boolean selectMode = false; |
3 | 23 |
9 | 24 public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException { |
3 | 25 // join multicast group on this interface, and also use this |
26 // interface for outgoing multicast datagrams | |
27 selector = SelectorProvider.provider().openSelector(); | |
28 NetworkInterface ni = NetworkInterface.getByName("en1"); | |
29 if (ni==null) { | |
30 System.err.println("Can't open network interface "+"en1"); | |
31 throw new IOException(); | |
32 } | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 |
3 | 34 if (!ni.supportsMulticast()) { |
35 System.err.println("Network interface does not support multicast"+"en1"); | |
36 throw new IOException(); | |
37 } | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 |
3 | 39 dc = DatagramChannel.open(StandardProtocolFamily.INET) |
40 .setOption(StandardSocketOptions.SO_REUSEADDR, true) | |
41 .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); | |
42 | |
43 mAddr = InetAddress.getByName(mCASTADDR); | |
11 | 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 | 46 |
47 if (sender == SocketType.Receiver) { | |
48 dc.bind(new InetSocketAddress(port)); | |
49 if (selectMode) { | |
50 dc.configureBlocking(false); | |
51 dc.register(selector, SelectionKey.OP_READ); | |
52 } | |
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 | 58 if (selectMode && selector.select(timeout)==0) { |
59 System.out.println("bad select "+timeout); | |
3 | 60 testData.limit(0); testData.position(0); |
11 | 61 return; |
3 | 62 } |
11 | 63 SocketAddress s = dc.receive(testData); |
64 testData.flip(); | |
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 | 70 while(testData.hasRemaining()) { |
71 dc.send(testData, sAddr); | |
72 } | |
73 try { | |
74 Thread.sleep(100); | |
75 } catch (InterruptedException e) { | |
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 } |