package uk.ac.ntu.n0521366.wsyd.libs.net;
import java.io.IOException;
+import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.NetworkInterface;
this._multicastSocket.setTimeToLive(2); // don't traverse more than 2 routers
this._multicastSocket.setSoTimeout(100); // 1/10th second blocking timeout on receive()
this._multicastSocket.setLoopbackMode(false); // inverted logic; true == disable. Don't want to receive our own sent packets
-
+ if (_socketAddress.getAddress() == Network.IPv4_WILDCARD || _socketAddress.getPort() == Network.PORTS_EPHEMERAL) {
+ // reflect the actual port in use if an ephermal port was requested
+ InetSocketAddress actualSA = (InetSocketAddress) _multicastSocket.getLocalSocketAddress();
+ _socketAddress.setAddress(actualSA.getAddress());
+ _socketAddress.setPort(actualSA.getPort());
+ }
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
while (ifs.hasMoreElements()) {
NetworkInterface iface = ifs.nextElement();
- messages.clear();
- messages.add(iface.getName());
- messages.add(Boolean.toString(iface.supportsMulticast()));
- log(Level.INFO, _title, "Interface {0}: probe multicast support: {1}", messages);
+ logp(Level.INFO, "Interface {0}: probe multicast support: {1}", iface.getName(), Boolean.toString(iface.supportsMulticast()));
if (iface.supportsMulticast()) {
try {
- messages.clear();
- messages.add(iface.getName());
- messages.add(_socketAddress.getSocketAddress().getAddress().toString());
this._multicastSocket.joinGroup(_socketAddress.getSocketAddress(), iface);
- log(Level.INFO, _title, "Interface {0}: joined multicast group {1}", messages);
+ logp(Level.INFO, "Interface {0}: joined multicast group {1}", iface.getName(), _socketAddress.getSocketAddress().getAddress().toString());
} catch (Exception e) {
- messages.clear();
- messages.add(iface.getName());
- log(Level.SEVERE, _title, "Interface {0}: failed to join multicast group", messages);
+ logp(Level.SEVERE, "Interface {0}: failed to join multicast group", iface.getName());
}
}
}
} catch (IOException e) {
- log(Level.SEVERE, _title, "Failed to open multicast socket");
+ logp(Level.SEVERE, _title, "Failed to open multicast socket");
}
}
public void serverClose() throws SocketException {
if (this._multicastSocket != null) {
try {
- log(Level.INFO, _title, "Leaving multicast group");
+ logp(Level.INFO, "Leaving multicast group");
this._multicastSocket.leaveGroup(_socketAddress.getAddress());
this._multicastSocket.close();
} catch (IOException e) {
- log(Level.SEVERE, _title, "failed to leave multicast group");
+ logp(Level.SEVERE, "failed to leave multicast group");
}
}