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_enable_ssl, #handle_enabled_ssl, #handle_noop, #handle_response, #host_with_port, #receive_line, #send_message, #send_message_reply, #ssl_handshake_completed

Constructor Details

#initialize(*args) ⇒ EMConnection

Returns a new instance of EMConnection.



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

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.



60
61
62
# File 'lib/marvin/distributed/client.rb', line 60

def client
  @client
end

#configurationObject

Returns the value of attribute configuration.



60
61
62
# File 'lib/marvin/distributed/client.rb', line 60

def configuration
  @configuration
end

#connection_hostObject

Returns the value of attribute connection_host.



60
61
62
# File 'lib/marvin/distributed/client.rb', line 60

def connection_host
  @connection_host
end

#connection_portObject

Returns the value of attribute connection_port.



60
61
62
# File 'lib/marvin/distributed/client.rb', line 60

def connection_port
  @connection_port
end

#portObject

Returns the value of attribute port.



60
61
62
# File 'lib/marvin/distributed/client.rb', line 60

def port
  @port
end

Class Method Details

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



149
150
151
152
153
154
155
# File 'lib/marvin/distributed/client.rb', line 149

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



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

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

#handle_authentication_failed(options = {}) ⇒ Object



144
145
146
147
# File 'lib/marvin/distributed/client.rb', line 144

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

#handle_event(options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/marvin/distributed/client.rb', line 106

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



134
135
136
137
# File 'lib/marvin/distributed/client.rb', line 134

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

#handle_welcome(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/marvin/distributed/client.rb', line 97

def handle_welcome(options = {})
  if should_use_ssl? && !ssl_enabled?
    request_ssl!
  else
    @connected = true
    post_connect
  end
end

#post_connectObject



75
76
77
78
# File 'lib/marvin/distributed/client.rb', line 75

def post_connect
  logger.info "Connection started; processing authentication"
  process_authentication
end

#post_initObject



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

def post_init
  super
  logger.info "Connected to distributed server"
end

#process_authenticationObject



90
91
92
93
94
95
# File 'lib/marvin/distributed/client.rb', line 90

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

#request_ssl!Object



157
158
159
160
# File 'lib/marvin/distributed/client.rb', line 157

def request_ssl!
  logger.info "Requesting SSL for Distributed Client"
  send_message(:enable_ssl) unless ssl_enabled?
end

#unbindObject



80
81
82
83
84
85
86
87
88
# File 'lib/marvin/distributed/client.rb', line 80

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