Class: Marvin::Distributed::Client::EMConnection

Inherits:
Protocol
  • Object
show all
Defined in:
lib/marvin/distributed/client.rb

Instance Attribute Summary collapse

Attributes inherited from Protocol

#callbacks

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Protocol

#handle_response, #receive_line, #send_message

Constructor Details

#initialize(*args) ⇒ EMConnection

Returns a new instance of EMConnection.



52
53
54
55
56
57
58
# File 'lib/marvin/distributed/client.rb', line 52

def initialize(*args)
  @configuration = args.last.is_a?(Marvin::Nash) ? args.pop : Marvin::Nash.new
  super(*args)
  @callbacks = {}
  @client = Marvin::Distributed::Client.new(self)
  @authenticated = false
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



50
51
52
# File 'lib/marvin/distributed/client.rb', line 50

def client
  @client
end

#configurationObject

Returns the value of attribute configuration.



50
51
52
# File 'lib/marvin/distributed/client.rb', line 50

def configuration
  @configuration
end

#connection_hostObject

Returns the value of attribute connection_host.



50
51
52
# File 'lib/marvin/distributed/client.rb', line 50

def connection_host
  @connection_host
end

#connection_portObject

Returns the value of attribute connection_port.



50
51
52
# File 'lib/marvin/distributed/client.rb', line 50

def connection_port
  @connection_port
end

#portObject

Returns the value of attribute port.



50
51
52
# File 'lib/marvin/distributed/client.rb', line 50

def port
  @port
end

Class Method Details

.connect(host, port, config = Marvin::Nash.new) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/marvin/distributed/client.rb', line 137

def self.connect(host, port, config = Marvin::Nash.new)
  logger.info "Attempting to connect to #{host}:#{port}"
  EventMachine.connect(host, port, self, config) do |c|
    c.connection_host = host
    c.connection_port = port
  end
end

Instance Method Details

#handle_authenticated(options = {}) ⇒ Object



127
128
129
130
# File 'lib/marvin/distributed/client.rb', line 127

def handle_authenticated(options = {})
  @authenticated = true
  logger.info "Successfully authenticated with #{host_with_port}"
end

#handle_authentication_failed(options = {}) ⇒ Object



132
133
134
135
# File 'lib/marvin/distributed/client.rb', line 132

def handle_authentication_failed(options = {})
  logger.info "Authentication with #{host_with_port} failed. Stopping."
  Marvin::Distributed::Client.stop
end

#handle_event(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/marvin/distributed/client.rb', line 94

def handle_event(options = {})
  event       = options["event-name"]
  client_host = options["client-host"]
  client_nick = options["client-nick"]
  options     = options["event-options"]
  options     = {} unless options.is_a?(Hash)
  return if event.blank?
  begin
    logger.debug "Handling #{event}"
    @client.remote_client_host = client_host
    @client.remote_client_nick = client_nick
    @client.setup_handlers
    @client.dispatch(event.to_sym, options)
  rescue Exception => e
    logger.warn "Got Exception - Forwarding to Remote"
    Marvin::ExceptionTracker.log(e)
    send_message(:exception, {
      "name"      => e.class.name,
      "message"   => e.message,
      "backtrace" => e.backtrace
    })
  ensure
    logger.debug "Sending completed message"
    send_message(:completed)
    @client.reset!
  end
end

#handle_unauthorized(options = {}) ⇒ Object



122
123
124
125
# File 'lib/marvin/distributed/client.rb', line 122

def handle_unauthorized(options = {})
  logger.warn "Attempted action when unauthorized. Stopping client."
  Marvin::Distributed::Client.stop
end

#post_initObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/marvin/distributed/client.rb', line 60

def post_init
  super
  logger.info "Connected to distributed server"
  if should_use_tls?
    logger.info "Attempting to initialize tls"
    start_tls
  else
    process_authentication
  end
end

#process_authenticationObject



87
88
89
90
91
92
# File 'lib/marvin/distributed/client.rb', line 87

def process_authentication
  if configuration.token?
    logger.info "Attempting to authenticate..." 
    send_message(:authenticate, {:token => configuration.token})
  end
end

#ssl_handshake_completedObject



71
72
73
74
# File 'lib/marvin/distributed/client.rb', line 71

def ssl_handshake_completed
  logger.info "tls handshake completed"
  process_authentication if should_use_tls?
end

#unbindObject



77
78
79
80
81
82
83
84
85
# File 'lib/marvin/distributed/client.rb', line 77

def unbind
  if self.stopping
    logger.info "Stopping distributed client"
  else
    logger.info "Lost connection to distributed client - Scheduling reconnect"
    EventMachine.add_timer(15) { EMConnection.connect(connection_host, connection_port, @configuration) }
  end
  super
end