Class: Jabber::Protocol::Connection

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

Overview

The connection class encapsulates the connection to the Jabber service including managing the socket and controlling the parsing of the Jabber XML stream.

Constant Summary collapse

DISCONNECTED =
1
CONNECTED =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 5222) ⇒ Connection

Returns a new instance of Connection.



62
63
64
65
66
67
68
69
# File 'lib/jabber4r/protocol.rb', line 62

def initialize(host, port=5222)
  @host = host
  @port = port
  @status = DISCONNECTED
  @filters = {}
  @threadBlocks = {}
  @pollCounter = 10
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#add_filter(ref, proc = nil, &block) ⇒ Object

Adds a filter block/proc to process received XML messages

xml
String

The xml data to send

proc
Proc = nil

The optional proc

&block
Block

The optional block



197
198
199
200
201
# File 'lib/jabber4r/protocol.rb', line 197

def add_filter(ref, proc=nil, &block)
  block = proc if proc
  raise "Must supply a block or Proc object to the addFilter method" if block.nil?
  @filters[ref] = block
end

#closeObject

Closes the connection to the Jabber service



210
211
212
213
214
215
# File 'lib/jabber4r/protocol.rb', line 210

def close
  @parserThread.kill if @parserThread
  @pollThread.kill
  @socket.close if @socket
  @status = DISCONNECTED
end

#connectObject

Connects to the Jabber server through a TCP Socket and starts the Jabber parser.



75
76
77
78
79
80
81
# File 'lib/jabber4r/protocol.rb', line 75

def connect
  @socket = TCPSocket.new(@host, @port)
  @parser = Jabber::Protocol.Parser.new(@socket, self)
  @parserThread = Thread.new {@parser.parse}
  @pollThread = Thread.new {poll}
  @status = CONNECTED
end

#delete_filter(ref) ⇒ Object



203
204
205
# File 'lib/jabber4r/protocol.rb', line 203

def delete_filter(ref)
  @filters.delete(ref)
end

#is_connected?Boolean

Returns if this connection is connected to a Jabber service

return
Boolean

Connection status

Returns:

  • (Boolean)


101
102
103
# File 'lib/jabber4r/protocol.rb', line 101

def is_connected?
  return @status == CONNECTED
end

#is_disconnected?Boolean

Returns if this connection is NOT connected to a Jabber service

return
Boolean

Connection status

Returns:

  • (Boolean)


110
111
112
# File 'lib/jabber4r/protocol.rb', line 110

def is_disconnected?
  return @status == DISCONNECTED
end

#on_connection_exception(&block) ⇒ Object

Mounts a block to handle exceptions if they occur during the poll send. This will likely be the first indication that the socket dropped in a Jabber Session.



88
89
90
# File 'lib/jabber4r/protocol.rb', line 88

def on_connection_exception(&block)
  @exception_block = block
end

#parse_failureObject



92
93
94
# File 'lib/jabber4r/protocol.rb', line 92

def parse_failure
  Thread.new {@exception_block.call if @exception_block}
end

#pollObject

Starts a polling thread to send “keep alive” data to prevent the Jabber connection from closing for inactivity.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/jabber4r/protocol.rb', line 174

def poll
  sleep 10
  while true
    sleep 2
    @pollCounter = @pollCounter - 1
    if @pollCounter < 0
      begin
        send("  \t  ")
      rescue
        Thread.new {@exception_block.call if @exception_block}
        break
      end
    end
  end
end

#receive(element) ⇒ Object

Processes a received ParsedXMLElement and executes registered thread blocks and filters against it.

element
ParsedXMLElement

The received element



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jabber4r/protocol.rb', line 120

def receive(element)
  while @threadBlocks.size==0 && @filters.size==0
    sleep 0.1
  end        
  Jabber::DEBUG && puts("RECEIVED:\n#{element.to_s}")
  @threadBlocks.each do |thread, proc|
    begin
      proc.call(element)
      if element.element_consumed?
        @threadBlocks.delete(thread)
        thread.wakeup if thread.alive?
        return
      end
    rescue Exception => error
      puts error.to_s
      puts error.backtrace.join("\n")
    end
  end
  @filters.each_value do |proc|
    begin
      proc.call(element)
      return if element.element_consumed?
    rescue Exception => error
      puts error.to_s
      puts error.backtrace.join("\n")
    end
  end
end

#send(xml, proc = nil, &block) ⇒ Object

Sends XML data to the socket and (optionally) waits to process received data.

xml
String

The xml data to send

proc
Proc = nil

The optional proc

&block
Block

The optional block



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jabber4r/protocol.rb', line 157

def send(xml, proc=nil, &block)
  Jabber::DEBUG && puts("SENDING:\n#{ xml.kind_of?(String) ? xml : xml.to_s }")
  xml = xml.to_s if not xml.kind_of? String
  block = proc if proc
  @threadBlocks[Thread.current]=block if block
  begin
    @socket << xml
  rescue
    raise JabberConnectionException.new(true, xml)
  end
  @pollCounter = 10
end