Module: PDTP::Server::StatusHelper

Included in:
StatusHandler
Defined in:
lib/pdtp/server/status_helper.rb

Overview

Helpers to be called from the DistribuStream status page ERb

Defined Under Namespace

Classes: Cycle

Instance Method Summary collapse

Instance Method Details

#chunks_active(file) ⇒ Object

Number of requested or completed chunks



101
102
103
# File 'lib/pdtp/server/status_helper.rb', line 101

def chunks_active(file)
  file.file_chunks
end

#chunks_completed(file) ⇒ Object

Number of requested chunks that have been completed



96
97
98
# File 'lib/pdtp/server/status_helper.rb', line 96

def chunks_completed(file)
  file.chunks_provided
end

#client_countObject

Number of clients presently connected



28
29
30
31
# File 'lib/pdtp/server/status_helper.rb', line 28

def client_count
  clients = @dispatcher.connections.size - 1
  clients == 1 ? "1 client" : "#{clients} clients"
end

#cycle(first_value, *values) ⇒ Object

Cycle code excerpted from ActionPack and released under the MIT license Copyright © 2004-2006 David Heinemeier Hansson



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pdtp/server/status_helper.rb', line 115

def cycle(first_value, *values)
  if (values.last.instance_of? Hash)
    params = values.pop
    name = params[:name]
  else
    name = "default"
  end
  values.unshift(first_value)

  cycle = get_cycle(name)
  if (cycle.nil? || cycle.values != values)
    cycle = set_cycle(name, Cycle.new(*values))
  end
  return cycle.to_s
end

#downstream_bandwidth(peer) ⇒ Object

Downstream bandwidth of a peer



78
79
80
81
82
83
# File 'lib/pdtp/server/status_helper.rb', line 78

def downstream_bandwidth(peer)
  bandwidth = peer.downstream_bandwidth
  return 'N/A' if bandwidth.nil?
  
  "#{bandwidth / 1024} kBps"
end

#each_file(peer, &block) ⇒ Object

Iterate over all of a peer’s active files



50
51
52
# File 'lib/pdtp/server/status_helper.rb', line 50

def each_file(peer, &block)
  peer.chunk_info.get_file_stats.each(&block)
end

#each_peer(&block) ⇒ Object

Iterate over all connected peers



39
40
41
# File 'lib/pdtp/server/status_helper.rb', line 39

def each_peer(&block)
  @dispatcher.connections.each(&block)
end

#each_transfer(peer) ⇒ Object

Iterate over all of a peer’s active transfers

Raises:

  • (ArgumentError)


44
45
46
47
# File 'lib/pdtp/server/status_helper.rb', line 44

def each_transfer(peer)
  raise ArgumentError, "no block given" unless block_given?
  peer.transfers.each { |_, transfer| yield transfer }
end

#file_path(file) ⇒ Object

Path to a file



91
92
93
# File 'lib/pdtp/server/status_helper.rb', line 91

def file_path(file)
  file.url.sub(/.+?:\/\/.+?\//, '/')
end

#peer_address(peer) ⇒ Object

IP address and port of a peer



64
65
66
67
# File 'lib/pdtp/server/status_helper.rb', line 64

def peer_address(peer)
  host, port = peer.get_peer_info
  "#{host}:#{port}"
end

#peer_name(peer) ⇒ Object

Name of a peer (client ID or file service)



55
56
57
58
59
60
61
# File 'lib/pdtp/server/status_helper.rb', line 55

def peer_name(peer)
  if peer.file_service?
    "<b>File Service</b>"
  else
    peer.client_id
  end
end

#percent_complete(file) ⇒ Object

Percent of a file that has been transferred



106
107
108
# File 'lib/pdtp/server/status_helper.rb', line 106

def percent_complete(file)
  (chunks_completed(file).to_f / chunks_active(file) * 100).to_i
end

#reset_cycle(name = "default") ⇒ Object



131
132
133
134
# File 'lib/pdtp/server/status_helper.rb', line 131

def reset_cycle(name = "default")
  cycle = get_cycle(name)
  cycle.reset unless cycle.nil?
end

#transfer_info(peer, transfer) ⇒ Object

Information about an active transfer



86
87
88
# File 'lib/pdtp/server/status_helper.rb', line 86

def transfer_info(peer, transfer)
  "#{peer == transfer.giver ? 'UP' : 'DOWN'}: id=#{transfer.transfer_id.split('$')[2..3].join(':')}"
end

#upstream_bandwidth(peer) ⇒ Object

Upstream bandwidth of a peer



70
71
72
73
74
75
# File 'lib/pdtp/server/status_helper.rb', line 70

def upstream_bandwidth(peer)
  bandwidth = peer.upstream_bandwidth
  return 'N/A' if bandwidth.nil?
  
  "#{bandwidth / 1024} kBps"
end

#vhostObject

Server’s virtual host name



34
35
36
# File 'lib/pdtp/server/status_helper.rb', line 34

def vhost
  @vhost
end