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/connection.rb,
lib/pdtp/server/dispatcher.rb,
lib/pdtp/server/client_info.rb,
lib/pdtp/server/file_service.rb,
lib/pdtp/server/status_handler.rb,
lib/pdtp/server/file_service_protocol.rb

Overview

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

Defined Under Namespace

Classes: ChunkInfo, ClientInfo, Connection, Dispatcher, FileInfo, FileService, StatusHandler, Transfer, 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)



116
117
118
# File 'lib/pdtp/server.rb', line 116

def debug(message)
  log message, :debug
end

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

Serve files from the given directory



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pdtp/server.rb', line 58

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



76
77
78
# File 'lib/pdtp/server.rb', line 76

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

def enable_status_page(path = '/status')
  log "status at http://#{@addr}:#{@http_port}#{path}"
  http_server.register path, StatusHandler.new(@dispatcher), true
end

#file_service_enabled?Boolean

Is there an internal file service available for this server?

Returns:

  • (Boolean)


71
72
73
# File 'lib/pdtp/server.rb', line 71

def file_service_enabled?
  @file_service_enabled
end

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

Write a message to the server log



110
111
112
113
# File 'lib/pdtp/server.rb', line 110

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

#runObject

Run the PDTP server event loop



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

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