Class: Fargo::Client

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, ActiveSupport::Configurable, Publisher, Supports::Chat, Supports::Downloads, Supports::FileList, Supports::NickList, Supports::Persistence, Supports::Searches, Supports::Timeout, Supports::Uploads
Defined in:
lib/fargo/client.rb

Instance Attribute Summary collapse

Attributes included from Supports::Downloads

#current_downloads, #download_slots, #failed_downloads, #finished_downloads, #open_download_slots, #queued_downloads, #timed_out, #trying

Attributes included from Supports::NickList

#nicks

Attributes included from Publisher

#subscribers

Instance Method Summary collapse

Methods included from Supports::FileList

#file_list, #file_list!

Methods included from Supports::Timeout

#timeout_response

Methods included from Supports::Persistence

#connected_with?, #connection_for, #disconnect_from, #lock_connection_with!, #nicks_connected_with, #setup_connection_cache

Methods included from Supports::Downloads

#clear_failed_downloads, #clear_finished_downloads, #download, #lock_next_download!, #remove_download, #retry_download, #try_again

Methods included from Supports::Searches

#remove_search, #search, #search_results, #searches

Methods included from Supports::NickList

#has_slot?, #info, #subscribe_to_nicks

Methods included from Supports::Uploads

#open_slots, #share_size

Methods included from Supports::Chat

#messages, #messages_with, #subscribe_to_chats

Methods included from Publisher

#publish, #subscribe, #subscribed_to?, #unsubscribe

Constructor Details

#initializeClient

Returns a new instance of Client.



47
48
49
# File 'lib/fargo/client.rb', line 47

def initialize
  @connection_timeout_threads = {}
end

Instance Attribute Details

#active_serverObject (readonly)

Returns the value of attribute active_server.



45
46
47
# File 'lib/fargo/client.rb', line 45

def active_server
  @active_server
end

#hubObject (readonly)

Returns the value of attribute hub.



45
46
47
# File 'lib/fargo/client.rb', line 45

def hub
  @hub
end

#searcherObject (readonly)

Returns the value of attribute searcher.



45
46
47
# File 'lib/fargo/client.rb', line 45

def searcher
  @searcher
end

Instance Method Details

#connectObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fargo/client.rb', line 100

def connect
  setup if hub.nil?

  # connect all our associated servers
  hub.connect

  unless config.passive
    searcher.connect
    active_server.connect
  end

  true
end

#connect_with(nick) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fargo/client.rb', line 82

def connect_with nick
  @connection_timeout_threads[nick] = Thread.start do
    sleep 10
    connection_timeout! nick
  end

  if config.passive
    hub.write "$RevConnectToMe #{self.config.nick} #{nick}"
  else
    hub.write "$ConnectToMe #{nick} #{config.address}:#{config.active_port}"
  end
end

#connected?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/fargo/client.rb', line 114

def connected?
  hub.try :connected?
end

#connected_with!(nick) ⇒ Object



95
96
97
98
# File 'lib/fargo/client.rb', line 95

def connected_with! nick
  return unless @connection_timeout_threads.has_key?(nick)
  @connection_timeout_threads.delete(nick).exit
end

#descriptionObject



149
150
151
# File 'lib/fargo/client.rb', line 149

def description
  "<++ V:#{config.version},M:#{config.passive ? 'P' : 'A'},H:1/0/0,S:#{open_slots},Dt:1.2.0/W>"
end

#disconnectObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/fargo/client.rb', line 118

def disconnect
  return if hub.nil?

  Fargo.logger.info "Disconnecting from hub."
  hub.disconnect
  unless config.passive
    searcher.disconnect
    active_server.disconnect
  end
end

#get_info(nick) ⇒ Object



74
75
76
# File 'lib/fargo/client.rb', line 74

def get_info nick
  hub.write "$GetINFO #{nick} #{config.nick}"
end

#get_ip(*nicks) ⇒ Object



78
79
80
# File 'lib/fargo/client.rb', line 78

def get_ip *nicks
  hub.write "$UserIP #{nicks.flatten.join '$$'}"
end

#search_files(options) ⇒ Object

see hub/parser#@@search for what’s passed in searches this client’s files based on those options and returns an array of SearchResult(s)



144
145
146
147
# File 'lib/fargo/client.rb', line 144

def search_files options
  # TODO: implement me
  []
end

#search_hub(query) ⇒ Object

Raises:



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fargo/client.rb', line 129

def search_hub query
  raise ConnectionError.new('Not connected Yet!') if hub.nil?

  if config.passive
    location = "Hub:#{config.nick}"
  else
    location = "#{config.address}:#{config.search_port}"
  end

  hub.write "$Search #{location} #{query.to_s}"
end

#setupObject

Don’t do this in initialization so we have time to set all the options



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

def setup
  @hub = Fargo::Connection::Hub.new self
  @hub.config.port    = config.hub_port if config.hub_port
  @hub.config.address = config.hub_address if config.hub_address

  unless config.passive
    # TODO: get this working again
    # Always create a search connection for this.
    # searcher_options = new_options.merge :port => search_port,
    #                                      :connection => Fargo::Connection::Search
    # @searcher    = Fargo::Server.new searcher_options
    #
    # # For now, being active means that you can only download things. Always make a
    # # connection which downloads things.
    # active_options     = new_options.merge :port => active_port,
    #                                        :connection => Fargo::Connection::Download
    # @active_server = Fargo::Server.new active_options
  end

  run_callbacks :setup
end