Class: UR::Psi

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

Defined Under Namespace

Modules: ConnectionState

Instance Method Summary collapse

Constructor Details

#initialize(host, logger = Logger.new(STDOUT,level: :INFO)) ⇒ Psi

Returns a new instance of Psi.



16
17
18
19
20
21
22
23
24
# File 'lib/psi.rb', line 16

def initialize(host, logger=Logger.new(STDOUT,level: :INFO))
  host = '//' + host if host !~ /\/\//
  uri = URI::parse(host)
  @logger = logger
  @hostname = uri.host
  @port = uri.port.nil? ? 30003 : uri.port
  @conn_state = ConnectionState::DISCONNECTED
  @sock = nil
end

Instance Method Details

#connectObject



26
27
28
29
30
31
32
33
# File 'lib/psi.rb', line 26

def connect
  return if @sock
  @sock = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
  @sock.setsockopt Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1
  @sock = TCPSocket.new(@hostname, @port)
  @conn_state = ConnectionState::CONNECTED
  self
end

#connected?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/psi.rb', line 35

def connected?
  @conn_state != ConnectionState::DISCONNECTED
end

#disconnectObject



39
40
41
42
43
44
45
46
# File 'lib/psi.rb', line 39

def disconnect
  if @sock
    @sock.close
    @sock = nil
    @conn_state = ConnectionState::DISCONNECTED
    @logger.info "Connection closed " + @hostname + ":" + @port.to_s
  end
end

#transfer(filename) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/psi.rb', line 48

def transfer(filename)
  File.open(filename) do |file|
    while not file.eof?
      @sock.write(file.read(1024))
    end
  end
end