anotherim/src/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java

144 lines
3.2 KiB
Java
Raw Normal View History

2014-04-17 12:52:10 +00:00
package eu.siacs.conversations.xmpp.jingle;
import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.xml.Element;
public class JingleCandidate {
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public static int TYPE_UNKNOWN;
public static int TYPE_DIRECT = 0;
public static int TYPE_PROXY = 1;
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
private boolean ours;
private boolean usedByCounterpart = false;
private String cid;
private String host;
private int port;
private int type;
private String jid;
private int priority;
2014-08-31 14:28:21 +00:00
public JingleCandidate(String cid, boolean ours) {
2014-04-17 12:52:10 +00:00
this.ours = ours;
this.cid = cid;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public String getCid() {
return cid;
}
public void setHost(String host) {
this.host = host;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public String getHost() {
return this.host;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public void setJid(String jid) {
this.jid = jid;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public String getJid() {
return this.jid;
}
public void setPort(int port) {
this.port = port;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public int getPort() {
return this.port;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public void setType(int type) {
this.type = type;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public void setType(String type) {
if ("proxy".equals(type)) {
this.type = TYPE_PROXY;
} else if ("direct".equals(type)) {
this.type = TYPE_DIRECT;
} else {
this.type = TYPE_UNKNOWN;
}
}
public void setPriority(int i) {
this.priority = i;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public int getPriority() {
return this.priority;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public boolean equals(JingleCandidate other) {
return this.getCid().equals(other.getCid());
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public boolean equalValues(JingleCandidate other) {
2014-08-31 14:28:21 +00:00
return other.getHost().equals(this.getHost())
&& (other.getPort() == this.getPort());
2014-04-17 12:52:10 +00:00
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public boolean isOurs() {
return ours;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public int getType() {
return this.type;
}
public static List<JingleCandidate> parse(List<Element> canditates) {
List<JingleCandidate> parsedCandidates = new ArrayList<JingleCandidate>();
2014-08-31 14:28:21 +00:00
for (Element c : canditates) {
2014-04-17 12:52:10 +00:00
parsedCandidates.add(JingleCandidate.parse(c));
}
return parsedCandidates;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public static JingleCandidate parse(Element candidate) {
2014-08-31 14:28:21 +00:00
JingleCandidate parsedCandidate = new JingleCandidate(
candidate.getAttribute("cid"), false);
2014-04-17 12:52:10 +00:00
parsedCandidate.setHost(candidate.getAttribute("host"));
parsedCandidate.setJid(candidate.getAttribute("jid"));
parsedCandidate.setType(candidate.getAttribute("type"));
2014-08-31 14:28:21 +00:00
parsedCandidate.setPriority(Integer.parseInt(candidate
.getAttribute("priority")));
parsedCandidate
.setPort(Integer.parseInt(candidate.getAttribute("port")));
2014-04-17 12:52:10 +00:00
return parsedCandidate;
}
public Element toElement() {
Element element = new Element("candidate");
element.setAttribute("cid", this.getCid());
element.setAttribute("host", this.getHost());
2014-09-01 08:40:45 +00:00
element.setAttribute("port", Integer.toString(this.getPort()));
2014-04-17 12:52:10 +00:00
element.setAttribute("jid", this.getJid());
2014-09-01 08:40:45 +00:00
element.setAttribute("priority", Integer.toString(this.getPriority()));
2014-08-31 14:28:21 +00:00
if (this.getType() == TYPE_DIRECT) {
element.setAttribute("type", "direct");
} else if (this.getType() == TYPE_PROXY) {
element.setAttribute("type", "proxy");
2014-04-17 12:52:10 +00:00
}
return element;
}
public void flagAsUsedByCounterpart() {
2014-08-31 14:28:21 +00:00
this.usedByCounterpart = true;
2014-04-17 12:52:10 +00:00
}
public boolean isUsedByCounterpart() {
return this.usedByCounterpart;
}
2014-08-31 14:28:21 +00:00
2014-04-17 12:52:10 +00:00
public String toString() {
2014-08-31 14:28:21 +00:00
return this.getHost() + ":" + this.getPort() + " (prio="
+ this.getPriority() + ")";
2014-04-17 12:52:10 +00:00
}
}