Class: Erlectricity::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/erlectricity/port.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = STDIN, output = STDOUT) ⇒ Port

Returns a new instance of Port.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/erlectricity/port.rb', line 7

def initialize(input=STDIN, output=STDOUT)
  @input = input
  @output = output

  input.sync = true
  output.sync = true

  @encoder = Erlectricity::Encoder.new(nil)
  @skipped = []
  @queue = []
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/erlectricity/port.rb', line 3

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/erlectricity/port.rb', line 3

def output
  @output
end

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/erlectricity/port.rb', line 5

def queue
  @queue
end

#skippedObject (readonly)

Returns the value of attribute skipped.



4
5
6
# File 'lib/erlectricity/port.rb', line 4

def skipped
  @skipped
end

Instance Method Details

#receiveObject



19
20
21
# File 'lib/erlectricity/port.rb', line 19

def receive
  queue.empty? ? read_from_input : queue.shift
end

#restore_skippedObject



31
32
33
# File 'lib/erlectricity/port.rb', line 31

def restore_skipped
  @queue = self.skipped + self.queue
end

#send(term) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/erlectricity/port.rb', line 23

def send(term)
  @encoder.out = StringIO.new('', 'w')
  @encoder.write_any(term)
  data = @encoder.out.string
  output.write([data.length].pack("N"))
  output.write(data)
end