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



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

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

Instance Method Details

#channelsObject



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

def channels
  @channels ||= {}
end

#halt(message = nil) ⇒ Object



46
47
48
49
50
# 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

#hostsObject



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

def hosts
  @hosts ||= []
end

#initialize(key) ⇒ Object



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

def initialize key
  super
  @key = key
end

#keyObject



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

def key
  @options[:key]
end

#log(*args) ⇒ Object



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

def log (*args)
  Envoy.log(*args)
end

#message(level, message) ⇒ Object



38
39
40
41
42
43
44
# 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_initObject



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

def post_init
  self.comm_inactivity_timeout = 60
end

#receive_close(id, code = nil) ⇒ Object



67
68
69
70
71
72
# File 'lib/envoy/server/trunk.rb', line 67

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

#receive_options(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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)]
    message Envoy::INFO, "Service accessible at http://#{hosts[0]}.#{$zone}/"
  else
    @hosts = hosts.each do |host|
      Trunk.trunks[host] << self
      message Envoy::INFO, "Service accessible at http://#{host}.#{$zone}/"
    end
  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_pongObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/envoy/server/trunk.rb', line 56

def receive_pong
  unless @pinged
    send_object :ping
    @pinged = true
  else
    EM.add_timer 30 do
      send_object :ping
    end
  end
end

#receive_start_tlsObject



74
75
76
77
# File 'lib/envoy/server/trunk.rb', line 74

def receive_start_tls
  send_object :start_tls
  start_tls
end

#receive_stream(id, data) ⇒ Object



79
80
81
82
83
# 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

#unbindObject



128
129
130
131
132
# File 'lib/envoy/server/trunk.rb', line 128

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

#version?(*requirement) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/envoy/server/trunk.rb', line 52

def version? *requirement
  Gem::Requirement.new(*requirement) =~ Gem::Version.new(@options[:version])
end