1package eu.siacs.conversations.xmpp.jingle;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.xml.Element;
7import rocks.xmpp.addr.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 != null && other.getHost().equals(this.getHost()) && (other.getPort() == this.getPort());
89 }
90
91 public boolean isOurs() {
92 return ours;
93 }
94
95 public int getType() {
96 return this.type;
97 }
98
99 public static List<JingleCandidate> parse(List<Element> canditates) {
100 List<JingleCandidate> parsedCandidates = new ArrayList<>();
101 for (Element c : canditates) {
102 parsedCandidates.add(JingleCandidate.parse(c));
103 }
104 return parsedCandidates;
105 }
106
107 public static JingleCandidate parse(Element candidate) {
108 JingleCandidate parsedCandidate = new JingleCandidate(
109 candidate.getAttribute("cid"), false);
110 parsedCandidate.setHost(candidate.getAttribute("host"));
111 parsedCandidate.setJid(candidate.getAttributeAsJid("jid"));
112 parsedCandidate.setType(candidate.getAttribute("type"));
113 parsedCandidate.setPriority(Integer.parseInt(candidate
114 .getAttribute("priority")));
115 parsedCandidate
116 .setPort(Integer.parseInt(candidate.getAttribute("port")));
117 return parsedCandidate;
118 }
119
120 public Element toElement() {
121 Element element = new Element("candidate");
122 element.setAttribute("cid", this.getCid());
123 element.setAttribute("host", this.getHost());
124 element.setAttribute("port", Integer.toString(this.getPort()));
125 element.setAttribute("jid", this.getJid().toString());
126 element.setAttribute("priority", Integer.toString(this.getPriority()));
127 if (this.getType() == TYPE_DIRECT) {
128 element.setAttribute("type", "direct");
129 } else if (this.getType() == TYPE_PROXY) {
130 element.setAttribute("type", "proxy");
131 }
132 return element;
133 }
134
135 public void flagAsUsedByCounterpart() {
136 this.usedByCounterpart = true;
137 }
138
139 public boolean isUsedByCounterpart() {
140 return this.usedByCounterpart;
141 }
142
143 public String toString() {
144 return this.getHost() + ":" + this.getPort() + " (prio="
145 + this.getPriority() + ")";
146 }
147}