Class: CheckTaskr::XmppChatAction

Inherits:
JobsAction show all
Includes:
Socket::Constants
Defined in:
lib/check-taskr/task/xmpp_chat.rb

Instance Attribute Summary collapse

Attributes inherited from JobsAction

#name

Instance Method Summary collapse

Methods inherited from JobsAction

setup

Constructor Details

#initialize(options) ⇒ XmppChatAction

Returns a new instance of XmppChatAction.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/check-taskr/task/xmpp_chat.rb', line 27

def initialize(options)
  @name ||= options[:name]
  @ip = options[:ip]
  @port = options[:port] || 5222
  @sjid1 = options[:jid1]
  @password1 = options[:password1]
  @sjid2 = options[:jid2]
  @password2 = options[:password2]
  @error_code = options[:error_code] || @@default_error_code
  @error_msg = options[:error_msg] || @@default_error_msg

  login1
  login2
end

Instance Attribute Details

#client1Object

Returns the value of attribute client1.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def client1
  @client1
end

#client2Object

Returns the value of attribute client2.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def client2
  @client2
end

#error_codeObject

Returns the value of attribute error_code.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def error_code
  @error_code
end

#error_msgObject

Returns the value of attribute error_msg.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def error_msg
  @error_msg
end

#ipObject

Returns the value of attribute ip.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def ip
  @ip
end

#is_failedObject

Returns the value of attribute is_failed.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def is_failed
  @is_failed
end

#jid1Object

Returns the value of attribute jid1.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def jid1
  @jid1
end

#jid2Object

Returns the value of attribute jid2.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def jid2
  @jid2
end

#password1Object

Returns the value of attribute password1.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def password1
  @password1
end

#password2Object

Returns the value of attribute password2.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def password2
  @password2
end

#portObject

Returns the value of attribute port.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def port
  @port
end

#sjid1Object

Returns the value of attribute sjid1.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def sjid1
  @sjid1
end

#sjid2Object

Returns the value of attribute sjid2.



23
24
25
# File 'lib/check-taskr/task/xmpp_chat.rb', line 23

def sjid2
  @sjid2
end

Instance Method Details

#executeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/check-taskr/task/xmpp_chat.rb', line 69

def execute
  log = Log4r::Logger['default']
  log.debug "xmpp action: ip=#{@ip}, port=#{@port}, name=#{@name}"
  hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code }
  begin
    unless @client1.is_connected?
      login1
    end

    unless @client2.is_connected?
      login2
    end

    body = UUID.generate
    message = Jabber::Message::new(@jid2, body).set_type(:normal).set_id('1')
    @client1.send(message)

    sleep(0.2)

    hash[:timestamp] = Time.now.to_i
    unless body.eql?(@message_body)
      hash[:stat] = 3
      hash[:msg] = "在0.2秒内没有收到回应"
    end

  rescue Exception => e
    hash[:stat] = 2
    hash[:timestamp] = Time.now.to_i
    hash[:msg] = "XMPP异常:#{e}"
    log.error hash.to_json
  end
  hash
end

#login1Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/check-taskr/task/xmpp_chat.rb', line 42

def login1
  @jid1 = Jabber::JID.new("#{@sjid1}/check-taskr")
  if !@client1.nil? && @client1.is_connected?
    @client1.close
  end
  @client1 = Jabber::Client.new(@jid1)
  @client1.connect(@ip, @port)
  @client1.auth(@password1)
  @client1.send(Jabber::Presence.new.set_show(:chat).set_status('check-taskr!'))
end

#login2Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/check-taskr/task/xmpp_chat.rb', line 53

def login2
  @jid2 = Jabber::JID.new("#{@sjid2}/check-taskr")
  if !@client2.nil? && @client2.is_connected?
    @client2.close
  end
  @client2 = Jabber::Client.new(@jid2)
  @client2.add_message_callback do |m|
  if m.type != :error
    @message_body = m.body
  end
end
  @client2.connect(@ip, @port)
  @client2.auth(@password2)
  @client2.send(Jabber::Presence.new.set_show(:chat).set_status('check-taskr!'))
end