Class: ScripTTY::RequestDispatcher

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Classes: RequestCancelled

Instance Method Summary collapse

Constructor Details

#initializeRequestDispatcher

Returns a new instance of RequestDispatcher.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scriptty/request_dispatcher.rb', line 39

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.



79
80
81
# File 'lib/scriptty/request_dispatcher.rb', line 79

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.



58
59
60
# File 'lib/scriptty/request_dispatcher.rb', line 58

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.



72
73
74
# File 'lib/scriptty/request_dispatcher.rb', line 72

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.



65
66
67
# File 'lib/scriptty/request_dispatcher.rb', line 65

def before_finish(how=nil, &block)
  add_hook(:before_finish, how, &block)
end

#finishObject

Stop the dispatcher thread



91
92
93
94
95
# File 'lib/scriptty/request_dispatcher.rb', line 91

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/scriptty/request_dispatcher.rb', line 98

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

#startObject

Start the dispatcher thread

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/scriptty/request_dispatcher.rb', line 84

def start
  raise ArgumentError.new("Already started") if @thread
  @thread = Thread.new{ main }
  nil
end