Adds basic support for freeing candidates, components and streams.

This commit is contained in:
emcho 2010-03-05 12:49:19 +00:00
parent 48dde7ea8c
commit c00e6de61f
6 changed files with 116 additions and 16 deletions

View File

@ -385,6 +385,22 @@ public class Agent
this.isControlling = isControlling;
}
/**
* Removes <tt>stream</tt> and all its child <tt>Component</tt>s and
* <tt>Candidate</tt>s from the this agent and releases all resources that
* they had allocated (like sockets for example)
*
* @param stream the <tt>Component</tt> 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.

View File

@ -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

View File

@ -67,7 +67,8 @@ public class Component
/**
* The list locally gathered candidates for this media stream.
*/
private List<Candidate> localCandidates = new LinkedList<Candidate>();
private List<LocalCandidate> localCandidates
= new LinkedList<LocalCandidate>();
/**
* 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 <tt>List</tt> of candidates to be added
*/
public void addLocalCandidates(List<Candidate> candidates)
public void addLocalCandidates(List<LocalCandidate> 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<Candidate> localCandsIter = localCandidates.iterator();
Iterator<LocalCandidate> localCandsIter
= localCandidates.iterator();
while (localCandsIter.hasNext())
{
@ -499,7 +501,8 @@ public class Component
{
synchronized(localCandidates)
{
Iterator<Candidate> localCandsIter = localCandidates.iterator();
Iterator<LocalCandidate> localCandsIter
= localCandidates.iterator();
while (localCandsIter.hasNext())
{
@ -519,4 +522,22 @@ public class Component
}
}
}
/**
* Releases all resources allocated by this <tt>Component</tt> and its
* <tt>Candidate</tt>s like sockets for example.
*/
protected void free()
{
synchronized (localCandidates)
{
Iterator<LocalCandidate> cndIter = localCandidates.iterator();
while( cndIter.hasNext())
{
LocalCandidate candidate = cndIter.next();
candidate.free();
cndIter.remove();
}
}
}
}

View File

@ -116,9 +116,7 @@ public class IceMediaStream
List<Component> 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 <tt>Candidate</tt>s associated with its child
* <tt>Component</tt>s.
*/
protected void free()
{
synchronized (components)
{
Iterator<Map.Entry<Integer, Component>> cmpEntries
= components.entrySet().iterator();
while (cmpEntries.hasNext())
{
Component component = cmpEntries.next().getValue();
component.free();
cmpEntries.remove();
}
}
}
/**
* Removes <tt>component</tt> and all its <tt>Candidate</tt>s from the
* this stream and releases all associated resources that they had
* allocated (like sockets for example)
*
* @param component the <tt>Component</tt> we'd like to remove and free.
*/
public void removeComponent(Component component)
{
synchronized (components)
{
components.remove(component.getComponentID());
component.free();
}
}
}

View File

@ -45,4 +45,13 @@ public abstract class LocalCandidate
* <tt>Candidate</tt>
*/
public abstract DatagramSocket getSocket();
/**
* Frees resources allocated by this candidate such as its
* <tt>DatagramSocket</tt> for example.
*/
protected void free()
{
getSocket().close();
}
}

View File

@ -7,8 +7,6 @@
*/
package org.ice4j.ice;
import java.net.*;
import org.ice4j.*;
/**
@ -31,11 +29,34 @@ public class RemoteCandidate
* @param parentComponent the <tt>Component</tt> that this candidate
* belongs to.
* @param type the <tt>CandidateType</tt> for this <tt>Candidate</tt>.
* @param foundation the <tt>RemoteCandidate</tt>'s foundation as reported
* by the session description protocol.
* @param priority the <tt>RemoteCandidate</tt>'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 <tt>RemoteCandidate</tt>. 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;
}
}