Class: Baykit::BayServer::Agent::Transporter::SpinWriteTransporter

Inherits:
SpinHandler
  • Object
show all
Includes:
Util, Util::Postman, Util::Reusable, Util::Valve
Defined in:
lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb

Instance Attribute Summary collapse

Attributes inherited from SpinHandler

#agent, #listeners, #spin_count

Instance Method Summary collapse

Methods included from Util::Postman

#abort, #flush, #is_zombie, #post_end

Methods inherited from SpinHandler

#ask_to_callback, #empty?, #on_timer, #process_data, #stop_timeout_spins

Methods included from Baykit::BayServer::Agent::TimerHandler

#on_timer

Constructor Details

#initializeSpinWriteTransporter



25
26
27
28
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 25

def initialize()
  @write_queue = []
  @lock = Mutex.new()
end

Instance Attribute Details

#data_listenerObject (readonly)

Returns the value of attribute data_listener.



19
20
21
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 19

def data_listener
  @data_listener
end

#lockObject (readonly)

Returns the value of attribute lock.



22
23
24
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 22

def lock
  @lock
end

#outfileObject (readonly)

Returns the value of attribute outfile.



20
21
22
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 20

def outfile
  @outfile
end

#spin_handlerObject (readonly)

Returns the value of attribute spin_handler.



18
19
20
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 18

def spin_handler
  @spin_handler
end

#write_queueObject (readonly)

Returns the value of attribute write_queue.



21
22
23
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 21

def write_queue
  @write_queue
end

Instance Method Details

#check_timeout(duration_sec) ⇒ Object



90
91
92
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 90

def check_timeout(duration_sec)
  return false
end

#closeObject



94
95
96
97
98
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 94

def close()
  if @outfile != nil
    @outfile.close()
  end
end

#init(spin_hnd, outfile, lis) ⇒ Object



30
31
32
33
34
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 30

def init(spin_hnd, outfile, lis)
  @spin_handler = spin_hnd
  @data_listener = lis
  @outfile = outfile
end

#lapObject

Implements SpinListener



53
54
55
56
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
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 53

def lap()
  begin

    buf = nil
    @lock.synchronize do
      if @write_queue.empty?
        BayLog.warn("%s Write queue empty", self)
        return NextSocketAction::SUSPEND
      end
      buf = @write_queue[0]
    end

    len = @outfile.syswrite(buf)

    if len == 0
      return NextSocketAction::CONTINUE
    elsif len < buf.length
      buf[0 .. len-1] = ""
      return NextSocketAction::CONTINUE
    end

    @lock.synchronize do
      @write_queue.delete_at(0)
      if @write_queue.empty?
        return NextSocketAction::SUSPEND
      else
        return NextSocketAction::CONTINUE
      end
    end

  rescue Exception => e
    BayLog.error_e(e)
    close()
    return NextSocketAction::CLOSE
  end
end

#open_valveObject

Implements Valve



104
105
106
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 104

def open_valve()
  @spin_handler.ask_to_callback(self)
end

#post(bytes, tag) ⇒ Object

Other methods



111
112
113
114
115
116
117
118
119
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 111

def post(bytes, tag)
  @lock.synchronize do
    empty = @write_queue.empty?
    @write_queue << bytes
    if empty
      open_valve()
    end
  end
end

#resetObject

Implements Reusable



44
45
46
47
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 44

def reset()
  @data_listener = nil
  @outfile = nil
end

#to_sObject



36
37
38
# File 'lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb', line 36

def to_s
  data_listener.to_s()
end