Module: Envoy::Server::Trunk

Includes:
Protocol
Defined in:
lib/envoy/server/trunk.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#receive_object, #send_object, #serializer

Class Method Details

.trunksObject



18
19
20
# File 'lib/envoy/server/trunk.rb', line 18

def self.trunks
  @trunks ||= Hash.new{|h,k|h[k] = []}
end

Instance Method Details

#channelsObject



30
31
32
# File 'lib/envoy/server/trunk.rb', line 30

def channels
  @channels ||= {}
end

#halt(message = nil) ⇒ Object



59
60
61
62
63
# File 'lib/envoy/server/trunk.rb', line 59

def halt message = nil
  send_object :message, message if message
  send_object :halt
  close_connection(true)
end

#hostsObject



26
27
28
# File 'lib/envoy/server/trunk.rb', line 26

def hosts
  @hosts ||= []
end

#initialize(key) ⇒ Object



13
14
15
16
# File 'lib/envoy/server/trunk.rb', line 13

def initialize key
  super
  @key = key
end

#keyObject



55
56
57
# File 'lib/envoy/server/trunk.rb', line 55

def key
  @options[:key]
end

#log(message) ⇒ Object



8
9
10
11
# File 'lib/envoy/server/trunk.rb', line 8

def log message
  t = Time.now.strftime("%F %T")
  STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ")
end

#post_initObject



22
23
24
# File 'lib/envoy/server/trunk.rb', line 22

def post_init
  self.comm_inactivity_timeout = 25
end

#receive_close(id, code = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/envoy/server/trunk.rb', line 37

def receive_close id, code = nil
  if chan = channels[id]
    chan.web.close(code)
    channels.delete id
  end
end

#receive_options(options) ⇒ Object



65
66
67
68
69
70
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
# File 'lib/envoy/server/trunk.rb', line 65

def receive_options options
  @options = options
  if (@options[:version].split(".").map(&:to_i) <=> [0, 1, 0]) > -1
    EM.add_periodic_timer 5 do
      send_object :ping
    end
  else
    EM.add_periodic_timer 20 do
      send_object :message, "Checking client is still here. Upgrade to hide this message."
    end
  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}: label is reserved"
      true
    elsif label =~ /\./
      send_object :message, "#{label}: labels may not contain dots"
      true
    elsif other_trunk = Trunk.trunks[label][0]
      unless other_trunk.key == key
        send_object :message, "#{label}: label in use, and you don't have the key"
        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
    m << "http://#{host}.#{$zone}/"
  end
  send_object :message, m.join(" ")
  unless @options[:key]
    @options[:key] ||= SecureRandom.hex(8)
    send_object :message, "Your key is #{@options[:key]}"
  end
end

#receive_pongObject



34
35
# File 'lib/envoy/server/trunk.rb', line 34

def receive_pong
end

#receive_start_tlsObject



44
45
46
47
# File 'lib/envoy/server/trunk.rb', line 44

def receive_start_tls
  send_object :start_tls
  start_tls
end

#receive_stream(id, data) ⇒ Object



49
50
51
52
53
# File 'lib/envoy/server/trunk.rb', line 49

def receive_stream id, data
  c = channels[id]
  w = c && c.web
  w && w.send_data(data)
end

#unbindObject



108
109
110
111
112
# File 'lib/envoy/server/trunk.rb', line 108

def unbind
  hosts.each do |host|
    Trunk.trunks[host].delete self
  end
end