Class: ScripTTY::RequestDispatcher
- Inherits:
-
Object
- Object
- ScripTTY::RequestDispatcher
- Defined in:
- lib/scriptty/request_dispatcher.rb
Overview
Request dispatcher thread
RequestDispatcher allows a single ScripTTY instance to maintain a single, persistent connection to a remote host while multiple threads perform requests via that connection.
RequestDispatcher can be used, for example, to provide an HTTP interface to functions of a screen-scraped terminal.
Instance Method Summary collapse
-
#after_each_request(how = nil, &block) ⇒ Object
Add a block that will be called before each request is performed.
-
#after_init(how = nil, &block) ⇒ Object
Specify a block that will be called every time a new ScripTTY::Expect object is initialized.
-
#before_each_request(how = nil, &block) ⇒ Object
Add a block that will be called before each request is performed.
-
#before_finish(how = nil, &block) ⇒ Object
Add a block that will be called just before finally disconnecting, when the finish method is called.
-
#finish ⇒ Object
Stop the dispatcher thread.
-
#initialize ⇒ RequestDispatcher
constructor
A new instance of RequestDispatcher.
-
#request(how = nil, &block) ⇒ Object
Queue a request and wait for it to complete.
-
#start ⇒ Object
Start the dispatcher thread.
Constructor Details
#initialize ⇒ RequestDispatcher
Returns a new instance of RequestDispatcher.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/scriptty/request_dispatcher.rb', line 36 def initialize # Graceful shutdown flag @finishing_lock = Mutex.new @finishing = false # Request queue @queue_lock = Mutex.new @queue = [] # Hooks @hooks_lock = Mutex.new @hooks = {} end |
Instance Method Details
#after_each_request(how = nil, &block) ⇒ Object
Add a block that will be called before each request is performed.
See the add_hook method for a descripton of how the arguments are interpreted.
76 77 78 |
# File 'lib/scriptty/request_dispatcher.rb', line 76 def after_each_request(how=nil, &block) add_hook(:after_each_request, how, &block) end |
#after_init(how = nil, &block) ⇒ Object
Specify a block that will be called every time a new ScripTTY::Expect object is initialized.
This can be used to specify a transcript_writer or to execute a login script.
See the add_hook method for a descripton of how the arguments are interpreted.
55 56 57 |
# File 'lib/scriptty/request_dispatcher.rb', line 55 def after_init(how=nil, &block) add_hook(:after_init, how, &block) end |
#before_each_request(how = nil, &block) ⇒ Object
Add a block that will be called before each request is performed.
See the add_hook method for a descripton of how the arguments are interpreted.
69 70 71 |
# File 'lib/scriptty/request_dispatcher.rb', line 69 def before_each_request(how=nil, &block) add_hook(:before_each_request, how, &block) end |
#before_finish(how = nil, &block) ⇒ Object
Add a block that will be called just before finally disconnecting, when the finish method is called.
See the add_hook method for a descripton of how the arguments are interpreted.
62 63 64 |
# File 'lib/scriptty/request_dispatcher.rb', line 62 def before_finish(how=nil, &block) add_hook(:before_finish, how, &block) end |
#finish ⇒ Object
Stop the dispatcher thread
88 89 90 91 92 |
# File 'lib/scriptty/request_dispatcher.rb', line 88 def finish @finishing_lock.synchronize{ @finishing = true } @thread.join nil end |
#request(how = nil, &block) ⇒ Object
Queue a request and wait for it to complete
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/scriptty/request_dispatcher.rb', line 95 def request(how=nil, &block) cv_mutex = Mutex.new cv = ConditionVariable.new # Build the request request = {:block => block, :cv_mutex => cv_mutex, :cv => cv } # Queue the request @queue_lock.synchronize{ @queue << request } # Wait for the request to complete cv_mutex.synchronize{ cv.wait(cv_mutex) } # Raise an exception if there was any. raise request[:exception] if request[:exception] # Return the result request[:result] end |
#start ⇒ Object
Start the dispatcher thread
81 82 83 84 85 |
# File 'lib/scriptty/request_dispatcher.rb', line 81 def start raise ArgumentError.new("Already started") if @thread @thread = Thread.new{ main } nil end |