Class: Baykit::BayServer::Docker::Base::InboundShip
- Inherits:
-
WaterCraft::Ship
- Object
- WaterCraft::Ship
- Baykit::BayServer::Docker::Base::InboundShip
- Includes:
- Agent, Tours, Util, WaterCraft
- Defined in:
- lib/baykit/bayserver/docker/base/inbound_ship.rb
Constant Summary collapse
- MAX_TOURS =
128
Constants inherited from WaterCraft::Ship
WaterCraft::Ship::INVALID_SHIP_ID, WaterCraft::Ship::SHIP_ID_NOCHECK
Class Attribute Summary collapse
-
.err_counter ⇒ Object
readonly
Returns the value of attribute err_counter.
Instance Attribute Summary collapse
-
#active_tours ⇒ Object
readonly
Returns the value of attribute active_tours.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
-
#need_end ⇒ Object
Returns the value of attribute need_end.
-
#port_docker ⇒ Object
readonly
Returns the value of attribute port_docker.
-
#socket_timeout_sec ⇒ Object
readonly
Returns the value of attribute socket_timeout_sec.
-
#tour_store ⇒ Object
readonly
Returns the value of attribute tour_store.
Attributes inherited from WaterCraft::Ship
#agent, #initialized, #keeping, #object_id, #postman, #protocol_handler, #ship_id, #socket
Instance Method Summary collapse
- #abort_tours ⇒ Object
- #end_ship ⇒ Object
- #get_error_tour ⇒ Object
-
#get_tour(tur_key) ⇒ Object
Other methods.
- #init_inbound(skt, agt, postman, port, proto_hnd) ⇒ Object
-
#initialize ⇒ InboundShip
constructor
A new instance of InboundShip.
-
#reset ⇒ Object
Implements Reusable.
- #return_tour(tur) ⇒ Object
- #send_end_tour(chk_ship_id, tur, &callback) ⇒ Object
- #send_error(chk_id, tour, status, message, e) ⇒ Object
- #send_headers(check_id, tur) ⇒ Object
- #send_redirect(check_id, tur, status, location) ⇒ Object
- #send_res_content(check_id, tur, bytes, ofs, len, &callback) ⇒ Object
- #to_s ⇒ Object
Methods inherited from WaterCraft::Ship
#check_ship_id, #id, #init, #protocol, #resume, #set_protocol_handler
Constructor Details
#initialize ⇒ InboundShip
Returns a new instance of InboundShip.
32 33 34 35 36 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 32 def initialize() super @lock = Monitor.new() @active_tours = [] end |
Class Attribute Details
.err_counter ⇒ Object (readonly)
Returns the value of attribute err_counter.
18 19 20 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 18 def err_counter @err_counter end |
Instance Attribute Details
#active_tours ⇒ Object (readonly)
Returns the value of attribute active_tours.
30 31 32 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 30 def active_tours @active_tours end |
#lock ⇒ Object (readonly)
Returns the value of attribute lock.
29 30 31 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 29 def lock @lock end |
#need_end ⇒ Object
Returns the value of attribute need_end.
27 28 29 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 27 def need_end @need_end end |
#port_docker ⇒ Object (readonly)
Returns the value of attribute port_docker.
24 25 26 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 24 def port_docker @port_docker end |
#socket_timeout_sec ⇒ Object (readonly)
Returns the value of attribute socket_timeout_sec.
28 29 30 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 28 def socket_timeout_sec @socket_timeout_sec end |
#tour_store ⇒ Object (readonly)
Returns the value of attribute tour_store.
26 27 28 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 26 def tour_store @tour_store end |
Instance Method Details
#abort_tours ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 273 def abort_tours() return_list = [] # Abort tours @active_tours.each do |tur| if tur.valid? BayLog.debug("%s is valid, abort it: stat=%s", tur, tur.state) if tur.req.abort() return_list << tur end end end return_list.each do |tur| return_tour(tur) end end |
#end_ship ⇒ Object
266 267 268 269 270 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 266 def end_ship() BayLog.debug("%s endShip", self) @port_docker.return_protocol_handler(@agent, @protocol_handler) @port_docker.return_ship(self) end |
#get_error_tour ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 85 def get_error_tour tur_key = InboundShip.err_counter.next() store_key = InboundShip.uniq_key(@ship_id, -tur_key) tur = @tour_store.rent(store_key, true) tur.init(-tur_key, self) @active_tours.append(tur) return tur end |
#get_tour(tur_key) ⇒ Object
Other methods
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 68 def get_tour(tur_key) tur = nil store_key = InboundShip.uniq_key(@ship_id, tur_key) @lock.synchronize do tur = @tour_store.get(store_key) if tur == nil tur = @tour_store.rent(store_key, false) if tur == nil return nil end tur.init(tur_key, self) @active_tours.append(tur) end end return tur end |
#init_inbound(skt, agt, postman, port, proto_hnd) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 42 def init_inbound(skt, agt, postman, port, proto_hnd) self.init(skt, agt, postman) @port_docker = port @socket_timeout_sec = @port_docker.timeout_sec >= 0 ? @port_docker.timeout_sec : BayServer.harbor.socket_timeout_sec @tour_store = TourStore.get_store(agt.agent_id) set_protocol_handler(proto_hnd) end |
#reset ⇒ Object
Implements Reusable
54 55 56 57 58 59 60 61 62 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 54 def reset() super @lock.synchronize do if !@active_tours.empty? raise Sink.new("%s There are some running tours", self) end end @need_end = false end |
#return_tour(tur) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 322 def return_tour(tur) BayLog.debug("%s Return tour: %s", self, tur) @lock.synchronize do if !@active_tours.include?(tur) raise Sink.new("Tour is not in acive list: %s", tur); end tour_store.Return(InboundShip.uniq_key(@ship_id, tur.req.key)) @active_tours.delete(tur) if @need_end && @active_tours.empty? end_ship() end end end |
#send_end_tour(chk_ship_id, tur, &callback) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 191 def send_end_tour(chk_ship_id, tur, &callback) @lock.synchronize do check_ship_id(chk_ship_id) BayLog.debug("%s sendEndTour: %s state=%s", self, tur, tur.state) if tur.zombie? || tur.aborted? # Don't send peer any data. Do nothing BayLog.debug("%s Aborted or zombie tour. do nothing: %s state=%s", self, tur, tur.state) tur.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ENDED) callback.call() else if !tur.valid? raise Sink.new("Tour is not valid") end keep_alive = false if tur.req.headers.get_connection() == Headers::CONNECTION_KEEP_ALIVE keep_alive = true if keep_alive res_conn = tur.res.headers.get_connection() keep_alive = (res_conn == Headers::CONNECTION_KEEP_ALIVE) || (res_conn == Headers::CONNECTION_UNKOWN) end if keep_alive if tur.res.headers.content_length() < 0 keep_alive = false end end end tur.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ENDED) @protocol_handler.send_end_tour(tur, keep_alive, &callback) end end end |
#send_error(chk_id, tour, status, message, e) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 227 def send_error(chk_id, tour, status, , e) check_ship_id(chk_id) BayLog.info("%s send error: status=%d, message=%s ex=%s", self, status, , e == nil ? "" : e.) if e != nil BayLog.debug_e(e) end # Create body str = HttpStatus.description(status) # print status body = StringUtil.alloc(8192) body << "<h1>" << status.to_s << " " << str << "</h1>\r\n" # print message #if message != nil && BayLog.debug_mode? # body << message #end # print stack trace #if e != nil && BayLog.debug_mode? # body << "<P><HR><P>\r\n" # body << "<pre>\r\n" # e.backtrace.each do |item| # body << item << "\r\n" # end # body << "</pre>" #end tour.res.headers.status = status send_error_content(chk_id, tour, body) end |
#send_headers(check_id, tur) ⇒ Object
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 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 94 def send_headers(check_id, tur) check_ship_id(check_id) if tur.zombie? || tur.aborted? # Don't send peer any data return end handled = false if !tur.error_handling && tur.res.headers.status >= 400 trouble = BayServer.harbor.trouble if trouble != nil cmd = trouble.find(tur.res.headers.status) if cmd != nil err_tour = get_error_tour err_tour.req.uri = cmd.target tur.req.headers.copy_to(err_tour.req.headers) tur.res.headers.copy_to(err_tour.res.headers) err_tour.req.remote_port = tur.req.remote_port err_tour.req.remote_address = tur.req.remote_address err_tour.req.server_address = tur.req.server_address err_tour.req.server_port = tur.req.server_port err_tour.req.server_name = tur.req.server_name err_tour.res.header_sent = tur.res.header_sent tur.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ZOMBIE) case cmd.method when :GUIDE err_tour.go when :TEXT @protocol_handler.send_headers(err_tour) data = cmd.target err_tour.res.send_content(Tour::TOUR_ID_NOCHECK, data, 0, data.length) err_tour.end_content(Tour::TOUR_ID_NOCHECK) when :REROUTE err_tour.res.send_http_exception(Tour::TOUR_ID_NOCHECK, HttpException.moved_temp(cmd.target)) end handled = true end end end if !handled @port_docker.additional_headers.each do |nv| tur.res.headers.add(nv[0], nv[1]) end begin @protocol_handler.send_res_headers(tur) rescue IOError => e BayLog.debug_e(e, "%s abort: %s", tur, e) tur.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED) raise e end end end |
#send_redirect(check_id, tur, status, location) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 148 def send_redirect(check_id, tur, status, location) check_ship_id(check_id) hdr = tur.res.headers hdr.status = status hdr.set(Headers::LOCATION, location) body = "<H2>Document Moved.</H2><BR>" + "<A HREF=\"" + location + "\">" + location + "</A>" send_error_content(check_id, tur, body) end |
#send_res_content(check_id, tur, bytes, ofs, len, &callback) ⇒ Object
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 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 160 def send_res_content(check_id, tur, bytes, ofs, len, &callback) BayLog.debug("%s send_res_content bytes: %d", self, len) check_ship_id(check_id) if tur.zombie? || tur.aborted? # Don't send peer any data BayLog::debug("%s Aborted or zombie tour. do nothing: %s state=%s", self, tur, tur.state) tur.change_state(Tour::TOUR_ID_NOCHECK, TourState::ENDED) if callback != nil callback.call() end return end max_len = @protocol_handler.max_res_packet_data_size(); if len > max_len send_res_content(Ship::SHIP_ID_NOCHECK, tur, bytes, ofs, max_len) send_res_content(Ship::SHIP_ID_NOCHECK, tur, bytes, ofs + max_len, len - max_len, &callback) else begin @protocol_handler.send_res_content(tur, bytes, ofs, len, &callback) rescue IOError => e BayLog.debug_e(e, "%s abort: %s", tur, e) tur.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED) raise e end end end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/baykit/bayserver/docker/base/inbound_ship.rb', line 38 def to_s return "#{@agent} ship##{@ship_id}/#{@object_id}[#{protocol()}]" end |