Class: Meshchat::Ui::CLI

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/meshchat/ui/cli.rb,
lib/meshchat/ui/cli/base.rb,
lib/meshchat/ui/cli/input_factory.rb,
lib/meshchat/ui/cli/readline_input.rb,
lib/meshchat/ui/cli/keyboard_line_input.rb

Overview

A user interface is responsible for for creating a client and sending messages to that client

Defined Under Namespace

Classes: Base, InputFactory, KeyboardLineInput, ReadlineInput

Constant Summary collapse

AWAY_TIMEOUT =

60 seconds times 5 minutes

60 * 5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, message_factory, _display) ⇒ CLI

Returns a new instance of CLI.



23
24
25
26
27
28
29
30
31
32
# File 'lib/meshchat/ui/cli.rb', line 23

def initialize(dispatcher, message_factory, _display)
  @_message_dispatcher = dispatcher
  @_message_factory = message_factory
  @_command_factory = InputFactory.new(dispatcher, message_factory, self)
  self._sent_idle_message = false
  self._last_input_received_at = Time.now

  # only check for timeout once a minute
  EM.add_periodic_timer(60) { away_timeout }
end

Instance Attribute Details

#_command_factoryObject (readonly)

Returns the value of attribute _command_factory.



19
20
21
# File 'lib/meshchat/ui/cli.rb', line 19

def _command_factory
  @_command_factory
end

#_last_input_received_atObject

Returns the value of attribute _last_input_received_at.



20
21
22
# File 'lib/meshchat/ui/cli.rb', line 20

def _last_input_received_at
  @_last_input_received_at
end

#_message_dispatcherObject (readonly)

Returns the value of attribute _message_dispatcher.



19
20
21
# File 'lib/meshchat/ui/cli.rb', line 19

def _message_dispatcher
  @_message_dispatcher
end

#_message_factoryObject (readonly)

Returns the value of attribute _message_factory.



19
20
21
# File 'lib/meshchat/ui/cli.rb', line 19

def _message_factory
  @_message_factory
end

#_sent_idle_messageObject

Returns the value of attribute _sent_idle_message.



21
22
23
# File 'lib/meshchat/ui/cli.rb', line 21

def _sent_idle_message
  @_sent_idle_message
end

Instance Method Details

#activity_timeout_triggred?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/meshchat/ui/cli.rb', line 45

def activity_timeout_triggred?
  return false if _sent_idle_message
  seconds_passed = Time.now - _last_input_received_at
  seconds_passed > AWAY_TIMEOUT
end

#away_timeoutObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/meshchat/ui/cli.rb', line 34

def away_timeout
  return unless activity_timeout_triggred?
  message = _message_factory.create(Network::Message::EMOTE,
    data: {
      message: 'has become idle'
    })

  _message_dispatcher.send_to_all(message)
  self._sent_idle_message = true
end

#create_input(msg) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/meshchat/ui/cli.rb', line 56

def create_input(msg)
  reset_timeout_timer
  handler = _command_factory.create(for_input: msg)
  handler.handle
rescue => e
  Debug.creating_input_failed(e)
end

#reset_timeout_timerObject



51
52
53
54
# File 'lib/meshchat/ui/cli.rb', line 51

def reset_timeout_timer
  self._last_input_received_at = Time.now
  self._sent_idle_message = false
end

#send_disconnectObject



75
76
77
78
79
# File 'lib/meshchat/ui/cli.rb', line 75

def send_disconnect
  klass   = Command::SendDisconnect
  command = _command_factory.create(with_class: klass)
  command.handle
end

#shutdownObject

save config and exit



65
66
67
68
69
70
71
72
73
# File 'lib/meshchat/ui/cli.rb', line 65

def shutdown
  # close_server
  Display.info 'saving config...'
  APP_CONFIG.user.save
  Display.info 'notifying of disconnection...'
  send_disconnect
  Display.alert "\n\nGoodbye.  \n\nThank you for using #{Meshchat.name}"
  exit
end