class Apex::IGateTcp

Constants

DEFAULT_APRSIS_FILTER_PORT
DEFAULT_APRSIS_SERVER
DEFAULT_APRSIS_URL

Public Class Methods

new(user, password='-1') click to toggle source
# File lib/apex/igate_tcp.rb, line 10
def initialize(user, password='-1')
    @user = user
    @auth = ['user', user, 'pass', password, 'vers', "APEX #{VERSION}"].join(' ')
    @aprsis_sock = nil
    @data_buffer = ''
    @packet_buffer = []
    @lock = Mutex.new
end

Private Class Methods

decode_frame(frame) click to toggle source
# File lib/apex/igate_tcp.rb, line 42
def self.decode_frame(frame)
    decoded_frame = {}
    frame_so_far = ''
    path = nil
    frame.chars.each do |char|
        if char == '>' and !decoded_frame.include? :source
            decoded_frame[:source] = frame_so_far
            frame_so_far = ''
        elsif char == ':' and !path
            path = frame_so_far
            frame_so_far = ''
        else
            frame_so_far = [frame_so_far, char].join
        end
    end

    path = path.split(',')
    decoded_frame[:destination] = path.shift
    decoded_frame[:path] = path
    decoded_frame[:text] = frame_so_far

    decoded_frame
end
encode_frame(frame) click to toggle source
# File lib/apex/igate_tcp.rb, line 31
def self.encode_frame(frame)
    formatted_frame = [frame[:source], frame[:destination]].join('>')
    if frame[:path] and frame[:path].length > 0
        formatted_frame = [formatted_frame, IGateTcp::format_path(frame[:path])].join(',')
    end
    formatted_frame += ':'
    formatted_frame += frame[:text]
    return formatted_frame
end
format_path(path_list) click to toggle source
# File lib/apex/igate_tcp.rb, line 26
def self.format_path(path_list)
    path_list.join(',')
end

Public Instance Methods

close(*args, **kwargs) click to toggle source
# File lib/apex/igate_tcp.rb, line 95
def close(*args, **kwargs)
    @lock.synchronize do
        if @aprsis_sock
            @aprsis_sock.close
            reset_buffer
            @aprsis_sock = nil
        end
    end
end
connect(server=nil, port=nil, aprs_filter=nil, *args, **kwargs) click to toggle source
# File lib/apex/igate_tcp.rb, line 67
def connect(server=nil, port=nil, aprs_filter=nil, *args, **kwargs)
    @lock.synchronize do
        unless @aprsis_sock
            reset_buffer

            unless server
                server = DEFAULT_APRSIS_SERVER
            end

            unless port
                port = DEFAULT_APRSIS_FILTER_PORT
            end

            if aprs_filter.nil?
                @full_auth = @auth
            else
                @full_auth = [@auth, 'filter', aprs_filter].join(' ')
            end

            @server = server
            @port = port
            @aprsis_sock = TCPSocket.open(@server, @port)
            @aprsis_sock.puts( @full_auth + "\r\n" )
        end
    end
end
read(filter_logresp=true, *args, **kwargs) click to toggle source
# File lib/apex/igate_tcp.rb, line 106
def read(filter_logresp=true, *args, **kwargs)
    @lock.synchronize do
        # check if there is any data waiting
        read_more = true
        while read_more
            begin
                 read_line = @aprsis_sock.read_nonblock(100)
                 unless read_line.nil?
                     @data_buffer << read_line
                 end
            rescue IO::WaitReadable
                 read_more = false
            end
        end

        # check for any complete packets and move them to the packet buffer
        if @data_buffer.include? "\r\n"
            partial = true
            if @data_buffer.end_with? "\r\n"
                partial = false
            end

            packets = @data_buffer.split("\r\n")
            if partial
                @data_buffer = packets.pop.dup
            else
                @data_buffer = ''
            end

            packets.each do |packet|
                @packet_buffer << packet.dup
            end
        end

        # return the next packet that matches the filter
        while @packet_buffer.length > 0
            packet = @packet_buffer.pop
            unless (filter_logresp and packet.start_with?('#'))
                return IGateTcp::decode_frame(packet)
            end
        end

        return nil
    end
end
write(frame, *args, **kwargs) click to toggle source
# File lib/apex/igate_tcp.rb, line 153
def write(frame, *args, **kwargs)
    @lock.synchronize do
        encoded_frame = IGateTcp::encode_frame(frame)
        @aprsis_sock.puts( encoded_frame )
    end
end

Private Instance Methods

reset_buffer() click to toggle source
# File lib/apex/igate_tcp.rb, line 20
def reset_buffer
    @data_buffer = ''
    @packet_buffer = []
end