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.



69
70
71
72
73
74
75
76
# File 'lib/meshchat/cli.rb', line 69

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
56
# File 'lib/meshchat/cli.rb', line 53

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

.get_inputObject



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

def get_input
  instance.get_input
end

.instanceObject



62
63
64
65
# File 'lib/meshchat/cli.rb', line 62

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

Instance Method Details

#check_startup_settingsObject



150
151
152
# File 'lib/meshchat/cli.rb', line 150

def check_startup_settings
  start_server if Settings['autolisten']
end

#close_programObject



154
155
156
# File 'lib/meshchat/cli.rb', line 154

def close_program
  exit
end

#close_serverObject



137
138
139
140
141
142
143
144
# File 'lib/meshchat/cli.rb', line 137

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



97
98
99
100
101
102
103
104
# File 'lib/meshchat/cli.rb', line 97

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

#get_inputObject



93
94
95
# File 'lib/meshchat/cli.rb', line 93

def get_input
  _input_device.get_input
end

#listen_for_commandsObject



78
79
80
# File 'lib/meshchat/cli.rb', line 78

def listen_for_commands
  process_input while true
end

#process_inputObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/meshchat/cli.rb', line 82

def process_input
  msg = 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



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

def server_location
  Settings.location
end

#shutdownObject

save config and exit



159
160
161
162
163
164
165
# File 'lib/meshchat/cli.rb', line 159

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



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
133
134
135
# File 'lib/meshchat/cli.rb', line 106

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