Class: Citrus::Components::Connector

Inherits:
Component
  • Object
show all
Defined in:
lib/citrus/components/connector.rb

Overview

Connector

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Connector

Initialize the component

Parameters:

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


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/citrus/components/connector.rb', line 22

def initialize app, args={}
  @app = app
  @connector = get_connector args

  @encode = args[:encode]
  @decode = args[:decode]

  @blacklist_cb = args[:black_list_cb]
  @black_lists = []

  @server = nil
  @session = nil
  @connection = nil
end

Instance Method Details

#after_start(&block) ⇒ Object

Component lifecycle callback



61
62
63
64
# File 'lib/citrus/components/connector.rb', line 61

def after_start &block
  @connector.start &block
  @connector.on(:connection) { |socket| host_filter socket }
end

#send(req_id, route, msg, recvs, args, &block) ⇒ Object

Send message to the client

Parameters:

  • req_id (Integer)
  • route (String)
  • msg (Hash)
  • recvs (Array)
  • args (Hash)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/citrus/components/connector.rb', line 82

def send req_id, route, msg, recvs, args, &block
  if @encode
    # use customized encode
    msg = @encode.call self, req_id, route, msg
  elsif @connector.respond_to? :encode
    # use connector default encode
    msg = @connector.encode req_id, route, msg
  end

  if msg.empty?
    EM.next_tick {
      block_given? and yield Exception.new 'failed to send message for encode result is empty'
    }
    return
  end

  @app.components['push_scheduler'].schedule req_id, route, msg, recvs, args, &block
end

#start(&block) ⇒ Object

Start the component



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/citrus/components/connector.rb', line 38

def start &block
  @server = @app.components['server']
  @session = @app.components['session']
  @connection = @app.components['connection']

  unless @server
    EM.next_tick {
      block_given? and yield Exception.new 'failed to start connector component for server component not loaded'
    }
    return
  end

  unless @session
    EM.next_tick {
      block_given? and yield Exception.new 'failed to start connector component for session component not loaded'
    }
    return
  end

  EM.next_tick { block_given? and yield }
end

#stop(force = false, &block) ⇒ Object

Stop the component



67
68
69
70
71
72
73
# File 'lib/citrus/components/connector.rb', line 67

def stop force=false, &block
  if @connector
    @connector.stop force, &block
    @connector = nil
  end
  EM.next_tick { block_given? and yield }
end