Class: MonoTalk::Client
- Inherits:
-
Object
- Object
- MonoTalk::Client
- Defined in:
- lib/module/client.rb
Constant Summary collapse
- @@retry =
5
Instance Method Summary collapse
-
#initialize(servicename) ⇒ Client
constructor
A new instance of Client.
- #open(host = @host, port = @port) ⇒ Object
- #write(command_hash) ⇒ Object
Constructor Details
#initialize(servicename) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/module/client.rb', line 8 def initialize(servicename) @io = STDERR servicename = servicename.to_sym @host = Mono::check_local(Mono::HOST[servicename]) @port = Mono::PORT[servicename] @status = :close @io.puts "Connect to #{@host}:#{@port}" (@@retry+1).times do |index| begin open(@host,@port) break rescue if index > @@retry raise "Connection failed." end @io.puts "Connection failed, retrying connect to server... [#{index}]" sleep 1 end end @io.puts "Connection successful." end |
Instance Method Details
#open(host = @host, port = @port) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/module/client.rb', line 32 def open(host=@host,port=@port) if @status == :close @client = TCPSocket.open(host, port) @status = :open end end |
#write(command_hash) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/module/client.rb', line 39 def write(command_hash) open() @client.write(JSON.generate(command_hash)+"\n") @io.puts ret = @client.gets.gsub("$n","\n") @client.close @status = :close @io.puts 'Connection closed.' return ret end |