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