Class: CRuby::IoChannel

Inherits:
Channel show all
Includes:
IOT
Defined in:
lib/cruby/channel.rb

Direct Known Subclasses

TcpServerChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IOT

exec, input, output

Methods inherited from Channel

#recv, #send

Constructor Details

#initialize(io) ⇒ IoChannel

Returns a new instance of IoChannel.



111
112
113
114
# File 'lib/cruby/channel.rb', line 111

def initialize io
  super()
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



109
110
111
# File 'lib/cruby/channel.rb', line 109

def io
  @io
end

Instance Method Details

#inputObject

入力処理を行う



143
144
145
146
147
148
149
150
# File 'lib/cruby/channel.rb', line 143

def input
  # EOFの処理
  msg = @io.readpartial(4096)
  # チャネルの受信待ちキューのプロセスを取り出す
  item = @recvQ.dequeue
  item['flg'][0] = true
  item['cont'].call(msg)
end

#outputObject

出力処理を行う



153
154
155
156
157
158
159
# File 'lib/cruby/channel.rb', line 153

def output
  # チャネルの送信待ちキューのプロセスを取り出す
  item = @sendQ.dequeue
  item['flg'][0] = true
  CRuby::Coroutine.enqueue(item['cont'])
  io.write(item['msg'])
end

#recvEvtObject

受信イベントを生成する



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cruby/channel.rb', line 130

def recvEvt
  pollFn = Proc.new { false }
  blockFn = Proc.new {|flg,k|
    if flg[0] == true then
      CRuby::Coroutine.dispatch
    end
    @@inTbl[@io] <<= self
    @recvQ.enqueue({'flg'=>flg,'cont'=>k})
  }
  CRuby::Event.new([CRuby::Event::BEvt.new(pollFn, nil, blockFn)])
end

#sendEvt(msg) ⇒ Object

送信イベントを生成する



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cruby/channel.rb', line 117

def sendEvt msg
  pollFn = Proc.new { false }
  blockFn = Proc.new {|flg,k|
    if flg[0] == true then
      CRuby::Coroutine.dispatch
    end
    @@outTbl[@io] <<= self
    @sendQ.enqueue({'flg'=>flg,'msg'=>msg,'cont'=>k})
  }
  CRuby::Event.new([CRuby::Event::BEvt.new(pollFn, nil, blockFn)])
end