Class: Chan::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/xchan/counter.rb

Overview

Chan::Counter provides a counter for the number of written and received bytes on a given channel.

Instance Method Summary collapse

Constructor Details

#initialize(tmpdir) ⇒ Chan::Counter

Parameters:

  • tmpdir (String)

    Directory where temporary files are stored



12
13
14
15
16
17
# File 'lib/xchan/counter.rb', line 12

def initialize(tmpdir)
  @io = Chan.temporary_file(%w[counter .bin], tmpdir:)
  @io.binmode
  @io.sync = true
  write(@io, 0, 0)
end

Instance Method Details

#bytes_readInteger

Returns the number of bytes read from a channel

Returns:

  • (Integer)

    Returns the number of bytes read from a channel



30
31
32
33
# File 'lib/xchan/counter.rb', line 30

def bytes_read
  bytes, _ = read(@io)
  bytes
end

#bytes_writtenInteger

Returns the number of bytes written to a channel

Returns:

  • (Integer)

    Returns the number of bytes written to a channel



22
23
24
25
# File 'lib/xchan/counter.rb', line 22

def bytes_written
  _, bytes = read(@io)
  bytes
end

#closevoid

This method returns an undefined value.

Close the counter



52
53
54
# File 'lib/xchan/counter.rb', line 52

def close
  @io.close
end

#increment!(bytes_read: 0, bytes_written: 0) ⇒ void

This method returns an undefined value.

Parameters:

  • bytes_read (Integer) (defaults to: 0)

    Number of bytes read to increment the counter by

  • bytes_written (Integer) (defaults to: 0)

    Number of bytes written to increment the counter by



42
43
44
45
46
47
# File 'lib/xchan/counter.rb', line 42

def increment!(bytes_read: 0, bytes_written: 0)
  bytes_in, bytes_out = read(@io)
  bytes_in += bytes_read
  bytes_out += bytes_written
  write(@io, bytes_in, bytes_out)
end