Class: RStyx::Keyring::FileWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rstyx/keyring.rb

Overview

A connection wrapper, which provides read and write methods just like a socket, given a connection object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ FileWrapper

Create a new FileWrapper, given an EventMachine connection object.



677
678
679
680
681
682
# File 'lib/rstyx/keyring.rb', line 677

def initialize(conn)
  @conn = conn
  @data = ""
  @datalock = Mutex.new
  @dcondvar = ConditionVariable.new
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



672
673
674
# File 'lib/rstyx/keyring.rb', line 672

def data
  @data
end

Instance Method Details

#<<(str) ⇒ Object

Add data received from the connection here



708
709
710
711
712
713
# File 'lib/rstyx/keyring.rb', line 708

def <<(str)
  @datalock.synchronize do
    @data << str
    @dcondvar.signal
  end
end

#printf(str, *args) ⇒ Object

Print data to the connection



718
719
720
721
# File 'lib/rstyx/keyring.rb', line 718

def printf(str, *args)
  str = sprintf(str, *args)
  write(str)
end

#read(length) ⇒ Object

Read data from the connection



694
695
696
697
698
699
700
701
702
703
# File 'lib/rstyx/keyring.rb', line 694

def read(length)
  @datalock.synchronize do
    while @data.length < length
      @dcondvar.wait(@datalock)
    end
    retval, rest = @data.unpack("a#{length}a*")
    @data = rest
    return(retval)
  end
end

#write(data) ⇒ Object

Write data to the connection



687
688
689
# File 'lib/rstyx/keyring.rb', line 687

def write(data)
  @conn.send_data(data)
end