class Ramp::AmpClient

AmpClient class is responsble for establishing a connection to a AMP server

Public Class Methods

new(host, port, kwargs={secure: false, ssl_key: nil, ssl_cert:nil, async: false}) click to toggle source
host

address of remote amp server

port

port to connect to.

kwarrgs

is a hash that contains extra optional arguments.

  • secure

    Use an SSL secured connection

  • ssl_key

    Path to SSL key.

  • ssl_cert

    Path to client SSL cert file.

  • async

    If this argument was true then Ramp use a threaded solution to send the request and handle the response

# File lib/ramp.rb, line 97
def initialize (host, port, kwargs={secure: false,
                  ssl_key: nil,
                  ssl_cert:nil,
                  async: false})

  @async = kwargs[:async]
  @secure = kwargs[:secure]
  @ssl_key = kwargs[:ssl_key]
  @ssl_cert = kwargs[:ssl_cert]

  @host = host
  @port = port

  make_connection
  
end

Public Instance Methods

call(command, kwargs) click to toggle source

This method will call the given command on the remote server with given arguments in kwargs

command

is a subclass of Command class.

kwargs

the arguments for the command’s initilize method.

# File lib/ramp.rb, line 118
def call(command, kwargs)

  # Create a new command instance
  obj = command.new kwargs

  # Add the curretn command instance to the sent hash
  @@sent_packets[obj.ask] = obj

  if @async
    t = Thread.new {
      transfer obj.to_s
    }
    t.abort_on_exception = true
    t.run
  else
    transfer obj.to_s
  end

end

Private Instance Methods

make_connection() click to toggle source

Private members ———————————–

# File lib/ramp.rb, line 141
def make_connection 

  begin
    socket = TCPSocket.new @host, @port
  rescue Errno::ECONNREFUSED
    abort("Connection Refused")        
  end

  if @secure
    ctx = OpenSSL::SSL::SSLContext.new()
    ctx.cert = OpenSSL::X509::Certificate.new(File.open(@ssl_cert))
    ctx.key = OpenSSL::PKey::RSA.new(File.open(@ssl_key))
    ctx.ssl_version = :SSLv23
    ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ctx)
    ssl_socket.sync_close = true
    ssl_socket.connect
    @socket = ssl_socket
    return 

  end

  @socket = socket

end
transfer(data) click to toggle source
# File lib/ramp.rb, line 166
def transfer data
  # send the encoded data across the net
  @socket.syswrite(data)
  # TODO: Should i specify a recieving limitation ?
  rawdata = @socket.recv(1024)
  data = Command::loads(rawdata)

  if data.include? :_answer
    if @@sent_packets.keys.include? data
      @@sent_packets[data[:_answer]].callback(data)
    end
  elsif data.include? :_error
    # Generate an exception from _error_code and rise it
    exception = Object.const_set(data[:_error_code], Class.new(StandardError))
    raise exception, data[:_error_description]
  end
        
end