Class: HTTPProvider

Inherits:
Object
  • Object
show all
Includes:
WEBrick
Defined in:
lib/appswarm/http/http_provider.rb

Overview

provides direct access to web-servlets

Constant Summary collapse

@@mutex =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, port = 80) ⇒ HTTPProvider

Returns a new instance of HTTPProvider.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/appswarm/http/http_provider.rb', line 15

def initialize(app,port=80)
  @@mutex.synchronize do
    @app=app
    @@webricks||={}
    if @@webricks[port]
      raise HTTPProviderException.new("Webrick on #{port} already exists!")
    end
    @s = HTTPServer.new(
     :Port => port,
     :DocumentRoot=>File.expand_path("../http/static",__FILE__),
     :BindAddress=>"127.0.0.1",
     :Logger=>WebrickLogger.new(@app)
    )
    @@webricks[port]=self
    @started=false
    
  end
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



11
12
13
# File 'lib/appswarm/http/http_provider.rb', line 11

def thread
  @thread
end

Instance Method Details

#add(app, controller, controllerName = nil) ⇒ Object



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
# File 'lib/appswarm/http/http_provider.rb', line 34

def add(app,controller,controllerName=nil)
  controllerName||="/"+File.split(controller)[1].gsub(/\.rb/,"")
  puts "MOUNT #{controllerName}"
  
  @s.mount_proc(controllerName){|req, res|
    puts "REQUEST <#{req.path_info}>"
    load controller if $DEBUGGING
    listenerClass=getClass(controller.gsub(/.*\//,"").gsub(".rb","").camelCase)
    l=listenerClass.new(app)
    func=req.path_info[1..-1]
    l.params=req.query
    pp "REQ::::::",req
    args=req.post
    begin
      l.run(res,func,args)
    rescue HTTPController::UnknownAction=>e
      
      puts "TRY FIND FILE"
      # try to find a file
      path=func.to_s
      path.gsub!(/^(\.\.\/)*/,"")
      raise e unless l.render_static(res,path)
    end
  }
  
end

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/appswarm/http/http_provider.rb', line 61

def run
  @thread=Thread.new {
    trap("INT"){ @s.shutdown }
    puts "Starting Webrick #{@s}"
    unless @started
      @started=true
      begin 
        @s.start
      rescue WEBrick::HTTPStatus::EOFError=>e
        pp e,e.backtrace
      rescue WEBrick::HTTPStatus::NotFound=>e
        pp e,e.backtrace
      end
    end 
  }
end

#stopObject



77
78
79
# File 'lib/appswarm/http/http_provider.rb', line 77

def stop
  @thread.kill
end