polyphony 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ class ::IO
7
7
  class << self
8
8
  # @!visibility private
9
9
  alias_method :orig_binread, :binread
10
-
10
+
11
11
  # @!visibility private
12
12
  def binread(name, length = nil, offset = nil)
13
13
  File.open(name, 'rb:ASCII-8BIT') do |f|
@@ -15,10 +15,10 @@ class ::IO
15
15
  length ? f.read(length) : f.read
16
16
  end
17
17
  end
18
-
18
+
19
19
  # @!visibility private
20
20
  alias_method :orig_binwrite, :binwrite
21
-
21
+
22
22
  # @!visibility private
23
23
  def binwrite(name, string, offset = nil)
24
24
  File.open(name, 'wb:ASCII-8BIT') do |f|
@@ -26,13 +26,13 @@ class ::IO
26
26
  f.write(string)
27
27
  end
28
28
  end
29
-
29
+
30
30
  # @!visibility private
31
31
  EMPTY_HASH = {}.freeze
32
-
32
+
33
33
  # @!visibility private
34
34
  alias_method :orig_foreach, :foreach
35
-
35
+
36
36
  # @!visibility private
37
37
  def foreach(name, sep = $/, limit = nil, getline_args = EMPTY_HASH, &block)
38
38
  if sep.is_a?(Integer)
@@ -43,10 +43,10 @@ class ::IO
43
43
  f.each_line(sep, limit, chomp: getline_args[:chomp], &block)
44
44
  end
45
45
  end
46
-
46
+
47
47
  # @!visibility private
48
48
  alias_method :orig_read, :read
49
-
49
+
50
50
  # @!visibility private
51
51
  def read(name, length = nil, offset = nil, opt = EMPTY_HASH)
52
52
  if length.is_a?(Hash)
@@ -58,17 +58,17 @@ class ::IO
58
58
  length ? f.read(length) : f.read
59
59
  end
60
60
  end
61
-
61
+
62
62
  # alias_method :orig_readlines, :readlines
63
63
  # def readlines(name, sep = $/, limit = nil, getline_args = EMPTY_HASH)
64
64
  # File.open(name, 'r') do |f|
65
65
  # f.readlines(sep, limit, getline_args)
66
66
  # end
67
67
  # end
68
-
68
+
69
69
  # @!visibility private
70
70
  alias_method :orig_write, :write
71
-
71
+
72
72
  # @!visibility private
73
73
  def write(name, string, offset = nil, opt = EMPTY_HASH)
74
74
  File.open(name, opt[:mode] || 'w') do |f|
@@ -76,19 +76,21 @@ class ::IO
76
76
  f.write(string)
77
77
  end
78
78
  end
79
-
79
+
80
80
  # @!visibility private
81
81
  alias_method :orig_popen, :popen
82
-
83
-
82
+
83
+
84
84
  # @!visibility private
85
85
  def popen(cmd, mode = 'r')
86
86
  return orig_popen(cmd, mode) unless block_given?
87
-
87
+
88
88
  Open3.popen2(cmd) { |_i, o, _t| yield o }
89
89
  end
90
-
90
+
91
91
  # Splices from one IO to another IO. At least one of the IOs must be a pipe.
92
+ # If maxlen is negative, splices repeatedly using absolute value of maxlen
93
+ # until EOF is encountered.
92
94
  #
93
95
  # @param src [IO, Polyphony::Pipe] source to splice from
94
96
  # @param dest [IO, Polyphony::Pipe] destination to splice to
@@ -97,26 +99,36 @@ class ::IO
97
99
  def splice(src, dest, maxlen)
98
100
  Polyphony.backend_splice(src, dest, maxlen)
99
101
  end
102
+
103
+ # Creates a pipe and splices data between the two given IOs using the
104
+ # pipe, splicing until EOF.
105
+ #
106
+ # @param src [IO, Polyphony::Pipe] source to splice from
107
+ # @param dest [IO, Polyphony::Pipe] destination to splice to
108
+ # @return [Integer] total bytes spliced
109
+ def double_splice(src, dest)
110
+ Polyphony.backend_double_splice(src, dest)
111
+ end
112
+
113
+ # Tees data from the source to the desination.
114
+ #
115
+ # @param src [IO, Polyphony::Pipe] source to tee from
116
+ # @param dest [IO, Polyphony::Pipe] destination to tee to
117
+ # @param maxlen [Integer] maximum bytes to tee
118
+ # @return [Integer] total bytes teed
119
+ def tee(src, dest, maxlen)
120
+ Polyphony.backend_tee(src, dest, maxlen)
121
+ end
100
122
 
