2017-11-11 20:29:13 +00:00
|
|
|
using Gee;
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
namespace Xmpp {
|
2017-03-10 15:02:32 +00:00
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
public string random_uuid() {
|
|
|
|
uint32 b1 = Random.next_int();
|
|
|
|
uint16 b2 = (uint16)Random.next_int();
|
|
|
|
uint16 b3 = (uint16)(Random.next_int() | 0x4000u) & ~0xb000u;
|
|
|
|
uint16 b4 = (uint16)(Random.next_int() | 0x8000u) & ~0x4000u;
|
|
|
|
uint16 b5_1 = (uint16)Random.next_int();
|
|
|
|
uint32 b5_2 = Random.next_int();
|
|
|
|
return "%08x-%04x-%04x-%04x-%04x%08x".printf(b1, b2, b3, b4, b5_1, b5_2);
|
|
|
|
}
|
2017-11-11 20:29:13 +00:00
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public abstract class StanzaListener<T> : OrderedListener {
|
|
|
|
|
|
|
|
public abstract async bool run(XmppStream stream, T stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class StanzaListenerHolder<T> : ListenerHolder {
|
|
|
|
|
|
|
|
public async void run(XmppStream stream, T stanza) {
|
2018-04-17 18:11:44 +00:00
|
|
|
|
|
|
|
// listeners can change e.g. when switching to another stream
|
|
|
|
ArrayList<OrderedListener> listeners_copy = new ArrayList<OrderedListener>();
|
|
|
|
listeners_copy.add_all(listeners);
|
|
|
|
|
|
|
|
foreach (OrderedListener ol in listeners_copy) {
|
2018-01-19 21:37:02 +00:00
|
|
|
StanzaListener<T> l = ol as StanzaListener<T>;
|
2018-04-17 18:11:44 +00:00
|
|
|
bool stop = yield l.run(stream, stanza);
|
2018-01-19 21:37:02 +00:00
|
|
|
if (stop) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class OrderedListener : Object {
|
2017-11-11 20:29:13 +00:00
|
|
|
public abstract string action_group { get; }
|
|
|
|
public abstract string[] after_actions { get; }
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public abstract class ListenerHolder : Object {
|
|
|
|
protected ArrayList<OrderedListener> listeners = new ArrayList<OrderedListener>();
|
2017-11-11 20:29:13 +00:00
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public new void connect(OrderedListener listener) {
|
2017-11-11 20:29:13 +00:00
|
|
|
listeners.add(listener);
|
|
|
|
resort_list();
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public new void disconnect(OrderedListener listener) {
|
2017-11-21 22:27:27 +00:00
|
|
|
listeners.remove(listener);
|
|
|
|
resort_list();
|
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
private bool set_contains_action(Gee.List<OrderedListener> s, string[] actions) {
|
|
|
|
foreach(OrderedListener l in s) {
|
2017-11-11 20:29:13 +00:00
|
|
|
if (l.action_group in actions) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resort_list() {
|
2018-01-19 21:37:02 +00:00
|
|
|
ArrayList<OrderedListener> new_list = new ArrayList<OrderedListener>();
|
|
|
|
ArrayList<OrderedListener> remaining = new ArrayList<OrderedListener>();
|
2017-11-22 19:06:50 +00:00
|
|
|
remaining.add_all(listeners);
|
|
|
|
while (remaining.size > 0) {
|
2017-11-11 20:29:13 +00:00
|
|
|
bool changed = false;
|
2018-01-19 21:37:02 +00:00
|
|
|
Gee.Iterator<OrderedListener> iter = remaining.iterator();
|
2017-11-22 19:06:50 +00:00
|
|
|
while (iter.has_next()) {
|
2018-01-30 16:29:54 +00:00
|
|
|
iter.next();
|
2018-01-19 21:37:02 +00:00
|
|
|
OrderedListener l = iter.get();
|
2017-11-11 20:29:13 +00:00
|
|
|
if (!set_contains_action(remaining, l.after_actions)) {
|
|
|
|
new_list.add(l);
|
2017-11-22 19:06:50 +00:00
|
|
|
iter.remove();
|
2017-11-11 20:29:13 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 19:06:50 +00:00
|
|
|
if (!changed) error("Can't sort listeners");
|
2017-11-11 20:29:13 +00:00
|
|
|
}
|
|
|
|
listeners = new_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 12:29:02 +00:00
|
|
|
}
|