Class: MeshChat::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/cli.rb,
lib/meshchat/cli/irb.rb,
lib/meshchat/cli/who.rb,
lib/meshchat/cli/exit.rb,
lib/meshchat/cli/init.rb,
lib/meshchat/cli/ping.rb,
lib/meshchat/cli/input.rb,
lib/meshchat/cli/share.rb,
lib/meshchat/cli/config.rb,
lib/meshchat/cli/import.rb,
lib/meshchat/cli/listen.rb,
lib/meshchat/cli/server.rb,
lib/meshchat/cli/command.rb,
lib/meshchat/cli/whisper.rb,
lib/meshchat/cli/identity.rb,
lib/meshchat/cli/ping_all.rb,
lib/meshchat/cli/stop_listening.rb

Overview

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

Defined Under Namespace

Classes: Command, Config, Exit, IRB, Identity, Import, Init, Input, Listen, Ping, PingAll, Server, Share, StopListening, Whisper, Who

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  # Set up auto complete
  completion = proc{ |s| self.class.autocompletes.grep(/^#{Regexp.escape(s)}/) }
  Readline.completion_proc = completion

  # this will allow our listener / server to print exceptions,
  # rather than  silently fail
  Thread.abort_on_exception = true
end

Class Method Details

.autocompletesObject

TODO: extract this for sub commands



54
55
56
57
58
# File 'lib/meshchat/cli.rb', line 54

def autocompletes
  commands = COMMAND_MAP.map{ |k, v| "/#{k}" }
  aliases = MeshChat::Node.all.map{ |n| "#{n.alias_name}" }
  commands + aliases
end

.instanceObject



49
50
51
# File 'lib/meshchat/cli.rb', line 49

def instance
  @instance ||= new
end

Instance Method Details

#check_startup_settingsObject



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

def check_startup_settings
  start_server if Settings['autolisten']
end

#close_programObject



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

def close_program
  exit
end

#close_serverObject



134
135
136
137
138
139
140
141
# File 'lib/meshchat/cli.rb', line 134

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

#get_inputObject



98
99
100
# File 'lib/meshchat/cli.rb', line 98

def get_input
  Readline.readline('> ', true)
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 = 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



143
144
145
# File 'lib/meshchat/cli.rb', line 143

def server_location
  Settings.location
end

#shutdownObject

save config and exit



156
157
158
159
160
161
162
# File 'lib/meshchat/cli.rb', line 156

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



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
127
128
129
130
131
132
# File 'lib/meshchat/cli.rb', line 102

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