-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bryanp/chg/bridges-redux
Refactor bridges (again)
- Loading branch information
Showing
17 changed files
with
375 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Goru | ||
# [public] | ||
# | ||
class Bridge | ||
def initialize(routine:, channel:) | ||
@routine = routine | ||
@channel = channel | ||
@channel.add_observer(self) | ||
update_status | ||
end | ||
|
||
# [public] | ||
# | ||
attr_reader :status | ||
|
||
# [public] | ||
# | ||
private def set_status(status) | ||
@status = status | ||
status_changed | ||
end | ||
|
||
# [public] | ||
# | ||
def update_status | ||
# noop | ||
end | ||
|
||
private def status_changed | ||
case @status | ||
when :ready | ||
@routine.bridged | ||
when :finished | ||
@channel.remove_observer(self) | ||
@routine.unbridge | ||
end | ||
end | ||
|
||
def channel_received | ||
update_status | ||
end | ||
|
||
def channel_read | ||
update_status | ||
end | ||
|
||
def channel_closed | ||
update_status | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../bridge" | ||
|
||
module Goru | ||
module Bridges | ||
class Readable < Bridge | ||
private def update_status | ||
status = if @routine.status == :finished | ||
:finished | ||
elsif @channel.full? | ||
:idle | ||
elsif @channel.closed? | ||
:finished | ||
else | ||
:ready | ||
end | ||
|
||
set_status(status) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../bridge" | ||
|
||
module Goru | ||
module Bridges | ||
class Writable < Bridge | ||
private def update_status | ||
status = if @routine.status == :finished | ||
:finished | ||
elsif @channel.any? | ||
:ready | ||
elsif @channel.closed? | ||
:finished | ||
else | ||
:idle | ||
end | ||
|
||
set_status(status) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require "nio" | ||
|
||
module Goru | ||
# [public] | ||
# | ||
class IOEventLoop | ||
def initialize | ||
@commands = [] | ||
@selector = NIO::Selector.new | ||
@stopped = false | ||
end | ||
|
||
# [public] | ||
# | ||
def run | ||
until @stopped | ||
while (command = @commands.shift) | ||
action, routine = command | ||
|
||
case action | ||
when :register | ||
monitor = @selector.register(routine.io, routine.intent) | ||
monitor.value = routine.method(:wakeup) | ||
routine.monitor = monitor | ||
when :deregister | ||
routine.monitor = nil | ||
routine.monitor&.close | ||
end | ||
end | ||
|
||
@selector.select do |monitor| | ||
monitor.value.call | ||
end | ||
end | ||
ensure | ||
@selector.close | ||
end | ||
|
||
# [public] | ||
# | ||
def stop | ||
@stopped = true | ||
@selector.wakeup | ||
rescue IOError | ||
end | ||
|
||
# [public] | ||
# | ||
def <<(tuple) | ||
@commands << tuple | ||
@selector.wakeup | ||
end | ||
end | ||
end |
Oops, something went wrong.