Module: Pastejour
- Includes:
- Socket::Constants
- Defined in:
- lib/pastejour.rb,
lib/pastejour/cli.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.2.1"
- PORT =
42424
- SERVICE =
"_pastejour._tcp"
Class Method Summary collapse
- .find(name, first = true) ⇒ Object
- .get(name) ⇒ Object
- .list ⇒ Object
- .serve(name, multiple, contents) ⇒ Object
Class Method Details
.find(name, first = true) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pastejour.rb', line 35 def self.find(name, first=true) hosts = Set.new waiting = Thread.current service = DNSSD.browse(SERVICE) do |reply| if name === reply.name DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr| hosts << Paste.new(reply.name, rr.target, rr.port) waiting.run if first end end end sleep 5 service.stop hosts end |
.get(name) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pastejour.rb', line 55 def self.get(name) hosts = find(name) if hosts.empty? STDERR.puts "ERROR: Unable to find #{name}" elsif hosts.size > 1 STDERR.puts "ERROR: Multiple possibles found:" hosts.each do |host| STDERR.puts " #{host.name} (#{host.host}:#{host.port})" end else hosts.each do |host| STDERR.puts "(#{host.name} from #{host.host}:#{host.port})" sock = TCPSocket.open host.host, host.port return sock.read end end end |
.list ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pastejour.rb', line 17 def self.list servers = {} service = DNSSD.browse(SERVICE) do |reply| servers[reply.name] ||= reply end STDERR.puts "Searching for servers (3 seconds)" # Wait for something to happen sleep 3 service.stop servers.each { |string,obj| name, port = string.split ":" STDERR.puts "Found pastejour at '#{name}'" } end |
.serve(name, multiple, contents) ⇒ Object
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 |
# File 'lib/pastejour.rb', line 74 def self.serve(name, multiple, contents) tr = DNSSD::TextRecord.new tr["description"] = "A paste." DNSSD.register(name, SERVICE, "local", PORT, tr) do |reply| STDERR.puts "Pasting #{name}..." end log = WEBrick::Log.new(true) # true fools it def log.log(*anything); end # send it to the abyss server = WEBrick::GenericServer.new(:Port => PORT, :Logger => log) %w(INT TERM).each do |signal| trap signal do server.shutdown exit! end end server.start do |socket| socket.print(contents) server.shutdown unless multiple end end |