Class: Murmur::Ice::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/murmur-rpc/interfaces/ice.rb

Instance Method Summary collapse

Constructor Details

#initialize(glacierHost = nil, glacierPort = nil, user = nil, pass = nil, host = "127.0.0.1", port = "6502", icesecret = nil, connect_timeout = nil, idletime = nil) ⇒ Meta

Returns a new instance of Meta.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/murmur-rpc/interfaces/ice.rb', line 12

def initialize(glacierHost = nil, glacierPort = nil, user = nil, pass = nil, host = "127.0.0.1", port = "6502", icesecret = nil, connect_timeout=nil, idletime = nil)
  ic = nil
  if icesecret and icesecret != ""
    props = ::Ice::createProperties
    props.setProperty "Ice.ImplicitContext", "Shared"
    props.setProperty "Ice.Override.Timeout", connect_timeout.to_s unless connect_timeout == nil
    props.setProperty "Ice.MessageSizeMax", "65536"
    props.setProperty "Ice.Default.EncodingVersion", "1.0"
    idd = ::Ice::InitializationData.new
    idd.properties = props
    ic = ::Ice::initialize idd
    ic.getImplicitContext.put("secret", icesecret)
  else
    ic = ::Ice::initialize
  end

  if glacierHost and glacierHost != "" then
    @glacierHost = glacierHost
    @glacierPort = glacierPort
    validate_connection(glacierHost, glacierPort)
    prx = ic.stringToProxy("Glacier2/router:tcp -h #{glacierHost} -p #{glacierPort}")
    @router = ::Glacier2::RouterPrx::uncheckedCast(prx).ice_router(nil)
    @session = @router.createSession(user, pass)
  end
  conn = "tcp -h #{host} -p #{port}"
  @meta = add_proxy_router(Murmur::MetaPrx::checkedCast(ic.stringToProxy("Meta:#{conn}")))
  raise "Invalid proxy" unless @meta

  @servers = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



82
83
84
85
86
# File 'lib/murmur-rpc/interfaces/ice.rb', line 82

def method_missing(method, *args)
  method = method.to_s
  method.gsub!(/_([a-z])/) { $1.upcase }
  @meta.send method, *args
end

Instance Method Details

#add_proxy_router(prx) ⇒ Object



52
53
54
# File 'lib/murmur-rpc/interfaces/ice.rb', line 52

def add_proxy_router(prx)
  @router ? prx.ice_router(@router) : prx
end

#destroyObject



43
44
45
46
47
48
49
50
# File 'lib/murmur-rpc/interfaces/ice.rb', line 43

def destroy
  begin
    @router.destroySession @session unless @router.nil?
  rescue ::Ice::ConnectionLostException
    # Expected - Ice raises this when the connection is terminated. Yay for exceptions as flow control?
  end
  return nil
end

#get_server(id) ⇒ Object



56
57
58
# File 'lib/murmur-rpc/interfaces/ice.rb', line 56

def get_server(id)
  @servers[id] ||= Server.new(self, @meta, id)
end

#list_servers(only_booted = false) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/murmur-rpc/interfaces/ice.rb', line 64

def list_servers(only_booted = false)
  method = only_booted ? :getBootedServers : :getAllServers
  @meta.send(method).collect do |server|
    server = add_proxy_router(server)
    @servers[server.id] ||= Server.new(self, @meta, nil, server)
  end
end

#new_server(port = nil) ⇒ Object



77
78
79
80
# File 'lib/murmur-rpc/interfaces/ice.rb', line 77

def new_server(port = nil)
  server = @meta.newServer
  @servers[server.id] = Server.new(self, @meta, nil, add_proxy_router(server))
end

#uncache_server(id) ⇒ Object



60
61
62
# File 'lib/murmur-rpc/interfaces/ice.rb', line 60

def uncache_server(id)
  @servers[id] = nil
end

#validateObject



72
73
74
75
# File 'lib/murmur-rpc/interfaces/ice.rb', line 72

def validate
  @meta.getVersion
  return true
end

#validate_connection(host, port) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/murmur-rpc/interfaces/ice.rb', line 88

def validate_connection(host, port)
  Timeout::timeout(2) do
    begin
      s = TCPSocket.new(host, port)
      s.close
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      raise InvalidMetaException
    end
  end
rescue Timeout::Error
  raise InvalidMetaException
end