stream {
  upstream dynamic_server0 {
    server 127.0.0.1:58080;
  }
  upstream dynamic_server1 {
    server 127.0.0.1:58081;
  }
  upstream static_server0 {
    server 127.0.0.1:58080;
  }

  mruby_stream_init_code '
    p "ngx_mruby: STREAM: mruby_stream_init_code"
    Userdata.new.new_upstream = "127.0.0.1:58081"
  ';

  mruby_stream_init_worker_code '
    p "ngx_mruby: STREAM: mruby_stream_init_worker_code"
  ';

  mruby_stream_exit_worker_code '
    p "ngx_mruby: STREAM: mruby_stream_exit_worker_code"
  ';

  server {
      listen 12346;
      mruby_stream_code '
        c = Nginx::Stream::Connection.new "dynamic_server0"
        # get from mruby_stream_init
        c.upstream_server = Userdata.new.new_upstream
        Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "mruby_stream"
      ';
      proxy_pass dynamic_server0;
  }

  # test for add listener
  server {
      mruby_stream_server_context_code '
        Nginx::Stream.add_listener({address: "127.0.0.1:12350"})
        Nginx::Stream.add_listener({address: "12351"})
      ';
      proxy_pass static_server0;
  }

  # test for dynamic tcp load balancer
  # upstream changed from 127.0.0.1:58081 to 127.0.0.1:58080 in mruby
  server {
      listen 12345;
      # cached only
      mruby_stream build/nginx/html/stream_lb.rb;
      proxy_pass dynamic_server1;
  }
  server {
      listen 12347;
      mruby_stream_code '
        if Nginx::Stream::Connection.remote_ip == "127.0.0.1"
          current_status = Nginx::Stream::Connection.stream_status
          Nginx::Stream::Connection.stream_status = Nginx::Stream::ABORT
          Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "current status=#{(current_status == Nginx::Stream::DECLINED) ? "NGX_DECLINED" : current_status} but deny from #{Nginx::Stream::Connection.remote_ip} return NGX_ABORT"
        end
      ';
      proxy_pass dynamic_server1;
  }

  server {
      listen 127.0.0.1:12348;
      mruby_stream_code '
        Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "local is #{Nginx::Stream::Connection.local_ip}:#{Nginx::Stream::Connection.local_port} remote_port is #{Nginx::Stream::Connection.remote_ip}:#{Nginx::Stream::Connection.remote_port}"
        if Nginx::Stream::Connection.local_port != 12348 && Nginx::Stream::Connection.local_ip != "127.0.0.1"
          Nginx::Stream::Connection.stream_status = Nginx::Stream::ABORT
        end
      ';
      proxy_pass static_server0;
  }

  server {
      listen 127.0.0.1:12349;
      mruby_stream_code '
        Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "local_ip_port is #{Nginx::Stream::Connection.local_ip_port}"
        if Nginx::Stream::Connection.local_ip_port != "127.0.0.1:12349"
          Nginx::Stream::Connection.stream_status = Nginx::Stream::ABORT
        end
      ';
      proxy_pass static_server0;
  }

  server {
      listen 127.0.0.1:12352;
      mruby_stream_code '
        c = Nginx::Stream::Connection.new "dynamic_server1"
        before = Nginx::Stream::Connection.remote_port
        Nginx::Stream::Async.sleep 3000
        after = Nginx::Stream::Connection.remote_port

        Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "before port: #{before} after port: #{after}"
        Nginx::Stream::Connection.stream_status = Nginx::Stream::ABORT if before != after
      ';
      proxy_pass static_server0;
  }
}

