Class: Yakr

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

Constant Summary collapse

VERSION =
"0.2.4"
REQUIRE_SERVER =
0.1
USAGE_BANNER =
"Use `#{File.basename($0)} --help` for available options."

Class Method Summary collapse

Class Method Details

.connect(host, port) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/yakr.rb', line 105

def self.connect(host, port)
  
  begin
    timeout(20) do
      
      puts "Attempting connection.."
      
      # establish connection
      @server = TCPSocket.open(host, port)
      
      # wait for server response
      @line = @server.readline
    end
  
    if @line.start_with?('yakr=>')
      
      puts "yakr: connected to #{host} on port #{port}"
      
      # read from standard input and send
      # text line by line to server
      STDIN.each do |str|
        @server.puts "#{str}"
      end
    else
      puts "host connect: unexpected server response"
      self.exit_with_banner(1)
    end
  
  rescue Interrupt
    puts "\nCancelled, exiting.."
    exit 1
  rescue Timeout::Error => ex
    puts "host connect: timed out"
    self.exit_with_banner(1)
  rescue => e
    puts "host connect: unable to connect #{e}"
    self.exit_with_banner(1)
  ensure
    @server.close unless @server.nil?
  end    
end

.exit_with_banner(x) ⇒ Object



172
173
174
175
# File 'lib/yakr.rb', line 172

def self.exit_with_banner(x)
  puts USAGE_BANNER
  exit x
end

.listen(port, max_lines) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/yakr.rb', line 147

def self.listen(port, max_lines)
  begin
    limit_output = true if max_lines > 0
    @server = TCPServer.open(port)
    loop do
      client = @server.accept
      client.puts "yakr=>version:#{VERSION}"
      client.each do |str|         
        exit 0 if max_lines <= 0 and limit_output
        puts "#{str}"
        max_lines -= 1 if limit_output
      end
    end
  rescue Interrupt
    puts "\nCancelled, exiting.."
    exit 1
  rescue SystemExit
  rescue
    puts "listen server: unable to start"
    exit 1
  ensure
    @server.close unless @server.nil?
  end
end

.runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yakr.rb', line 37

def self.run
  
  # test for zero arguments
  if ARGV.empty? then
    self.exit_with_banner(1)
  end
  
  # start processing command line arguments
  begin
    options = OptionReader.parse(ARGV)
  rescue OptionParser::InvalidOption => t
    puts t
    self.exit_with_banner(1)
  rescue OptionParser::MissingArgument => m
    puts m
    self.exit_with_banner(1)
  end
  
  # validate mode options
  if (options.connect_host or options.connect_port) and
     (options.listen_port or options.limit_lines) then
    puts "mode options: specify either listen or connect options"
    self.exit_with_banner(1)
  elsif not (options.connect_host or options.listen_port)
    puts "mode options: specify listen or connect options"
    self.exit_with_banner(1)
  end
  
  # validate server options
  if options.connect_port and not options.connect_host
    puts "host connect: no host specified"
    self.exit_with_banner(1)
  end
  
  if options.connect_host and not options.connect_port
    puts "host connect: no port specified"
    self.exit_with_banner(1)
  end
  
  # validate connection port
  if options.connect_port
    if not (options.connect_port.to_i > 0 and options.connect_port.to_i <= 65535)
      puts "invalid port: #{options.connect_port.to_s}"
      self.exit_with_banner(1)
    end
  end
  
  # validate listen port
  if options.listen_port
    if not (options.listen_port.to_i > 0 and options.listen_port.to_i <= 65535)
      puts "invalid port: #{options.listen_port.to_s}"
      self.exit_with_banner(1)
    end
  end
  
  # validate line limit
  if options.limit_lines and not options.limit_lines.to_i > 0
      puts "invalid max lines: #{options.limit_lines}"
      self.exit_with_banner(1)
  end
  
  if options.connect_host
    connect(options.connect_host, options.connect_port.to_i)
  else
    listen(options.listen_port.to_i, options.limit_lines.to_i)
  end
end