Mercurial > hg > Members > kono > WifiBroadcast
comparison 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 |
comparison
equal
deleted
inserted
replaced
10:fc180f38257e | 11:7912fd3af027 |
---|---|
15 | 15 |
16 public class WifiMulticastChannel implements WifiReceiver { | 16 public class WifiMulticastChannel implements WifiReceiver { |
17 private InetAddress mAddr; | 17 private InetAddress mAddr; |
18 private DatagramChannel dc; | 18 private DatagramChannel dc; |
19 private SocketAddress sAddr; | 19 private SocketAddress sAddr; |
20 // select on DatagramChannel does not work now | |
20 private Selector selector; | 21 private Selector selector; |
22 private boolean selectMode = false; | |
21 | 23 |
22 public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException { | 24 public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException { |
23 // join multicast group on this interface, and also use this | 25 // join multicast group on this interface, and also use this |
24 // interface for outgoing multicast datagrams | 26 // interface for outgoing multicast datagrams |
25 selector = SelectorProvider.provider().openSelector(); | 27 selector = SelectorProvider.provider().openSelector(); |
34 throw new IOException(); | 36 throw new IOException(); |
35 } | 37 } |
36 | 38 |
37 dc = DatagramChannel.open(StandardProtocolFamily.INET) | 39 dc = DatagramChannel.open(StandardProtocolFamily.INET) |
38 .setOption(StandardSocketOptions.SO_REUSEADDR, true) | 40 .setOption(StandardSocketOptions.SO_REUSEADDR, true) |
39 .bind(sAddr = new InetSocketAddress(port)) | |
40 .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); | 41 .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); |
41 | 42 |
42 mAddr = InetAddress.getByName(mCASTADDR); | 43 mAddr = InetAddress.getByName(mCASTADDR); |
43 | 44 sAddr = new InetSocketAddress(mAddr,port); |
44 dc.join(mAddr, ni); | 45 dc.join(mAddr, ni); |
45 dc.configureBlocking(false); | 46 |
46 dc.register(selector, SelectionKey.OP_READ); | 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 } | |
47 } | 54 } |
48 | 55 |
49 @Override | 56 @Override |
50 public void recieve(ByteBuffer testData, long timeout) throws IOException { | 57 public void recieve(ByteBuffer testData, long timeout) throws IOException { |
51 if (selector.select(timeout)>0) { | 58 if (selectMode && selector.select(timeout)==0) { |
52 dc.receive(testData); | 59 System.out.println("bad select "+timeout); |
53 } else { | |
54 testData.limit(0); testData.position(0); | 60 testData.limit(0); testData.position(0); |
61 return; | |
55 } | 62 } |
63 SocketAddress s = dc.receive(testData); | |
64 testData.flip(); | |
65 System.out.println("From "+s+" "+testData.remaining()+" bytes."); | |
56 } | 66 } |
57 | 67 |
58 @Override | 68 @Override |
59 public void send(ByteBuffer testData) throws IOException { | 69 public void send(ByteBuffer testData) throws IOException { |
60 dc.send(testData, sAddr); | 70 while(testData.hasRemaining()) { |
71 dc.send(testData, sAddr); | |
72 } | |
73 try { | |
74 Thread.sleep(100); | |
75 } catch (InterruptedException e) { | |
76 } | |
61 } | 77 } |
62 | 78 |
63 } | 79 } |