Class: IOMultiplex::Multiplexer
- Inherits:
-
Object
- Object
- IOMultiplex::Multiplexer
- Includes:
- IOMultiplex::Mixins::Callback, IOMultiplex::Mixins::Logger, IOMultiplex::Mixins::Post, IOMultiplex::Mixins::Select, IOMultiplex::Mixins::State, IOMultiplex::Mixins::Timer
- Defined in:
- lib/iomultiplex/iomultiplex.rb
Overview
A single multiplexer that can process hundreds of clients in a single thread
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Attributes included from IOMultiplex::Mixins::Logger
Instance Method Summary collapse
- #add(client) ⇒ Object
- #connections ⇒ Object
-
#initialize(options = {}) ⇒ Multiplexer
constructor
A new instance of Multiplexer.
- #remove(client) ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
Methods included from IOMultiplex::Mixins::Callback
Methods included from IOMultiplex::Mixins::Timer
Methods included from IOMultiplex::Mixins::Post
#defer, #force_read, #remove_post
Methods included from IOMultiplex::Mixins::Select
#stop_all, #stop_read, #stop_write, #wait_read, #wait_write
Constructor Details
#initialize(options = {}) ⇒ Multiplexer
Returns a new instance of Multiplexer.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/iomultiplex/iomultiplex.rb', line 29 def initialize( = {}) @mutex = Mutex.new @connections = 0 initialize_logger [:logger], [:logger_context] initialize_state initialize_select initialize_post initialize_timers initialize_callbacks @id = [:id] || object_id add_logger_context 'multiplexer', @id nil end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
27 28 29 |
# File 'lib/iomultiplex/iomultiplex.rb', line 27 def id @id end |
Instance Method Details
#add(client) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/iomultiplex/iomultiplex.rb', line 56 def add(client) raise ArgumentError, 'Client must be an instance of IOMultiplex::IOReactor' \ unless client.is_a? IOReactor raise ArgumentError, 'Client is already attached' \ unless get_state(client).nil? register_state client client.attach self @mutex.synchronize do @connections += 1 end nil end |
#connections ⇒ Object
88 89 90 91 92 |
# File 'lib/iomultiplex/iomultiplex.rb', line 88 def connections @mutex.synchronize do @connections end end |
#remove(client) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/iomultiplex/iomultiplex.rb', line 73 def remove(client) must_get_state(client) @mutex.synchronize do @connections -= 1 end client.detach stop_all client remove_post client remove_timer client deregister_state client nil end |
#run ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/iomultiplex/iomultiplex.rb', line 46 def run run_once until @shutdown log_debug 'Shutdown' # Forced shutdown each_registered_client(&:force_close) nil end |
#shutdown ⇒ Object
94 95 96 97 98 |
# File 'lib/iomultiplex/iomultiplex.rb', line 94 def shutdown @shutdown = true @nio.wakeup nil end |