Skip to content

Instantly share code, notes, and snippets.

@lefloh
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lefloh/61e27a2b0e9559b96174 to your computer and use it in GitHub Desktop.
Save lefloh/61e27a2b0e9559b96174 to your computer and use it in GitHub Desktop.
Testing a connection to a turn server with Ice4J
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import org.ice4j.Transport;
import org.ice4j.TransportAddress;
import org.ice4j.ice.Agent;
import org.ice4j.ice.Component;
import org.ice4j.ice.IceMediaStream;
import org.ice4j.ice.harvest.TurnCandidateHarvester;
import org.ice4j.security.LongTermCredential;
import org.junit.Test;
public class TurnServerTestClient {
private static final String TURN_SERVER_HOST = "localhost";
private static final int TURN_SERVER_PORT = 3478;
@Test
public void testTurnServer() throws Exception {
TurnServerTestClient client = new TurnServerTestClient();
int validCandidates = client.connect("turn", "server");
int invalidCandidates = client.connect("turn", "invalid");
// if connections was successfull we should get 6 instead of 5 local candidates
assertTrue(validCandidates > invalidCandidates);
}
/**
* connects to the server and returns the number of local candidates
*/
private int connect(String user, String password) throws BindException, IllegalArgumentException, IOException {
Agent agent = new Agent();
TransportAddress address = new TransportAddress(InetAddress.getByName(TURN_SERVER_HOST), TURN_SERVER_PORT, Transport.UDP);
TurnCandidateHarvester harvester = new TurnCandidateHarvester(address, new LongTermCredential(user, password));
agent.addCandidateHarvester(harvester);
IceMediaStream stream = agent.createMediaStream("audio");
int port = 5000; // Choose any port
Component component = agent.createComponent(stream, Transport.UDP, port, port, port + 100);
int localCandidateCount = component.getLocalCandidateCount();
agent.free(); // like close()
return localCandidateCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment