Class: SPSPub

Inherits:
Object
  • Object
show all
Defined in:
lib/sps-pub.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: 'sps', address: host, port: '59000') ⇒ SPSPub

Returns a new instance of SPSPub.



14
15
16
17
18
19
20
# File 'lib/sps-pub.rb', line 14

def initialize(host: 'sps', address: host, port: '59000')
  
  
  @client = WebSocket::Client::Simple.connect "ws://%s:%s/" % [address, port]    
  sleep 0.02

end

Class Method Details

.notice(s, hostx = 'sps', host: hostx, address: host, port: '59000', retries: 3, hosts: []) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sps-pub.rb', line 28

def self.notice(s, hostx='sps', host: hostx, address: host, 
                port: '59000', retries: 3, hosts: [])


  ws = nil
  begin

    if hosts.any? then
      address, portx = hosts.first.split(':', 2)

      portx ||= port
      port = portx
    end

    ws = WebSocket::Client::Simple.connect "ws://%s:%s/" % [address, port]
  rescue Errno::ECONNREFUSED => e
    
    if hosts.any? then
      hosts.rotate!
      puts 'retrying ...' + hosts.inspect
      sleep 0.1
      #self.notice s, hosts: hosts.rotate
      retry
    else
      raise SPSPubError, "connection refused"
    end
    
  end
   
  ws.on :open do    
    
    retry_attempts = 0      
    
    begin
      
      ws.send s
    
    #exception

    rescue Exception => e
      puts 'dig it'
    rescue Errno::ETIMEDOUT => e
      
      if retries and retry_attempts < retries then
        
        retries += 1
        puts 'SPSPub timeout retrying ...'
        retry
        
      else
        puts 'SPSPub timeout while trying to contact the host'        
      end
      

    end
  end    
  
  sleep 0.01 # give the above message time to be sent
  
  :message_sent

end

.pub(s, address: 'sps', host: address, port: '59000') ⇒ Object



91
92
93
# File 'lib/sps-pub.rb', line 91

def self.pub(s, address: 'sps', host: address, port: '59000')
  self.notice(s, address: 'sps', port: '59000')
end

.publish(s, address: 'sps', host: address, port: '59000') ⇒ Object



95
96
97
# File 'lib/sps-pub.rb', line 95

def self.publish(s, address: 'sps', host: address, port: '59000')
  self.notice(s, address: 'sps', port: '59000')
end

Instance Method Details

#notice(s) ⇒ Object



22
23
24
# File 'lib/sps-pub.rb', line 22

def notice(s)
  @client.send s
end

#publish ⇒ Object



26
27
28
# File 'lib/sps-pub.rb', line 26

def notice(s)
  @client.send s
end