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())&&(other.getPort()==this.getPort());
84 }
85
86 public boolean isOurs() {
87 return ours;
88 }
89
90 public int getType() {
91 return this.type;
92 }
93
94 public static List<JingleCandidate> parse(List<Element> canditates) {
95 List<JingleCandidate> parsedCandidates = new ArrayList<JingleCandidate>();
96 for(Element c : canditates) {
97 parsedCandidates.add(JingleCandidate.parse(c));
98 }
99 return parsedCandidates;
100 }
101
102 public static JingleCandidate parse(Element candidate) {
103 JingleCandidate parsedCandidate = new JingleCandidate(candidate.getAttribute("cid"), false);
104 parsedCandidate.setHost(candidate.getAttribute("host"));
105 parsedCandidate.setJid(candidate.getAttribute("jid"));
106 parsedCandidate.setType(candidate.getAttribute("type"));
107 parsedCandidate.setPriority(Integer.parseInt(candidate.getAttribute("priority")));
108 parsedCandidate.setPort(Integer.parseInt(candidate.getAttribute("port")));
109 return parsedCandidate;
110 }
111
112 public Element toElement() {
113 Element element = new Element("candidate");
114 element.setAttribute("cid", this.getCid());
115 element.setAttribute("host", this.getHost());
116 element.setAttribute("port", ""+this.getPort());
117 element.setAttribute("jid", this.getJid());
118 element.setAttribute("priority",""+this.getPriority());
119 if (this.getType()==TYPE_DIRECT) {
120 element.setAttribute("type","direct");
121 } else if (this.getType()==TYPE_PROXY) {
122 element.setAttribute("type","proxy");
123 }
124 return element;
125 }
126
127 public void flagAsUsedByCounterpart() {
128 this.usedByCounterpart = true;
129 }
130
131 public boolean isUsedByCounterpart() {
132 return this.usedByCounterpart;
133 }
134
135 public String toString() {
136 return this.getHost()+":"+this.getPort()+" (prio="+this.getPriority()+")";
137 }
138}