Class: CitrusRpc::RpcServer::Gateway

Inherits:
Object
  • Object
show all
Includes:
Dispatcher, Utils::EventEmitter
Defined in:
lib/citrus-rpc/rpc-server/gateway.rb

Overview

Gateway

Instance Method Summary collapse

Methods included from Utils::EventEmitter

#emit, #on, #once

Methods included from Dispatcher

#dispatch

Constructor Details

#initialize(args = {}) ⇒ Gateway

Create a gateway

Parameters:

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

    Options

Options Hash (args):

  • port (Integer)
  • acceptor_class (Class)
  • services (Hash)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/citrus-rpc/rpc-server/gateway.rb', line 27

def initialize args={}
  @port = args[:port] || 3050
  @started = false
  @stoped = false

  @acceptor_class = args[:acceptor_class] || WsAcceptor
  @services = args[:services]

  @acceptor = @acceptor_class.new(args) { |msg, &block|
    dispatch msg, @services, &block
  }
end

Instance Method Details

#startObject

Start the gateway



41
42
43
44
45
46
47
48
# File 'lib/citrus-rpc/rpc-server/gateway.rb', line 41

def start
  raise RuntimeError 'gateway already started' if @started
  @started = true

  @acceptor.on(:error) { |*args| emit :error, *args }
  @acceptor.on(:closed) { |*args| emit :closed, *args }
  @acceptor.listen @port
end

#stopObject

Stop the gateway



51
52
53
54
55
# File 'lib/citrus-rpc/rpc-server/gateway.rb', line 51

def stop
  return unless @started && !@stoped
  @stoped = true
  @acceptor.close
end