Class: Baykit::BayServer::Agent::SpinHandler

Inherits:
Object
  • Object
show all
Includes:
TimerHandler
Defined in:
lib/baykit/bayserver/agent/spin_handler.rb

Direct Known Subclasses

Transporter::SpinWriteTransporter

Defined Under Namespace

Modules: SpinListener Classes: ListenerInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agt) ⇒ SpinHandler

Returns a new instance of SpinHandler.



36
37
38
39
40
41
42
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 36

def initialize(agt)
  @listeners = []
  @lock = Mutex.new
  @agent = agt
  @spin_count = 0
  @agent.add_timer_handler(self)
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



33
34
35
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 33

def agent
  @agent
end

#listenersObject (readonly)

Returns the value of attribute listeners.



31
32
33
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 31

def listeners
  @listeners
end

#lockObject (readonly)

Returns the value of attribute lock.



32
33
34
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 32

def lock
  @lock
end

#spin_countObject (readonly)

Returns the value of attribute spin_count.



34
35
36
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 34

def spin_count
  @spin_count
end

Instance Method Details

#ask_to_callback(lis) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 104

def ask_to_callback(lis)
  BayLog.debug("%s Ask to callback: %s", self, lis)

  found = false
  for ifo in @listeners do
    if ifo.listener == lis
      found = true
      break
    end
  end

  if found
    BayLog.error("Already registered")
  else
    @lock.synchronize do
      @listeners.append(ListenerInfo.new(lis, Time.now.tv_sec))
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 124

def empty?()
  return @listeners.empty?
end

#on_timerObject

Implements TimerHandler



52
53
54
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 52

def on_timer()
  stop_timeout_spins()
end

#process_dataObject

Custom methods



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

def process_data()
  if @listeners.empty?
    return false
  end

  all_spun = true
  remove_list = []
  @listeners.length.downto(1) do |i|
    lis = listeners[i-1].listener
    act, spun = lis.lap()

    case act
    when NextSocketAction::SUSPEND
      remove_list.append(i-1)
    when NextSocketAction::CLOSE
      remove_list.append(i-1)
    when NextSocketAction::CONTINUE
      next
    else
      raise Sink.new()
    end

    @listeners[i].last_access = Time.now.tv_sec()
    all_spun = all_spun & spun
  end

  if all_spun
    @spin_count += 1
    if @spin_count > 10
      sleep(0.01)
    else
      @spin_count = 0
    end
  end

  remove_list.each do |i|
    @lock.synchronize do
      @listeners.delete_at(i)
    end
  end

  return true
end

#stop_timeout_spinsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 129

def stop_timeout_spins()
  if !@listeners.empty?
    return
  end

  remove_list = []
  @lock.synchronize do
    now = Time.now.tv_sec
    @listeners.length.downto(1) do |i|
      ifo = @listeners[i-1]
      if ifo.listener.check_timeout(int(now - ifo.last_access))
        ifo.listener.close()
        remove_list.append(i)
      end
    end
  end

  remove_list.each do |i|
    @lock.synchronize do
      self.listeners.pop(i)
    end
  end
end

#to_sObject



44
45
46
# File 'lib/baykit/bayserver/agent/spin_handler.rb', line 44

def to_s()
  return @agent.to_s()
end