Debian Codegraph @

xmpp-vala/src/module/xep/0176_jingle_ice_udp/candidate.vala

raw
1 using Gee;
2 using Xmpp.Xep;
3 using Xmpp;
4
5 public class Xmpp.Xep.JingleIceUdp.Candidate {
6 public uint8 component;
7 public string foundation;
8 public uint8 generation;
9 public string id;
10 public string ip;
11 public uint8 network;
12 public uint16 port;
13 public uint32 priority;
14 public string protocol;
15 public string? rel_addr;
16 public uint16 rel_port;
17 public Type type_;
18
19 public static Candidate parse(StanzaNode node) throws Jingle.IqError {
20 Candidate candidate = new Candidate();
21 candidate.component = (uint8) node.get_attribute_uint("component");
22 candidate.foundation = (string) node.get_attribute("foundation");
23 candidate.generation = (uint8) node.get_attribute_uint("generation");
24 candidate.id = node.get_attribute("id");
25 candidate.ip = node.get_attribute("ip");
26 candidate.network = (uint8) node.get_attribute_uint("network");
27 candidate.port = (uint16) node.get_attribute_uint("port");
28 candidate.priority = (uint32) node.get_attribute_uint("priority");
29 candidate.protocol = node.get_attribute("protocol");
30 candidate.rel_addr = node.get_attribute("rel-addr");
31 candidate.rel_port = (uint16) node.get_attribute_uint("rel-port");
32 candidate.type_ = Type.parse(node.get_attribute("type"));
33 return candidate;
34 }
35
36 public enum Type {
37 HOST, PRFLX, RELAY, SRFLX;
38 public static Type parse(string str) throws Jingle.IqError {
39 switch (str) {
40 case "host": return HOST;
41 case "prflx": return PRFLX;
42 case "relay": return RELAY;
43 case "srflx": return SRFLX;
44 default: throw new Jingle.IqError.BAD_REQUEST("Illegal ICE-UDP candidate type");
45 }
46 }
47 public string to_string() {
48 switch (this) {
49 case HOST: return "host";
50 case PRFLX: return "prflx";
51 case RELAY: return "relay";
52 case SRFLX: return "srflx";
53 default: assert_not_reached();
54 }
55 }
56 }
57
58 public StanzaNode to_xml() {
59 StanzaNode node = new StanzaNode.build("candidate", NS_URI)
60 .put_attribute("component", component.to_string())
61 .put_attribute("foundation", foundation.to_string())
62 .put_attribute("generation", generation.to_string())
63 .put_attribute("id", id)
64 .put_attribute("ip", ip)
65 .put_attribute("network", network.to_string())
66 .put_attribute("port", port.to_string())
67 .put_attribute("priority", priority.to_string())
68 .put_attribute("protocol", protocol)
69 .put_attribute("type", type_.to_string());
70 if (rel_addr != null) node.put_attribute("rel-addr", rel_addr);
71 if (rel_port != 0) node.put_attribute("rel-port", rel_port.to_string());
72 return node;
73 }
74
75 public bool equals(Candidate c) {
76 return equals_func(this, c);
77 }
78
79 public static bool equals_func(Candidate c1, Candidate c2) {
80 return c1.component == c2.component &&
81 c1.foundation == c2.foundation &&
82 c1.generation == c2.generation &&
83 c1.id == c2.id &&
84 c1.ip == c2.ip &&
85 c1.network == c2.network &&
86 c1.port == c2.port &&
87 c1.priority == c2.priority &&
88 c1.protocol == c2.protocol &&
89 c1.rel_addr == c2.rel_addr &&
90 c1.rel_port == c2.rel_port &&
91 c1.type_ == c2.type_;
92 }
93 }