Class: BERTREM::Client

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
BERTRPC::Encodes
Defined in:
lib/bertrem/client.rb

Overview

NOTE: ernie closes connections after responding, so we can’t send

multiple requests per connection.  Hence, the default for
persistent is false.  If you are working with a server that
supports more than one request per connection, like
BERTREM::Server, call BERTREM.service with persistent = true
and it will Just Work.

Defined Under Namespace

Classes: Request

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.persistentObject

Returns the value of attribute persistent.



33
34
35
# File 'lib/bertrem/client.rb', line 33

def persistent
  @persistent
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



15
16
17
# File 'lib/bertrem/client.rb', line 15

def requests
  @requests
end

Class Method Details

.service(host, port, persistent = false, timeout = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/bertrem/client.rb', line 38

def self.service(host, port, persistent = false, timeout = nil)
  self.persistent = persistent
  c = EM.connect(host, port, self)
  c.pending_connect_timeout = timeout if timeout
  c
end

Instance Method Details

#call(options = nil) ⇒ Object



93
94
95
96
# File 'lib/bertrem/client.rb', line 93

def call(options = nil)
  verify_options(options)
  Request.new(self, :call, options)
end

#cast(options = nil) ⇒ Object



98
99
100
101
# File 'lib/bertrem/client.rb', line 98

def cast(options = nil)
  verify_options(options)
  Request.new(self, :cast, options)
end

#persistentObject



57
58
59
# File 'lib/bertrem/client.rb', line 57

def persistent
  Client.persistent
end

#post_initObject



45
46
47
48
# File 'lib/bertrem/client.rb', line 45

def post_init
  @receive_buf = ""; @receive_len = 0; @more = false
  @requests = []
end

#receive_data(bert_response) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bertrem/client.rb', line 61

def receive_data(bert_response)
  @receive_buf << bert_response

  while @receive_buf.length > 0
    unless @more
      begin
        if @receive_buf.length > 4
          @receive_len = @receive_buf.slice!(0..3).unpack('N').first if @receive_len == 0
          raise BERTRPC::ProtocolError.new(BERTRPC::ProtocolError::NO_DATA) unless @receive_buf.length > 0
        else
          raise BERTRPC::ProtocolError.new(BERTRPC::ProtocolError::NO_HEADER)
        end
      rescue Exception => e
        log "Bad BERT message: #{e.message}"
        return       
      end
    end

    if @receive_buf.length >= @receive_len
      bert = @receive_buf.slice!(0..(@receive_len - 1))
      @receive_len = 0; @more = false
      @requests.pop.succeed(decode_bert_response(bert))
      break unless persistent
    else
      @more = true
      break
    end
  end

  close_connection unless (persistent || @more)
end

#unbindObject



50
51
52
53
54
55
# File 'lib/bertrem/client.rb', line 50

def unbind
  super
  @receive_buf = ""; @receive_len = 0; @more = false
  (@requests || []).each {|r| r.fail}
  raise BERTREM::ConnectionError.new("Connection to server lost!") if error?
end

#verify_options(options) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bertrem/client.rb', line 103

def verify_options(options)
  if options
    if cache = options[:cache]
      unless cache[0] == :validation && cache[1].is_a?(String)
        raise BERTRPC::InvalidOption.new("Valid :cache args are [:validation, String]")
      end
    else
      raise BERTRPC::InvalidOption.new("Valid options are :cache")
    end
  end
end