module Envoy::Server::Trunk

Public Class Methods

new(key) click to toggle source
Calls superclass method
# File lib/envoy/server/trunk.rb, line 9
def initialize key
  super
  @key = key
end
trunks() click to toggle source
# File lib/envoy/server/trunk.rb, line 14
def self.trunks
  @trunks ||= Hash.new{|h,k|h[k] = []}
end

Public Instance Methods

channels() click to toggle source
# File lib/envoy/server/trunk.rb, line 26
def channels
  @channels ||= {}
end
halt(message = nil) click to toggle source
# File lib/envoy/server/trunk.rb, line 46
def halt message = nil
  message Envoy::FATAL, message if message
  send_object :halt
  close_connection(true)
end
hosts() click to toggle source
# File lib/envoy/server/trunk.rb, line 22
def hosts
  @hosts ||= []
end
key() click to toggle source
# File lib/envoy/server/trunk.rb, line 30
def key
  @options[:key]
end
log(*args) click to toggle source
# File lib/envoy/server/trunk.rb, line 34
def log (*args)
  Envoy.log(*args)
end
message(level, message) click to toggle source
# File lib/envoy/server/trunk.rb, line 38
def message (level, message)
  if @options[:verbosity]
    send_object :message, message, level
  else
    send_object :message, message
  end
end
post_init() click to toggle source
# File lib/envoy/server/trunk.rb, line 18
def post_init
  self.comm_inactivity_timeout = 60
end
receive_close(id, code = nil) click to toggle source
# File lib/envoy/server/trunk.rb, line 72
def receive_close id, code = nil
  if chan = channels[id]
    chan.web.close(code)
    channels.delete id
  end
end
receive_options(options) click to toggle source
# File lib/envoy/server/trunk.rb, line 85
def receive_options options
  @options = options
  receive_pong if version? "> 0.1"
  if version? "< #{Envoy::VERSION}"
    message Envoy::WARN, "Your client is out of date. Please upgrade to #{Envoy::VERSION}."
  elsif version? "> #{Envoy::VERSION}"
    message Envoy::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"
      message Envoy::FATAL, "label is reserved: `#{label}'"
      true
    elsif label =~ /\./
      message Envoy::FATAL, "label is invalid: `#{label}'"
      true
    elsif other_trunk = Trunk.trunks[label][0]
      unless other_trunk.key == key
        message Envoy::FATAL, "label is protected with a key: `#{label}'"
        true
      end
    end
  end && halt
  if hosts.empty?
    hosts = [SecureRandom.random_number(36 ** 4).to_s(36)]
  end
  @hosts = hosts.each do |host|
    Trunk.trunks[host] << self
    message Envoy::INFO, "Service accessible at http://#{host}.#{$zone}/"
  end
  unless @options[:key]
    @options[:key] = SecureRandom.hex(8)
    message Envoy::INFO, "Service access key is `#{@options[:key]}'"
  end
  send_object :confirm, @options if version? ">= 0.2.2"
end
receive_pong() click to toggle source
# File lib/envoy/server/trunk.rb, line 61
def receive_pong
  unless @pinged
    send_object :ping
    @pinged = true
  else
    EM.add_timer 30 do
      send_object :ping
    end
  end
end
receive_start_tls() click to toggle source
# File lib/envoy/server/trunk.rb, line 56
def receive_start_tls
  send_object :start_tls
  start_tls
end
receive_stream(id, data) click to toggle source
# File lib/envoy/server/trunk.rb, line 79
def receive_stream id, data
  c = channels[id]
  w = c && c.web
  w && w.send_data(data)
end
unbind() click to toggle source
# File lib/envoy/server/trunk.rb, line 126
def unbind
  hosts.each do |host|
    Trunk.trunks[host].delete self
  end
end
version?(*requirement) click to toggle source
# File lib/envoy/server/trunk.rb, line 52
def version? *requirement
  Gem::Requirement.new(*requirement) =~ Gem::Version.new(@options[:version])
end