-
-
Save keltia/e3bc40e9b2b1d84da5f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# | |
# @abstract Poor Man's version of a FlightAware client | |
# | |
# @author Ollivier Robert <ollivier.robert@eurocontrol.int> | |
# @copyright 2015 by Ollivier Robert for ECTL | |
# | |
# Usual housekeeping variables | |
BASEDIR = File.dirname(File.expand_path($0)) | |
$LOAD_PATH << File.join(BASEDIR, '..', 'lib') | |
# Standard modules | |
require 'openssl' | |
require 'yaml' | |
# External modules | |
require 'celluloid/io' | |
# Default config file | |
DEF_CONFIG = File.join(ENV['HOME'], '.flightaware', 'config.yml') | |
# Small class to avoid putting login/pwd info | |
class MyConfig | |
attr_reader :user, :password, :site, :port | |
def initialize(path) | |
cfg = {} | |
if File.exist?(path) | |
File.open(path) do |fh| | |
cfg = YAML.load(fh) | |
end | |
else | |
raise "File not present: #{path}" | |
end | |
@user = cfg['user'] | |
@password = cfg['password'] | |
@site = cfg['site'] | |
@port = cfg['port'] | |
end | |
def self.load(path = DEF_CONFIG) | |
MyConfig::new(path) | |
end | |
end | |
trap(:INT) { puts("INT: stopping..."); exit(0) } | |
trap(:TERM) { puts("TERM: stopping..."); exit(0) } | |
module FlightAware | |
class Client | |
include Celluloid::IO | |
def initialize(config) | |
puts("Connecting to #{config.site}:#{config.port} using TLS.") | |
raw_socket = TCPSocket.new(config.site, config.port) | |
puts(" Initiating TLS negociation") | |
@ssl = SSLSocket.new(raw_socket) | |
@ssl.connect | |
puts(" Authenticating to FlightAware") | |
@ssl.write("live version 4.0 username #{config.user} password #{config.password} events \"position\"\n") | |
puts("Init done.") | |
async.run | |
end | |
# Read buffer, one line at a time | |
# | |
def run | |
buf = @ssl.read | |
puts("#{buf.size} bytes read.") | |
puts(buf) | |
end | |
def wait | |
sleep(1) | |
end | |
end | |
end | |
def main(argv) | |
config = MyConfig.load | |
fa = FlightAware::Client.new(config) | |
loop { fa.run } | |
end | |
if __FILE__ == $0 | |
exit(main(ARGV) || 1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment