Class: Baykit::BayServer::Util::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/selector.rb

Overview

Like Selector class of Python

Constant Summary collapse

OP_READ =
1
OP_WRITE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSelector

Returns a new instance of Selector.



15
16
17
18
# File 'lib/baykit/bayserver/util/selector.rb', line 15

def initialize
  @channels = {}
  @lock = Mutex.new()
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



12
13
14
# File 'lib/baykit/bayserver/util/selector.rb', line 12

def channels
  @channels
end

#lockObject (readonly)

Returns the value of attribute lock.



13
14
15
# File 'lib/baykit/bayserver/util/selector.rb', line 13

def lock
  @lock
end

Instance Method Details

#countObject



104
105
106
107
108
# File 'lib/baykit/bayserver/util/selector.rb', line 104

def count
  @lock.synchronize do
    return @channels.length
  end
end

#get_op(ch) ⇒ Object



59
60
61
62
63
64
# File 'lib/baykit/bayserver/util/selector.rb', line 59

def get_op(ch)
  if not ((ch.kind_of? IO) || (ch.kind_of? OpenSSL::SSL::SSLSocket))
    raise ArgumentError
  end
  return @channels[ch]
end

#modify(ch, op) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/baykit/bayserver/util/selector.rb', line 42

def modify(ch, op)
  if  not ((ch.kind_of? IO) || (ch.kind_of? OpenSSL::SSL::SSLSocket))
    raise ArgumentError
  end
  if op & OP_READ != 0
    register_read(ch, @channels)
  else
    unregister_read(ch, @channels)
  end

  if op & OP_WRITE != 0
    register_write(ch, @channels)
  else
    unregister_write(ch, @channels)
  end
end

#register(ch, op) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/baykit/bayserver/util/selector.rb', line 20

def register(ch, op)
  #BayLog.debug("register io=%s", ch)
  if not ((ch.kind_of? IO) || (ch.kind_of? OpenSSL::SSL::SSLSocket))
    raise ArgumentError
  end
  if op & OP_READ != 0
    register_read(ch, @channels)
  end
  if op & OP_WRITE != 0
    register_write(ch, @channels)
  end
end

#select(timeout_sec = nil) ⇒ Object



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
94
95
96
97
98
99
100
101
102
# File 'lib/baykit/bayserver/util/selector.rb', line 66

def select(timeout_sec = nil)
  if timeout_sec == nil
    timeout_sec = 0
  end
  except_list = []

  read_list = []
  write_list = []
  @lock.synchronize do
    @channels.keys().each do |ch|
      if (@channels[ch] & OP_READ) != 0
        read_list << ch
      end
      if (@channels[ch] & OP_WRITE) != 0
        write_list << ch
      end
    end
  end
  #BayLog.debug("Select read_list=%s", read_list)
  #BayLog.debug("Select write_list=%s", write_list)
  selected_read_list, selected_write_list = Kernel.select(read_list, write_list, except_list, timeout_sec)

  result = {}
  if selected_read_list != nil
    selected_read_list.each do |ch|
      register_read(ch, result)
    end
  end

  if selected_write_list != nil
    selected_write_list.each do |ch|
      register_write(ch, result)
    end
  end

  return result
end

#unregister(ch) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/baykit/bayserver/util/selector.rb', line 33

def unregister(ch)
  #BayLog.debug("unregister io=%s", ch)
  if  not ((ch.kind_of? IO) || (ch.kind_of? OpenSSL::SSL::SSLSocket))
    raise ArgumentError
  end
  unregister_read(ch, @channels)
  unregister_write(ch, @channels)
end