Class: CitrusRpc::RpcClient::Client

Inherits:
Object
  • Object
show all
Includes:
CitrusLoader, Proxy, Router
Defined in:
lib/citrus-rpc/rpc-client/client.rb

Overview

Client

Create a new rpc client

client = CitrusRpc::RpcClient::Client.new

Add a proxy

dirname = File.expand_path File.dirname(__FILE__)
client.add_proxy(
  :namespace => 'user',
  :server_type => 'test',
  :path => dirname + '/remote/test' # remote service interface path
)

Add a remote server

client.add_server(
  :server_id => 'test-server-1',
  :server_type => 'test',
  :host => '127.0.0.1',
  :port => 3333
)

Do the rpc invoke

client.start do |err|
  client.proxies.sys.connector.WhoAmIRemote.do(nil, 'hello') do |err, resp|
    ...
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Router

#ch_route, #df_route, #la_route, #rd_route, #rr_route, #wrr_route

Constructor Details

#initialize(args = {}) ⇒ Client

Create a new rpc client

Parameters:

  • args (Hash) (defaults to: {})

    Options

Options Hash (args):

  • context (Object)
  • route_context (Object)
  • router (#call)
  • router_type (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/citrus-rpc/rpc-client/client.rb', line 64

def initialize args={}
  @args = args

  @context = @args[:context]
  @route_context = @args[:route_context]

  @router = @args[:router] || method(:df_route)
  @router_type = @args[:router_type]

  @proxies = OpenStruct.new
  @station = MailStation.new args

  @state = :state_inited
end

Instance Attribute Details

#proxiesObject (readonly)

Returns the value of attribute proxies.



54
55
56
# File 'lib/citrus-rpc/rpc-client/client.rb', line 54

def proxies
  @proxies
end

Instance Method Details

#add_proxies(records) ⇒ Object

Batch version for add_proxy

Parameters:

  • records (Array)


123
124
125
126
127
# File 'lib/citrus-rpc/rpc-client/client.rb', line 123

def add_proxies records
  if records && records.length > 0
    records.each { |record| add_proxy record }
  end
end

#add_proxy(record) ⇒ Object

Add a new proxy to the rpc client which would override the proxy under the same key

Parameters:

  • record (Hash)


111
112
113
114
115
116
117
118
# File 'lib/citrus-rpc/rpc-client/client.rb', line 111

def add_proxy record
  return unless record

  proxy = generate_proxy record
  return unless proxy

  insert_proxy @proxies, record[:namespace], record[:server_type], proxy
end

#add_server(server) ⇒ Object

Add new remote server to the rpc client

Parameters:

  • server (Hash)


132
133
134
# File 'lib/citrus-rpc/rpc-client/client.rb', line 132

def add_server server
  @station.add_server server
end

#add_servers(servers) ⇒ Object

Batch version for add new remote server

Parameters:

  • servers (Array)


139
140
141
# File 'lib/citrus-rpc/rpc-client/client.rb', line 139

def add_servers servers
  @station.add_servers servers
end

#after(filter) ⇒ Object

Add rpc after filter

Parameters:

  • filter (#call)


186
187
188
# File 'lib/citrus-rpc/rpc-client/client.rb', line 186

def after filter
  @station.after filter
end

#before(filter) ⇒ Object

Add rpc before filter

Parameters:

  • filter (#call)


179
180
181
# File 'lib/citrus-rpc/rpc-client/client.rb', line 179

def before filter
  @station.before filter
end

#filter(filter) ⇒ Object

Add rpc filter

Parameters:

  • filter (#call)


193
194
195
# File 'lib/citrus-rpc/rpc-client/client.rb', line 193

def filter filter
  @station.filter filter
end

#remove_server(id) ⇒ Object

Remove remote server from the rpc client

Parameters:

  • id (String)


146
147
148
# File 'lib/citrus-rpc/rpc-client/client.rb', line 146

def remove_server id
  @station.remove_server id
end

#remove_servers(ids) ⇒ Object

Batch version for remove remote server

Parameters:

  • server_ids (Array)


153
154
155
# File 'lib/citrus-rpc/rpc-client/client.rb', line 153

def remove_servers ids
  @station.remove_servers ids
end

#replace_servers(servers) ⇒ Object

Replace remote servers

Parameters:

  • servers (Array)


160
161
162
# File 'lib/citrus-rpc/rpc-client/client.rb', line 160

def replace_servers servers
  @station.replace_servers servers
end

#rpc_invoke(server_id, msg, &block) ⇒ Object

Do the rpc invoke directly

Parameters:

  • server_id (String)
  • msg (Hash)


168
169
170
171
172
173
174
# File 'lib/citrus-rpc/rpc-client/client.rb', line 168

def rpc_invoke server_id, msg, &block
  unless @state == :state_started
    block_given? and yield Exception.new 'fail to do rpc invoke for client is not running'
    return
  end
  @station.dispatch server_id, msg, @args, block
end

#set_error_handler(handler) ⇒ Object

Set rpc filter error handler

Parameters:

  • handler (#call)


200
201
202
# File 'lib/citrus-rpc/rpc-client/client.rb', line 200

def set_error_handler handler
  @station.error_handler = handler
end

#startObject

Start the rpc client which would try to connect the remote servers



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/citrus-rpc/rpc-client/client.rb', line 80

def start
  unless @state == :state_inited
    block_given? and yield Exception.new 'rpc client has started'
    return
  end

  @station.start { |err|
    if err
      block_given? and yield err
      return
    end
    @state = :state_started
    block_given? and yield
  }
end

#stop(force = false) ⇒ Object

Stop the rpc client

Parameters:

  • force (Boolean) (defaults to: false)


99
100
101
102
103
104
105
# File 'lib/citrus-rpc/rpc-client/client.rb', line 99

def stop force=false
  unless @state == :state_started
    return
  end
  @state = :state_closed
  @station.stop force
end