Skip to content

Instantly share code, notes, and snippets.

@keltia

keltia/fa-export Secret

Created January 22, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keltia/e3bc40e9b2b1d84da5f8 to your computer and use it in GitHub Desktop.
Save keltia/e3bc40e9b2b1d84da5f8 to your computer and use it in GitHub Desktop.
#! /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