-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathansi_server.rb
executable file
·49 lines (38 loc) · 1 KB
/
ansi_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
$LOAD_PATH << File.expand_path('../lib', __dir__)
require 'ssdp'
require 'eventmachine'
require 'net/http'
require 'json'
require 'ansi_server'
require 'ansi_reader'
require 'url_carousel'
STDOUT.sync = true
# TODO: args?
puts 'Booting server'
producer = SSDP::Producer.new()
producer.add_service('ansi', 'AL': 'lol', 'LOCATION': 'server')
thread = producer.start
carousel = UrlCarousel.new('http://asm.dj/ansi/index.json')
EventMachine.run do
EventMachine.start_server('0.0.0.0', 1337, AnsiServer, carousel)
EventMachine::PeriodicTimer.new(0.3) do
AnsiServer.tick
carousel.reader.advance
end
EventMachine::PeriodicTimer.new(600) do
carousel.next
end
EventMachine::PeriodicTimer.new(1) do
# This happens when the wifi radio bonks
begin
if not thread.alive?
puts 'restarting SSDP thread'
producer.stop(false) # false -> no bye bye
thread = producer.start
end
rescue => e
puts 'Caught: ' + e.message
end
end
end