Class: Ubiquity::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/protocol.rb

Instance Method Summary collapse

Constructor Details

#initialize(remote_ip = 'localhost', port = 1978) ⇒ Client

Returns a new instance of Client.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ubiquity/protocol.rb', line 40

def initialize remote_ip='localhost', port=1978
    @hooks = []
    @client = TCPSocket.new remote_ip, port
    Thread.new {
        while line = @client.gets
            if line =~ /NOTIFY:(.+)/
                notify_msg = $1.gsub("\n", "")
                @hooks.each do |h|
                    if notify_msg == h[:hook]
                        h[:cb].call
                    end
                end
            elsif line =~ /PUTS:(.+)/
                puts $1
            end

        end
    }
end

Instance Method Details

#exec(&block) ⇒ Object



60
61
62
63
# File 'lib/ubiquity/protocol.rb', line 60

def exec &block
    a = block.to_source
    @client.puts ({:cmd => a}.to_json)
end

#on(msg, &block) ⇒ Object



65
66
67
# File 'lib/ubiquity/protocol.rb', line 65

def on msg, &block
    @hooks << {:hook => msg, :cb => block}
end