Class: Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb

Direct Known Subclasses

Download, Upload

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filetransfer, peer, filename, filesize, msgblock) ⇒ Transfer

Returns a new instance of Transfer.



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
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 24

def initialize(filetransfer, peer, filename, filesize, msgblock)
  @filetransfer = filetransfer
  @peer = peer
  @filename = filename
  @filesize = filesize
  @msgblock = msgblock

  @done = false
  @bytes = 0
  @stats = [0]
  @stats_lock = Mutex.new
  @stats_thread = Thread.new { stats_loop }


  @streamhost_cb = lambda { |streamhost,state,e|
    case state
      when :connecting
        say "Connecting to #{streamhost.jid} (#{streamhost.host}:#{streamhost.port})"
      when :success
        say "Successfully using #{streamhost.jid} (#{streamhost.host}:#{streamhost.port})"
      when :failure
        say "Error using #{streamhost.jid} (#{streamhost.host}:#{streamhost.port}): #{e}"
    end
  }
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



22
23
24
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 22

def bytes
  @bytes
end

#filenameObject (readonly)

Returns the value of attribute filename.



22
23
24
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 22

def filename
  @filename
end

#filesizeObject (readonly)

Returns the value of attribute filesize.



22
23
24
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 22

def filesize
  @filesize
end

#peerObject (readonly)

Returns the value of attribute peer.



22
23
24
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 22

def peer
  @peer
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 50

def done?
  @stats_thread.kill if @done
  @done
end

#say(text) ⇒ Object



75
76
77
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 75

def say(text)
  @msgblock.call(text)
end

#statsObject



65
66
67
68
69
70
71
72
73
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 65

def stats
  @stats_lock.synchronize {
    if @stats.first >= @stats.last
      0
    else
      (@stats.last - @stats.first) / (@stats.size - 1)
    end
  }
end

#stats_loopObject



55
56
57
58
59
60
61
62
63
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 55

def stats_loop
  loop {
    sleep 1
    @stats_lock.synchronize {
      @stats.push @bytes
      @stats.shift if @stats.size > 5
    }
  }
end

#transfer(from, to) ⇒ Object



79
80
81
82
83
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 79

def transfer(from, to)
  while buf = from.read
    @bytes += to.write buf
  end
end