Class: Baykit::BayServer::Docker::SendFile::DirectoryTrain

Inherits:
Train::Train
  • Object
show all
Includes:
Agent, Tours, Tours::ReqContentHandler, Train, Util
Defined in:
lib/baykit/bayserver/docker/send_file/directory_train.rb

Constant Summary

Constants included from Tours::ReqContentHandler

Tours::ReqContentHandler::DEV_NULL

Instance Attribute Summary collapse

Attributes inherited from Train::Train

#tour, #tour_id, #train_id

Instance Method Summary collapse

Methods included from Tours::ReqContentHandler

#on_abort_req, #on_end_req_content, #on_read_req_content

Methods inherited from Train::Train

#run, #to_s

Constructor Details

#initialize(tur, path) ⇒ DirectoryTrain

Returns a new instance of DirectoryTrain.



25
26
27
28
29
30
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 25

def initialize(tur, path)
  super(tur)
  @path = path
  @available = false
  @abortable = true
end

Instance Attribute Details

#abortableObject (readonly)

Returns the value of attribute abortable.



23
24
25
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 23

def abortable
  @abortable
end

#availableObject (readonly)

Returns the value of attribute available.



22
23
24
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 22

def available
  @available
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 21

def path
  @path
end

Instance Method Details

#departObject

Implements Train



40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 40

def depart()
  begin
    @tour.res.headers.set_content_type("text/html")

    @tour.res.set_consume_listener do |len, resume|
      if resume
        @available = true
      end
    end

    @tour.res.send_headers(@tour_id)

    w = StringIO.new()
    w.write("<html><body><br>")

    if tour.req.uri != "/"
      print_link(w, "../")
    end

    Dir.foreach(path) do |f|
      if File.directory?(f)
        if f != "." && f != ".."
          print_link(w, "#{f}/")
        end
      else
        print_link(w, f)
      end
    end

    w.write("</body></html>")
    bytes = StringUtil.to_bytes(w.string())
    w.close()

    BayLog.trace("%s Directory: send contents: len=%d", @tour, bytes.length)
    @available = tour.res.send_content(@tour_id, bytes, 0, bytes.length)

    while !@available
      sleep(0.1)
    end

    tour.res.end_content(@tour_id)

  rescue IOError => e
    BayLog.error_e(e)
    raise HttpException.new(HttpStatus.INTERNAL_SERVER_ERROR, e)
  end
end

#on_abort(tur) ⇒ Object



105
106
107
108
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 105

def on_abort(tur)
  BayLog.debug("%s onAbort aborted=%s", tur, @abortable)
  return @abortable
end

#on_end_content(tur) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 96

def on_end_content(tur)
  BayLog.debug("%s endContent", tur)
  @abortable = false

  if !TrainRunner.post(self)
    raise HttpException.new(HttpStatus.SERVICE_UNAVAILABLE, "TourRunner is busy")
  end
end

#on_read_content(tur, buf, start, len) ⇒ Object

Implements ReqContentHandler



92
93
94
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 92

def on_read_content(tur, buf, start, len)
  BayLog.debug("%s onReadContent(Ignore) len=%d", tur, len)
end


111
112
113
114
115
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 111

def print_link(w, path)
  w.write("<a href='#{path}'>")
  w.write(path)
  w.write("</a><br>")
end

#start_tourObject



32
33
34
# File 'lib/baykit/bayserver/docker/send_file/directory_train.rb', line 32

def start_tour()
  @tour.req.set_content_handler(self)
end