Class: PingPongPear

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

Constant Summary collapse

VERSION =
'2.0.0'
SERVICE =
"_http._tcp,pingpongpear"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePingPongPear

Returns a new instance of PingPongPear.



26
27
28
29
30
31
# File 'lib/ping_pong_pear.rb', line 26

def initialize
  @pull_requests      = Queue.new
  @send_pull_requests = Queue.new
  @peers              = Set.new
  @logger             = Logger.new $stdout
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



24
25
26
# File 'lib/ping_pong_pear.rb', line 24

def logger
  @logger
end

#peersObject (readonly)

Returns the value of attribute peers.



24
25
26
# File 'lib/ping_pong_pear.rb', line 24

def peers
  @peers
end

#pull_requestsObject (readonly)

Returns the value of attribute pull_requests.



24
25
26
# File 'lib/ping_pong_pear.rb', line 24

def pull_requests
  @pull_requests
end

Class Method Details

.run(args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/ping_pong_pear.rb', line 14

def self.run args
  new.public_send args.first, *args.drop(1)
rescue
  cmds = public_instance_methods(false).find_all { |x|
    instance_method(x).arity < 0
  }
  $stderr.puts "USAGE: pingpongpear #{cmds}"
  exit 1
end

Instance Method Details

#clone(name, dir = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ping_pong_pear.rb', line 79

def clone name, dir = nil
  browser = DNSSD::Service.browse SERVICE
  browser.each do |response|
    r = response.resolve
    if r.text_record['project'] == name
      url = "http://#{r.target}:#{r.port}"
      system "git clone #{Shellwords.escape(url)} #{dir || Shellwords.escape(name)}"
      break
    end
  end
end

#start(name = File.basename(Dir.pwd)) ⇒ Object



33
34
35
36
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
# File 'lib/ping_pong_pear.rb', line 33

def start name = File.basename(Dir.pwd)
  post_commit_hook = '.git/hooks/post-commit'
  pidfile = '.git/pingpongpear.pid'

  if File.exist? pidfile
    raise "Another instance of Ping Pong Pear is running"
  else
    File.open(pidfile, 'w') { |f| f.write $$ }
  end

  File.open(post_commit_hook, 'w') { |f|
    f.write <<-eof
#!/bin/sh

git update-server-info
kill -INFO $(cat #{pidfile})
    eof
  }
  File.chmod 0755, post_commit_hook

  system "git update-server-info"

  at_exit {
    File.unlink pidfile
    File.unlink post_commit_hook
  }

  identifier    = make_ident name

  logger.info "SEND THIS TO YOUR PEAR `git clone #{name}`"
  logger.info "CTRL-T sends pull requests to all peers"
  logger.debug "MY PROJECT NAME: #{name} IDENT: #{identifier}"

  server        = start_server pull_requests
  http_port     = server.listeners.map { |x| x.addr[1] }.first
  hostname      = Socket.gethostname

  discover identifier, name, peers
  process_pull_requests pull_requests
  process_send_pull_requests @send_pull_requests

  trap('INFO') { send_pull_requests peers, hostname, http_port }

  advertise(identifier, name, hostname, http_port).each { |x| x }
end