This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

switch to using dnssd

  • Loading branch information...
technomancy committed Jul 24, 2008
1 parent c92c168 commit 8ce2c59e8d07a000b2c3f75e639d4e1a10b23c1e
Showing with 51 additions and 30 deletions.
  1. +4 −0 README.rdoc
  2. +2 −6 bin/conspire
  3. +17 −18 lib/conspire.rb
  4. +8 −5 lib/conspire/conspirator.rb
  5. +12 −1 lib/conspire/gitjour_exts.rb
  6. +8 −0 test/test_conspire.rb
View
@@ -11,6 +11,10 @@ supported.
== Installation
+You'll need git 1.5.5 or higher.
+
+TODO: instructions for avahi-compatible dnssd from source.
+
* sudo gem install net-mdns
* sudo gem install technomancy-conspire --source=http://gems.github.com
View
@@ -3,12 +3,8 @@
require File.dirname(__FILE__) + '/../lib/conspire'
# TODO: real option parsing
-# Should be able to set:
-# * port
-# * path
-# * sync interval
-# *
-Conspire.start :port => ARGV.first || Conspire::DEFAULT_OPTIONS[:port]
+Conspire.start(:port => ARGV.shift || Conspire::DEFAULT_OPTIONS[:port],
+ :name => ARGV.shift || Conspire::DEFAULT_OPTIONS[:name])
Thread.new { Conspire.discover_loop }
Conspire.sync_loop
View
@@ -10,45 +10,44 @@
module Conspire
VERSION = '0.0.1'
- DEFAULT_OPTIONS = { :port => 7456, :path => Dir.pwd }
- SERVICE_NAME = 'conspire'
- DISCOVER_INTERVAL = 10
- SYNC_INTERVAL = 1
+ DEFAULT_OPTIONS = { :port => 7456, :path => Dir.pwd, :name => 'conspiracy',
+ :discover_interval => 10, :sync_interval => 1 }
@conspirators = Set.new
module_function
# Begin a conspiracy session
def start(options = {})
- @options = DEFAULT_OPTIONS.merge(options)
-
- FileUtils.mkdir_p(@options[:path]) unless File.exist? @options[:path]
- `cd #{@options[:path]}; git init` if ! File.exist? @options[:path] + '/.git'
-
- @thread = Thread.new do
- Gitjour::Application.serve(@options[:path], SERVICE_NAME, @options[:port])
- end
- at_exit { @thread && @thread.join }
+ @options = DEFAULT_OPTIONS.merge options
+ puts "Starting with #{@options.inspect}" if ENV['DEBUG']
+ Gitjour::Application.start @options[:path], @options[:name], @options[:port]
end
# This should be called periodically
- def discover(wait = DISCOVER_INTERVAL)
+ def discover(wait = @options[:discover_interval])
Gitjour::Application.discover(wait) do |service|
- next if service.name !~ Regexp.new(SERVICE_NAME)
+ next if service.name !~ /conspiracy/ # TODO: better way of choosing names
next if service.port.to_i == @options[:port].to_i # TODO: and local
+
# No-op if we've got it already, since @conspirators is a Set
@conspirators << Conspirator.new(service.host, service.port, service.name)
end
end
def sync_all
- # TODO: drop conspirators if they shut down their repo
- @conspirators.map{ |s| s.sync(File.dirname(@options[:path])) }
+ @conspirators.map do |c|
+ begin
+ c.sync(File.dirname(@options[:path]))
+ rescue => e
+ puts "Dropping #{c} because #{e.message}"
+ @conspirators.delete c
+ end
+ end
end
def sync_loop
- loop { sync_all and sleep SYNC_INTERVAL }
+ loop { sync_all and sleep @options[:sync_interval] }
end
def discover_loop
@@ -8,17 +8,20 @@ def initialize(host, port, name = SERVICE_NAME)
def sync(path)
# TODO: suppress output
- `cd #{path} && git pull --rebase #{url}`
+ puts "Rebasing from #{url}" if ENV['DEBUG']
+ system "cd #{path} && git pull --rebase #{url}" or
+ raise "Could not rebase from #{url}"
+
@last_synced = Time.now
end
- def url; "git://#{@host}.local:#{@port}/" end
+ def url; "git://#{@host}:#{@port}/" end
alias_method :to_s, :url
alias_method :inspect, :url
- def eql?(other)
- self.to_s == other.to_s
- end
+ # For set equality
+ def eql?(other); self.url == other.url end
+ def hash; url.hash end
end
end
@@ -1,7 +1,18 @@
module Gitjour
class Application
class << self
- public :serve, :discover
+ def start(path, name, port)
+ init path
+ @thread = Thread.new { Gitjour::Application.start(path, name, port) }
+ end
+
+ def init(path)
+ FileUtils.mkdir_p(path) unless File.exist? path
+ `cd #{path}; git init` if ! File.exist? path + '/.git'
+ `touch #{path}/.git/git-daemon-export-ok`
+ end
+
+ public :discover
def puts(*args); end unless ENV['DEBUG']
end
end
View
@@ -47,4 +47,12 @@ def test_sync
Conspire.sync_all
assert_equal ["#{LOCAL_SPACE}/file"], Dir.glob("#{LOCAL_SPACE}/*")
end
+
+ def test_conspirator_set
+ Conspire.conspirators << Conspire::Conspirator.new('dynabook', '7458')
+ Conspire.conspirators << Conspire::Conspirator.new('dynabook', '7458')
+ Conspire.conspirators << Conspire::Conspirator.new('dynabook', '7458')
+ Conspire.conspirators << Conspire::Conspirator.new('dynabook', '7458')
+ assert_equal 1, Conspire.conspirators.size
+ end
end

0 comments on commit 8ce2c59

Please sign in to comment.