Class: PDTP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/pdtp/server.rb,
lib/pdtp/server/trust.rb,
lib/pdtp/server/transfer.rb,
lib/pdtp/server/file_info.rb,
lib/pdtp/server/chunk_info.rb,
lib/pdtp/server/connection.rb,
lib/pdtp/server/dispatcher.rb,
lib/pdtp/server/file_service.rb,
lib/pdtp/server/status_helper.rb,
lib/pdtp/server/status_handler.rb,
lib/pdtp/server/transfer_manager.rb,
lib/pdtp/server/bandwidth_estimator.rb,
lib/pdtp/server/file_service_protocol.rb,
lib/pdtp/server/file_service_connection.rb

Overview

PDTP::Server provides an interface for creating a PDTP server

Defined Under Namespace

Modules: FileServiceConnection, StatusHelper Classes: BandwidthEstimator, ChunkInfo, Connection, Dispatcher, FileInfo, FileService, StatusHandler, Transfer, TransferManager, Trust

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, pdtp_port = DEFAULT_PORT, http_port = pdtp_port + 1) ⇒ Server

Create a new PDTP::Server which will listen on the given address and port



43
44
45
46
47
48
49
# File 'lib/pdtp/server.rb', line 43

def initialize(addr, pdtp_port = DEFAULT_PORT, http_port = pdtp_port + 1)
  @orig_addr, @port, @http_port = addr, pdtp_port, http_port
  
  @addr = IPSocket.getaddress(@orig_addr)
  @file_service = FileService.new
  @dispatcher = Dispatcher.new self, @file_service
end

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



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

def addr
  @addr
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#debug(message) ⇒ Object

Write a debug message to the server log (ignored in quiet mode)



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

def debug(message)
  log message, :debug
end

#enable_file_service(path, options = {}) ⇒ Object

Serve files from the given directory



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pdtp/server.rb', line 74

def enable_file_service(path, options = {})
  opts = { 
    :chunk_size => 100000,
    :vhost => @orig_addr
  }.merge(options)
  
  @file_service.root = path
  @file_service.default_chunk_size = opts[:chunk_size]
  @vhost = opts[:vhost]
  @file_service_enabled = true
end

#enable_logging(logger = nil) ⇒ Object

Enable logging



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

def enable_logging(logger = nil)
  @log = logger || default_logger
end

#enable_status_page(path = '/status') ⇒ Object

Run a web server to display statistics on the given address and port



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pdtp/server.rb', line 52

def enable_status_page(path = '/status')
  # Strip trailing slash (if present)
  path.sub!(/\/$/, '')
  
  log "status at http://#{@addr}:#{@http_port}#{path}/"
  
  # Redirect requests without a trailing slash
  http_server.register path, Mongrel::RedirectHandler.new(path + '/')
  
  # Register the status handler
  http_server.register path + '/', StatusHandler.new(@orig_addr, @dispatcher), true
  
  # Serve status images
  images = Mongrel::DirHandler.new(File.dirname(__FILE__) + '/../../status/images', false)
  http_server.register path + '/images', images
  
  # Serve status stylesheets
  styles = Mongrel::DirHandler.new(File.dirname(__FILE__) + '/../../status/stylesheets', false)
  http_server.register path + '/stylesheets', styles
end

#file_service_enabled?Boolean

Is there an internal file service available for this server?

Returns:

  • (Boolean)


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

def file_service_enabled?
  @file_service_enabled
end

#log(message, type = :info) ⇒ Object

Write a message to the server log



126
127
128
129
# File 'lib/pdtp/server.rb', line 126

def log(message, type = :info)
  return unless @log
  @log.send type, message
end

#runObject

Run the PDTP server event loop



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
# File 'lib/pdtp/server.rb', line 97

def run
  EventMachine.run do
    EventMachine.start_server(@addr, @port, Connection) do |connection|
      connection.dispatcher = @dispatcher    
      connection.connection_completed
    end
    
    # Enable the file service if #enable_file_service has been called
    if file_service_enabled?
      EventMachine.connect(@addr, @port, PDTP::Server::FileService::Protocol) do |c|
        # In future versions of EventMachine we'll be able to pass these as parameters to EM.connect
        base_path, listen_port, vhost, server = @file_service.root, @http_port, @vhost, http_server
        
        c.instance_eval do
          @base_path, @listen_port, @vhost, @http_server = base_path, listen_port, vhost, server
        end
      end
    end

    log "accepting connections on #{@addr}:#{@port}"
    
    # Start Mongrel in Evented mode if it's been requested
    http_server.run_evented if @http_server

    EventMachine.add_periodic_timer(2) { @dispatcher.clear_all_stalled_transfers }
  end
end