This page must use the Rübÿ Spëëd Mëtäl Cödïng template!!!111

This commit is contained in:
AstRobot 2006-07-25 19:23:42 +00:00
parent 478e595f0c
commit 045236affd
1 changed files with 4 additions and 26 deletions

View File

@ -1,21 +1,16 @@
=Telnet-Haltestellenmonitor= =Telnet-Haltestellenmonitor=
<pre>#!/usr/bin/env ruby <pre>#!/usr/bin/env ruby
require 'net/http' require 'net/http'
require 'socket' require 'socket'
require 'rexml/document' require 'rexml/document'
class MultipleStations < RuntimeError class MultipleStations < RuntimeError
def initialize(stations) def initialize(stations)
@stations = stations @stations = stations
end end
def to_s def to_s
"Mehrere mögliche Haltestellen: " + @stations.join(', ') "Mehrere mögliche Haltestellen: " + @stations.join(', ')
end end
end end
class StationResult class StationResult
def initialize(card) def initialize(card)
strong_n = 0 strong_n = 0
@ -26,16 +21,13 @@ class StationResult
when 1 when 1
@time = e.text @time = e.text
end end
strong_n += 1 strong_n += 1
} }
@trams = [] @trams = []
card.to_s.scan(/br\/>(\d+:\d+\*?) (.+?)<br\/>-> (.+?)</) { |time,tram,direction| card.to_s.scan(/br\/>(\d+:\d+\*?) (.+?)<br\/>-> (.+?)</) { |time,tram,direction|
@trams << [time, tram, direction] @trams << [time, tram, direction]
} }
end end
def to_s def to_s
column_widths = [4, 5, 4] column_widths = [4, 5, 4]
@trams.each { |a| @trams.each { |a|
@ -43,17 +35,13 @@ class StationResult
column_widths[i] = b.size if b.size > column_widths[i] column_widths[i] = b.size if b.size > column_widths[i]
} }
} }
"\n\n#{@name}, #{@time}:\n\n" + "\n\n#{@name}, #{@time}:\n\n" +
'Zeit'.ljust(column_widths[0]) + ' | ' + 'Zeit'.ljust(column_widths[0]) + ' | ' +
'Linie'.ljust(column_widths[1]) + ' | ' + 'Linie'.ljust(column_widths[1]) + ' | ' +
'Ziel'.ljust(column_widths[2]) + "\n" + 'Ziel'.ljust(column_widths[2]) + "\n" +
('-' * column_widths[0]) + '-+-' + ('-' * column_widths[0]) + '-+-' +
('-' * column_widths[1]) + '-+-' + ('-' * column_widths[1]) + '-+-' +
('-' * column_widths[2]) + "\n" + ('-' * column_widths[2]) + "\n" +
@trams.collect { |time,tram,direction| @trams.collect { |time,tram,direction|
time.ljust(column_widths[0]) + ' | ' + time.ljust(column_widths[0]) + ' | ' +
tram.ljust(column_widths[1]) + ' | ' + tram.ljust(column_widths[1]) + ' | ' +
@ -61,12 +49,10 @@ class StationResult
}.join("\n") }.join("\n")
end end
end end
class ClientHandler class ClientHandler
def initialize(socket) def initialize(socket)
@socket = socket @socket = socket
puts "#{address} connected" puts "#{address} connected"
Thread.new { Thread.new {
begin begin
handle handle
@ -77,7 +63,6 @@ class ClientHandler
end end
} }
end end
def address def address
if @socket.peeraddr[0] == 'AF_INET6' if @socket.peeraddr[0] == 'AF_INET6'
"[#{@socket.peeraddr[3]}]" "[#{@socket.peeraddr[3]}]"
@ -86,7 +71,6 @@ class ClientHandler
end + end +
":#{@socket.peeraddr[1]}" ":#{@socket.peeraddr[1]}"
end end
def ask_haltestellenmonitor(station) def ask_haltestellenmonitor(station)
param = { :station => station, param = { :station => station,
:action => :check, :action => :check,
@ -98,7 +82,6 @@ class ClientHandler
res = Net::HTTP.start('wap.dvbag.de') { |http| res = Net::HTTP.start('wap.dvbag.de') { |http|
http.get('/wapVVO/wap-rbl.php?' + param_s) http.get('/wapVVO/wap-rbl.php?' + param_s)
} }
if res.kind_of? Net::HTTPSuccess if res.kind_of? Net::HTTPSuccess
wml = REXML::Document.new(res.body).root wml = REXML::Document.new(res.body).root
card = nil card = nil
@ -109,7 +92,6 @@ class ClientHandler
card.each_element('p/select/option') { |option| card.each_element('p/select/option') { |option|
stations << option.text stations << option.text
} }
raise MultipleStations.new(stations) raise MultipleStations.new(stations)
elsif card.attributes['id'] == 'result' elsif card.attributes['id'] == 'result'
StationResult.new(card).to_s StationResult.new(card).to_s
@ -123,31 +105,27 @@ class ClientHandler
raise "#{res.class}" raise "#{res.class}"
end end
end end
def handle def handle
@socket.print "Hallo #{address}\n\nHaltestelle: " @socket.print "Hallo #{address}\n\nHaltestelle: "
@socket.flush @socket.flush
haltestelle = @socket.gets haltestelle = @socket.gets
if haltestelle if haltestelle
haltestelle.strip! haltestelle.strip!
puts "#{address} asks for #{haltestelle.inspect}" puts "#{address} asks for #{haltestelle.inspect}"
@socket.puts "Anfrage nach #{haltestelle}..." @socket.puts "Anfrage nach #{haltestelle}..."
@socket.puts ask_haltestellenmonitor(haltestelle) @socket.puts ask_haltestellenmonitor(haltestelle)
end end
end end
end end
serv = TCPServer.new('0.0.0.0', 65023) serv = TCPServer.new('0.0.0.0', 65023)
while client = serv.accept while client = serv.accept
ClientHandler.new(client) ClientHandler.new(client)
end end
</pre> </pre>
Und dann: Und dann:
telnet localhost 65023 telnet localhost 65023
[[Kategorie:Ruby]] [[Kategorie:Ruby]]
{{Rübÿ Spëëd Mëtäl Cödïng}}