Class: PDTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pdtp/client.rb,
lib/pdtp/client/transfer.rb,
lib/pdtp/client/callbacks.rb,
lib/pdtp/client/connection.rb,
lib/pdtp/client/file_buffer.rb,
lib/pdtp/client/http_client.rb,
lib/pdtp/client/file_service.rb,
lib/pdtp/client/http_handler.rb

Overview

PDTP::Client provides an interface for accessing resources on PDTP servers

Defined Under Namespace

Modules: Transfer Classes: Callbacks, Connection, FileBuffer, FileInfo, FileService, HttpClient, HttpHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = PDTP::DEFAULT_PORT, options = {}) ⇒ Client

Create a PDTP::Client object for accessing the PDTP server at the given host and port



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pdtp/client.rb', line 41

def initialize(host, port = PDTP::DEFAULT_PORT, options = {})
  @host, @port = host, port
        
  @listen_addr = options[:listen_addr] || '0.0.0.0'
  @listen_port = options[:listen_port] || 60860
  
  @file_service = PDTP::Client::FileService.new
  @transfers = []

  # Start a Mongrel server on the specified port.  If it isnt available, keep trying higher ports
  # FIXME: Evented Mongrel won't throw an exception if the port is unavailable.  This needs to
  # be dealt with through EventMachine
  #begin
    @http_server = Mongrel::HttpServer.new @listen_addr, @listen_port
  #rescue
  #  @listen_port += 1
  #  retry
  #end

  @client_id = Digest::MD5.hexdigest "#{Time.now.to_f}#{$$}"

  #@@log.info "listening on port #{@listen_port}"
  @http_handler = HttpHandler.new(self)
  @http_server.register '/', @http_handler
end

Instance Attribute Details

#client_idObject (readonly)

None of these should be publically accessible, and will be factored away in time



38
39
40
# File 'lib/pdtp/client.rb', line 38

def client_id
  @client_id
end

#connectionObject (readonly)

None of these should be publically accessible, and will be factored away in time



38
39
40
# File 'lib/pdtp/client.rb', line 38

def connection
  @connection
end

#file_serviceObject (readonly)

None of these should be publically accessible, and will be factored away in time



38
39
40
# File 'lib/pdtp/client.rb', line 38

def file_service
  @file_service
end

#listen_portObject (readonly)

None of these should be publically accessible, and will be factored away in time



38
39
40
# File 'lib/pdtp/client.rb', line 38

def listen_port
  @listen_port
end

#transfersObject (readonly)

None of these should be publically accessible, and will be factored away in time



38
39
40
# File 'lib/pdtp/client.rb', line 38

def transfers
  @transfers
end

Instance Method Details

#connect(callbacks = nil) ⇒ Object

Connect to the PDTP server. This is a blocking call which runs the client event loop



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdtp/client.rb', line 68

def connect(callbacks = nil)
  callbacks = Callbacks.new if callbacks.nil?
  
  unless callbacks.is_a?(Callbacks)
    raise ArgumentError, "callbacks must be an instance of PDTP::Client::Callbacks"
  end
            
  # Run the EventMachine reactor loop
  EventMachine.run do
    @http_server.run_evented
    
    @connection = EventMachine.connect(@host, @port, Connection) do |c|
      # Set connection variables and configure callbacks
      c.client = self
      c.callbacks = callbacks
    end

    #@@log.info "connecting with ev=#{EventMachine::VERSION}"
    #@@log.info "host= #{host} port=#{opts[:port]}"
  end
end

#connected?Boolean

Are we currently connected to a server?

Returns:

  • (Boolean)


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

def connected?
  not @connection.nil?
end

#get(path, io, options = {}) ⇒ Object

Retrieve the resource at the given path and write it to the given IO object

Raises:

  • (RuntimeError)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pdtp/client.rb', line 96

def get(path, io, options = {})
  raise RuntimeError, "not connected to server yet" unless connected?
  
  path = '/' + path unless path[0] == ?/
  url = "http://#{@host}#{path}"
  filename = path.split('/').last
  
  # Register the file and its IO object with the local file service
  file_service.set_info url, FileInfo.new(filename, io)
  
  # Ask the server for some information on the file we want
  @connection.send_message :ask_info, :url => url

  # Request the file (should probably be done after receiving :tell_info)
  @connection.send_message :request, :url => url

  #@@log.info "This client is requesting"
end

#stopObject

Stop the client event loop. This only works within callbacks given to the #connect method



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

def stop
  EventMachine.stop_event_loop
end