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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
1 package alice.datasegment;
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
2
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
3 import java.io.IOException;
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
4 import java.net.InetAddress;
361
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
5 import java.net.InetSocketAddress;
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
6 import java.net.NetworkInterface;
361
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
7 import java.net.SocketAddress;
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
8 import java.net.StandardProtocolFamily;
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
9 import java.net.StandardSocketOptions;
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
10 import java.nio.channels.DatagramChannel;
361
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
11
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
12 import org.apache.log4j.Logger;
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
13
361
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
14 import alice.daemon.IncomingUdpConnection;
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
15 import alice.daemon.MulticastConnection;
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
16 import alice.daemon.OutboundTcpConnection;
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
17
369
0c24894db37e MulticastDataSegment's extends change from LocalDataSegment to RemoteDataSegment
sugi
parents: 366
diff changeset
18 public class MulticastDataSegmentManager extends RemoteDataSegmentManager {
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
19
508
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
20 public enum SocketType{Sender, Receiver, Both};
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
21 public MulticastDataSegmentManager(String connectionKey ,final String MCASTADDR, final int port, final String nis, SocketType type) {
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
22 logger = Logger.getLogger(connectionKey);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
23 InetAddress mAddr;
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
24 try {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
25 mAddr = InetAddress.getByName(MCASTADDR);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
26
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
27 DatagramChannel dcr = createDatagramChannel(mAddr, port, nis);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
28 dcr.bind(new InetSocketAddress(port));
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
29 SocketAddress sAddrr = new InetSocketAddress(mAddr,port);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
30 MulticastConnection receiver = new MulticastConnection(dcr, sAddrr);
361
60eee1fb0fd3 create sender with udp
sugi
parents: 360
diff changeset
31
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
32 DatagramChannel dcs = createDatagramChannel(mAddr, port, nis);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
33 SocketAddress sAddrs = new InetSocketAddress(mAddr,port);
467
6e304a7a60e7 remove white space
sugi
parents: 419
diff changeset
34 connection = new MulticastConnection(dcs, sAddrs); // sender
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
35
508
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
36 if (type !=SocketType.Sender) {
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
37 IncomingUdpConnection in = new IncomingUdpConnection((MulticastConnection) connection, receiver, this);
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
38 in.setName("multicast-IncomingUdp");
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
39 in.start();
509
53d7cff1fe10 register IncomingUDPConnection acceptHash
sugi
parents: 508
diff changeset
40 DataSegment.setAccept(connectionKey, in);
508
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
41 }
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
42 if (type !=SocketType.Receiver) {
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
43 OutboundTcpConnection out = new OutboundTcpConnection(connection); // OutboundUdpConnection sender
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
44 out.setName(connectionKey+"OutboundUdp");
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
45 out.start();
b7d02ea79850 change multicast Data Segment API
sugi
parents: 480
diff changeset
46 }
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
47 } catch (Exception e) {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
48 e.printStackTrace();
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
49 }
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
50
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
51 }
369
0c24894db37e MulticastDataSegment's extends change from LocalDataSegment to RemoteDataSegment
sugi
parents: 366
diff changeset
52
419
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
53 private DatagramChannel createDatagramChannel(InetAddress group, int port, String nis) {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
54 DatagramChannel dc = null;
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
55 NetworkInterface ni;
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
56 try {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
57 ni = NetworkInterface.getByName(nis);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
58 if (ni==null) {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
59 System.err.println("Can't open network interface "+nis);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
60 throw new IOException();
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
61 }
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
62 if (!ni.supportsMulticast()) {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
63 System.err.println("Network interface does not support multicast"+nis);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
64 throw new IOException();
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
65 }
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
66
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
67 dc = DatagramChannel.open(StandardProtocolFamily.INET);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
68 dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
69 dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
70 dc.join(group, ni);
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
71 } catch (Exception e) {
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
72 e.printStackTrace();
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
73 }
aefbe41fcf12 change tab to space
sugi
parents: 369
diff changeset
74 return dc;
467
6e304a7a60e7 remove white space
sugi
parents: 419
diff changeset
75 }
360
6cf08aebfc31 add MulticastConnection and DataSegmentManager
sugi
parents:
diff changeset
76 }