diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-12-01 13:08:59 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-12-01 13:08:59 -0500 |
| commit | 773c4b8a696dd49b3f0452783e1634350a82226a (patch) | |
| tree | ef12f77af9c8f6fd1ba80cd58bb2e811bbc2e978 | |
| parent | fc28d5d3607ac5056272e61b272a823c25dc9bc1 (diff) | |
| download | soen423-773c4b8a696dd49b3f0452783e1634350a82226a.zip | |
pass network interface name to sequencer explicitely
4 files changed, 52 insertions, 8 deletions
diff --git a/src/main/java/derms/Sequencer.java b/src/main/java/derms/Sequencer.java index 530e0c1..88b15a1 100644 --- a/src/main/java/derms/Sequencer.java +++ b/src/main/java/derms/Sequencer.java @@ -6,6 +6,7 @@ import derms.net.tomulticast.TotalOrderMulticastSender; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; import java.util.logging.Logger; /** @@ -13,7 +14,7 @@ import java.util.logging.Logger; * sends them to the RMs via {@link derms.net.tomulticast.TotalOrderMulticastSender}. */ public class Sequencer implements Runnable { - public static final String usage = "Usage: java Sequencer <laddr>"; + public static final String usage = "Usage: java Sequencer <ip address> <network interface>"; private final ReliableUnicastReceiver<Request> in; // From FE. private final TotalOrderMulticastSender<Request> out; // To RMs. @@ -22,10 +23,11 @@ public class Sequencer implements Runnable { /** * * @param laddr The local IP address. + * @param ifs The network interface to use. */ - public Sequencer(InetAddress laddr) throws IOException { + public Sequencer(InetAddress laddr, NetworkInterface ifs) throws IOException { this.in = new ReliableUnicastReceiver<Request>(new InetSocketAddress(laddr, Config.sequencerInPort)); - this.out = new TotalOrderMulticastSender<Request>(Config.group, laddr); + this.out = new TotalOrderMulticastSender<Request>(Config.group, laddr, ifs); this.log = Logger.getLogger(getClass().getName()); } @@ -41,7 +43,7 @@ public class Sequencer implements Runnable { Sequencer seq = null; try { - seq = new Sequencer(args.laddr); + seq = new Sequencer(args.laddr, args.ifs); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); @@ -82,15 +84,25 @@ public class Sequencer implements Runnable { private static class Args { private final InetAddress laddr; + private final NetworkInterface ifs; private Args(String[] args) throws IllegalArgumentException { if (args.length < 1) { - throw new IllegalArgumentException("Missing argument 'laddr'"); + throw new IllegalArgumentException("Missing argument 'ip address'"); } try { this.laddr = InetAddress.getByName(args[0]); } catch (Exception e) { - throw new IllegalArgumentException("Bad value of 'laddr': " + e.getMessage()); + throw new IllegalArgumentException("Bad value of 'ip address': " + e.getMessage()); + } + + if (args.length < 2) { + throw new IllegalArgumentException("Missing argument 'network interface'"); + } + try { + this.ifs = NetworkInterface.getByName(args[1]); + } catch (Exception e ) { + throw new IllegalArgumentException("Bad value of 'network interface': " + e.getMessage()); } } } diff --git a/src/main/java/derms/net/rmulticast/ReliableMulticast.java b/src/main/java/derms/net/rmulticast/ReliableMulticast.java index f3db55c..c08d5a1 100644 --- a/src/main/java/derms/net/rmulticast/ReliableMulticast.java +++ b/src/main/java/derms/net/rmulticast/ReliableMulticast.java @@ -38,8 +38,9 @@ public class ReliableMulticast<T extends MessagePayload> { * * @param group The IP address and port of the multicast group. * @param laddr The IP address of the local process. + * @param ifs The network interface to use. */ - public ReliableMulticast(InetSocketAddress group, InetAddress laddr) throws IOException { + public ReliableMulticast(InetSocketAddress group, InetAddress laddr, NetworkInterface ifs) throws IOException { this.group = group; this.laddr = laddr; @@ -49,7 +50,7 @@ public class ReliableMulticast<T extends MessagePayload> { this.retransmissions = new LinkedBlockingQueue<Message<T>>(); this.groupMembers = ConcurrentHashMap.newKeySet(); - NetworkInterface ifs = Net.getMulticastInterface(); + System.out.println(getClass().getSimpleName() + " using network interface " + ifs); this.sock = DatagramChannel.open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true) .bind(new InetSocketAddress(group.getAddress(), group.getPort())) @@ -67,6 +68,16 @@ public class ReliableMulticast<T extends MessagePayload> { pool.execute(new Heartbeat(group, laddr, acks, nacks, sock)); } + /** + * Join the specified multicast group using the default network interface on the machine. + * + * @param group The IP address and port of the multicast group. + * @param laddr The IP address of the local process. + */ + public ReliableMulticast(InetSocketAddress group, InetAddress laddr) throws IOException { + this(group, laddr, Net.getMulticastInterface()); + } + public void close() throws IOException { log.info("Shutting down..."); sock.close(); diff --git a/src/main/java/derms/net/tomulticast/TotalOrderMulticast.java b/src/main/java/derms/net/tomulticast/TotalOrderMulticast.java index 0d8b690..1fb4348 100644 --- a/src/main/java/derms/net/tomulticast/TotalOrderMulticast.java +++ b/src/main/java/derms/net/tomulticast/TotalOrderMulticast.java @@ -6,6 +6,7 @@ import derms.net.rmulticast.ReliableMulticast; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; import java.util.logging.Logger; /** @@ -29,6 +30,13 @@ public abstract class TotalOrderMulticast<T extends MessagePayload> { protected Long seq; // Sequence number. protected final Logger log; + protected TotalOrderMulticast(InetSocketAddress group, InetAddress laddr, NetworkInterface ifs) throws IOException { + this.sock = new ReliableMulticast<Message<T>>(group, laddr, ifs); + this.group = group; + this.seq = (long) 0; + this.log = Logger.getLogger(this.getClass().getName()); + } + protected TotalOrderMulticast(InetSocketAddress group, InetAddress laddr) throws IOException { this.sock = new ReliableMulticast<Message<T>>(group, laddr); this.group = group; diff --git a/src/main/java/derms/net/tomulticast/TotalOrderMulticastSender.java b/src/main/java/derms/net/tomulticast/TotalOrderMulticastSender.java index 0de0ad0..b9ca99a 100644 --- a/src/main/java/derms/net/tomulticast/TotalOrderMulticastSender.java +++ b/src/main/java/derms/net/tomulticast/TotalOrderMulticastSender.java @@ -5,6 +5,7 @@ import derms.net.MessagePayload; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; /** * The single sending process in a {@link TotalOrderMulticast} group. <b>Only one sender is @@ -16,6 +17,18 @@ public class TotalOrderMulticastSender<T extends MessagePayload> extends TotalOr * * @param group The IP address and port of the multicast group to join. * @param laddr The IP address of the local process. + * @param ifs The network interface to use. + */ + public TotalOrderMulticastSender(InetSocketAddress group, InetAddress laddr, NetworkInterface ifs) throws IOException { + super(group, laddr, ifs); + } + + /** + * Join the specified totally-ordered multicast group as its lone sender using the + * default network interface of the machine. + * + * @param group The IP address and port of the multicast group to join. + * @param laddr The IP address of the local process. */ public TotalOrderMulticastSender(InetSocketAddress group, InetAddress laddr) throws IOException { super(group, laddr); |