1 /*
2  * hunt-amqp-client: AMQP Client Library for D Programming Language. Support for RabbitMQ and other AMQP Server.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.amqp.client.WriteStream;
13 
14 import hunt.amqp.client.StreamBase;
15 import hunt.amqp.Handler;
16 import hunt.Object;
17 import hunt.Exceptions;
18 
19 import hunt.concurrency.Future;
20 import hunt.concurrency.FuturePromise;
21 
22 interface WriteStream(T) : StreamBase {
23 
24     WriteStream!T exceptionHandler(Handler!Throwable var1);
25 
26     /**
27      * Write some data to the stream. The data is put on an internal write queue, and the write actually happens
28      * asynchronously. To avoid running out of memory by putting too much on the write queue,
29      * check the {@link #writeQueueFull} method before writing. This is done automatically if using a {@link Pump}.
30      *
31      * @param data  the data to write
32      * @return a future completed with the result
33      */
34     Future!Void write(T data);
35 
36     /**
37      * Same as {@link #write(T)} but with an {@code handler} called when the operation completes
38      */
39     void write(T data, VoidAsyncHandler handler);
40 
41     /**
42      * Ends the stream.
43      * <p>
44      * Once the stream has ended, it cannot be used any more.
45      *
46      * @return a future completed with the result
47      */
48     final Future!Void end() {
49         FuturePromise!Void f = new FuturePromise!Void();
50         this.end((VoidAsyncResult ar) {
51             if (ar.succeeded()) {
52                 f.succeeded(ar.result());
53             } else {
54                 f.failed(cast(Exception) ar.cause());
55             }
56         });
57 
58         return f;
59     }
60 
61     /**
62      * Same as {@link #end()} but with an {@code handler} called when the operation completes
63      */
64     void end(VoidAsyncHandler var1);
65 
66     final Future!Void end(T data) {
67         FuturePromise!Void f = new FuturePromise!Void();
68         this.end(data, (VoidAsyncResult ar) {
69             if (ar.succeeded()) {
70                 f.succeeded(ar.result());
71             } else {
72                 f.failed(cast(Exception) ar.cause());
73             }
74         });
75 
76         return f;
77     }
78 
79     final void end(T data, VoidAsyncHandler handler) {
80         if (handler !is null) {
81             this.write(data, (ar) {
82                 if (ar.succeeded()) {
83                     this.end(handler);
84                 } else {
85                     handler(ar);
86                 }
87             });
88         } else {
89             end(data);
90         }
91     }
92 
93     WriteStream!T setWriteQueueMaxSize(int var1);
94 
95     bool writeQueueFull();
96 
97     WriteStream!T drainHandler(Handler!Void var1);
98 
99 }