101
- if RUBY_PLATFORM =~ /linux/
102
- # Creates a pipe and splices data between the two given IOs using the
103
- # pipe, splicing until EOF.
104
- #
105
- # @param src [IO, Polyphony::Pipe] source to splice from
106
- # @param dest [IO, Polyphony::Pipe] destination to splice to
107
- # @return [Integer] total bytes spliced
123
+ if RUBY_PLATFORM !~ /linux/
124
+ # @!visibility private
108
125
  def double_splice(src, dest)
109
- Polyphony.backend_double_splice(src, dest)
126
+ raise NotImplementedError
110
127
  end
111
-
112
- # Tees data from the source to the desination.
113
- #
114
- # @param src [IO, Polyphony::Pipe] source to tee from
115
- # @param dest [IO, Polyphony::Pipe] destination to tee to
116
- # @param maxlen [Integer] maximum bytes to tee
117
- # @return [Integer] total bytes teed
128
+
129
+ # @!visibility private
118
130
  def tee(src, dest, maxlen)
119
- Polyphony.backend_tee(src, dest, maxlen)
131
+ raise NotImplementedError
120
132
  end
121
133
  end
122
134
  end
@@ -128,51 +140,51 @@ class ::IO
128
140
  def __read_method__
129
141
  :backend_read
130
142
  end
131
-
143
+
132
144
  # @!visibility private
133
145
  def __write_method__
134
146
  :backend_write
135
147
  end
136
-
148
+
137
149
  # def each(sep = $/, limit = nil, chomp: nil)
138
150
  # sep, limit = $/, sep if sep.is_a?(Integer)
139
151
  # end
140
152
  # alias_method :each_line, :each
141
-
153
+
142
154
  # def each_byte
143
155
  # end
144
-
156
+
145
157
  # def each_char
146
158
  # end
147
-
159
+
148
160
  # def each_codepoint
149
161
  # end
150
-
162
+
151
163
  # @!visibility private
152
164
  alias_method :orig_getbyte, :getbyte
153
-
154
-
165
+
166
+
155
167
  # @!visibility private
156
168
  def getbyte
157
169
  char = getc
158
170
  char ? char.getbyte(0) : nil
159
171
  end
160
-
172
+
161
173
  # @!visibility private
162
174
  alias_method :orig_getc, :getc
163
-
164
-
175
+
176
+
165
177
  # @!visibility private
166
178
  def getc
167
179
  return @read_buffer.slice!(0) if @read_buffer && !@read_buffer.empty?
168
-
180
+
169
181
  @read_buffer ||= +''
170
182
  Polyphony.backend_read(self, @read_buffer, 8192, false, -1)
171
183
  return @read_buffer.slice!(0) if !@read_buffer.empty?
172
-
184
+
173
185
  nil
174
186
  end
175
-
187
+
176
188
  # @!visibility private
177
189
  def ungetc(c)
178
190
  c = c.chr if c.is_a?(Integer)
@@ -183,59 +195,59 @@ class ::IO
183
195
  end
184
196
  end
185
197
  alias_method :ungetbyte, :ungetc
186
-
198
+
187
199
  # @!visibility private
188
200
  alias_method :orig_read, :read
189
-
190
-
201
+
202
+
191
203
  # @!visibility private
192
204
  def read(len = nil, buf = nil, buf_pos = 0)
193
205
  return '' if len == 0
194
-
206
+
195
207
  if buf
196
208
  return Polyphony.backend_read(self, buf, len, true, buf_pos)
197
209
  end
198
-
210
+
199
211
  @read_buffer ||= +''
200
212
  result = Polyphony.backend_read(self, @read_buffer, len, true, -1)
201
213
  return nil unless result
202
-
214
+
203
215
  already_read = @read_buffer
204
216
  @read_buffer = +''
205
217
  already_read
206
218
  end
207
-
219
+
208
220
  # @!visibility private
209
221
  alias_method :orig_readpartial, :read
210
-
222
+
211
223
  # @!visibility private
212
224
  def readpartial(len, str = +'', buffer_pos = 0, raise_on_eof = true)
213
225
  result = Polyphony.backend_read(self, str, len, false, buffer_pos)
214
226
  raise EOFError if !result && raise_on_eof
215
-
227
+
216
228
  result
217
229
  end
