Class: XMPP::Client

Inherits:
Object
  • Object
show all
Includes:
Loggable, IQProcessor, StanzaProcessor, Stream
Defined in:
lib/kintara/client.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from IQProcessor

#bind_resource, #fake_session, #get_iq, #process_iq, #set_iq

Methods included from Stream

#authorize, #process_new_stream, #send_features, #send_stream, #stanza_error, #start_tls, #stream_error

Methods included from StanzaProcessor

#process_stanza, #verify_xid

Methods included from Loggable

#log, #log_level=, #logger=

Constructor Details

- (Client) initialize(host, socket) {|_self| ... }

XXX

Yields:

  • (_self)

Yield Parameters:

  • _self (XMPP::Client)

    the object that the method was called on



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kintara/client.rb', line 69

def initialize(host, socket)
    # Is our socket dead?
    @dead = false

    # Our event queue
    @eventq = EventQueue.new

    # Our hostname
    @host = host

    # Our Logger object
    @logger     = nil
    self.logger = nil

    # Our Parser object
    @parser = REXML::Parsers::SAX2Parser.new('')

    # Received data waiting to be parsed
    @recvq = ''

    # Our XMPP::Resource object
    @resource = nil

    # Data waiting to be sent
    @sendq = []

    # Our socket
    @socket = socket

    # Our connection state
    # Either empty, or an array => :tls, :sasl, :bind
    @state = []

    # Our DB user object
    @user = nil

    # The OpenStruct from configuration
    @vhost = nil

    # The current XML stanza we're parsing
    @xml = nil

    # If we have a block let it set up our instance attributes
    yield(self) if block_given?

    @logger.progname = "#@host"

    # Set up event handlers
    set_default_handlers

    # Initialize the parser
    initialize_parser

    log(:debug, "new client from #@host")

    self
end

Instance Attribute Details

- (Object) host (readonly)

instance attributes



61
62
63
# File 'lib/kintara/client.rb', line 61

def host
  @host
end

- (Object) resource (readonly)

instance attributes



61
62
63
# File 'lib/kintara/client.rb', line 61

def resource
  @resource
end

- (Object) socket (readonly)

instance attributes



61
62
63
# File 'lib/kintara/client.rb', line 61

def socket
  @socket
end

Instance Method Details

- (Boolean) dead?

Returns:

  • (Boolean)


234
235
236
# File 'lib/kintara/client.rb', line 234

def dead?
    @dead
end

- (Boolean) has_events?

Returns:

  • (Boolean)


226
227
228
# File 'lib/kintara/client.rb', line 226

def has_events?
    @eventq.needs_ran?
end

- (Boolean) need_write?

Returns:

  • (Boolean)


222
223
224
# File 'lib/kintara/client.rb', line 222

def need_write?
    @sendq.empty? ? false : true
end

- (Object) read

Called when we're ready to read.


returns

self



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/kintara/client.rb', line 243

def read
    begin
        ret = @socket.read_nonblock(8192)
    rescue IO::WaitReadable
        retry
    rescue Exception => e
        ret = nil
    end

    if not ret or ret.empty?
        log(:info, "error from #@host: #{e}") if e
        self.dead = true
        return
    end

    string = ''
    string += "(#{@resource.name}) " if @resource
    string += '-> '
    string += ret.gsub("\n", '')
    log(:debug, string)

    @recvq += ret

    @eventq.post(:recvq_ready)

    self
end

- (Object) run_events



230
231
232
# File 'lib/kintara/client.rb', line 230

def run_events
    @eventq.run
end

- (Object) write

Called when we're ready to write.


returns

self



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/kintara/client.rb', line 276

def write
    # Use shift because we need it to fall off immediately
    while stanza = @sendq.shift
        begin
            @socket.write_nonblock(stanza.to_s)
        rescue IO::WaitReadable
            retry
        rescue Exception => e
            log(:info, "write error on #@host: #{e}")
            @sendq = []
            self.dead = true
            return
        else
            if @resource
                log(:debug, "(#{@resource.name}) <- #{stanza.to_s}")
            else
                log(:debug, "<- #{stanza.to_s}")
            end
        end
    end
end