Class: Vagrant::Notify::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-notify/server.rb

Constant Summary collapse

HTTP_RESPONSE =
"Hi! You just reached the vagrant notification server"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, machine_name = :default, provider = :virtualbox) ⇒ Server

Returns a new instance of Server.



22
23
24
25
26
# File 'lib/vagrant-notify/server.rb', line 22

def initialize(id, machine_name = :default, provider = :virtualbox)
  @id           = id
  @machine_name = machine_name
  @provider     = provider
end

Class Method Details

.run(env, port) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vagrant-notify/server.rb', line 6

def self.run(env, port)
  id           = env[:machine].id
  machine_name = env[:machine].name
  provider     = env[:machine].provider_name
  fork do
    $0 = "vagrant-notify-server (#{port})"
    tcp_server = TCPServer.open(port)
    server = self.new(id, machine_name, provider)
    loop {
      Thread.start(tcp_server.accept) { |client|
        server.receive_data(client)
      }
    }
  end
end

Instance Method Details

#receive_data(client) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-notify/server.rb', line 28

def receive_data(client)
  args = read_args(client)
  if http_request?(args)
    client.puts HTTP_RESPONSE
  else
    fix_icon_path!(args)
    system("notify-send #{args}")
  end
  client.close
rescue => ex
  log ex.message
end