218
-
230
+
219
231
  # @!visibility private
220
232
  alias_method :orig_write, :write
221
-
233
+
222
234
  # @!visibility private
223
235
  def write(str, *args)
224
236
  Polyphony.backend_write(self, str, *args)
225
237
  end
226
-
238
+
227
239
  # @!visibility private
228
240
  alias_method :orig_write_chevron, :<<
229
-
241
+
230
242
  # @!visibility private
231
243
  def <<(str)
232
244
  Polyphony.backend_write(self, str)
233
245
  self
234
246
  end
235
-
247
+
236
248
  # @!visibility private
237
249
  alias_method :orig_gets, :gets
238
-
250
+
239
251
  # @!visibility private
240
252
  def gets(sep = $/, _limit = nil, _chomp: nil)
241
253
  if sep.is_a?(Integer)
@@ -243,20 +255,20 @@ class ::IO
243
255
  _limit = sep
244
256
  end
245
257
  sep_size = sep.bytesize
246
-
258
+
247
259
  @read_buffer ||= +''
248
-
260
+
249
261
  while true
250
262
  idx = @read_buffer.index(sep)
251
263
  return @read_buffer.slice!(0, idx + sep_size) if idx
252
-
264
+
253
265
  result = readpartial(8192, @read_buffer, -1)
254
266
  return nil unless result
255
267
  end
256
268
  rescue EOFError
257
269
  return nil
258
270
  end
259
-
271
+
260
272
  # @!visibility private
261
273
  def each_line(sep = $/, limit = nil, chomp: false)
262
274
  if sep.is_a?(Integer)
@@ -264,48 +276,48 @@ class ::IO
264
276
  sep = $/
265
277
  end
266
278
  sep_size = sep.bytesize
267
-
268
-
279
+
280
+
269
281
  @read_buffer ||= +''
270
-
282
+
271
283
  while true
272
284
  while (idx = @read_buffer.index(sep))
273
285
  line = @read_buffer.slice!(0, idx + sep_size)
274
286
  line = line.chomp if chomp
275
287
  yield line
276
288
  end
277
-
289
+
278
290
  result = readpartial(8192, @read_buffer, -1)
279
291
  return self if !result
280
292
  end
281
293
  rescue EOFError
282
294
  return self
283
295
  end
284
-
296
+
285
297
  # def print(*args)
286
298
  # end
287
-
299
+
288
300
  # def printf(format, *args)
289
301
  # end
290
-
302
+
291
303
  # def putc(obj)
292
304
  # end
293
-
305
+
294
306
  # @!visibility private
295
307
  LINEFEED = "\n"
296
308
  # @!visibility private
297
309
  LINEFEED_RE = /\n$/.freeze
298
-
310
+
299
311
  # @!visibility private
300
312
  alias_method :orig_puts, :puts
301
-
313
+
302
314
  # @!visibility private
303
315
  def puts(*args)
304
316
  if args.empty?
305
317
  write LINEFEED
306
318
  return
307
319
  end
308
-
320
+
309
321
  idx = 0
310
322
  while idx < args.size
311
323
  arg = args[idx]
@@ -317,121 +329,119 @@ class ::IO
317
329
  idx += 2
318
330
  end
319
331
  end
320
-
332
+
321
333
  write(*args)
322
334
  nil
323
335
  end
324
-
336
+
325
337
  # def readbyte
326
338
  # end
327
-
339
+
328
340
  # def readchar
329
341
  # end
330
-
342
+
331
343
  # def readline(sep = $/, limit = nil, chomp: nil)
332
344
  # end
333
-
345
+
334
346
  # def readlines(sep = $/, limit = nil, chomp: nil)
335
347
  # end
336
-
348
+
337
349
  # @!visibility private
338
350
  alias_method :orig_write_nonblock, :write_nonblock
339
-
351
+
340
352
  # @!visibility private
341
353
  def write_nonblock(string, _options = {})
342
- write(string)
343
- end
344
-
345
- # @!visibility private
346
- alias_method :orig_read_nonblock, :read_nonblock
354
+ write(string)
355
+ end
347
356
 
348
- # @!visibility private
349
- def read_nonblock(maxlen, buf = nil, _options = nil)
350
- buf ? readpartial(maxlen, buf) : readpartial(maxlen)
351
- end
357
+ # @!visibility private
358
+ alias_method :orig_read_nonblock, :read_nonblock
352
359
 
