Class: UR::Psi

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

Defined Under Namespace

Modules: ConnectionState Classes: Reconnect

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Psi.



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

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



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

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)


37
38
39
# File 'lib/psi.rb', line 37

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

#disconnectObject



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

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

#execute_ur_script(str) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/psi.rb', line 65

def execute_ur_script(str)
  @logger.info 'Executing UR Script ...'
  begin
    @sock.write(str)
    line = @sock.gets.strip
    @logger.debug line
  rescue => e
    raise UR::Psi::Reconnect.new('UR Script can not be got. PSI server down or not in Remote Mode')
  end
end

#execute_ur_script_file(filename) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/psi.rb', line 50

def execute_ur_script_file(filename)
  @logger.info 'Executing UR Script File: ' + filename
  begin
    File.open(filename) do |file|
      while not file.eof?
        @sock.write(file.readline)
        line = @sock.gets.strip
        @logger.debug line
      end
    end
  rescue => e
    raise UR::Psi::Reconnect.new('UR Script can not be got. PSI server down or not in Remote Mode')
  end
end