attributes = desc.getAttributes(true);
+ for( Attribute attribute : attributes)
+ {
+ if(!attribute.getName().equals(CandidateAttribute.NAME))
+ continue;
+
+ parseCandidate(attribute, stream);
+ }
+
+ //set default candidates
+ Connection streamConn = desc.getConnection();
+ String streamConnAddr = null;
+ if(streamConn != null)
+ streamConnAddr = streamConn.getAddress();
+ else
+ streamConnAddr = globalConnAddr;
+
+ int port = desc.getMedia().getMediaPort();
+
+ TransportAddress defaultRtpAddress =
+ new TransportAddress(streamConnAddr, port, Transport.UDP);
+
+ int rtcpPort = port + 1;
+ String rtcpAttributeValue = desc.getAttribute("rtcp");
+
+ if (rtcpAttributeValue != null)
+ rtcpPort = Integer.parseInt(rtcpAttributeValue);
+
+ TransportAddress defaultRtcpAddress =
+ new TransportAddress(streamConnAddr, rtcpPort, Transport.UDP);
+
+ Component rtpComponent = stream.getComponent(Component.RTP);
+ Component rtcpComponent = stream.getComponent(Component.RTCP);
+
+ Candidate defaultRtpCandidate
+ = rtpComponent.findRemoteCandidate(defaultRtpAddress);
+ rtpComponent.setDefaultRemoteCandidate(defaultRtpCandidate);
+
+ if(rtcpComponent != null)
+ {
+ Candidate defaultRtcpCandidate
+ = rtcpComponent.findRemoteCandidate(defaultRtcpAddress);
+ rtcpComponent.setDefaultRemoteCandidate(defaultRtcpCandidate);
+ }
+ }
+ }
+
+ /**
+ * Parses the attribute.
+ *
+ * @param attribute the attribute that we need to parse.
+ * @param stream the {@link IceMediaStream} that the candidate is supposed
+ * to belong to.
+ *
+ * @return a newly created {@link RemoteCandidate} matching the
+ * content of the specified attribute or null if the
+ * candidate belonged to a component we don't have.
+ */
+ private static RemoteCandidate parseCandidate(Attribute attribute,
+ IceMediaStream stream)
+ {
+ String value = null;
+
+ try{
+ value = attribute.getValue();
+ }catch (Throwable t){}//can't happen
+
+ StringTokenizer tokenizer = new StringTokenizer(value);
+
+ //XXX add exception handling.
+ String foundation = tokenizer.nextToken();
+ int componentID = Integer.parseInt( tokenizer.nextToken() );
+ Transport transport = Transport.parse(tokenizer.nextToken());
+ long priority = Long.parseLong(tokenizer.nextToken());
+ String address = tokenizer.nextToken();
+ int port = Integer.parseInt(tokenizer.nextToken());
+
+ TransportAddress transAddr
+ = new TransportAddress(address, port, transport);
+
+ tokenizer.nextToken(); //skip the "typ" String
+ CandidateType type = CandidateType.parse(tokenizer.nextToken());
+
+ Component component = stream.getComponent(componentID);
+
+ if(component == null)
+ return null;
+
+ // check if there's a related address property
+
+ RemoteCandidate relatedCandidate = null;
+ if (tokenizer.countTokens() >= 4)
+ {
+ tokenizer.nextToken(); // skip the raddr element
+ String relatedAddr = tokenizer.nextToken();
+ tokenizer.nextToken(); // skip the rport element
+ int relatedPort = Integer.parseInt(tokenizer.nextToken());
+
+ TransportAddress raddr = new TransportAddress(
+ relatedAddr, relatedPort, Transport.UDP);
+
+ relatedCandidate = component.findRemoteCandidate(raddr);
+ }
+
+ RemoteCandidate cand = new RemoteCandidate(transAddr, component, type,
+ foundation, priority, relatedCandidate);
+
+ component.addRemoteCandidate(cand);
+
+ return cand;
+ }
+
+
+}
diff --git a/test/test/TrickleIce.java b/test/test/TrickleIce.java
new file mode 100644
index 0000000..05c498a
--- /dev/null
+++ b/test/test/TrickleIce.java
@@ -0,0 +1,146 @@
+/*
+ * 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 test;
+
+import org.ice4j.ice.*;
+import org.ice4j.ice.harvest.*;
+import org.ice4j.ice.sdp.*;
+
+import javax.sdp.*;
+import java.io.*;
+import java.util.*;
+import java.util.logging.*;
+
+/**
+ * An example of using trickle ICE.
+ *
+ * @author Emil Ivov
+ */
+public class TrickleIce
+ extends Ice
+{
+ /**
+ * The Logger used by the TrickleIce
+ * class and its instances for logging output.
+ */
+ private static final Logger logger
+ = Logger.getLogger(TrickleIce.class.getName());
+
+ /**
+ * Runs a test application that allocates streams, generates an SDP, dumps
+ * it on stdout, waits for a remote peer SDP on stdin, then feeds that
+ * to our local agent and starts ICE processing.
+ *
+ * @param args none currently handled
+ * @throws Throwable every now and then.
+ */
+ public static void main(String[] args) throws Throwable
+ {
+ Agent localAgent = createAgent(2020, true);
+ localAgent.setNominationStrategy(
+ NominationStrategy.NOMINATE_HIGHEST_PRIO);
+
+ localAgent.addStateChangeListener(new IceProcessingListener());
+
+ //let them fight ... fights forge character.
+ localAgent.setControlling(false);
+ String localSDP = SdpUtils.createSDPDescription(localAgent);
+
+ //wait a bit so that the logger can stop dumping stuff:
+ Thread.sleep(500);
+
+ System.err.println("=================== feed the following"
+ +" to the remote agent ===================");
+
+
+ System.err.println(localSDP);
+
+ System.err.println("======================================"
+ + "========================================\n");
+
+ CandidatePrinter printer = new CandidatePrinter();
+ printer.agent = localAgent;
+ localAgent.startCandidateTrickle(printer);
+
+
+ List allComponents = new LinkedList();
+ int allCandidates = 0;
+ for (IceMediaStream stream : localAgent.getStreams())
+ {
+ for(Component component : stream.getComponents())
+ {
+ allComponents.add(component);
+ allCandidates += component.getLocalCandidateCount();
+ }
+ }
+ System.out.println("all candidates = " + allCandidates);
+
+ String sdp = IceDistributed.readSDP();
+
+ startTime = System.currentTimeMillis();
+ SdpUtils.parseSDP(localAgent, sdp);
+
+ localAgent.startConnectivityEstablishment();
+
+
+ //Give processing enough time to finish. We'll System.exit() anyway
+ //as soon as localAgent enters a final state.
+ Thread.sleep(60000);
+ }
+
+ static class CandidatePrinter implements TrickleCallback
+ {
+ /**
+ * Number of candidates that we have seen.
+ */
+ static int candidateCounter = 0;
+
+ /**
+ * The ICE agent that's running the show.
+ */
+ Agent agent;
+
+ /**
+ * Converts every trickled candidate to SDP and prints it on stdout.
+ *
+ * @param iceCandidates the newly discovered list of candidates or,
+ * similarly to WebRTC, null in case all candidate harvesting
+ * is now completed.
+ */
+ public void onIceCandidates(Collection iceCandidates)
+ {
+ if( iceCandidates != null)
+ candidateCounter++ ;
+
+ Collection update
+ = IceSdpUtils.createTrickleUpdate(iceCandidates);
+
+ for(Attribute attribute : update)
+ {
+ System.err.print(attribute);
+ }
+
+ if(iceCandidates == null)
+ {
+ try{Thread.sleep(1000);}catch(Exception e){};
+
+ System.err.println("ICE stats: time="
+ + agent.getTotalHarvestingTime() + "ms");
+
+ //print statistics
+ for (CandidateHarvester harvester : agent.getHarvesters())
+ {
+ System.err.println(harvester.getHarvestStatistics());
+ }
+
+
+
+ }
+ }
+ }
+
+}