Class: MeshChat::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/cli.rb,
lib/meshchat/cli/base.rb,
lib/meshchat/cli/input.rb

Overview

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

Defined Under Namespace

Classes: Base, Input

Constant Summary collapse

COMMAND_MAP =
{
  MeshChat::Command::Base::CONFIG => MeshChat::Command::Config,
  MeshChat::Command::Base::PING => MeshChat::Command::Ping,
  MeshChat::Command::Base::PING_ALL => MeshChat::Command::PingAll,
  MeshChat::Command::Base::STOP_LISTENING => MeshChat::Command::StopListening,
  MeshChat::Command::Base::SERVERS => MeshChat::Command::Server,
  MeshChat::Command::Base::SERVER => MeshChat::Command::Server,
  MeshChat::Command::Base::EXIT => MeshChat::Command::Exit,
  MeshChat::Command::Base::QUIT => MeshChat::Command::Exit,
  MeshChat::Command::Base::LISTEN => MeshChat::Command::Listen,
  MeshChat::Command::Base::WHO => MeshChat::Command::Who,
  MeshChat::Command::Base::IDENTITY => MeshChat::Command::Identity,
  MeshChat::Command::Base::IRB => MeshChat::Command::IRB,
  MeshChat::Command::Base::INIT => MeshChat::Command::Init,
  MeshChat::Command::Base::SHARE => MeshChat::Command::Share,
  MeshChat::Command::Base::IMPORT => MeshChat::Command::Import,
  MeshChat::Command::Base::EXPORT => MeshChat::Command::Share
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_klass = nil) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(input_klass = nil)
  input_klass ||= MeshChat::CLI::Base
  # instantiate the interface with which we are communicated with
  self._input_device = input_klass.new
  # this will allow our listener / server to print exceptions,
  # rather than  silently fail
  Thread.abort_on_exception = true
end

Instance Attribute Details

#_input_deviceObject

Returns the value of attribute _input_device.



44
45
46
# File 'lib/meshchat/cli.rb', line 44

def _input_device
  @_input_device
end

Class Method Details

.create(input_klass) ⇒ Object



53
54
55
# File 'lib/meshchat/cli.rb', line 53

def create(input_klass)
  @instance = new(input_klass)
end

.instanceObject



57
58
59
60
# File 'lib/meshchat/cli.rb', line 57

def instance
  # default input collector
  @instance ||= new
end

Instance Method Details

#check_startup_settingsObject



141
142
143
# File 'lib/meshchat/cli.rb', line 141

def check_startup_settings
  start_server if Settings['autolisten']
end

#close_programObject



145
146
147
# File 'lib/meshchat/cli.rb', line 145

def close_program
  exit
end

#close_serverObject



128
129
130
131
132
133
134
135
# File 'lib/meshchat/cli.rb', line 128

def close_server
  puts 'shutting down server'
  if @server.present?
    server = @server.pop
    server.try(:server).try(:close)
  end
  puts 'no longer listening...'
end

#create_input(msg) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/meshchat/cli.rb', line 88

def create_input(msg)
  handler = Input.create(msg)
  handler.handle
rescue => e
  Display.error e.message
  Display.error e.class.name
  Display.error e.backtrace.join("\n").colorize(:red)
end

#listen_for_commandsObject



73
74
75
# File 'lib/meshchat/cli.rb', line 73

def listen_for_commands
  process_input while true
end

#process_inputObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/meshchat/cli.rb', line 77

def process_input
  msg = _input_device.get_input
  create_input(msg)
rescue SystemExit, Interrupt
  close_program
rescue Exception => e
  Display.error e.class.name
  Display.error e.message.colorize(:red)
  Display.error e.backtrace.join("\n").colorize(:red)
end

#server_locationObject



137
138
139
# File 'lib/meshchat/cli.rb', line 137

def server_location
  Settings.location
end

#shutdownObject

save config and exit



150
151
152
153
154
155
156
# File 'lib/meshchat/cli.rb', line 150

def shutdown
  # close_server
  Display.info 'saving config...'
  Settings.save
  Display.alert "\n\nGoodbye.  \n\nThank you for using #{MeshChat.name}"
  exit
end

#start_serverObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/meshchat/cli.rb', line 97

def start_server

  unless Settings.valid?
    Display.alert("settings not fully valid\n")
    errors = Settings.errors
    errors.each do |error|
      Display.alert(" - #{error}")
    end

    if errors.present?
      Display.info('set these with /config set <field> <value>')
    end

    return
  end

  Thread.abort_on_exception = false
  Thin::Logging.silent = true

  Thread.new do
    MeshChat::Net::Listener::Server.run!(
      port: MeshChat::Settings['port'],
      # logger: MeshChat::Display,
      show_exceptions: true,
      server: :thin,
      dump_errors: true,
      threaded: true
    )
  end
end