Class: Vetinari::Dcc::ServerManager

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/vetinari/dcc/server_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ ServerManager

Returns a new instance of ServerManager.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vetinari/dcc/server_manager.rb', line 8

def initialize(bot)
  @bot = bot
  @external_ip = @bot.config.dcc.external_ip

  ip = Socket.ip_address_list.detect { |i| i.ipv4_private? }
  @internal_ip = ip ? ip.ip_address : '0.0.0.0'

  @ports = @bot.config.dcc.ports
  @queue = []
  @mutex = Mutex.new
  @running_servers = {} # {port => #<Vetinari::Dcc::Server>}
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



6
7
8
# File 'lib/vetinari/dcc/server_manager.rb', line 6

def bot
  @bot
end

#external_ipObject (readonly)

Returns the value of attribute external_ip.



6
7
8
# File 'lib/vetinari/dcc/server_manager.rb', line 6

def external_ip
  @external_ip
end

#internal_ipObject (readonly)

Returns the value of attribute internal_ip.



6
7
8
# File 'lib/vetinari/dcc/server_manager.rb', line 6

def internal_ip
  @internal_ip
end

Instance Method Details

#add_offering(user, filepath, filename) ⇒ Object



21
22
23
24
25
26
# File 'lib/vetinari/dcc/server_manager.rb', line 21

def add_offering(user, filepath, filename)
  server = Server.new(user, filepath, filename, Actor.current)
  @mutex.synchronize { @queue << server }
  async.start_sending
  server
end

#release_port(port) ⇒ Object



46
47
48
49
50
# File 'lib/vetinari/dcc/server_manager.rb', line 46

def release_port(port)
  @mutex.synchronize do
    @running_servers.delete(port)
  end
end

#start_sendingObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vetinari/dcc/server_manager.rb', line 28

def start_sending
  @mutex.synchronize do
    if @queue.any?
      port = get_available_port

      if port
        server = @queue.pop
        server.async.run(port)
        @running_servers[port] = server
      else
        # There are items in the queue but for some reasons no ports are
        # available. Try again later.
        after(3) { start_sending }
      end
    end
  end
end