use of de.javawi.jstun.util.UtilityException in project Smack by igniterealtime.
the class ICEResolver method resolve.
/**
* Resolve the IP and obtain a valid transport method.
* @throws SmackException
* @throws InterruptedException
*/
@Override
public synchronized void resolve(JingleSession session) throws XMPPException, SmackException, InterruptedException {
this.setResolveInit();
for (TransportCandidate candidate : this.getCandidatesList()) {
if (candidate instanceof ICECandidate) {
ICECandidate iceCandidate = (ICECandidate) candidate;
iceCandidate.removeCandidateEcho();
}
}
this.clear();
// Create a transport candidate for each ICE negotiator candidate we have.
ICENegociator iceNegociator = negociatorsMap.get(server);
for (Candidate candidate : iceNegociator.getSortedCandidates()) try {
Candidate.CandidateType type = candidate.getCandidateType();
ICECandidate.Type iceType = ICECandidate.Type.local;
if (type.equals(Candidate.CandidateType.ServerReflexive))
iceType = ICECandidate.Type.srflx;
else if (type.equals(Candidate.CandidateType.PeerReflexive))
iceType = ICECandidate.Type.prflx;
else if (type.equals(Candidate.CandidateType.Relayed))
iceType = ICECandidate.Type.relay;
else
iceType = ICECandidate.Type.host;
// JBW/GW - 17JUL08: Figure out the zero-based NIC number for this candidate.
short nicNum = 0;
try {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
short i = 0;
NetworkInterface nic = NetworkInterface.getByInetAddress(candidate.getAddress().getInetAddress());
while (nics.hasMoreElements()) {
NetworkInterface checkNIC = nics.nextElement();
if (checkNIC.equals(nic)) {
nicNum = i;
break;
}
i++;
}
} catch (SocketException e1) {
LOGGER.log(Level.WARNING, "exeption", e1);
}
TransportCandidate transportCandidate = new ICECandidate(candidate.getAddress().getInetAddress().getHostAddress(), 1, nicNum, String.valueOf(Math.abs(random.nextLong())), candidate.getPort(), "1", candidate.getPriority(), iceType);
transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress());
transportCandidate.setPort(getFreePort());
try {
transportCandidate.addCandidateEcho(session);
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
this.addCandidate(transportCandidate);
LOGGER.fine("Candidate addr: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " Priority:" + candidate.getPriority());
} catch (UtilityException e) {
LOGGER.log(Level.WARNING, "exception", e);
} catch (UnknownHostException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
if (RTPBridge.serviceAvailable(connection)) {
// try {
String localIp;
int network;
// JBW/GW - 17JUL08: ICENegotiator.getPublicCandidate() always returned null in JSTUN 1.7.0, and now the API doesn't exist in JSTUN 1.7.1
// if (iceNegociator.getPublicCandidate() != null) {
// localIp = iceNegociator.getPublicCandidate().getBase().getAddress().getInetAddress().getHostAddress();
// network = iceNegociator.getPublicCandidate().getNetwork();
// }
// else {
{
localIp = BridgedResolver.getLocalHost();
network = 0;
}
sid = Math.abs(random.nextLong());
RTPBridge rtpBridge = RTPBridge.getRTPBridge(connection, String.valueOf(sid));
TransportCandidate localCandidate = new ICECandidate(rtpBridge.getIp(), 1, network, String.valueOf(Math.abs(random.nextLong())), rtpBridge.getPortA(), "1", 0, ICECandidate.Type.relay);
localCandidate.setLocalIp(localIp);
TransportCandidate remoteCandidate = new ICECandidate(rtpBridge.getIp(), 1, network, String.valueOf(Math.abs(random.nextLong())), rtpBridge.getPortB(), "1", 0, ICECandidate.Type.relay);
remoteCandidate.setLocalIp(localIp);
localCandidate.setSymmetric(remoteCandidate);
remoteCandidate.setSymmetric(localCandidate);
localCandidate.setPassword(rtpBridge.getPass());
remoteCandidate.setPassword(rtpBridge.getPass());
localCandidate.setSessionId(rtpBridge.getSid());
remoteCandidate.setSessionId(rtpBridge.getSid());
localCandidate.setConnection(this.connection);
remoteCandidate.setConnection(this.connection);
addCandidate(localCandidate);
// if (iceNegociator.getPublicCandidate() == null) {
if (true) {
String publicIp = RTPBridge.getPublicIP(connection);
if (publicIp != null && !publicIp.equals("")) {
Enumeration<NetworkInterface> ifaces = null;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
// If detect this address in local machine, don't use it.
boolean found = false;
while (ifaces.hasMoreElements() && !false) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (iaddress.getHostAddress().indexOf(publicIp) > -1) {
found = true;
break;
}
}
}
if (!found) {
try {
TransportCandidate publicCandidate = new ICECandidate(publicIp, 1, 0, String.valueOf(Math.abs(random.nextLong())), getFreePort(), "1", 0, ICECandidate.Type.srflx);
publicCandidate.setLocalIp(InetAddress.getLocalHost().getHostAddress());
try {
publicCandidate.addCandidateEcho(session);
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
addCandidate(publicCandidate);
} catch (UnknownHostException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
}
}
}
}
this.setResolveEnd();
}
use of de.javawi.jstun.util.UtilityException in project Smack by igniterealtime.
the class STUNResolverTest method testICEPriority.
/**
* Test priority generated by STUN lib
*
* @throws Exception
*/
public void testICEPriority() throws Exception {
String first = "";
for (int i = 0; i < 100; i++) {
ICENegociator cc = new ICENegociator((short) 1);
// gather candidates
cc.gatherCandidateAddresses();
// priorize candidates
cc.prioritizeCandidates();
for (Candidate candidate : cc.getSortedCandidates()) {
short nicNum = 0;
try {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
short tempNic = 0;
NetworkInterface nic = NetworkInterface.getByInetAddress(candidate.getAddress().getInetAddress());
while (nics.hasMoreElements()) {
NetworkInterface checkNIC = nics.nextElement();
if (checkNIC.equals(nic)) {
nicNum = tempNic;
break;
}
i++;
}
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
TransportCandidate transportCandidate = new ICECandidate(candidate.getAddress().getInetAddress().getHostAddress(), 1, nicNum, "1", candidate.getPort(), "1", candidate.getPriority(), ICECandidate.Type.prflx);
transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress());
System.out.println("C: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " p:" + candidate.getPriority());
} catch (UtilityException e) {
LOGGER.log(Level.WARNING, "exception", e);
} catch (UnknownHostException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
}
Candidate candidate = cc.getSortedCandidates().get(0);
String temp = "C: " + candidate.getAddress().getInetAddress() + "|" + candidate.getBase().getAddress().getInetAddress() + " p:" + candidate.getPriority();
if (first.equals(""))
first = temp;
assertEquals(first, temp);
first = temp;
}
}
Aggregations