Class: Baykit::BayServer::Tours::SendFileTrain

Inherits:
Baykit::BayServer::Train::Train show all
Includes:
Util
Defined in:
lib/baykit/bayserver/tours/send_file_train.rb

Instance Attribute Summary collapse

Attributes inherited from Baykit::BayServer::Train::Train

#tour, #tour_id, #train_id

Instance Method Summary collapse

Methods inherited from Baykit::BayServer::Train::Train

#to_s

Constructor Details

#initialize(tur, file) ⇒ SendFileTrain

Returns a new instance of SendFileTrain.



17
18
19
20
# File 'lib/baykit/bayserver/tours/send_file_train.rb', line 17

def initialize(tur, file)
  super(tur)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



15
16
17
# File 'lib/baykit/bayserver/tours/send_file_train.rb', line 15

def file
  @file
end

Instance Method Details

#runObject

implements Train



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/baykit/bayserver/tours/send_file_train.rb', line 26

def run()

  @tour.res.set_consume_listener do |len, resume| end

  size = @tour.ship.protocol_handler.max_packet_data_size
  buf = StringUtil.alloc(size)
  File.open(file, "r") do |fd|
    begin
      while true

        while !@tour.res.available
          sleep(0.1)
        end

        begin
          fd.sysread(size, buf);
        rescue EOFError => e
          break
        rescue Exception => e
          @tour.res.send_error(@tour_id, HttpStatus::INTERNAL_SERVER_ERROR, e)
          break
        end

        @tour.res.send_content(@tour_id, buf, 0, buf.length);

        while !@tour.res.available
          sleep(0.1)
        end
      end

      @tour.res.end_content(@tour_id)
    rescue Exception => e
      BayLog.error_e(e);
    end
  end
end