| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.StreamManagement { |
| 4 |
|
| 5 |
public const string NS_URI = "urn:xmpp:sm:3"; |
| 6 |
|
| 7 |
public class Module : XmppStreamNegotiationModule, WriteNodeFunc { |
| 8 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0198_stream_management"); |
| 9 |
|
| 10 |
public int h_inbound = 0; |
| 11 |
public int h_outbound = 0; |
| 12 |
|
| 13 |
public string? session_id { get; set; default=null; } |
| 14 |
public Gee.List<XmppStreamFlag> flags = null; |
| 15 |
private HashMap<int, QueueItem> in_flight_stanzas = new HashMap<int, QueueItem>(); |
| 16 |
private Gee.Queue<QueueItem> node_queue = new PriorityQueue<QueueItem>((a, b) => { |
| 17 |
return a.io_priority - b.io_priority; |
| 18 |
}); |
| 19 |
|
| 20 |
private class QueueItem { |
| 21 |
public StanzaNode node; |
| 22 |
public int io_priority; |
| 23 |
public Cancellable? cancellable; |
| 24 |
public Promise<void*> promise; |
| 25 |
|
| 26 |
public QueueItem(StanzaNode node, int io_priority, Cancellable? cancellable) { |
| 27 |
this.node = node; |
| 28 |
this.io_priority = io_priority; |
| 29 |
this.cancellable = cancellable; |
| 30 |
this.promise = new Promise<void*>(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public async void write_stanza(XmppStream stream, StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { |
| 35 |
var future = enqueue_stanza(stream, node, io_priority, cancellable); |
| 36 |
try { |
| 37 |
yield future.wait_async(); |
| 38 |
} catch (FutureError e) { |
| 39 |
if (e is FutureError.ABANDON_PROMISE) { |
| 40 |
throw new IOError.FAILED("Future abandoned: %s".printf(e.message)); |
| 41 |
} else if (e is FutureError.EXCEPTION) { |
| 42 |
if (future.exception is IOError) { |
| 43 |
throw (IOError) future.exception; |
| 44 |
} else { |
| 45 |
throw new IOError.FAILED("Unknown error %s".printf(future.exception.message)); |
| 46 |
} |
| 47 |
} else { |
| 48 |
throw new IOError.FAILED("Unknown future error: %s".printf(e.message)); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
private Future<void*> enqueue_stanza(XmppStream stream, StanzaNode node, int io_priority, Cancellable? cancellable) { |
| 54 |
var queue_item = new QueueItem(node, io_priority, cancellable); |
| 55 |
node_queue.offer(queue_item); |
| 56 |
check_queue(stream); |
| 57 |
return queue_item.promise.future; |
| 58 |
} |
| 59 |
|
| 60 |
internal async void write_node(XmppStream stream, StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) { |
| 61 |
StanzaWriter? writer = ((IoXmppStream)stream).writer; |
| 62 |
if (writer == null) return; |
| 63 |
try { |
| 64 |
stream.log.node("OUT", node, stream); |
| 65 |
if (node.name == "message" || node.name == "iq" || node.name == "presence") { |
| 66 |
var r_node = new StanzaNode.build("r", NS_URI).add_self_xmlns(); |
| 67 |
stream.log.node("OUT", r_node, stream); |
| 68 |
yield ((!)writer).write_nodes(node, r_node, io_priority, cancellable); |
| 69 |
} else { |
| 70 |
yield ((!)writer).write_node(node, io_priority, cancellable); |
| 71 |
} |
| 72 |
} catch (IOError e) { } |
| 73 |
} |
| 74 |
|
| 75 |
private void check_queue(XmppStream stream) { |
| 76 |
while (!node_queue.is_empty && in_flight_stanzas.size < 10) { |
| 77 |
QueueItem queue_item = node_queue.poll(); |
| 78 |
try { |
| 79 |
queue_item.cancellable.set_error_if_cancelled(); |
| 80 |
} catch (IOError e) { |
| 81 |
queue_item.promise.set_exception(e); |
| 82 |
continue; |
| 83 |
} |
| 84 |
StanzaNode node = queue_item.node; |
| 85 |
|
| 86 |
if (node.name == "message" || node.name == "iq" || node.name == "presence") { |
| 87 |
in_flight_stanzas[++h_outbound] = queue_item; |
| 88 |
} |
| 89 |
write_node.begin(stream, node, queue_item.io_priority, queue_item.cancellable); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public override void attach(XmppStream stream) { |
| 94 |
stream.get_module(Bind.Module.IDENTITY).bound_to_resource.connect(check_enable); |
| 95 |
stream.received_features_node.connect(check_resume); |
| 96 |
|
| 97 |
stream.received_nonza.connect(on_received_nonza); |
| 98 |
stream.received_message_stanza.connect(on_stanza_received); |
| 99 |
stream.received_presence_stanza.connect(on_stanza_received); |
| 100 |
stream.received_iq_stanza.connect(on_stanza_received); |
| 101 |
} |
| 102 |
|
| 103 |
public override void detach(XmppStream stream) { |
| 104 |
stream.get_module(Bind.Module.IDENTITY).bound_to_resource.disconnect(check_enable); |
| 105 |
stream.received_features_node.disconnect(check_resume); |
| 106 |
|
| 107 |
stream.received_nonza.disconnect(on_received_nonza); |
| 108 |
stream.received_message_stanza.disconnect(on_stanza_received); |
| 109 |
stream.received_presence_stanza.disconnect(on_stanza_received); |
| 110 |
stream.received_iq_stanza.disconnect(on_stanza_received); |
| 111 |
} |
| 112 |
|
| 113 |
public static void require(XmppStream stream) { |
| 114 |
if (stream.get_module(IDENTITY) == null) stream.add_module(new PrivateXmlStorage.Module()); |
| 115 |
} |
| 116 |
|
| 117 |
public override bool mandatory_outstanding(XmppStream stream) { return false; } |
| 118 |
|
| 119 |
public override bool negotiation_active(XmppStream stream) { |
| 120 |
return stream.has_flag(Flag.IDENTITY) && !stream.get_flag(Flag.IDENTITY).finished; |
| 121 |
} |
| 122 |
|
| 123 |
public override string get_ns() { return NS_URI; } |
| 124 |
public override string get_id() { return IDENTITY.id; } |
| 125 |
|
| 126 |
private void on_stanza_received(XmppStream stream, StanzaNode node) { |
| 127 |
h_inbound++; |
| 128 |
} |
| 129 |
|
| 130 |
private void check_resume(XmppStream stream) { |
| 131 |
if (stream_has_sm_feature(stream) && session_id != null) { |
| 132 |
StanzaNode node = new StanzaNode.build("resume", NS_URI).add_self_xmlns() |
| 133 |
.put_attribute("h", h_inbound.to_string()) |
| 134 |
.put_attribute("previd", session_id); |
| 135 |
write_node.begin(stream, node); |
| 136 |
stream.add_flag(new Flag()); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private void check_enable(XmppStream stream) { |
| 141 |
if (stream_has_sm_feature(stream) && session_id == null) { |
| 142 |
StanzaNode node = new StanzaNode.build("enable", NS_URI).add_self_xmlns().put_attribute("resume", "true"); |
| 143 |
write_node.begin(stream, node); |
| 144 |
stream.add_flag(new Flag()); |
| 145 |
h_outbound = 0; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
private void on_received_nonza(XmppStream stream, StanzaNode node) { |
| 150 |
if (node.ns_uri == NS_URI) { |
| 151 |
if (node.name == "r") { |
| 152 |
send_ack(stream); |
| 153 |
} else if (node.name == "a") { |
| 154 |
handle_ack(stream, node); |
| 155 |
} else if (node.name in new string[]{"enabled", "resumed", "failed"}) { |
| 156 |
stream.get_flag(Flag.IDENTITY).finished = true; |
| 157 |
|
| 158 |
if (node.name == "enabled") { |
| 159 |
h_inbound = 0; |
| 160 |
session_id = node.get_attribute("id", NS_URI); |
| 161 |
flags = stream.flags; |
| 162 |
((IoXmppStream)stream).write_obj = this; |
| 163 |
} else if (node.name == "resumed") { |
| 164 |
stream.get_flag(Flag.IDENTITY).resumed = true; |
| 165 |
|
| 166 |
foreach (XmppStreamFlag flag in flags) { |
| 167 |
stream.add_flag(flag); |
| 168 |
} |
| 169 |
|
| 170 |
h_outbound = int.parse(node.get_attribute("h", NS_URI)); |
| 171 |
handle_incoming_h(stream, h_outbound); |
| 172 |
foreach (var id in in_flight_stanzas.keys) { |
| 173 |
node_queue.add(in_flight_stanzas[id]); |
| 174 |
} |
| 175 |
in_flight_stanzas.clear(); |
| 176 |
check_queue(stream); |
| 177 |
((IoXmppStream)stream).write_obj = this; |
| 178 |
} else if (node.name == "failed") { |
| 179 |
session_id = null; |
| 180 |
string? h_acked = node.get_attribute("h", NS_URI); |
| 181 |
if (h_acked != null) { |
| 182 |
h_outbound = int.parse(h_acked); |
| 183 |
handle_incoming_h(stream, h_outbound); |
| 184 |
} |
| 185 |
foreach (var id in in_flight_stanzas.keys) { |
| 186 |
in_flight_stanzas[id].promise.set_exception(new IOError.FAILED("Stanza not acked and session not resumed")); |
| 187 |
} |
| 188 |
in_flight_stanzas.clear(); |
| 189 |
check_queue(stream); |
| 190 |
stream.received_features_node(stream); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
private void send_ack(XmppStream stream) { |
| 197 |
StanzaNode node = new StanzaNode.build("a", NS_URI).add_self_xmlns().put_attribute("h", h_inbound.to_string()); |
| 198 |
write_node.begin(stream, node); |
| 199 |
} |
| 200 |
|
| 201 |
private void handle_ack(XmppStream stream, StanzaNode node) { |
| 202 |
string? h_acked = node.get_attribute("h", NS_URI); |
| 203 |
int parsed_int = int.parse(h_acked); |
| 204 |
handle_incoming_h(stream, parsed_int); |
| 205 |
check_queue(stream); |
| 206 |
} |
| 207 |
|
| 208 |
private void handle_incoming_h(XmppStream stream, int h) { |
| 209 |
var remove_nrs = new ArrayList<int>(); |
| 210 |
foreach (int nr in in_flight_stanzas.keys) { |
| 211 |
if (nr <= h) { |
| 212 |
remove_nrs.add(nr); |
| 213 |
} |
| 214 |
} |
| 215 |
foreach (int nr in remove_nrs) { |
| 216 |
in_flight_stanzas[nr].promise.set_value(null); |
| 217 |
in_flight_stanzas.unset(nr); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
private bool stream_has_sm_feature(XmppStream stream) { |
| 222 |
return stream.features.get_subnode("sm", NS_URI) != null; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public class Flag : XmppStreamFlag { |
| 227 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "stream_management"); |
| 228 |
public bool finished = false; |
| 229 |
public bool resumed = false; |
| 230 |
|
| 231 |
public override string get_ns() { return NS_URI; } |
| 232 |
public override string get_id() { return IDENTITY.id; } |
| 233 |
} |
| 234 |
|
| 235 |
} |