summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/http/webrick.rb
blob: 00d13437c00a4b6e145e4b2f650283dc97c197f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
require 'webrick'
require 'webrick/https'

class Puppet::Network::HTTP::WEBrick
    def initialize(args = {})
        @listening = false
    end
    
    def listen(args = {})
        raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].empty?
        raise ArgumentError, ":protocols must be specified." if !args[:protocols] or args[:protocols].empty?
        raise ArgumentError, ":address must be specified." unless args[:address]
        raise ArgumentError, ":port must be specified." unless args[:port]
        raise "WEBrick server is already listening" if listening?
        
        @protocols = args[:protocols]
        @handlers = args[:handlers]
        
        setup_handlers
        
        @server = WEBrick::HTTPServer.new(:BindAddress => args[:address], :Port => args[:port])
        
        # TODO / FIXME is this really necessary? -- or can we do it in both mongrel and webrick?
        Puppet.newservice(@server)
        Puppet.start
        
        @listening = true
    end
    
    def unlisten
        raise "WEBrick server is not listening" unless listening?
        shutdown
        @listening = false
    end
    
    def listening?
        @listening
    end
    
  private
    
    def setup_handlers
        @handlers.each do |handler|
            @protocols.each do |protocol|
                class_for_protocol_handler(protocol, handler).new
            end
        end
    end
    
    def class_for_protocol_handler(protocol, handler)
        Class.new
    end
end