1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import org.vertx.java.core.streams.Pump
16
17 from core.handlers import BufferHandler, StreamEndHandler
18
19 __author__ = "Scott Horn"
20 __email__ = "scott@hornmicro.com"
21 __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
24 """
25 A mixin module which represents a stream of data that can be written to.
26
27 Any class that mixes in this module can be used by a to pump data from a to it.
28
29 """
30
32 """Write some data to the stream. The data is put on an internal write queue, and the write actually happens
33 asynchronously. To avoid running out of memory by putting too much on the write queue,
34 check the method before writing. This is done automatically if using a .
35 param [Buffer]. The buffer to write.
36 """
37 self.java_obj.writeBuffer(buff._to_java_buffer())
38
40 """Set the maximum size of the write queue. You will still be able to write to the stream even
41 if there is more data than this in the write queue. This is used as an indicator by classes such as
42 to provide flow control.
43
44 Keyword arguments:
45 @param size: The maximum size, in bytes.
46 """
47 self.java_obj.setWriteQueueMaxSize(size)
48
49 write_queue_max_size = property(fset=set_write_queue_max_size)
50
51 @property
53 """Is the write queue full?
54
55 return True if there are more bytes in the write queue than the max write queue size.
56 """
57 return self.java_obj.writeQueueFull()
58
60 """Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write
61 queue has been reduced to maxSize / 2. See for an example of this being used.
62
63 Keyword arguments:
64 @param handler: The drain handler
65 """
66 self.java_obj.drainHandler(BufferHandler(handler))
67
69 """Set an execption handler on the stream.
70
71 Keyword arguments:
72 @param handler: The exception handler
73 """
74 self.java_obj.exceptionHandler(ExceptionHandler(handler))
75
78
80 """A mixin module which represents a stream of data that can be read from.
81
82 Any class that mixes in this module can be used by a to pump data from a to it.
83 """
84
86 """Set a data handler. As data is read, the handler will be called with the data.
87
88 Keyword arguments:
89 @param handler: The data handler
90 """
91 self.java_obj.dataHandler(BufferHandler(handler))
92
94 """Pause the ReadStream. After calling this, the ReadStream will aim to send no more data to the """
95 self.java_obj.pause()
96
98 """Resume reading. If the ReadStream has been paused, reading will recommence on it."""
99 self.java_obj.resume()
100
102 """Set an execption handler on the stream.
103 param [Block] hndlr. The exception handler
104 """
105 self.java_obj.exceptionHandler(ExceptionHandler(handler))
106
108 """Set an end handler on the stream. Once the stream has ended, and there is no more data to be read, this handler will be called.
109
110 Keyword arguments:
111 @param handler: The exception handler"""
112 self.java_obj.endHandler(StreamEndHandler(handler))
113
116
118 """Pumps data from a ReadStream to a WriteStream and performs flow control where necessary to
119 prevent the write stream from getting overloaded.
120
121 Instances of this class read bytes from a ReadStream and write them to a WriteStream. If data
122 can be read faster than it can be written this could result in the write queue of the WriteStream growing
123 without bound, eventually causing it to exhaust all available RAM.
124 To prevent this, after each write, instances of this class check whether the write queue of the WriteStream
125 is full, and if so, the ReadStream is paused, and a WriteStreamdrain_handler is set on the WriteStream.
126 When the WriteStream has processed half of its backlog, the drain_handler will be called,
127 which results in the pump resuming the ReadStream.
128
129 This class can be used to pump from any ReadStream to any WriteStream,
130 e.g. from an HttpServerRequest to an AsyncFile, or from NetSocket to a WebSocket.
131 """
132
133 - def __init__(self, read_stream, write_stream):
139
141 """Set the write queue max size
142
143 Keyword arguments:
144 @param val: The write queue max size
145 """
146 self.j_pump.setWriteQueueMaxSize(val)
147
148 write_queue_max_size = property(fset=set_write_queue_max_size)
149
151 """Start the Pump. The Pump can be started and stopped multiple times."""
152 self.j_pump.start()
153
155 """Stop the Pump. The Pump can be started and stopped multiple times."""
156 self.j_pump.stop()
157
158 @property
160 """return the total number of bytes pumped by this pump."""
161 return self.j_pump.getBytesPumped()
162