lib/envoy/server/trunk.rb in envoy-proxy-0.2.0 vs lib/envoy/server/trunk.rb in envoy-proxy-0.2.2

- old
+ new

@@ -3,15 +3,10 @@ module Envoy module Server module Trunk include Protocol - def log message - t = Time.now.strftime("%F %T") - STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ") - end - def initialize key super @key = key end @@ -31,11 +26,10 @@ @channels ||= {} end def receive_pong EM.add_timer 5 do - log "ping" send_object :ping end end def receive_close id, code = nil @@ -58,58 +52,73 @@ def key @options[:key] end + def log message + t = Time.now.strftime("%F %T") + STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ") + end + + def message (level, message) + if @options[:verbosity] + send_object :message, message, level + else + send_object :message, message + end + end + def halt message = nil - send_object :message, message if message + message FATAL, message if message send_object :halt close_connection(true) end def version? *requirement Gem::Requirement.new(*requirement) =~ Gem::Version.new(@options[:version]) end def receive_options options @options = options - if version? "~> 0.1" - receive_pong - end + receive_pong if version? "> 0.1" if version? "< #{Envoy::VERSION}" - send_object :message, "Your client is out of date. Please upgrade to #{Envoy::VERSION}." + message WARN, "Your client is out of date. Please upgrade to #{Envoy::VERSION}." elsif version? "> #{Envoy::VERSION}" - send_object :message, "Your client is from the future. The server is expecting #{Envoy::VERSION}." + message WARN, "Your client is from the future. The server is expecting #{Envoy::VERSION}." end if @key and @key != @options[:key] halt "Key is invalid" return end hosts = @options[:hosts] || [] hosts.any? do |label| if label == "s" - send_object :message, "label is reserved: `#{label}'" + message FATAL, "label is reserved: `#{label}'" true elsif label =~ /\./ - send_object :message, "label is invalid: `#{label}'" + message FATAL, "label is invalid: `#{label}'" true elsif other_trunk = Trunk.trunks[label][0] unless other_trunk.key == key - send_object :message, "label is protected with a key: `#{label}'" + message FATAL, "label is protected with a key: `#{label}'" true end end end && halt - hosts << SecureRandom.random_number(36 ** 4).to_s(36) if hosts.empty? - m = ["#{options[:local_host]}:#{options[:local_port]} now available at:"] - @hosts = hosts.each do |host| - Trunk.trunks[host] << self - send_object :message, "host: #{host}.#{$zone}" + if hosts.empty? + hosts = [SecureRandom.random_number(36 ** 4).to_s(36)] + message INFO, "Service accessible at http://#{hosts[0]}.#{$zone}/" + else + @hosts = hosts.each do |host| + Trunk.trunks[host] << self + message INFO, "Service accessible at http://#{host}.#{$zone}/" + end end unless @options[:key] - @options[:key] ||= SecureRandom.hex(8) - send_object :message, "key: #{@options[:key]}" + @options[:key] = SecureRandom.hex(8) + message INFO, "Service access key is `#{@options[:key]}'" end + send_object :confirm, @options if version? ">= 0.2.2" end def unbind hosts.each do |host| Trunk.trunks[host].delete self