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 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(List<Element> canditates) {
105 List<JingleCandidate> parsedCandidates = new ArrayList<>();
106 for (Element c : canditates) {
107 parsedCandidates.add(JingleCandidate.parse(c));
108 }
109 return parsedCandidates;
110 }
111
112 public static JingleCandidate parse(Element candidate) {
113 JingleCandidate parsedCandidate = new JingleCandidate(candidate.getAttribute("cid"), false);
114 parsedCandidate.setHost(candidate.getAttribute("host"));
115 parsedCandidate.setJid(InvalidJid.getNullForInvalid(candidate.getAttributeAsJid("jid")));
116 parsedCandidate.setType(candidate.getAttribute("type"));
117 parsedCandidate.setPriority(Integer.parseInt(candidate.getAttribute("priority")));
118 parsedCandidate.setPort(Integer.parseInt(candidate.getAttribute("port")));
119 return parsedCandidate;
120 }
121
122 public Element toElement() {
123 Element element = new Element("candidate");
124 element.setAttribute("cid", this.getCid());
125 element.setAttribute("host", this.getHost());
126 element.setAttribute("port", Integer.toString(this.getPort()));
127 if (jid != null) {
128 element.setAttribute("jid", jid.toEscapedString());
129 }
130 element.setAttribute("priority", Integer.toString(this.getPriority()));
131 if (this.getType() == TYPE_DIRECT) {
132 element.setAttribute("type", "direct");
133 } else if (this.getType() == TYPE_PROXY) {
134 element.setAttribute("type", "proxy");
135 }
136 return element;
137 }
138
139 public void flagAsUsedByCounterpart() {
140 this.usedByCounterpart = true;
141 }
142
143 public boolean isUsedByCounterpart() {
144 return this.usedByCounterpart;
145 }
146
147 public String toString() {
148 return this.getHost() + ":" + this.getPort() + " (prio="
149 + this.getPriority() + ")";
150 }
151}