aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0166_jingle/component.vala
blob: e30175d592398d5b8f017de60c1ae8f912dfd81d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Xmpp.Xep.Jingle {

    public abstract class ComponentConnection : Object {
        public uint8 component_id { get; set; default = 0; }
        public ulong bytes_sent { get; protected set; default=0; }
        public ulong bytes_received { get; protected set; default=0; }
        public abstract async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null);
        public signal void connection_closed();
        public signal void connection_error(IOError e);
    }

    public abstract class DatagramConnection : ComponentConnection {
        public bool ready { get; set; default = false; }
        private string? terminate_reason_name = null;
        private string? terminate_reason_text = null;
        private bool terminated = false;

        public override async void terminate(bool we_terminated, string? reason_string = null, string? reason_text = null) {
            if (!terminated) {
                terminated = true;
                terminate_reason_name = reason_string;
                terminate_reason_text = reason_text;
                connection_closed();
            }
        }

        public signal void datagram_received(Bytes datagram);
        public abstract void send_datagram(Bytes datagram);
    }

    public class StreamingConnection : ComponentConnection {
        public Gee.Future<IOStream> stream { get { return promise.future; } }
        protected Gee.Promise<IOStream> promise = new Gee.Promise<IOStream>();
        private string? terminated = null;

        public async void set_stream(IOStream? stream) {
            if (stream == null) {
                promise.set_exception(new IOError.FAILED("Jingle connection failed"));
                return;
            }
            assert(!this.stream.ready);
            promise.set_value(stream);
            if (terminated != null) {
                yield stream.close_async();
            }
        }

        public void set_error(GLib.Error? e) {
            promise.set_exception(e);
        }

        public override async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null) {
            if (terminated == null) {
                terminated = (reason_name ?? "") + " - " + (reason_text ?? "") + @"we terminated? $we_terminated";
                if (stream.ready) {
                    yield stream.value.close_async();
                } else {
                    promise.set_exception(new IOError.FAILED("Jingle connection failed"));
                }
            }
        }
    }
}