Module: Envoy::Server::Trunk

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

Constant Summary

Constants included from Protocol

Protocol::VERBOSITIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#receive_object, #send_object, #serializer, #verbosity

Class Method Details

.trunksObject



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

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

Instance Method Details

#channelsObject



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

def channels
  @channels ||= {}
end

#halt(message = nil) ⇒ Object



70
71
72
73
74
# File 'lib/envoy/server/trunk.rb', line 70

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

#hostsObject



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

def hosts
  @hosts ||= []
end

#initialize(key) ⇒ Object



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

def initialize key
  super
  @key = key
end

#keyObject



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

def key
  @options[:key]
end

#log(message) ⇒ Object



57
58
59
60
# File 'lib/envoy/server/trunk.rb', line 57

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

#message(level, message) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/envoy/server/trunk.rb', line 62

def message (level, message)
  if @options[:verbosity]
    send_object :message, message, level
  else
    send_object :message, message
  end
end

#post_initObject



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

def post_init
  self.comm_inactivity_timeout = 60
end

#receive_close(id, code = nil) ⇒ Object



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

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

#receive_options(options) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/envoy/server/trunk.rb', line 80

def receive_options options
  @options = options
  receive_pong if version? "> 0.1"
  if version? "< #{Envoy::VERSION}"
    message WARN, "Your client is out of date. Please upgrade to #{Envoy::VERSION}."
  elsif version? "> #{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"
      message FATAL, "label is reserved: `#{label}'"
      true
    elsif label =~ /\./
      message FATAL, "label is invalid: `#{label}'"
      true
    elsif other_trunk = Trunk.trunks[label][0]
      unless other_trunk.key == key
        message 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 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)
    message INFO, "Service access key is `#{@options[:key]}'"
  end
  send_object :confirm, @options if version? ">= 0.2.2"
end

#receive_pongObject



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

def receive_pong
  EM.add_timer 30 do
    send_object :ping
  end
end

#receive_start_tlsObject



42
43
44
45
# File 'lib/envoy/server/trunk.rb', line 42

def receive_start_tls
  send_object :start_tls
  start_tls
end

#receive_stream(id, data) ⇒ Object



47
48
49
50
51
# File 'lib/envoy/server/trunk.rb', line 47

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

#unbindObject



123
124
125
126
127
# File 'lib/envoy/server/trunk.rb', line 123

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

#version?(*requirement) ⇒ Boolean

Returns:

  • (Boolean)


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

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