Class: Baykit::BayServer::Docker::Base::WarpBase

Inherits:
ClubBase show all
Includes:
Agent, Agent::Multiplexer, Common, Warp, Protocol, Rudders, Util
Defined in:
lib/baykit/bayserver/docker/base/warp_base.rb

Defined Under Namespace

Classes: AgentListener

Instance Attribute Summary collapse

Attributes inherited from ClubBase

#charset, #decode_path_info, #extension, #file_name, #locale

Attributes inherited from DockerBase

#type

Instance Method Summary collapse

Methods included from Docker

#type

Methods inherited from ClubBase

#inspect, #matches, #to_s

Methods included from Club

#charset, #decode_path_info, #extension, #file_name, #matches

Methods inherited from DockerBase

#init_docker, #to_s

Constructor Details

#initializeWarpBase

Initialize methods



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 75

def initialize
  super
  @scheme = nil
  @host = nil
  @port = 0
  @warp_base = nil
  @max_ships = -1
  @host_addr = nil
  @tour_list = []
  @timeout_sec = -1 #  -1 means "Use harbor.socketTimeoutSec"
  @stores = {}
  @lock = Mutex.new
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



45
46
47
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 45

def host
  @host
end

#host_addrObject (readonly)

Returns the value of attribute host_addr.



49
50
51
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 49

def host_addr
  @host_addr
end

#lockObject (readonly)

Returns the value of attribute lock.



52
53
54
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 52

def lock
  @lock
end

#max_shipsObject (readonly)

Returns the value of attribute max_ships.



48
49
50
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 48

def max_ships
  @max_ships
end

#portObject (readonly)

Returns the value of attribute port.



46
47
48
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 46

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



44
45
46
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 44

def scheme
  @scheme
end

#storesObject (readonly)

Agent ID => WarpShipStore



55
56
57
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 55

def stores
  @stores
end

#timeout_secObject (readonly)

Returns the value of attribute timeout_sec.



50
51
52
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 50

def timeout_sec
  @timeout_sec
end

#tour_listObject (readonly)

Returns the value of attribute tour_list.



51
52
53
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 51

def tour_list
  @tour_list
end

#warp_baseObject (readonly)

Returns the value of attribute warp_base.



47
48
49
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 47

def warp_base
  @warp_base
end

Instance Method Details

#arrive(tur) ⇒ Object

Implements Club



179
180
181
182
183
184
185
186
187
188
189
190
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
226
227
228
229
230
231
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 179

def arrive(tur)
  agt = GrandAgent.get(tur.ship.agent_id)
  sto = get_ship_store(agt.agent_id)

  wsip = sto.rent()
  if wsip == nil
    BayLog.warn("%s Busy!", self)
    raise HttpException.new HttpStatus::INTERNAL_SERVER_ERROR, "WarpDocker busy"
  end

  begin
    BayLog.trace("%s got from store", wsip)
    need_connect = false

    if !wsip.initialized
      if @host_addr[0] == :UNIX
        skt = Socket.new(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
      else
        skt = Socket.new(@host_addr[1].ipv4? ? Socket::AF_INET : Socket::AF_INET6, Socket::SOCK_STREAM, 0)
      end
      rd = IORudder.new(skt)

      tp = new_transporter(agt, rd, wsip)
      proto_hnd = ProtocolHandlerStore.get_store(protocol(), false, agt.agent_id).rent()
      wsip.init_warp(rd, agt.agent_id, tp, self, proto_hnd)

      BayLog.debug("%s init warp ship", wsip)
      BayLog.debug("%s Connect to %s:%d skt=%s", wsip, @host, @port, skt)

      need_connect = true
    end

    @lock.synchronize do
      @tour_list.append(tur)
    end

    wsip.start_warp_tour(tur)

    if need_connect
      st = RudderStateStore.get_store(agt.agent_id).rent()
      st.init(wsip.rudder, tp)
      agt.net_multiplexer.add_rudder_state(wsip.rudder, st)
      agt.net_multiplexer.get_transporter(wsip.rudder).req_connect(wsip.rudder, @host_addr[1])
    end

  rescue SystemCallError => e
    BayLog.error_e(e, "%s Error on starting warp tour", wsip)
    raise HttpException.new(HttpStatus::INTERNAL_SERVER_ERROR, "%s", e)
  rescue HttpException => e
    raise e
  end

end

#get_ship_store(agent_id) ⇒ Object

Other methods



253
254
255
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 253

def get_ship_store(agent_id)
  return @stores[agent_id]
end

#init(elm, parent) ⇒ Object

Implements DockerBase



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
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 93

def init(elm, parent)
  super

  if @warp_base == nil
    @warp_base = "/"
  end

  @host_addr = []
  if @host && @host.start_with?(":unix:")
    @host_addr << :UNIX
    @host_addr <<  Socket.sockaddr_un(@host[6 .. -1])
    @port = -1
  else

    if @port <= 0
      @port = 80
    end

    @host_addr << :INET

    begin
      addrs = Addrinfo.getaddrinfo(@host, @port, nil, :STREAM)
    rescue SocketError => e
      BayLog.error_e(e)
      raise ConfigException.new(elm.file_name, elm.line_no, "Invalid address: %s:%d", @host, @port)
    end
    inet4_addr = nil
    inet6_addr = nil
    if addrs
      addrs.each do |adr|
        if adr.ipv4?
          inet4_addr = adr
        elsif adr.ipv6?
          inet6_addr = adr
        end
      end
    end

    if inet4_addr
      @host_addr << inet4_addr
    elsif inet6_addr
      @host_addr << inet6_addr
    else
      raise ConfigException.new(elm.file_name, elm.line_no, "Host not found: %s", @host)
    end



  end

  GrandAgent.add_lifecycle_listener(AgentListener.new(self));

  BayLog.info("WarpDocker[%s] host=%s port=%d adr=%s", @warp_base, @host, @port, @host_addr)
end

#init_key_val(kv) ⇒ Object



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
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 148

def init_key_val(kv)
  case kv.key.downcase

  when "destcity"
    @host = kv.value

  when "destport"
    @port = kv.value.to_i

  when "desttown"
    @warp_base = kv.value
    if !@warp_base.end_with?("/")
      @warp_base += "/"
    end

  when "maxships"
    @max_ships = kv.value.to_i

  when "timeout"
    @timeout_sec = kv.value.to_i

  else
    return super
  end
  return true
end

#keep(wsip) ⇒ Object

Implements Warp



238
239
240
241
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 238

def keep(wsip)
  BayLog.debug("%s keepShip: %s", self, wsip)
  get_ship_store(wsip.agent_id).keep(wsip)
end

#new_transporter(agent, rd, ship) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 68

def new_transporter(agent, rd, ship)
  raise NotImplementedError.new
end

#on_end_ship(wsip) ⇒ Object



243
244
245
246
247
248
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 243

def on_end_ship(wsip)
  BayLog.debug("%s Return protocol handler: ", wsip)
  get_protocol_handler_store(wsip.agent_id).Return(wsip.protocol_handler)
  BayLog.debug("%s return ship: %s", self, wsip);
  get_ship_store(wsip.agent_id).Return(wsip)
end

#protocolObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 64

def protocol
  raise NotImplementedError.new
end

#secureObject

Abstract methods

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/baykit/bayserver/docker/base/warp_base.rb', line 60

def secure
  raise NotImplementedError.new
end