Class: ProconBypassMan::ExternalInput::Channels::TCPIPChannel

Inherits:
Base
  • Object
show all
Defined in:
lib/procon_bypass_man/external_input/channels/tcpip_channel.rb

Defined Under Namespace

Classes: AppServer, ShutdownSignal

Instance Method Summary collapse

Constructor Details

#initialize(port:) ⇒ TCPIPChannel

Returns a new instance of TCPIPChannel.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/procon_bypass_man/external_input/channels/tcpip_channel.rb', line 45

def initialize(port: )
  @port = port
  super()

  begin
    @server = AppServer.new('0.0.0.0', @port)
  rescue Errno::EADDRINUSE # NOTE: Address already in use - bind(2) for "0.0.0.0" port XXXX
    ProconBypassMan::SendErrorCommand.execute(error: "[ExternalInput][TCPIPChannel] 起動に失敗しました。#{e.message}")
    @server_thread = Thread.start {}
    return
  end

  # NOTE: masterプロセスで起動する
  @server_thread = Thread.start do
    begin
      loop do
        @server.start_server
        @server.run
      end
    rescue Errno::EPIPE, EOFError, Errno::ECONNRESET => e
      ProconBypassMan::SendErrorCommand.execute(error: "[ExternalInput][TCPIPChannel] #{e.message}(#{e})")
      sleep(5)

      @server.shutdown
      retry
    rescue ShutdownSignal => e
      ProconBypassMan::SendErrorCommand.execute(error: "[ExternalInput][TCPIPChannel] ShutdownSignalを受け取りました。終了します。")
      @server.shutdown
    rescue => e
      ProconBypassMan::SendErrorCommand.execute(error: "[ExternalInput][TCPIPChannel] #{e.message}(#{e})")
    end
  end
end

Instance Method Details

#alive_server?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/procon_bypass_man/external_input/channels/tcpip_channel.rb', line 113

def alive_server?
  return false if not @server_thread.alive?

  begin
    TCPSocket.new('0.0.0.0', @port).close
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
    return false
  end

  true
end

#display_name_for_boot_messageObject



125
126
127
# File 'lib/procon_bypass_man/external_input/channels/tcpip_channel.rb', line 125

def display_name_for_boot_message
  "TCPIP(port: #{@port})"
end

#readString, NilClass

NOTE: bypassプロセスから呼ばれ、masterプロセスへ繋ぐ

Returns:

  • (String, NilClass)


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
# File 'lib/procon_bypass_man/external_input/channels/tcpip_channel.rb', line 81

def read
  @socket ||= TCPSocket.new('0.0.0.0', @port)
  read_command = "\n"
  @socket.write(read_command)
  response = @socket.gets&.strip
  # ProconBypassMan.logger.debug { "Received: #{response}" }

  case response
  when /^{/
    return response
  when /^EMPTY/, ''
    return nil
  else
    ProconBypassMan.logger.warn { "[ExternalInput][TCPIPChannel] Unknown response(#{response}, codepoints: #{response.codepoints})" }
    return nil
  end
rescue Errno::EPIPE, EOFError => e
  @socket = nil
  sleep(10)
  ProconBypassMan.logger.error { "[ExternalInput][TCPIPChannel] #{e.message}!!!!!!!(#{e})" }
  retry
rescue => e
  @socket = nil
  ProconBypassMan.logger.error { "[ExternalInput][TCPIPChannel] #{e.message} が起きました(#{e})" }
  return nil
end

#shutdownObject



108
109
110
111
# File 'lib/procon_bypass_man/external_input/channels/tcpip_channel.rb', line 108

def shutdown
  ProconBypassMan.logger.info { "[ExternalInput][TCPIPChannel] shutdown" }
  @server_thread.raise(ShutdownSignal)
end