Class: AlWorker::Fd
- Inherits:
-
Object
- Object
- AlWorker::Fd
- Defined in:
- lib/al_worker_fd.rb
Overview
ファイルディスクリプタ
Instance Attribute Summary collapse
-
#file ⇒ IO
対象のファイル(IO)オブジェクト.
-
#mode_sync ⇒ Symbol
同期(:sync)/非同期(:async).
Class Method Summary collapse
-
.open(path, mode = "r") ⇒ AlWorker::Fd
ファイルオープン.
Instance Method Summary collapse
-
#close ⇒ Object
クローズ.
-
#initialize(file) ⇒ Fd
constructor
constructor.
-
#ready_read(*arg) { ... } ⇒ Object
読み込み準備完了時.
-
#ready_write(*arg) { ... } ⇒ Object
書き込み準備完了時.
Constructor Details
#initialize(file) ⇒ Fd
constructor
45 46 47 48 |
# File 'lib/al_worker_fd.rb', line 45 def initialize( file ) @file = file @mode_sync = :sync end |
Instance Attribute Details
#file ⇒ IO
Returns 対象のファイル(IO)オブジェクト.
21 22 23 |
# File 'lib/al_worker_fd.rb', line 21 def file @file end |
#mode_sync ⇒ Symbol
Returns 同期(:sync)/非同期(:async).
18 19 20 |
# File 'lib/al_worker_fd.rb', line 18 def mode_sync @mode_sync end |
Class Method Details
.open(path, mode = "r") ⇒ AlWorker::Fd
Note:
ファイルオープン
簡易記述によるファクトリ
33 34 35 36 |
# File 'lib/al_worker_fd.rb', line 33 def self.open( path, mode = "r" ) file = File.open( path, mode ) self.new( file ) end |
Instance Method Details
#close ⇒ Object
クローズ
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/al_worker_fd.rb', line 141 def close() # (note) # ready_*() 中から呼び出されるブロックからコールされる可能性がある。 # その場合、自分のスレッドをkillしないようにしている。 # 自分のスレッドは、return後のselectで例外によって終了するだろう。 # if @thread_read && @thread_read.alive? && @thread_read != Thread.current @thread_read.kill rescue 0 end if @thread_write && @thread_write.alive? && @thread_write != Thread.current @thread_write.kill rescue 0 end @file.close if ! @file.closed? end |
#ready_read(*arg) { ... } ⇒ Object
読み込み準備完了時
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/al_worker_fd.rb', line 57 def ready_read( *arg ) arg << @file th = Thread.start( arg ) { |arg| loop do begin ret = IO.select( [@file] ) rescue @thread_read = nil break end next if ! ret begin if mode_sync == :sync AlWorker.mutex_sync.synchronize { yield( *arg ) } else yield( *arg ) end rescue Exception => ex raise ex if ex.class == SystemExit AlWorker.log( ex ) end end } #(note) # ready_readからready_readを呼び出されても動くように、 # 若干の工夫がしてある。 if @thread_read th_bak = @thread_read @thread_read = th th_bak.kill rescue 0 else @thread_read = th end end |
#ready_write(*arg) { ... } ⇒ Object
書き込み準備完了時
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/al_worker_fd.rb', line 102 def ready_write( *arg ) arg << @file th = Thread.start( arg ) { |arg| loop do begin ret = IO.select( [], [@file] ) rescue @thread_write = nil break end next if ! ret begin if mode_sync == :sync AlWorker.mutex_sync.synchronize { yield( *arg ) } else yield( *arg ) end rescue Exception => ex raise ex if ex.class == SystemExit AlWorker.log( ex ) end end } if @thread_write th_bak = @thread_write @thread_write = th th_bak.kill rescue 0 else @thread_write = th end end |