353
- # Reads up to `maxlen` bytes at a time in an infinite loop. Read data
354
- # will be passed to the given block.
355
- #
356
- # @param maxlen [Integer] maximum bytes to receive
357
- # @yield [String] read data
358
- # @return [IO] self
359
- def read_loop(maxlen = 8192, &block)
360
- Polyphony.backend_read_loop(self, maxlen, &block)
361
- end
360
+ # @!visibility private
361
+ def read_nonblock(maxlen, buf = nil, _options = nil)
362
+ buf ? readpartial(maxlen, buf) : readpartial(maxlen)
363
+ end
362
364
 
363
- # Receives data from the io in an infinite loop, passing the data to the given
364
- # receiver using the given method. If a block is given, the result of the
365
- # method call to the receiver is passed to the block.
366
- #
367
- # This method can be used to feed data into parser objects. The following
368
- # example shows how to feed data from a io directly into a MessagePack
369
- # unpacker:
370
- #
371
- # unpacker = MessagePack::Unpacker.new
372
- # buffer = []
373
- # reader = spin do
374
- # io.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
375
- # end
376
- #
377
- # @param receiver [any] receiver object
378
- # @param method [Symbol] method to call
379
- # @return [IO] self
380
- def feed_loop(receiver, method = :call, &block)
381
- Polyphony.backend_feed_loop(self, receiver, method, &block)
382
- end
365
+ # Reads up to `maxlen` bytes at a time in an infinite loop. Read data
366
+ # will be passed to the given block.
367
+ #
368
+ # @param maxlen [Integer] maximum bytes to receive
369
+ # @yield [String] read data
370
+ # @return [IO] self
371
+ def read_loop(maxlen = 8192, &block)
372
+ Polyphony.backend_read_loop(self, maxlen, &block)
373
+ end
383
374
 
384
- # Waits for the IO to become readable, with an optional timeout.
385
- #
386
- # @param timeout [Integer, nil] optional timeout in seconds.
387
- # @return [IO] self
388
- def wait_readable(timeout = nil)
389
- return self if @read_buffer && @read_buffer.size > 0
375
+ # Receives data from the io in an infinite loop, passing the data to the given
376
+ # receiver using the given method. If a block is given, the result of the
377
+ # method call to the receiver is passed to the block.
378
+ #
379
+ # This method can be used to feed data into parser objects. The following
380
+ # example shows how to feed data from a io directly into a MessagePack
381
+ # unpacker:
382
+ #
383
+ # unpacker = MessagePack::Unpacker.new
384
+ # io.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
385
+ #
386
+ # @param receiver [any] receiver object
387
+ # @param method [Symbol] method to call
388
+ # @return [IO] self
389
+ def feed_loop(receiver, method = :call, &block)
390
+ Polyphony.backend_feed_loop(self, receiver, method, &block)
391
+ end
390
392
 
391
- if timeout
392
- move_on_after(timeout) do
393
- Polyphony.backend_wait_io(self, false)
394
- self
395
- end
396
- else
393
+ # Waits for the IO to become readable, with an optional timeout.
394
+ #
395
+ # @param timeout [Integer, nil] optional timeout in seconds.
396
+ # @return [IO] self
397
+ def wait_readable(timeout = nil)
398
+ return self if @read_buffer && @read_buffer.size > 0
399
+
400
+ if timeout
401
+ move_on_after(timeout) do
397
402
  Polyphony.backend_wait_io(self, false)
398
403
  self
399
404
  end
405
+ else
406
+ Polyphony.backend_wait_io(self, false)
407
+ self
400
408
  end
409
+ end
401
410
 
402
- # Waits for the IO to become writeable, with an optional timeout.
403
- #
404
- # @param timeout [Integer, nil] optional timeout in seconds.
405
- # @return [IO] self
406
- def wait_writable(timeout = nil)
407
- if timeout
408
- move_on_after(timeout) do
409
- Polyphony.backend_wait_io(self, true)
410
- self
411
- end
412
- else
411
+ # Waits for the IO to become writeable, with an optional timeout.
412
+ #
413
+ # @param timeout [Integer, nil] optional timeout in seconds.
414
+ # @return [IO] self
415
+ def wait_writable(timeout = nil)
416
+ if timeout
417
+ move_on_after(timeout) do
413
418
  Polyphony.backend_wait_io(self, true)
414
419
  self
415
420
  end
421
+ else
422
+ Polyphony.backend_wait_io(self, true)
423
+ self
416
424
  end
