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.



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

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



89
90
91
92
# File 'lib/brb/tunnel.rb', line 89

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

Instance Attribute Details

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



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

def ip_address
  @ip_address
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#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)


84
85
86
# File 'lib/brb/tunnel.rb', line 84

def active?
  @active
end

#close_connection(after_writing = false) ⇒ Object



57
58
59
60
# File 'lib/brb/tunnel.rb', line 57

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

#post_initObject

EventMachine Callback, called after connection has been initialized



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

def post_init
  @port, @ip_address = Socket.unpack_sockaddr_in(get_peername) 
  
  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



75
76
77
78
79
80
81
# File 'lib/brb/tunnel.rb', line 75

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



64
65
66
67
68
69
70
71
72
# File 'lib/brb/tunnel.rb', line 64

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