Class: TorqueBox::Console::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/console/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ "accept-version" => "1.1" }
DEFAULT_HOST =
{ :host => "localhost", :port => 8675 }
DEFAULT_PARAMS =
{ :max_reconnect_attempts => -1, :reliable => false }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_HOST) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
# File 'lib/torquebox/console/client.rb', line 27

def initialize (host = DEFAULT_HOST)
  build_globals(host)
  @client = Stomp::Client.new(@params)
rescue Stomp::Error::MaxReconnectAttempts
  puts "Cannot connect to TorqueBox. Are you sure the server is running?"
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



25
26
27
# File 'lib/torquebox/console/client.rb', line 25

def client
  @client
end

Class Method Details

.connect(host = DEFAULT_HOST) ⇒ Object



34
35
36
# File 'lib/torquebox/console/client.rb', line 34

def self.connect (host = DEFAULT_HOST)
  Client.new(host).run
end

Instance Method Details

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/torquebox/console/client.rb', line 38

def run
  if client
    trap("INT") {
      client.close if client.open?
      puts "Disconnecting console, press enter to exit"
    }
    prompt = "TorqueBox> "
    received_prompt = false
    client.subscribe("/stomplet/console") do |msg|
      if msg.headers['prompt']
        prompt = msg.body
        received_prompt = true
      else
        puts msg.body
      end
    end
    # Since our messaging is async, sleep
    # before displaying the prompt
    while !received_prompt && client.open?
      sleep 0.05
    end
    while client.open? && (input = Readline.readline(prompt, true))
      received_prompt = false
      if input == 'exit' || input == 'quit'
        client.unsubscribe('/stomplet/console')
        client.close
      end
      client.publish("/stomplet/console", input) if client.open?
      while !received_prompt && client.open?
        sleep 0.05 # again with the async
      end
    end
    if client.open?
      client.unsubscribe('/stomplet/console')
    end
    $stderr.puts "Connection closed."
    # Hide any errors printed after we've unsubscribed
    $stderr.close
  end
end