Class: Lorraine::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/lorraine/server.rb

Class Method Summary collapse

Class Method Details

.start(port) ⇒ Object



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
# File 'lib/lorraine/server.rb', line 8

def self.start(port)
  begin
    serial_connection = Lorraine::SerialConnection.new      
  rescue
    "Failed to start serial connection."
  end
  Thin::Server.start('0.0.0.0', port) do
    
    map "/admin" do
      files = File.expand_path(File.join(__FILE__, "..", "..", "..", "web"))
      puts "serving files from #{files}"
      run Rack::File.new(files)
    end
    
    Faye::WebSocket.load_adapter('thin')
    faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
    faye_server.bind(:publish) do |client_id, channel, data|
      begin
        # Process incoming things
        puts "Received message data: #{data}"
        m = Lorraine::Message.from_packet(data)
        puts "Interpereted as: #{m}"
        serial_connection.write_message(m)
        # serial_connection.write_message Lorraine::Message.new(:refresh)
      rescue
        puts "Failed to read data."
      end
    end
    run faye_server
  end
end