Class: Baykit::BayServer::Docker::SendFile::FileContentHandler

Inherits:
Object
  • Object
show all
Includes:
Agent, Agent::Multiplexer, Common, Rudders, Tours, Tours::ReqContentHandler, Util
Defined in:
lib/baykit/bayserver/docker/send_file/file_content_handler.rb

Constant Summary

Constants included from Tours::ReqContentHandler

Tours::ReqContentHandler::DEV_NULL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tur, store, path, charset) ⇒ FileContentHandler

Returns a new instance of FileContentHandler.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 31

def initialize(tur, store, path, charset)
  @tour = tur
  @store = store
  @path = path
  @charset = charset
  @abortable = true
  @lock = Mutex.new

  rname = File.basename(path)
  pos = rname.rindex('.')
  if pos
    ext = rname[pos + 1 .. -1].downcase
    @mime_type = Mimes.type(ext)
  end

  if @mime_type == nil
    @mime_type = "application/octet-stream"
  end

  if @mime_type.start_with?("text/") && charset != nil
    @mime_type = @mime_type + "; charset=" + charset
  end
end

Instance Attribute Details

#abortableObject (readonly)

Returns the value of attribute abortable.



26
27
28
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 26

def abortable
  @abortable
end

#charsetObject (readonly)

Returns the value of attribute charset.



24
25
26
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 24

def charset
  @charset
end

#file_contentObject (readonly)

Returns the value of attribute file_content.



28
29
30
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 28

def file_content
  @file_content
end

#lockObject (readonly)

Returns the value of attribute lock.



29
30
31
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 29

def lock
  @lock
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



25
26
27
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 25

def mime_type
  @mime_type
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#storeObject (readonly)

Returns the value of attribute store.



27
28
29
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 27

def store
  @store
end

#tourObject (readonly)

Returns the value of attribute tour.



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

def tour
  @tour
end

Instance Method Details

#on_abort_req(tur) ⇒ Object



70
71
72
73
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 70

def on_abort_req(tur)
  BayLog.debug("%s onAbortReq aborted=%s", tur, abortable)
  return abortable
end

#on_end_req_content(tur) ⇒ Object



64
65
66
67
68
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 64

def on_end_req_content(tur)
  BayLog.debug("%s endReqContent", tur)
  req_start_tour()
  @abortable = false
end

#on_read_req_content(tur, buf, start, len, &lis) ⇒ Object

Implements ReqContentHandler



59
60
61
62
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 59

def on_read_req_content(tur, buf, start, len, &lis)
  BayLog.debug("%s onReadReqContent(Ignore) len=%d", tur, len)
  tur.req.consumed(tur.tour_id, len, &lis)
end

#req_start_tourObject

Sending file methods



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 79

def req_start_tour

  @lock.synchronize do
    if @store == nil
      status = FileStore::FileContentStatus.new(nil, FileStore::FileContentStatus::EXCEEDED)
    else
      status = @store.get(path)
    end
    @file_content = status.file_content

    BayLog.debug("%s file content status: %d", @tour, status.status)
    case status.status
    when FileStore::FileContentStatus::STARTED, FileStore::FileContentStatus::EXCEEDED
      send_file_async

    when FileStore::FileContentStatus::READING
      # Wait file loaded
      BayLog.debug("%s Cannot start tour (file reading)", @tour)

      agt = GrandAgent.get(@tour.ship.agent_id)
      wait_file_ship = WaitFileShip.new()
      tp = PlainTransporter.new(
        agt.spider_multiplexer,
        wait_file_ship,
        true,
        8192,
        false)

      begin
        pipe = IO::pipe
        source_rd = IORudder.new(pipe[0])
        source_rd.set_non_blocking()
        wait_rd = IORudder.new(pipe[1])
      rescue IOError => e
        raise Sink.new("Fatal error: %s", e)
      end

      wait_file_ship.init(source_rd, tp, @tour, @file_content, self)
      @tour.res.set_consume_listener(&ContentConsumeListener::DEV_NULL)

      st = RudderStateStore.get_store(agt.agent_id).rent()
      st.init(source_rd, tp)
      agt.spider_multiplexer.add_rudder_state(source_rd, st)
      agt.spider_multiplexer.req_read(source_rd)

      @file_content.add_waiter(wait_rd)

      when FileStore::FileContentStatus::COMPLETED
        send_file_from_cache

    else
      raise Sink.new("Unknown file content status: %d", status.status)
    end
  end
end

#send_file_asyncObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 135

def send_file_async()

  if File.directory?(@path)
    raise HttpException.new(HttpStatus::FORBIDDEN, @path)
  elsif !File.exist?(@path)
    raise HttpException.new(HttpStatus::NOT_FOUND, @path)
  end

  file_len = ::File.size(@path)

  @tour.res.headers.set_content_type(@mime_type)
  @tour.res.headers.set_content_length(File.size(@path))

  begin
    @tour.res.send_headers(Tour::TOUR_ID_NOCHECK)

    bufsize = @tour.ship.protocol_handler.max_res_packet_data_size
    agt = GrandAgent.get(@tour.ship.agent_id)

    f = File.open(@path, "rb")
    rd = IORudder.new(f)

    case(BayServer.harbor.file_multiplexer)

    when Harbor::MULTIPLEXER_TYPE_SPIDER
      mpx = agt.spider_multiplexer

    when Harbor::MULTIPLEXER_TYPE_JOB
      mpx = agt.job_multiplexer

    when Harbor::MULTIPLEXER_TYPE_SPIN
      mpx = agt.spin_multiplexer

    when Harbor::MULTIPLEXER_TYPE_TAXI
      mpx = agt.taxi_multiplexer

    else
      raise Sink.new
    end

    send_file_ship = SendFileShip.new
    tp = PlainTransporter.new(
      mpx,
      send_file_ship,
      true,
      8195,
      false)

    send_file_ship.init(rd, tp, @tour, @file_content)
    sid = send_file_ship.ship_id

    @tour.res.set_consume_listener do |len, resume|
      if resume
        send_file_ship.resume_read(sid)
      end
    end

    st = RudderStateStore.get_store(agt.agent_id).rent()
    st.init(rd, tp)
    mpx.add_rudder_state(rd, st)
    mpx.req_read(rd)

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

end

#send_file_from_cacheObject



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 204

def send_file_from_cache
  @tour.res.set_consume_listener(&ContentConsumeListener::DEV_NULL)
  @tour.res.headers.set_content_type(@mime_type)
  @tour.res.headers.set_content_length(File.size(@path))
  begin
    @tour.res.send_headers(Tour::TOUR_ID_NOCHECK)
    @tour.res.send_res_content(Tour::TOUR_ID_NOCHECK, @file_content.content, 0, @file_content.content_length)
    @tour.res.end_res_content(Tour::TOUR_ID_NOCHECK)
  rescue IOError => e
    BayLog.error_e(e)
    raise HttpException.new(HttpStatus::INTERNAL_SERVER_ERROR, @file_content.path)
  end
end