ice4j/src/org/ice4j/ice/ConnectivityCheckClient.java

143 lines
4.1 KiB
Java

/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.ice;
import java.net.*;
import java.util.logging.*;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* @author Emil Ivov
*/
public class ConnectivityCheckClient
implements ResponseCollector
{
/**
* The <tt>Logger</tt> used by the <tt>ConnectivityCheckClient</tt>
* class and its instances for logging output.
*/
private static final Logger logger = Logger
.getLogger(ConnectivityCheckClient.class.getName());
/**
* The agent that created us.
*/
private final Agent parentAgent;
/**
* The stun stack that we will use for connectivity checks.
*/
StunStack stunStack = StunStack.getInstance();
/**
* Creates a new <tt>ConnectivityCheckHandler</tt> setting
* <tt>parentAgent</tt> as the agent that will be used for retrieving
* information such as user fragments for example.
*
* @param parentAgent the <tt>Agent</tt> that is creating this instance.
*/
public ConnectivityCheckClient(Agent parentAgent)
{
this.parentAgent = parentAgent;
}
public void startChecks(CheckList checkList)
{
for(CandidatePair pair : checkList)
{
startCheckForPair(pair);
}
}
private void startCheckForPair(CandidatePair pair)
{
//we don't need to do a canReach() verification here as it has been
//already verified during the gathering process.
DatagramSocket stunSocket = ((HostCandidate)pair.getLocalCandidate())
.getStunSocket(null);
Request request = MessageFactory.createBindingRequest();
//priority
PriorityAttribute priority = AttributeFactory.createPriorityAttribute(
pair.getLocalCandidate().computePriorityForType(
CandidateType.PEER_REFLEXIVE_CANDIDATE));
request.addAttribute(priority);
//controlling controlled
if (parentAgent.isControlling())
{
request.addAttribute(AttributeFactory
.createIceControllingAttribute(parentAgent
.getTieBreaker()));
}
else
{
request.addAttribute(AttributeFactory
.createIceControlledAttribute(parentAgent
.getTieBreaker()));
}
//credentials
String localUserName = parentAgent.generateLocalUserName();
UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute(
localUserName);
request.addAttribute(unameAttr);
//todo: do this in the stun stack so that we could do the
//calculation once the request is ready (we'd need the transaction id
//for example.
//todo: also implement SASL prepare
MessageIntegrityAttribute msgIntegrity = AttributeFactory
.createMessageIntegrityAttribute(localUserName);
request.addAttribute(msgIntegrity);
TransactionID tran;
try
{
tran = stunStack.sendRequest(request,
pair.getRemoteCandidate().getTransportAddress(), stunSocket,
this);
System.out.println("checking pair " + pair + " with tran=" + tran.toString());
}
catch (Exception exception)
{
logger.log( Level.INFO,
"Failed to send " + request + " through "
+ stunSocket.getLocalSocketAddress(),
exception);
return;
}
}
public void processResponse(StunMessageEvent response)
{
}
public void processTimeout(StunTimeoutEvent event)
{
System.out.println("timeout event=" + event);
}
public void processUnreachable(StunFailureEvent event)
{
System.out.println("failure event=" + event);
}
}