Class: Baykit::BayServer::Tours::ReadFileTaxi

Inherits:
Baykit::BayServer::Taxi::Taxi show all
Includes:
Agent, Baykit::BayServer::Taxi, Util, Util::Valve
Defined in:
lib/baykit/bayserver/tours/read_file_taxi.rb

Instance Attribute Summary collapse

Attributes inherited from Baykit::BayServer::Taxi::Taxi

#taxi_id

Attributes inherited from Common::Vehicle

#id

Instance Method Summary collapse

Methods inherited from Baykit::BayServer::Taxi::Taxi

#run

Methods inherited from Common::Vehicle

#run

Constructor Details

#initialize(agt_id, buf_size) ⇒ ReadFileTaxi

Returns a new instance of ReadFileTaxi.



29
30
31
32
33
34
35
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 29

def initialize(agt_id, buf_size)
  super()
  @buf_size = buf_size
  @buf = StringUtil.alloc(buf_size)
  @lock = Monitor.new()
  @agent_id = agt_id
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



26
27
28
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 26

def agent_id
  @agent_id
end

#bufObject (readonly)

Returns the value of attribute buf.



22
23
24
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 22

def buf
  @buf
end

#buf_sizeObject (readonly)

Returns the value of attribute buf_size.



23
24
25
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 23

def buf_size
  @buf_size
end

#ch_validObject (readonly)

Returns the value of attribute ch_valid.



20
21
22
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 20

def ch_valid
  @ch_valid
end

#data_listenerObject (readonly)

Returns the value of attribute data_listener.



21
22
23
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 21

def data_listener
  @data_listener
end

#infileObject (readonly)

Returns the value of attribute infile.



19
20
21
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 19

def infile
  @infile
end

#lockObject (readonly)

Returns the value of attribute lock.



25
26
27
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 25

def lock
  @lock
end

#runningObject (readonly)

Returns the value of attribute running.



24
25
26
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 24

def running
  @running
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



27
28
29
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 27

def start_time
  @start_time
end

Instance Method Details

#closeObject



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

def close()
  @lock.synchronize do
    if !@ch_valid
      return
    end

    @ch_valid = false
    @data_listener.notify_eof()

    begin
      @infile.close()
    rescue IOError => e
    end

    @data_listener.notify_close()
  end
end

#departObject

implements Taxi



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
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 61

def depart()
  @start_time = Time.now.tv_sec
  begin
    @buf.clear()
    @infile.read(@buf_size, @buf)

    if @buf.length == 0
      close()
      return
    end

    act = @data_listener.notify_read(@buf, nil)

    @running = false
    if act == NextSocketAction::CONTINUE
      next_run()
    end

  rescue IOError => e
    BayLog.debug_e(e)
    close()
  rescue Exception => e
    close()
    raise e
  end
end

#init(infile, data_listener) ⇒ Object



41
42
43
44
45
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 41

def init(infile, data_listener)
  @data_listener = data_listener
  @infile = infile
  @ch_valid = true
end

#next_runObject



96
97
98
99
100
101
102
103
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 96

def next_run()
  if @running
    # If running, not posted because next run exists
    return
  end
  @running = true
  TaxiRunner.post(@agent_id, self)
end

#on_timerObject



88
89
90
91
92
93
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 88

def on_timer()
  duration_sec = Time.now.tv_sec - @start_time
  if (@data_listener.check_timeout(duration_sec))
    close()
  end
end

#open_valveObject

implements Valve



51
52
53
54
55
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 51

def open_valve()
  @lock.synchronize do
    next_run()
  end
end

#to_sObject



37
38
39
# File 'lib/baykit/bayserver/tours/read_file_taxi.rb', line 37

def to_s()
  return super.to_s() + " " + @data_listener.to_s()
end