Class: Fargo::Connection::Hub

Inherits:
Base
  • Object
show all
Includes:
Parser, Utils
Defined in:
lib/fargo/connection/hub.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#socket

Attributes included from Publisher

#subscribers

Instance Method Summary collapse

Methods included from Parser

#parse_command_message, #parse_message

Methods included from Utils

#encode_char, #generate_key, #generate_lock

Methods inherited from Base

#connect, #connected?, #initialize, #listen, #open_socket, #write

Methods included from Publisher

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

Constructor Details

This class inherits a constructor from Fargo::Connection::Base

Instance Attribute Details

#hubnameObject (readonly)

Returns the value of attribute hubname.



8
9
10
# File 'lib/fargo/connection/hub.rb', line 8

def hubname
  @hubname
end

Instance Method Details

#disconnectObject



109
110
111
112
113
114
115
116
# File 'lib/fargo/connection/hub.rb', line 109

def disconnect
  if @client_connections
    @client_connections.each &:disconnect
    @client_connections.clear
  end

  super
end

#receive(data) ⇒ Object

See <www.teamfair.info/DC-Protocol.htm> for specifics on the DC protocol



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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/fargo/connection/hub.rb', line 17

def receive data
  message = parse_message data

  case message[:type]
    when :lock 
      @validated = false
      write "$Key #{generate_key message[:lock]}"
    when :hubname
      @hubname = message[:name]
      write "$ValidateNick #{@client.config.nick}" unless @validated
    when :getpass
      write "$MyPass #{@client.password}"
    when :badpass, :hubfull
      Fargo.logger.warn "Disconnecting because of: #{message.inspect}"
      disconnect
    when :hello
      if message[:who] == @client.config.nick
        Fargo.logger.info "Connected to DC Hub #{@hubname} (#{config.address}:#{config.port})"
        @validated = true

        write '$Version 1,0091'
        write '$GetNickList'
        write "$MyINFO $ALL #{@client.config.nick} " +
          "#{@client.description}$ $#{@client.config.speed || 'DSL'}" + 
          "#{@status || 1.chr}$#{@client.config.email}" +
          "$#{@client.share_size}$"
      end
      
    when :connect_to_me
      if !@client.nicks.include?(message[:nick])
        Fargo.logger.info "Invalid connect_to_me request from: #{message[:nick]}"
        return
      end

      @client_connections ||= []
      
      connection = Fargo::Connection::Download.new @client
      connection.config.address = message[:address]
      connection.config.port    = message[:port]
      # we're going to initiate the download
      connection.config.first   = true

      # proxy all messages from them back to the client and delete the
      # connection if necessary
      connection.subscribe { |*args|
        @client.publish *args
        @client_connections.delete connection unless connection.connected?
      }
      
      # establish the connection. This will also listen for data to be
      # read/written
      connection.connect
      
      # keep track of who we're downloading from
      @client_connections << connection
      
    when :search
      # Make sure we received a valid search request
      if message[:searcher].nil? || !@client.nicks.include?(message[:searcher])
        Fargo.logger.info "Invalid search request: #{message.inspect}"
        return
      end
      
      # Let the client handle the results
      @results = @client.search_files message
      
      # Send all the results to the peer. Take care of active/passive
      # connections
      @results.each { |r| 
        if message[:address]
          r.active_send @client.config.nick, message[:ip], message[:port]
        else
          write "$SR #{@client.config.nick} #{r}" 
        end
      }

    when :revconnect
      # TODO: Don't send RevConnectToMe when we're passive and 
      # receiving is passive
      if @client.config.passive
        write "$RevConnectToMe #{@client.config.nick} #{message[:who]}"
      else
        write "$ConnectToMe #{@client.config.nick} #{@client.config.address}:#{@client.config.extport}"
      end
      
    # proxy this message on up the stack if we don't handle it
    else
      @client.publish message[:type], message
      
  end
end