425
+ end
417
426
 
418
- # Splices data from the given IO.
419
- #
420
- # @param src [IO, Polpyhony::Pipe] source to splice from
421
- # @param maxlen [Integer] maximum bytes to splice
422
- # @return [Integer] bytes spliced
423
- def splice_from(src, maxlen)
424
- Polyphony.backend_splice(src, self, maxlen)
425
- end
427
+ # Splices data from the given IO. If maxlen is negative, splices repeatedly
428
+ # using absolute value of maxlen until EOF is encountered.
429
+ #
430
+ # @param src [IO, Polpyhony::Pipe] source to splice from
431
+ # @param maxlen [Integer] maximum bytes to splice
432
+ # @return [Integer] bytes spliced
433
+ def splice_from(src, maxlen)
434
+ Polyphony.backend_splice(src, self, maxlen)
435
+ end
426
436
 
427
- if RUBY_PLATFORM =~ /linux/
428
- # Tees data from the given IO.
429
- #
430
- # @param src [IO, Polpyhony::Pipe] source to tee from
431
- # @param maxlen [Integer] maximum bytes to tee
432
- # @return [Integer] bytes teed
433
- def tee_from(src, maxlen)
434
- Polyphony.backend_tee(src, self, maxlen)
435
- end
437
+ if RUBY_PLATFORM =~ /linux/
438
+ # Tees data from the given IO.
439
+ #
440
+ # @param src [IO, Polpyhony::Pipe] source to tee from
441
+ # @param maxlen [Integer] maximum bytes to tee
442
+ # @return [Integer] bytes teed
443
+ def tee_from(src, maxlen)
444
+ Polyphony.backend_tee(src, self, maxlen)
436
445
  end
437
446
  end
447
+ end
@@ -192,10 +192,7 @@ class Polyphony::Pipe
192
192
  # unpacker:
193
193
  #
194
194
  # unpacker = MessagePack::Unpacker.new
195
- # buffer = []
196
- # reader = spin do
197
- # pipe.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
198
- # end
195
+ # pipe.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
199
196
  #
200
197
  # @param receiver [any] receiver object
201
198
  # @param method [Symbol] method to call
@@ -236,7 +233,8 @@ class Polyphony::Pipe
236
233
  end
237
234
  end
238
235
 
239
- # Splices to the pipe from the given source.
236
+ # Splices to the pipe from the given source. If maxlen is negative, splices
237
+ # repeatedly using absolute value of maxlen until EOF is encountered.
240
238
  #
241
239
  # @param src [IO] source to splice from
242
240
  # @param maxlen [Integer] maximum bytes to splice
@@ -124,10 +124,7 @@ class ::Socket < ::BasicSocket
124
124
  # unpacker:
125
125
  #
126
126
  # unpacker = MessagePack::Unpacker.new
127
- # buffer = []
128
- # reader = spin do
129
- # i.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
130
- # end
127
+ # conn.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
131
128
  #
132
129
  # @param receiver [any] receiver object
133
130
  # @param method [Symbol] method to call
@@ -391,10 +388,7 @@ class ::TCPSocket < ::IPSocket
391
388
  # unpacker:
392
389
  #
393
390
  # unpacker = MessagePack::Unpacker.new
394
- # buffer = []
395
- # reader = spin do
396
- # i.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
397
- # end
391
+ # conn.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
398
392
  #
399
393
  # @param receiver [any] receiver object
400
394
  # @param method [Symbol] method to call
@@ -603,10 +597,7 @@ class ::UNIXSocket < ::BasicSocket
603
597
  # unpacker:
604
598
  #
605
599
  # unpacker = MessagePack::Unpacker.new
606
- # buffer = []
607
- # reader = spin do
608
- # i.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
609
- # end
600
+ # conn.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
610
601
  #
611
602
  # @param receiver [any] receiver object
612
603
  # @param method [Symbol] method to call
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Polyphony
4
4
  # @!visibility private
5
- VERSION = '1.0'
5
+ VERSION = '1.0.1'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyphony
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-11 00:00:00.000000000 Z
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -154,6 +154,7 @@ files:
154
154
  - docs/assets/echo-fibers.svg
155
155
  - docs/assets/polyphony-logo.png
156
156
  - docs/assets/sleeping-fiber.svg
157
+ - docs/cheat-sheet.md
157
158
  - docs/concurrency.md
158
159
  - docs/design-principles.md
159
160
  - docs/exception-handling.md