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