Class: Baykit::BayServer::Common::InboundShip

Inherits:
Ships::Ship
  • Object
show all
Includes:
Baykit::BayServer, Agent, Rudders, Ships, Tours, Util
Defined in:
lib/baykit/bayserver/common/inbound_ship.rb

Constant Summary collapse

MAX_TOURS =
128

Constants inherited from Ships::Ship

Ships::Ship::INVALID_SHIP_ID, Ships::Ship::SHIP_ID_NOCHECK

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Ships::Ship

#agent_id, #initialized, #keeping, #object_id, #rudder, #ship_id, #transporter

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ships::Ship

#check_ship_id, #id, #init, #post_close, #resume_read

Constructor Details

#initializeInboundShip

Returns a new instance of InboundShip.



37
38
39
40
41
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 37

def initialize()
  super
  @lock = ::Monitor.new
  @active_tours = []
end

Class Attribute Details

.err_counterObject (readonly)

Returns the value of attribute err_counter.



22
23
24
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 22

def err_counter
  @err_counter
end

Instance Attribute Details

#active_toursObject (readonly)

Returns the value of attribute active_tours.



34
35
36
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 34

def active_tours
  @active_tours
end

#lockObject (readonly)

Returns the value of attribute lock.



35
36
37
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 35

def lock
  @lock
end

#need_endObject

Returns the value of attribute need_end.



31
32
33
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 31

def need_end
  @need_end
end

#port_dockerObject (readonly)

Returns the value of attribute port_docker.



28
29
30
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 28

def port_docker
  @port_docker
end

#protocol_handlerObject (readonly)

Returns the value of attribute protocol_handler.



30
31
32
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 30

def protocol_handler
  @protocol_handler
end

#socket_timeout_secObject (readonly)

Returns the value of attribute socket_timeout_sec.



32
33
34
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 32

def socket_timeout_sec
  @socket_timeout_sec
end

#tour_storeObject (readonly)

Returns the value of attribute tour_store.



33
34
35
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 33

def tour_store
  @tour_store
end

Class Method Details

.uniq_key(sip_id, tur_key) ⇒ Object



310
311
312
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 310

def self.uniq_key(sip_id, tur_key)
  return sip_id << 32 | (tur_key & 0xffffffff);
end

Instance Method Details

#abort_toursObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 262

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

#check_timeout(duration_sec) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 114

def check_timeout(duration_sec)
  if @socket_timeout_sec <= 0
    timeout = false
  elsif @keeping
    timeout = duration_sec >= BayServer.harbor.keep_timeout_sec
  else
    timeout = duration_sec >= @socket_timeout_sec
  end

  BayLog.debug("%s Check timeout: dur=%d, timeout=%s, keeping=%s limit=%d keeplim=%d",
               self, duration_sec, timeout, @keeping, @socket_timeout_sec, BayServer.harbor.keep_timeout_sec)
  return timeout;
end

#end_shipObject



255
256
257
258
259
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 255

def end_ship()
  BayLog.debug("%s endShip", self)
  @port_docker.return_protocol_handler(@agent_id, @protocol_handler)
  @port_docker.return_ship(self)
end

#get_error_tourObject



157
158
159
160
161
162
163
164
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 157

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, force = false, rent = true) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 138

def get_tour(tur_key, force=false, rent=true)
  tur = nil
  store_key = InboundShip.uniq_key(@ship_id, tur_key)
  @lock.synchronize do
    tur = @tour_store.get(store_key)
    if tur == nil && rent
      tur = @tour_store.rent(store_key, force)
      if tur == nil
        return nil
      end
      tur.init(tur_key, self)
      @active_tours.append(tur)
    else
      tur.ship.check_ship_id(@ship_id)
    end
  end
  return tur
end

#init_inbound(rd, agt_id, tp, port_dkr, proto_hnd) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 48

def init_inbound(rd, agt_id, tp, port_dkr, proto_hnd)
  self.init(agt_id, rd, tp)
  @port_docker = port_dkr
  @socket_timeout_sec = @port_docker.timeout_sec >= 0 ? @port_docker.timeout_sec : BayServer.harbor.socket_timeout_sec
  @tour_store = TourStore.get_store(agt_id)
  set_protocol_handler(proto_hnd)
end

#notify_closeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 100

def notify_close
  BayLog.debug("%s notifyClose", self)

  abort_tours

  if @active_tours.length > 0
    # cannot close because there are some running tours
    BayLog.debug("%s cannot end ship because there are some running tours (ignore)", self)
    @need_end = true
  else
    end_ship
  end
end

#notify_connectObject

Raises:



78
79
80
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 78

def notify_connect
  raise Sink.new
end

#notify_eofObject



86
87
88
89
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 86

def notify_eof
  BayLog.debug("%s EOF detected", self)
  return NextSocketAction::CLOSE
end

#notify_error(e) ⇒ Object



91
92
93
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 91

def notify_error(e)
  BayLog.debug_e(e, "%s Error notified", self)
end

#notify_handshake_done(proto) ⇒ Object

Implements Ship



74
75
76
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 74

def notify_handshake_done(proto)
  return NextSocketAction::CONTINUE
end

#notify_protocol_error(e) ⇒ Object



95
96
97
98
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 95

def notify_protocol_error(e)
  BayLog.debug_e(e)
  return tour_handler.on_protocol_error(e)
end

#notify_read(buf) ⇒ Object



82
83
84
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 82

def notify_read(buf)
  return @protocol_handler.bytes_received(buf)
end

#resetObject

Implements Reusable



60
61
62
63
64
65
66
67
68
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 60

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



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 315

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



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 203

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.valid?
      raise Sink.new("Tour is not valid")
    end

    tour_handler.send_end_tour(tur, &callback)
  end
end

#send_error(chk_id, tour, status, message, e) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
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
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 216

def send_error(chk_id, tour, status, message, e)

  check_ship_id(chk_id)

  BayLog.info("%s send error: status=%d, message=%s ex=%s", self, status, message, e == nil ? "" : e.message)

  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



166
167
168
169
170
171
172
173
174
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 166

def send_headers(check_id, tur)
  check_ship_id(check_id)

  @port_docker.additional_headers.each do |nv|
    tur.res.headers.add(nv[0], nv[1])
  end
  BayLog.debug("%s send_res_headers", tur)
  tour_handler.send_res_headers(tur)
end

#send_redirect(check_id, tur, status, location) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 176

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 188

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)

  max_len = @protocol_handler.max_res_packet_data_size();
  BayLog.debug("%s max_len=%d", self, max_len)
  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
    tour_handler.send_res_content(tur, bytes, ofs, len, &callback)
  end
end

#set_protocol_handler(proto_handler) ⇒ Object

Other methods



132
133
134
135
136
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 132

def set_protocol_handler(proto_handler)
  @protocol_handler = proto_handler
  proto_handler.init(self)
  BayLog.debug("%s protocol handler is set", self)
end

#to_sObject



43
44
45
46
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 43

def to_s
  proto = @protocol_handler != nil ? "[" + @protocol_handler.protocol + "]" : ""
  return "agt##{@agent_id} ship##{@ship_id}/#{@object_id}#{proto}"
end

#tour_handlerObject



280
281
282
# File 'lib/baykit/bayserver/common/inbound_ship.rb', line 280

def tour_handler
  return @protocol_handler.command_handler
end