JingleCandidate.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5
  6import eu.siacs.conversations.xml.Element;
  7import eu.siacs.conversations.xmpp.jid.Jid;
  8
  9public class JingleCandidate {
 10
 11	public static int TYPE_UNKNOWN;
 12	public static int TYPE_DIRECT = 0;
 13	public static int TYPE_PROXY = 1;
 14
 15	private boolean ours;
 16	private boolean usedByCounterpart = false;
 17	private String cid;
 18	private String host;
 19	private int port;
 20	private int type;
 21	private Jid jid;
 22	private int priority;
 23
 24	public JingleCandidate(String cid, boolean ours) {
 25		this.ours = ours;
 26		this.cid = cid;
 27	}
 28
 29	public String getCid() {
 30		return cid;
 31	}
 32
 33	public void setHost(String host) {
 34		this.host = host;
 35	}
 36
 37	public String getHost() {
 38		return this.host;
 39	}
 40
 41	public void setJid(final Jid jid) {
 42		this.jid = jid;
 43	}
 44
 45	public Jid getJid() {
 46		return this.jid;
 47	}
 48
 49	public void setPort(int port) {
 50		this.port = port;
 51	}
 52
 53	public int getPort() {
 54		return this.port;
 55	}
 56
 57	public void setType(int type) {
 58		this.type = type;
 59	}
 60
 61	public void setType(String type) {
 62        switch (type) {
 63            case "proxy":
 64                this.type = TYPE_PROXY;
 65                break;
 66            case "direct":
 67                this.type = TYPE_DIRECT;
 68                break;
 69            default:
 70                this.type = TYPE_UNKNOWN;
 71                break;
 72        }
 73	}
 74
 75	public void setPriority(int i) {
 76		this.priority = i;
 77	}
 78
 79	public int getPriority() {
 80		return this.priority;
 81	}
 82
 83	public boolean equals(JingleCandidate other) {
 84		return this.getCid().equals(other.getCid());
 85	}
 86
 87	public boolean equalValues(JingleCandidate other) {
 88		return other.getHost().equals(this.getHost())
 89				&& (other.getPort() == this.getPort());
 90	}
 91
 92	public boolean isOurs() {
 93		return ours;
 94	}
 95
 96	public int getType() {
 97		return this.type;
 98	}
 99
100	public static List<JingleCandidate> parse(List<Element> canditates) {
101		List<JingleCandidate> parsedCandidates = new ArrayList<>();
102		for (Element c : canditates) {
103			parsedCandidates.add(JingleCandidate.parse(c));
104		}
105		return parsedCandidates;
106	}
107
108	public static JingleCandidate parse(Element candidate) {
109		JingleCandidate parsedCandidate = new JingleCandidate(
110				candidate.getAttribute("cid"), false);
111		parsedCandidate.setHost(candidate.getAttribute("host"));
112		parsedCandidate.setJid(candidate.getAttributeAsJid("jid"));
113		parsedCandidate.setType(candidate.getAttribute("type"));
114		parsedCandidate.setPriority(Integer.parseInt(candidate
115				.getAttribute("priority")));
116		parsedCandidate
117				.setPort(Integer.parseInt(candidate.getAttribute("port")));
118		return parsedCandidate;
119	}
120
121	public Element toElement() {
122		Element element = new Element("candidate");
123		element.setAttribute("cid", this.getCid());
124		element.setAttribute("host", this.getHost());
125		element.setAttribute("port", Integer.toString(this.getPort()));
126		element.setAttribute("jid", this.getJid().toString());
127		element.setAttribute("priority", Integer.toString(this.getPriority()));
128		if (this.getType() == TYPE_DIRECT) {
129			element.setAttribute("type", "direct");
130		} else if (this.getType() == TYPE_PROXY) {
131			element.setAttribute("type", "proxy");
132		}
133		return element;
134	}
135
136	public void flagAsUsedByCounterpart() {
137		this.usedByCounterpart = true;
138	}
139
140	public boolean isUsedByCounterpart() {
141		return this.usedByCounterpart;
142	}
143
144	public String toString() {
145		return this.getHost() + ":" + this.getPort() + " (prio="
146				+ this.getPriority() + ")";
147	}
148}