Mercurial > hg > Database > Alice
annotate src/main/java/alice/datasegment/MulticastDataSegmentManager.java @ 650:4289b232b3fd
nulValue
author | suruga |
---|---|
date | Fri, 02 Feb 2018 18:26:49 +0900 |
parents | 53d7cff1fe10 |
children | 0832af83583f |
rev | line source |
---|---|
360 | 1 package alice.datasegment; |
2 | |
3 import java.io.IOException; | |
4 import java.net.InetAddress; | |
361 | 5 import java.net.InetSocketAddress; |
360 | 6 import java.net.NetworkInterface; |
361 | 7 import java.net.SocketAddress; |
360 | 8 import java.net.StandardProtocolFamily; |
9 import java.net.StandardSocketOptions; | |
10 import java.nio.channels.DatagramChannel; | |
361 | 11 |
360 | 12 import org.apache.log4j.Logger; |
13 | |
361 | 14 import alice.daemon.IncomingUdpConnection; |
15 import alice.daemon.MulticastConnection; | |
16 import alice.daemon.OutboundTcpConnection; | |
360 | 17 |
369
0c24894db37e
MulticastDataSegment's extends change from LocalDataSegment to RemoteDataSegment
sugi
parents:
366
diff
changeset
|
18 public class MulticastDataSegmentManager extends RemoteDataSegmentManager { |
419 | 19 |
508 | 20 public enum SocketType{Sender, Receiver, Both}; |
21 public MulticastDataSegmentManager(String connectionKey ,final String MCASTADDR, final int port, final String nis, SocketType type) { | |
419 | 22 logger = Logger.getLogger(connectionKey); |
23 InetAddress mAddr; | |
24 try { | |
25 mAddr = InetAddress.getByName(MCASTADDR); | |
26 | |
27 DatagramChannel dcr = createDatagramChannel(mAddr, port, nis); | |
28 dcr.bind(new InetSocketAddress(port)); | |
29 SocketAddress sAddrr = new InetSocketAddress(mAddr,port); | |
30 MulticastConnection receiver = new MulticastConnection(dcr, sAddrr); | |
361 | 31 |
419 | 32 DatagramChannel dcs = createDatagramChannel(mAddr, port, nis); |
33 SocketAddress sAddrs = new InetSocketAddress(mAddr,port); | |
467 | 34 connection = new MulticastConnection(dcs, sAddrs); // sender |
419 | 35 |
508 | 36 if (type !=SocketType.Sender) { |
37 IncomingUdpConnection in = new IncomingUdpConnection((MulticastConnection) connection, receiver, this); | |
38 in.setName("multicast-IncomingUdp"); | |
39 in.start(); | |
509 | 40 DataSegment.setAccept(connectionKey, in); |
508 | 41 } |
42 if (type !=SocketType.Receiver) { | |
43 OutboundTcpConnection out = new OutboundTcpConnection(connection); // OutboundUdpConnection sender | |
44 out.setName(connectionKey+"OutboundUdp"); | |
45 out.start(); | |
46 } | |
419 | 47 } catch (Exception e) { |
48 e.printStackTrace(); | |
49 } | |
50 | |
51 } | |
369
0c24894db37e
MulticastDataSegment's extends change from LocalDataSegment to RemoteDataSegment
sugi
parents:
366
diff
changeset
|
52 |
419 | 53 private DatagramChannel createDatagramChannel(InetAddress group, int port, String nis) { |
54 DatagramChannel dc = null; | |
55 NetworkInterface ni; | |
56 try { | |
57 ni = NetworkInterface.getByName(nis); | |
58 if (ni==null) { | |
59 System.err.println("Can't open network interface "+nis); | |
60 throw new IOException(); | |
61 } | |
62 if (!ni.supportsMulticast()) { | |
63 System.err.println("Network interface does not support multicast"+nis); | |
64 throw new IOException(); | |
65 } | |
66 | |
67 dc = DatagramChannel.open(StandardProtocolFamily.INET); | |
68 dc.setOption(StandardSocketOptions.SO_REUSEADDR, true); | |
69 dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); | |
70 dc.join(group, ni); | |
71 } catch (Exception e) { | |
72 e.printStackTrace(); | |
73 } | |
74 return dc; | |
467 | 75 } |
360 | 76 } |