Class: BrB::Tunnel::Handler

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
Request, Shared
Defined in:
lib/brb/tunnel.rb

Overview

Brb interface Handler for Tunnel over Event machine

Constant Summary

Constants included from Shared

Shared::SizeOfPackedInt

Constants included from Request

Request::CallbackRequestCode, Request::MessageRequestCode, Request::ReturnCode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#brb_send, #declare_callback, #get_callback, #load_request, #make_proxy, #receive_data, #recv, #treat_callback_return, #treat_request

Methods included from Request

#is_brb_request_blocking?, #new_brb_in_request, #new_brb_out_request

Constructor Details

#initialize(opts = {}) ⇒ Handler

Returns a new instance of Handler.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/brb/tunnel.rb', line 23

def initialize(opts = {})
  super
  @object = opts[:object]
  @verbose = opts[:verbose]
  BrB.logger.level = @verbose ? Logger::INFO : Logger::WARN
  @timeout_rcv_value = opts[:timeout] || 30 # Currently not implemented due to the lack of performance of ruby Timeout
  @close_after_timeout = opts[:close_after_timeout] || false
  @uri = opts[:uri]
  @replock = Mutex.new
  @responses = {}
  @block = opts[:block]
  
  @queue = Queue.new
  @buffer = ''
  
  # Callbacks handling :
  @callbacks = {}
  @callbacks_mutex = Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

When no method is found on tunnel interface, create an brb out request



86
87
88
89
# File 'lib/brb/tunnel.rb', line 86

def method_missing(meth, *args, &block)
  return nil if !@active
  new_brb_out_request(meth, *args, &block)
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



18
19
20
# File 'lib/brb/tunnel.rb', line 18

def uri
  @uri
end

Instance Method Details

#active?Boolean

Return true if the tunnel is currently active

Returns:

  • (Boolean)


81
82
83
# File 'lib/brb/tunnel.rb', line 81

def active?
  @active
end

#close_connection(after_writing = false) ⇒ Object



54
55
56
57
# File 'lib/brb/tunnel.rb', line 54

def close_connection(after_writing = false)
  @active = false
  super
end

#post_initObject

EventMachine Callback, called after connection has been initialized



44
45
46
47
48
49
50
51
52
# File 'lib/brb/tunnel.rb', line 44

def post_init
  BrB.logger.info " [BrB] Tunnel initialized on #{@uri}"
  @active = true
  if @block
    EM.defer do
      @block.call(:register, self)
    end
  end
end

#stop_serviceObject

Stop the service



72
73
74
75
76
77
78
# File 'lib/brb/tunnel.rb', line 72

def stop_service
  BrB.logger.info ' [BrB] Stopping Tunnel service...'
  @active = false
  EM.schedule do
    close_connection
  end
end

#unbindObject

EventMachine unbind event The connection has been closed



61
62
63
64
65
66
67
68
69
# File 'lib/brb/tunnel.rb', line 61

def unbind
  BrB.logger.info ' [BrB] Tunnel service closed'
  @active = false
  if @block
    EM.defer do
      @block.call(:unregister, self)
    end
  end
end