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 rocks.xmpp.addr.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 boolean ours;
 17	private boolean usedByCounterpart = false;
 18	private 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        switch (type) {
 64            case "proxy":
 65                this.type = TYPE_PROXY;
 66                break;
 67            case "direct":
 68                this.type = TYPE_DIRECT;
 69                break;
 70            default:
 71                this.type = TYPE_UNKNOWN;
 72                break;
 73        }
 74	}
 75
 76	public void setPriority(int i) {
 77		this.priority = i;
 78	}
 79
 80	public int getPriority() {
 81		return this.priority;
 82	}
 83
 84	public boolean equals(JingleCandidate other) {
 85		return this.getCid().equals(other.getCid());
 86	}
 87
 88	public boolean equalValues(JingleCandidate other) {
 89		return other != null && other.getHost().equals(this.getHost()) && (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(InvalidJid.getNullForInvalid(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}