Mercurial > hg > Members > kono > WifiBroadcast
comparison src/main/java/jp/ac/uryukyu/ie/kono/wifibroadcast/WifiMulticastChannel.java @ 27:12633f67f3cf default tip
jar
author | oshiro |
---|---|
date | Wed, 28 Nov 2018 18:28:39 +0900 |
parents | src/main/jp/ac/uryukyu/ie/kono/wifibroadcast/WifiMulticastChannel.java@42ecbd9364fa |
children |
comparison
equal
deleted
inserted
replaced
26:42ecbd9364fa | 27:12633f67f3cf |
---|---|
1 package jp.ac.uryukyu.ie.kono.wifibroadcast; | |
2 | |
3 import java.io.IOException; | |
4 import java.net.InetAddress; | |
5 import java.net.InetSocketAddress; | |
6 import java.net.NetworkInterface; | |
7 import java.net.SocketAddress; | |
8 import java.net.StandardProtocolFamily; | |
9 import java.net.StandardSocketOptions; | |
10 import java.nio.ByteBuffer; | |
11 import java.nio.channels.DatagramChannel; | |
12 import java.nio.channels.SelectionKey; | |
13 import java.nio.channels.Selector; | |
14 import java.nio.channels.spi.SelectorProvider; | |
15 | |
16 public class WifiMulticastChannel implements WifiReceiver { | |
17 protected InetAddress mAddr; | |
18 protected DatagramChannel dc; | |
19 protected SocketAddress sAddr; | |
20 // select on DatagramChannel does not work now | |
21 protected Selector selector; | |
22 protected boolean selectMode = false; | |
23 | |
24 public WifiMulticastChannel() {} | |
25 | |
26 public WifiMulticastChannel(int id, String mCASTADDR, int port,String nis, SocketType sender) throws IOException { | |
27 // join multicast group on this interface, and also use this | |
28 // interface for outgoing multicast datagrams | |
29 selector = SelectorProvider.provider().openSelector(); | |
30 NetworkInterface ni = NetworkInterface.getByName(nis); | |
31 if (ni==null) { | |
32 System.err.println("Can't open network interface "+nis); | |
33 throw new IOException(); | |
34 } | |
35 | |
36 if (!ni.supportsMulticast()) { | |
37 System.err.println("Network interface does not support multicast"+nis); | |
38 throw new IOException(); | |
39 } | |
40 | |
41 // dc = SelectorProvider.provider().openDatagramChannel(StandardProtocolFamily.INET); | |
42 dc = DatagramChannel.open(StandardProtocolFamily.INET); | |
43 dc.setOption(StandardSocketOptions.SO_REUSEADDR, true); | |
44 dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); | |
45 mAddr = InetAddress.getByName(mCASTADDR); | |
46 sAddr = new InetSocketAddress(mAddr,port); | |
47 dc.join(mAddr, ni); | |
48 | |
49 if (sender == SocketType.Receiver) { | |
50 dc.bind(new InetSocketAddress(port)); | |
51 if (selectMode) { | |
52 dc.configureBlocking(false); | |
53 dc.register(selector, SelectionKey.OP_READ); | |
54 } | |
55 } | |
56 } | |
57 | |
58 @Override | |
59 public void recieve(ByteBuffer testData, long timeout) throws IOException { | |
60 if (selectMode && selector.select(timeout)==0) { | |
61 System.out.println("bad select "+timeout); | |
62 testData.limit(0); testData.position(0); | |
63 return; | |
64 } | |
65 SocketAddress s = dc.receive(testData); | |
66 testData.flip(); | |
67 System.out.println("From "+s+" "+testData.remaining()+" bytes."); | |
68 } | |
69 | |
70 @Override | |
71 public void send(ByteBuffer testData) throws IOException { | |
72 while(testData.hasRemaining()) { | |
73 dc.send(testData, sAddr); | |
74 } | |
75 // try { | |
76 // Thread.sleep(100); | |
77 // } catch (InterruptedException e) { | |
78 // } | |
79 } | |
80 | |
81 @Override | |
82 public void selectMode(boolean mode) { | |
83 selectMode = mode; | |
84 } | |
85 | |
86 } |