Module: Lich::Common::Buffer

Defined in:
documented/common/buffer.rb

Constant Summary collapse

DOWNSTREAM_STRIPPED =

Represents the downstream stripped stream constant

1
DOWNSTREAM_RAW =

Represents the downstream raw stream constant

2
DOWNSTREAM_MOD =

Represents the downstream modified stream constant

4
UPSTREAM =

Represents the upstream stream constant

8
UPSTREAM_MOD =

Represents the upstream modified stream constant

16
SCRIPT_OUTPUT =

Represents the script output stream constant

32
@@index =
Hash.new
@@streams =
Hash.new
@@mutex =
Mutex.new
@@offset =
0
@@buffer =
Array.new
@@max_size =
3000

Class Method Summary collapse

Class Method Details

.cleanupBuffer

rubocop:enable Lint/HashCompareByIdentity Cleans up the index and streams for threads that are no longer active.

Returns:

  • (Buffer)

    The Buffer instance itself.



166
167
168
169
170
# File 'documented/common/buffer.rb', line 166

def Buffer.cleanup
  @@index.delete_if { |k, _v| not Thread.list.any? { |t| t.object_id == k } }
  @@streams.delete_if { |k, _v| not Thread.list.any? { |t| t.object_id == k } }
  return self
end

.clearArray<Line>

Clears the lines from the buffer for the current thread.

Examples:

Clearing lines from the buffer

cleared_lines = Buffer.clear

Returns:

  • (Array<Line>)

    An array of lines that were cleared from the buffer.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'documented/common/buffer.rb', line 96

def Buffer.clear
  thread_id = Thread.current.object_id
  if @@index[thread_id].nil?
    @@mutex.synchronize {
      @@index[thread_id] = (@@offset + @@buffer.length)
      @@streams[thread_id] ||= DOWNSTREAM_STRIPPED
    }
  end
  lines = Array.new
  loop {
    if (@@index[thread_id] - @@offset) >= @@buffer.length
      return lines
    end

    line = nil
    @@mutex.synchronize {
      if @@index[thread_id] < @@offset
        @@index[thread_id] = @@offset
      end
      line = @@buffer[@@index[thread_id] - @@offset]
    }
    @@index[thread_id] += 1
    lines.push(line) if ((line.stream & @@streams[thread_id]) != 0)
  }
  return lines
end

.getsLine

Note:

This method blocks until a line is available.

Retrieves the next line from the buffer, blocking if necessary.

Examples:

Retrieving a line from the buffer

line = Buffer.gets

Returns:

  • (Line)

    The next line from the buffer, or nil if no line is available.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'documented/common/buffer.rb', line 28

def Buffer.gets
  thread_id = Thread.current.object_id
  if @@index[thread_id].nil?
    @@mutex.synchronize {
      @@index[thread_id] = (@@offset + @@buffer.length)
      @@streams[thread_id] ||= DOWNSTREAM_STRIPPED
    }
  end
  line = nil
  loop {
    if (@@index[thread_id] - @@offset) >= @@buffer.length
      sleep 0.05 while ((@@index[thread_id] - @@offset) >= @@buffer.length)
    end
    @@mutex.synchronize {
      if @@index[thread_id] < @@offset
        @@index[thread_id] = @@offset
      end
      line = @@buffer[@@index[thread_id] - @@offset]
    }
    @@index[thread_id] += 1
    break if ((line.stream & @@streams[thread_id]) != 0)
  }
  return line
end

.gets?Line?

Retrieves the next line from the buffer, returning nil if no line is available.

Examples:

Attempting to retrieve a line from the buffer

line = Buffer.gets?

Returns:

  • (Line, nil)

    The next line from the buffer, or nil if no line is available.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'documented/common/buffer.rb', line 57

def Buffer.gets?
  thread_id = Thread.current.object_id
  if @@index[thread_id].nil?
    @@mutex.synchronize {
      @@index[thread_id] = (@@offset + @@buffer.length)
      @@streams[thread_id] ||= DOWNSTREAM_STRIPPED
    }
  end
  line = nil
  loop {
    if (@@index[thread_id] - @@offset) >= @@buffer.length
      return nil
    end

    @@mutex.synchronize {
      if @@index[thread_id] < @@offset
        @@index[thread_id] = @@offset
      end
      line = @@buffer[@@index[thread_id] - @@offset]
    }
    @@index[thread_id] += 1
    break if ((line.stream & @@streams[thread_id]) != 0)
  }
  return line
end

.rewindBuffer

Resets the buffer index for the current thread to the offset.

Returns:

  • (Buffer)

    The Buffer instance itself.



85
86
87
88
89
90
# File 'documented/common/buffer.rb', line 85

def Buffer.rewind
  thread_id = Thread.current.object_id
  @@index[thread_id] = @@offset
  @@streams[thread_id] ||= DOWNSTREAM_STRIPPED
  return self
end

.streamsInteger

rubocop:disable Lint/HashCompareByIdentity Retrieves the current stream value for the calling thread.

Returns:

  • (Integer)

    The current stream value for the thread.



148
149
150
# File 'documented/common/buffer.rb', line 148

def Buffer.streams
  @@streams[Thread.current.object_id]
end

.streams=(val) ⇒ Object

Sets the stream value for the calling thread.

Parameters:

  • val (Integer)

    The new stream value to set.

Raises:

  • (StandardError)

    If the provided value is invalid.



155
156
157
158
159
160
161
# File 'documented/common/buffer.rb', line 155

def Buffer.streams=(val)
  if (!val.is_a?(Integer)) or ((val & 63) == 0)
    respond "--- Lich: error: invalid streams value\n\t#{$!.caller[0..2].join("\n\t")}"
  else
    @@streams[Thread.current.object_id] = val
  end
end

.update(line, stream = nil) ⇒ Buffer

Updates the buffer with a new line, optionally setting its stream.

Examples:

Updating the buffer with a new line

Buffer.update(new_line, stream_id)

Parameters:

  • line (Line)

    The line to add to the buffer.

  • stream (Integer, nil) (defaults to: nil)

    The stream identifier for the line.

Returns:

  • (Buffer)

    The Buffer instance itself.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'documented/common/buffer.rb', line 129

def Buffer.update(line, stream = nil)
  @@mutex.synchronize {
    frozen_line = line.dup
    unless stream.nil?
      frozen_line.stream = stream
    end
    frozen_line.freeze
    @@buffer.push(frozen_line)
    while (@@buffer.length > @@max_size)
      @@buffer.shift
      @@offset += 1
    end
  }
  return self
end