Class: Baykit::BayServer::Docker::Base::PortBase

Inherits:
DockerBase
  • Object
show all
Includes:
Agent, Agent::Multiplexer, Bcf, Common, Baykit::BayServer::Docker, Baykit::BayServer::Docker::Base, Port, Protocol, Rudders, Util, WaterCraft
Defined in:
lib/baykit/bayserver/docker/base/port_base.rb

Instance Attribute Summary collapse

Attributes inherited from DockerBase

#type

Instance Method Summary collapse

Methods included from Port

#protocol

Methods included from Docker

#type

Constructor Details

#initializePortBase

Returns a new instance of PortBase.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 40

def initialize()
  @permission_list = []
  @timeout_sec = -1
  @host = nil
  @port = -1
  @anchored = true
  @additional_headers = []
  @socket_path = nil
  @secure_docker = nil
  @cities = Cities.new()
end

Instance Attribute Details

#additional_headersObject (readonly)

Returns the value of attribute additional_headers.



34
35
36
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 34

def additional_headers
  @additional_headers
end

#anchoredObject (readonly)

Returns the value of attribute anchored.



33
34
35
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 33

def anchored
  @anchored
end

#citiesObject (readonly)

Returns the value of attribute cities.



38
39
40
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 38

def cities
  @cities
end

#hostObject (readonly)

Returns the value of attribute host.



31
32
33
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 31

def host
  @host
end

#permission_listObject (readonly)

Returns the value of attribute permission_list.



30
31
32
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 30

def permission_list
  @permission_list
end

#portObject (readonly)

Returns the value of attribute port.



32
33
34
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 32

def port
  @port
end

#secure_dockerObject (readonly)

Returns the value of attribute secure_docker.



37
38
39
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 37

def secure_docker
  @secure_docker
end

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



35
36
37
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 35

def socket_path
  @socket_path
end

#timeout_secObject (readonly)

Returns the value of attribute timeout_sec.



36
37
38
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 36

def timeout_sec
  @timeout_sec
end

Instance Method Details

#addressObject

implements Port



168
169
170
171
172
173
174
175
176
177
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 168

def address()
  if @socket_path
    #  Unix domain socket
    return @socket_path
  elsif @host == nil
    return [@port, "0.0.0.0"]
  else
    return [@port, @host]
  end
end

#find_city(name) ⇒ Object



184
185
186
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 184

def find_city(name)
  return @cities.find_city(name)
end

#init(elm, parent) ⇒ Object

Implements Docker



71
72
73
74
75
76
77
78
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
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 71

def init(elm, parent)
  if StringUtil.empty?(elm.arg)
    raise ConfigException.new(elm.file_name, elm.line_no, BayMessage.get(:CFG_INVALID_PORT_NAME, elm.name))
  end

  super

  port_name = elm.arg.downcase()
  if port_name.start_with?(":unix:")
    # unix domain sokcet
    @port = -1
    @socket_path = elm.arg[6 .. -1]
    @host = elm.arg
  else
    # TCP or UDP port
    if port_name.start_with?(":tcp:")
      # tcp server socket
      @anchored = true
      host_port = elm.arg[5 .. -1]
    elsif port_name.start_with?(":udp:")
      # udp server socket
      @anchored = false
      host_port = elm.arg[5 .. -1]
    else
      # default: tcp server socket
      @anchored = true
      host_port = elm.arg
    end

    begin
      idx = host_port.index(':')
      if idx != nil
        @host = host_port[0 .. idx]
        @port = host_port[idx+1 .. -1].to_i
      else
        @host = nil
        @port = host_port.to_i
      end
    rescue => e
      raise ConfigException.new(elm.file_name, elm.line_no, BayMessage.get(:CFG_INVALID_PORT_NAME, elm.name))
    end

    if @anchored
      if !support_anchored()
        raise ConfigException.new(elm.file_name, elm.line_no, BayMessage.get(:CFG_TCP_NOT_SUPPORTED))
      end
    else
      if !support_unanchored()
        raise ConfigException.new(elm.file_name, elm.line_no, BayMessage.get(:CFG_UDP_NOT_SUPPORTED))
      end
    end

  end

end

#init_docker(dkr) ⇒ Object

Implements DockerBase



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 131

def init_docker(dkr)
  if dkr.kind_of? Permission
    @permission_list.append(dkr)
  elsif dkr.kind_of? City
    @cities.add(dkr)
  elsif dkr.kind_of? Secure
    @secure_docker = dkr
  else
    return super
  end
  return true
end

#init_key_val(kv) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 144

def init_key_val(kv)
  case kv.key.downcase
  when "timeout"
    @timeout_sec = Integer(kv.value)

  when "addheader"
    idx = kv.value.index(':')
    if idx == nil
      raise ConfigException.new(kv.file_name, kv.line_no, BayMessage.get(:CFG_INVALID_PARAMETER_VALUE, kv.value))
    end

    name = kv.value[0 .. idx].strip()
    value = kv.value[idx+1 .. -1].strip()
    @additional_headers << [name, value]

  else
    return super
  end
  return true
end

#on_connected(agt_id, rd) ⇒ Object



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

def on_connected(agt_id, rd)

  check_admitted(rd)

  sip = PortBase.get_ship_store(agt_id).rent()
  agt = GrandAgent.get(agt_id)

  if secure()
    tp = @secure_docker.new_transporter(
      agt_id,
      sip,
      IOUtil.get_sock_recv_buf_size(rd.io))

    ssl_socket = tp.new_ssl_socket(rd.io)
    rd = IORudder.new(ssl_socket)
    if agt.net_multiplexer.is_non_blocking
      rd.set_non_blocking
    end

  else
    size = IOUtil.get_sock_recv_buf_size(rd.io)

    tp = PlainTransporter.new(
      agt.net_multiplexer,
      sip,
      true,
      size,
      false)
  end

  proto_hnd = PortBase.get_protocol_handler_store(protocol(), agt_id).rent()
  sip.init_inbound(rd, agt_id, tp, self, proto_hnd)

  st = RudderState.new(rd, tp)
  agt.net_multiplexer.add_rudder_state(rd, st)
  agt.net_multiplexer.req_read(rd)
end

#return_protocol_handler(agt_id, proto_hnd) ⇒ Object



226
227
228
229
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 226

def return_protocol_handler(agt_id, proto_hnd)
  BayLog.debug("%s Return protocol handler", proto_hnd)
  PortBase.get_protocol_handler_store(proto_hnd.protocol, agt_id).Return(proto_hnd)
end

#return_ship(sip) ⇒ Object



231
232
233
234
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 231

def return_ship(sip)
  BayLog.debug("%s end (return ship)", sip)
  PortBase.get_ship_store(sip.agent_id).Return(sip)
end

#secureObject



179
180
181
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 179

def secure()
  return @secure_docker != nil
end

#support_anchoredObject

Abstract methods

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 59

def support_anchored()
  raise NotImplementedError.new
end

#support_unanchoredObject

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/baykit/bayserver/docker/base/port_base.rb', line 63

def support_unanchored()
  raise NotImplementedError.new
end

#to_sObject



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

def to_s()
  return super + "[#{@port}]"
end