Class: Yakr
- Inherits:
-
Object
- Object
- Yakr
- 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
- .connect(host, port) ⇒ Object
- .exit_with_banner(x) ⇒ Object
- .listen(port, max_lines) ⇒ Object
- .run ⇒ Object
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.(1) end rescue Interrupt puts "\nCancelled, exiting.." exit 1 rescue Timeout::Error => ex puts "host connect: timed out" self.(1) rescue => e puts "host connect: unable to connect #{e}" self.(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.(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 |
.run ⇒ Object
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.(1) end # start processing command line arguments begin = OptionReader.parse(ARGV) rescue OptionParser::InvalidOption => t puts t self.(1) rescue OptionParser::MissingArgument => m puts m self.(1) end # validate mode options if (.connect_host or .connect_port) and (.listen_port or .limit_lines) then puts "mode options: specify either listen or connect options" self.(1) elsif not (.connect_host or .listen_port) puts "mode options: specify listen or connect options" self.(1) end # validate server options if .connect_port and not .connect_host puts "host connect: no host specified" self.(1) end if .connect_host and not .connect_port puts "host connect: no port specified" self.(1) end # validate connection port if .connect_port if not (.connect_port.to_i > 0 and .connect_port.to_i <= 65535) puts "invalid port: #{.connect_port.to_s}" self.(1) end end # validate listen port if .listen_port if not (.listen_port.to_i > 0 and .listen_port.to_i <= 65535) puts "invalid port: #{.listen_port.to_s}" self.(1) end end # validate line limit if .limit_lines and not .limit_lines.to_i > 0 puts "invalid max lines: #{.limit_lines}" self.(1) end if .connect_host connect(.connect_host, .connect_port.to_i) else listen(.listen_port.to_i, .limit_lines.to_i) end end |