From c00e6de61f339069a7c40250c4df8a0a669a31d3 Mon Sep 17 00:00:00 2001 From: emcho Date: Fri, 5 Mar 2010 12:49:19 +0000 Subject: [PATCH] Adds basic support for freeing candidates, components and streams. --- src/org/ice4j/ice/Agent.java | 16 +++++++++++ src/org/ice4j/ice/Candidate.java | 2 +- src/org/ice4j/ice/Component.java | 39 ++++++++++++++++++++------ src/org/ice4j/ice/IceMediaStream.java | 39 ++++++++++++++++++++++++-- src/org/ice4j/ice/LocalCandidate.java | 9 ++++++ src/org/ice4j/ice/RemoteCandidate.java | 27 ++++++++++++++++-- 6 files changed, 116 insertions(+), 16 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 5f1176f..f5935ab 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -385,6 +385,22 @@ public class Agent this.isControlling = isControlling; } + /** + * Removes stream and all its child Components and + * Candidates from the this agent and releases all resources that + * they had allocated (like sockets for example) + * + * @param stream the Component we'd like to remove and free. + */ + public void removeStream(IceMediaStream stream) + { + synchronized (mediaStreams) + { + mediaStreams.remove(stream.getName()); + stream.free(); + } + } + /** * Determines whether this agent has the controlling role in an ICE * exchange. diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index 95fc757..b4bc425 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -65,7 +65,7 @@ public abstract class Candidate * determine the order of the connectivity checks and the relative * preference for candidates. */ - private long priority = 0; + protected long priority = 0; /** * Specifies whether the address associated with this candidate belongs to diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 56c65e0..e98f813 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -67,7 +67,8 @@ public class Component /** * The list locally gathered candidates for this media stream. */ - private List localCandidates = new LinkedList(); + private List localCandidates + = new LinkedList(); /** * The list of candidates that the peer agent sent for this stream. @@ -114,7 +115,7 @@ public class Component * * @param candidate the candidate object to be added */ - public void addLocalCandidate(Candidate candidate) + public void addLocalCandidate(LocalCandidate candidate) { synchronized(localCandidates) { @@ -211,7 +212,7 @@ public class Component * * @param candidates a List of candidates to be added */ - public void addLocalCandidates(List candidates) + public void addLocalCandidates(List candidates) { synchronized(localCandidates) { @@ -301,6 +302,7 @@ public class Component if(localCandidatesCount > 0) { + buff.append("\nDefault Candidate: " + getDefaultCandidate()); buff.append("\n" + localCandidatesCount + " local candidates:"); synchronized(localCandidates) @@ -310,8 +312,6 @@ public class Component buff.append("\n" + cand.toString()); } } - - buff.append("\nDefault Candidate: " + getDefaultCandidate()); } else { @@ -349,7 +349,8 @@ public class Component { synchronized(localCandidates) { - Candidate[] candidates = new Candidate[localCandidates.size()]; + LocalCandidate[] candidates + = new LocalCandidate[localCandidates.size()]; localCandidates.toArray(candidates); //first compute the actual priorities @@ -363,7 +364,7 @@ public class Component //now re-add the candidates in the order they've been sorted in. localCandidates.clear(); - for (Candidate cand : candidates) + for (LocalCandidate cand : candidates) localCandidates.add(cand); } } @@ -446,7 +447,8 @@ public class Component //base as cand and a lower priority. synchronized(localCandidates) { - Iterator localCandsIter = localCandidates.iterator(); + Iterator localCandsIter + = localCandidates.iterator(); while (localCandsIter.hasNext()) { @@ -499,7 +501,8 @@ public class Component { synchronized(localCandidates) { - Iterator localCandsIter = localCandidates.iterator(); + Iterator localCandsIter + = localCandidates.iterator(); while (localCandsIter.hasNext()) { @@ -519,4 +522,22 @@ public class Component } } } + + /** + * Releases all resources allocated by this Component and its + * Candidates like sockets for example. + */ + protected void free() + { + synchronized (localCandidates) + { + Iterator cndIter = localCandidates.iterator(); + while( cndIter.hasNext()) + { + LocalCandidate candidate = cndIter.next(); + candidate.free(); + cndIter.remove(); + } + } + } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index 9a7be79..2e78470 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -116,9 +116,7 @@ public class IceMediaStream List components = getComponents(); for (Component cmp : components) { - buff.append("Component ").append(cmp.getComponentID()) - .append(" :\n"); - buff.append(cmp); + buff.append("\n").append(cmp); } return buff.toString(); @@ -210,4 +208,39 @@ public class IceMediaStream { return parentAgent; } + + /** + * Removes this stream and all Candidates associated with its child + * Components. + */ + protected void free() + { + synchronized (components) + { + Iterator> cmpEntries + = components.entrySet().iterator(); + while (cmpEntries.hasNext()) + { + Component component = cmpEntries.next().getValue(); + component.free(); + cmpEntries.remove(); + } + } + } + + /** + * Removes component and all its Candidates from the + * this stream and releases all associated resources that they had + * allocated (like sockets for example) + * + * @param component the Component we'd like to remove and free. + */ + public void removeComponent(Component component) + { + synchronized (components) + { + components.remove(component.getComponentID()); + component.free(); + } + } } diff --git a/src/org/ice4j/ice/LocalCandidate.java b/src/org/ice4j/ice/LocalCandidate.java index bfce6e0..9158794 100644 --- a/src/org/ice4j/ice/LocalCandidate.java +++ b/src/org/ice4j/ice/LocalCandidate.java @@ -45,4 +45,13 @@ public abstract class LocalCandidate * Candidate */ public abstract DatagramSocket getSocket(); + + /** + * Frees resources allocated by this candidate such as its + * DatagramSocket for example. + */ + protected void free() + { + getSocket().close(); + } } diff --git a/src/org/ice4j/ice/RemoteCandidate.java b/src/org/ice4j/ice/RemoteCandidate.java index 74ee144..33a1ea0 100644 --- a/src/org/ice4j/ice/RemoteCandidate.java +++ b/src/org/ice4j/ice/RemoteCandidate.java @@ -7,8 +7,6 @@ */ package org.ice4j.ice; -import java.net.*; - import org.ice4j.*; /** @@ -31,11 +29,34 @@ public class RemoteCandidate * @param parentComponent the Component that this candidate * belongs to. * @param type the CandidateType for this Candidate. + * @param foundation the RemoteCandidate's foundation as reported + * by the session description protocol. + * @param priority the RemoteCandidate's priority as reported + * by the session description protocol. */ public RemoteCandidate(TransportAddress transportAddress, Component parentComponent, - CandidateType type) + CandidateType type, + String foundation, + long priority) { super(transportAddress, parentComponent, type); + super.setFoundation(foundation); + setPriority(priority); + + } + + /** + * Sets the priority of this RemoteCandidate. Priority is a unique + * priority number that MUST be a positive integer between 1 and + * (2**32 - 1). This priority will be set and used by ICE algorithms to + * determine the order of the connectivity checks and the relative + * preference for candidates. + * + * @param priority the priority number between 1 and (2**32 - 1). + */ + public void setPriority(long priority) + { + super.priority = priority; } }