Class: Hsp::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/hsp/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(marketplace, orchestrator, platform_id, platform_secret) ⇒ Agent

Returns a new instance of Agent.



12
13
14
15
16
17
# File 'lib/hsp/agent.rb', line 12

def initialize(marketplace, orchestrator, platform_id, platform_secret)
    self.marketplace = marketplace
    self.orchestrator = orchestrator
    self.platform_id = platform_id
    self.platform_secret = platform_secret
end

Instance Attribute Details

#marketplaceObject

Returns the value of attribute marketplace.



7
8
9
# File 'lib/hsp/agent.rb', line 7

def marketplace
  @marketplace
end

#orchestratorObject

Returns the value of attribute orchestrator.



8
9
10
# File 'lib/hsp/agent.rb', line 8

def orchestrator
  @orchestrator
end

#platform_idObject

Returns the value of attribute platform_id.



9
10
11
# File 'lib/hsp/agent.rb', line 9

def platform_id
  @platform_id
end

#platform_secretObject

Returns the value of attribute platform_secret.



10
11
12
# File 'lib/hsp/agent.rb', line 10

def platform_secret
  @platform_secret
end

Instance Method Details

#run(platform_id, pings = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hsp/agent.rb', line 19

def run(platform_id, pings = false)
    EventMachine.run do
        url = marketplace.websocket_url
        client = SpaceElevator::Client.new(url) do
            puts 'Disconnected. Exiting...'
            EventMachine.stop_event_loop
        end
        client.connect do |msg|
            case msg['type']
            when 'welcome'
                puts 'The server says "welcome".'
                client.subscribe(channel: 'ChatChannel') do |chat|
                    puts "Received Chat Event: #{chat}"
                    if chat['type'] == 'confirm_subscription'
                        puts "Subscription to #{chat['identifier']['channel']} confirmed!"
                        client.publish({channel: 'ChatChannel'}, {subject: 'Hi', text: "What's up, y'all!?!?"})
                    end
                end
                client.subscribe(channel: 'PlatformChannel', platform_id: platform_id) do |m|
                    puts "Received Platform #{platform_id} Event: #{m}"
                    case m['type']
                    when 'confirm_subscription'
                        # We don't need to dispatch this, I suppose.
                    else
                        self.orchestrator.dispatch(m)
                    end
                end
            when 'ping'
                puts 'The server just pinged us.' if pings
            else
                puts msg
            end
        end
    end
end