Skip to content
Browse files

Misc. changes

  • Loading branch information...
1 parent 52122e6 commit 85dbbaea1f3f917c87a0dff9bc3b4462974c074a @danopia committed Nov 10, 2009
View
4 agents/echoey.rb
@@ -1,8 +1,8 @@
class Echoey# < Sails::Agent
def handle remote, wave, blip
if blip
- Sails::Delta.build remote, wave, 'echoey@danopia.net' do |builder|
- builder.new_blip_under blip, blip.contents.gsub("\001", '')
+ @wave.build_delta 'echoey@danopia.net' do
+ new_blip_under blip, blip.digest
end
end
end
View
11 lib/blip.rb
@@ -27,15 +27,8 @@ def create_fedone_line(author, text)
arr
end
- def create_mutation new
- ops = []
- same_count = 0
-
- old = self.to_arr
- old_index = 0
- new_index = 0
-
-
+ def digest
+ @contents.gsub("\001", '')
end
# Dumps the current version of this Blip instance to XML. Note that said
View
176 lib/delta.rb
@@ -41,7 +41,7 @@ class Delta < BaseDelta
# Frozen deltas are considered to be unchanging, so the byte form is cached
# to greatly speed up the creation of packets.
- attr_reader :frozen
+ attr_reader :frozen, :commited
# Create a new delta. Defaults to applying itself to the latest delta from
# the wave, but if you want to add older history in, you can override it with
@@ -51,7 +51,7 @@ def initialize wave, author=nil
@author = author
@version = wave.newest_version
@time = Time.now.to_i * 1000
- @applied = false
+ @commited = false
@frozen = false
self.server = wave.provider.local
@@ -123,7 +123,7 @@ def self.parse provider, wavelet, data, applied=false
wave << delta
- delta.propagate(applied) unless applied
+ delta.commit!
delta
end
@@ -221,11 +221,14 @@ def freeze
#@signature = nil
end
+ alias commited? commited
+
# Send the delta out to remote servers. Called by SailsRemote#add_delta and
# Delta.parse.
- #
- # TODO: Handle each server better. (Queue, ping, etc.)
- def propagate(applied=false)
+ def commit!
+ return false if commited?
+ @commited = true
+
freeze
wave.apply self
@@ -266,167 +269,12 @@ def propagate(applied=false)
@wave.post self
end
- unless @author == 'echoey@danopia.net'
- puts 'poking Echoey' if @wave.participants.include? 'echoey@danopia.net'
- Echoey.new.handle $remote, @wave, @operations.select {|op| op.is_a? Operations::Mutate }.map {|op| @wave.blip(op.document_id) }.uniq.first if @wave.participants.include? 'echoey@danopia.net'
+ if @wave.participants.include?('echoey@danopia.net') && @author != 'echoey@danopia.net'
+ puts 'poking Echoey'
+ Echoey.new.handle $remote, @wave, @operations.select {|op| op.is_a? Operations::Mutate }.map {|op| @wave.blip(op.document_id) }.uniq.first
end
end
- def self.build remote, wave, author, &block
- wave = remote.provider[wave] if wave.is_a? String
-
- delta = Sails::Delta.new wave, author
- builder = DeltaBuilder.new delta
- block.arity < 1 ? builder.instance_eval(&block) : block.call(builder)
-
- remote << delta
- delta
- end
-
end # class
-
-
-
-class DeltaBuilder
- attr_reader :wave, :delta, :author
-
- # Generate a random alphanumeric string
- def random_string(length=12)
- @letters ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
- ([''] * length).map { @letters[rand * @letters.size] }.join('')
- end
-
- def initialize delta
- @wave = delta.wave
- @delta = delta
- @author = delta.author
- end
-
- def author= author
- @delta.author = author
- @author = author
- end
-
- def create_conv
- mutate 'conversation', [
- {:element_start => {:type => 'conversation'}},
- {:element_end => true}
- ]
- end
-
- def add_blip_at_end blip
- add_blip_at_index blip, @wave.conv.size - 1
- end
-
- def new_blip_at_end initial_line=nil
- blip = new_blip initial_line
- add_blip_at_end blip
- blip
- end
-
- def add_blip_after blip, target
- add_blip_x_after_end blip, target, 0
- end
-
- def new_blip_after target, initial_line=nil
- blip = new_blip initial_line
- add_blip_after blip, target
- blip
- end
-
- def add_blip_under blip, target
- add_blip_x_after_end blip, target, -1
- end
-
- def new_blip_under target, initial_line=nil
- blip = new_blip initial_line
- add_blip_under blip, target
- blip
- end
-
- def new_blip initial_line=nil, blip=nil
- blip = "b+#{random_string 6}" unless blip
- mutate blip # create with no operation
- first_line blip, initial_line if initial_line
- blip
- end
-
- def first_line blip, message
- mutate blip, [
- {:element_start=>{:type=>"body"}},
- {:element_start=>{:type=>"line"}},
- {:element_end => true},
- {:characters => message},
- {:element_end => true}
- ]
- end
-
- def append_line blip, message
- blip = @wave.blip(blip) unless blip.is_a? Sails::Blip
-
- mutate blip, [
- {:retain_item_count => blip.contents.size - 1},
- {:element_start=>{:type=>"line"}},
- {:element_end => true},
- {:characters => message},
- {:retain_item_count => 1}
- ]
- end
-
- def add operation
- @delta << operation
- end
-
- def add_user participant
- add Sails::Operations::AddUser.new(participant)
- end
- def remove_user participant
- add Sails::Operations::RemoveUser.new(participant)
- end
- def mutate blip, components=[]
- blip = blip.name if blip.is_a? Sails::Blip
- add Sails::Operations::Mutate.new(blip, components)
- end
-
- def add_self
- add_user @author
- end
-
- protected
-
- def add_blip_at_index blip, index=1
- blip = blip.name if blip.is_a? Sails::Blip
-
- mutate 'conversation', [
- {:retain_item_count => index},
- {:element_start=>{:type => 'blip', :attributes => [{:key=>'id', :value=>blip}]}},
- {:element_end => true},
- {:retain_item_count => @wave.conv.size - index}
- ]
- end
-
- def add_blip_x_after_end blip, target, x=0
- target = target.name if target.is_a? Sails::Blip
-
- target = @wave.conv.select do |item|
- item.is_a?(Sails::Element) && item['id'] == target
- end.first
- return nil unless target
-
- index = @wave.conv.index(target) + 1
- depth = 1
- while depth > 0
- if @wave.conv[index].is_a? Sails::Element
- depth += 1
- elsif @wave.conv[index] == :end
- depth -= 1
- end
- index += 1
- end
-
- add_blip_at_index blip, index + x
- end
-end
-
end # module
View
8 lib/operations.rb
@@ -78,10 +78,12 @@ def self.parse data
end
def to_hash
- {:mutate => {
- :mutation => {
- :components => @components},
+ hash = {:mutate => {
+ :mutation => {},
:document_id => @document_id}}
+
+ hash[:mutate][:mutation][:components] = @components if @components.any?
+ hash
end
def to_s
View
6 lib/playback.rb
@@ -122,10 +122,7 @@ def apply_conv_mutate(operations)
def read_conv
stack = [[]]
- pp @conv
@conv.each do |item|
- p item
- p stack
if item.is_a? Element
next if item.type == 'conversation'
stack.last << item['id']
@@ -136,9 +133,6 @@ def read_conv
end
end
- puts "New conversation structure:"
- pp stack.first
-
@blips = stack
end
View
12 lib/provider.rb
@@ -30,7 +30,7 @@ def by_signer_id hash
# Most popular class. Represents the local server and the waves on it, and
# keeps a list of external servers.
class Provider
- attr_accessor :sock, :servers, :key, :domain, :name, :local, :packet_ids, :ready
+ attr_accessor :sock, :servers, :key, :domain, :name, :local, :packet_ids, :ready, :remote
# Create a new provider.
def initialize(domain, subdomain='wave', sock=nil)
@@ -49,11 +49,13 @@ def initialize(domain, subdomain='wave', sock=nil)
alias ready? ready
- # Marks the provider as ready and flushes all queued packets.
+ # Marks the provider as ready and flushes all queued packets. Also starts a
+ # remote if not already started.
def ready!
return if ready?
@ready = true
flush
+ start_remote unless @remote
end
# Load the provider's certificate from a file.
@@ -70,6 +72,12 @@ def load_key(path)
def sign(data)
@key.sign OpenSSL::Digest::SHA1.new, data
end
+
+ # Start up a new SailsRemote
+ def start_remote bind=':9000'
+ @remote = Remote.serve self, bind
+ puts "DRb server running at #{@remote.uri}"
+ end
# Create a socket to the XMPP server.
def connect_sock(host='localhost', port=5275)
View
34 lib/remote.rb
@@ -1,30 +1,29 @@
require 'drb'
-# Not inside the module, might move it later if the rails autoloader would
-# still work.
+module Sails
# A class that's focused for use with DRb. There are a few methods that just
# call deeper methods, since DRb only sends method calls to the server if
# called on the main DRbObject. If it weren't for these methods, a DRb client
# wouldn't be able to do much.
-class SailsRemote
+class Remote
attr_accessor :drb, :provider
# Serve a remote up
- def self.serve(provider, host=':9000')
- remote = SailsRemote.new(provider)
- remote.drb = DRb.start_service("druby://#{host}", remote)
+ def self.serve provider, host=':9000'
+ remote = Sails::Remote.new provider
+ remote.drb = DRb.start_service "druby://#{host}", remote
remote
end
# Connect to a remote
- def self.connect(host=':9000')
+ def self.connect host=':9000'
DRb.start_service
DRbObject.new nil, "druby://#{host}"
end
# Create a remote for the provider
- def initialize(provider)
+ def initialize provider
@provider = provider
@drb = nil
end
@@ -50,12 +49,12 @@ def all_waves
end
# Look up and return a wave
- def [](name)
+ def [] name
@provider[name]
end
# Add a delta or wave
- def <<(item)
+ def << item
if item.is_a? Wave
@provider << item
elsif item.is_a? Delta
@@ -69,23 +68,18 @@ def <<(item)
# Add a delta to a wave (faster to give the wave's name). Also propagates the
# delta.
def add_delta(wave, delta)
- if wave.is_a? Sails::Wave
+ if wave.is_a? Wave
wave << delta# unless wave.deltas.include?(delta)
wave = wave.name
end
self[wave] << delta
- delta.propagate true
- end
-
- # Generate a random alphanumeric string
- def random_string(length=12)
- @letters ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
- ([''] * length).map { @letters[rand * @letters.size] }.join('')
end
def new_local_wave
- wave = Sails::Wave.new @provider
+ wave = Wave.new @provider
self << wave
wave
end
-end
+end # class
+
+end # module
View
10 lib/utils.rb
@@ -25,6 +25,16 @@ def to_hash
module Sails
+ class Utils
+
+ # Generate a random alphanumeric string
+ def self.random_string(length=12)
+ @letters ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
+ ([''] * length).map { @letters[rand * @letters.size] }.join('')
+ end
+
+ end
+
# Used for general Sails-related errors.
class Error < RuntimeError; end
View
15 lib/wave.rb
@@ -49,13 +49,13 @@ def [](version)
def <<(item)
if item.is_a? BaseDelta
@deltas[item.version] = item
- item.propagate if item.is_a?(Delta)
+ item.commit! if item.is_a? Delta
elsif item.is_a? Blip
if blip(item.name)
- raise SailsError, 'This blip already exists.'
+ raise Sails::Error, 'This blip already exists.'
else
- raise SailsError, 'Not implemented yet.'
+ raise Sails::Error, 'Not implemented yet.'
end
else
raise ArgumentError, 'expected a Blip, Delta, or FakeDelta'
@@ -117,6 +117,15 @@ def complete?(request_more=false)
true
end
+
+ def build_delta author, &block
+ delta = Delta.new self, author
+ builder = DeltaBuilder.new delta
+ block.arity < 1 ? builder.instance_eval(&block) : block.call(builder)
+
+ self << delta
+ delta
+ end
end # class
end # module
View
31 rack.ru
@@ -1,14 +1,16 @@
require 'pp'
require 'sails'
-class SailsAdapter
+module Sails
+
+class RackAdapter
def initialize(rails_app)
@rails = rails_app
end
def connect
return if @remote
- @remote = SailsRemote.connect
+ @remote = Remote.connect
DRb.start_service
end
@@ -34,8 +36,12 @@ class SailsAdapter
@wave = @remote[name]
version = @wave.newest_version
- body = @wave.blips.flatten.map do |blip|
- "<p><strong>#{blip}</strong></p>\n<p><em>by #{@wave.blip(blip).authors.join(', ')}</em></p>\n#{@wave.blip(blip).to_xml}\n<hr/>"
+ body = @wave.blips.map do |blip|
+ if blip.is_a? String
+ "<p><strong>#{blip}</strong></p>\n<p><em>by #{@wave.blip(blip).authors.join(', ')}</em></p>\n#{@wave.blip(blip).to_xml}\n<hr/>"
+ else
+ "<blockquote>\n#{render_html blip}\n</blockquote>"
+ end
end.join("\n")
env['async.callback'].call [200, {
@@ -60,7 +66,17 @@ class SailsAdapter
[-1, {}, []]
end
- # data = "<script type=\"text/javascript\">
+ def render_html blips
+ blips.map do |blip|
+ if blip.is_a? String
+ "<p><strong>#{blip}</strong></p>\n<p><em>by #{@wave.blip(blip).authors.join(', ')}</em></p>\n#{@wave.blip(blip).to_xml}\n<hr/>"
+ else
+ "<blockquote>\n#{render_html blip}\n</blockquote>"
+ end
+ end.join("\n")
+ end
+
+# data = "<script type=\"text/javascript\">
# document.getElementById('data').innerHTML = \"#{escape_js wave.to_xml}\";
# document.getElementById('version').innerHTML = \"#{version}\";
#</script>"
@@ -73,13 +89,14 @@ class SailsAdapter
def escape_js text
escape(text).gsub('\\', '\\\\').gsub('"', '\\"').gsub("\n", ' ')
end
-end
+end # class
+end # module
use Rack::CommonLogger
rails_app = Rack::Adapter::Rails.new(:root => './rails')
-mapping = {'/ajax' => SailsAdapter.new(rails_app),
+mapping = {'/ajax' => Sails::RackAdapter.new(rails_app),
'/' => rails_app}
if File.exists? File.join(File.dirname(__FILE__), 'doc')
View
2 rails/app/controllers/application_controller.rb
@@ -65,7 +65,7 @@ def current_user
# Connect the SailsRemote. Also sets @address to the current user's address.
def connect_remote
unless @remote
- @remote = SailsRemote.connect
+ @remote = Sails::Remote.connect
end
@address = "#{current_user.login}@#{@remote.provider.domain}"
View
43 rails/app/controllers/waves_controller.rb
@@ -9,7 +9,7 @@ def show
if params[:id] == 'new'
@wave = @remote.new_local_wave
- Sails::Delta.build @remote, @wave, @address do
+ Sails::DeltaBuilder.build @wave, @address do
add_self
create_conv
end
@@ -19,15 +19,37 @@ def show
end
@wave = @remote[params[:id]]
+ unless @wave
+ render :text => 'No such wave.', :status => 404
+ return
+ end
unless @wave.participants.include? @address
- Sails::Delta.build @remote, @wave, @address do
+ Sails::DeltaBuilder.build @wave, @address do
add_self
end
end
+ @html = @wave.blips.map do |blip|
+ if blip.is_a? String
+ "<p><strong>#{blip}</strong></p>\n<p><em>by #{@wave.blip(blip).authors.join(', ')}</em></p>\n#{@wave.blip(blip).to_xml}\n<hr/>"
+ else
+ "<blockquote>\n#{render_html blip}\n</blockquote>"
+ end
+ end.join("\n")
+
end
+ def render_html blips
+ blips.map do |blip|
+ if blip.is_a? String
+ "<p><strong>#{blip}</strong></p>\n<p><em>by #{@wave.blip(blip).authors.join(', ')}</em></p>\n#{@wave.blip(blip).to_xml}\n<hr/>"
+ else
+ "<blockquote>\n#{render_html blip}\n</blockquote>"
+ end
+ end.join("\n")
+ end
+
def ajax
@wave = @remote[params[:id]]
unless @wave
@@ -41,10 +63,19 @@ def ajax
def update
@wave = @remote[params[:id]]
+ if params[:message].empty?
+ render :text => 'Please enter a message.'
+ return
+ end
+
if @wave.participants.include? @address
- Sails::Delta.build @remote, @wave, @address do |builder|
- builder.new_blip_at_end params[:message]
+ Sails::DeltaBuilder.build @wave, @address do |builder|
+ if params[:parent] && params[:parent].any? && @wave.blip(params[:parent])
+ builder.new_blip_under params[:parent], params[:message]
+ else
+ builder.new_blip_at_end params[:message]
+ end
end
#flash[:notice] = "Your message has been added."
@@ -64,7 +95,7 @@ def remove
elsif !( params[:who] && @wave.participants.include?(params[:who]) )
flash[:error] = "#{params[:who]} isn't in this wave."
else
- Sails::Delta.build @remote, @wave, @address do |builder|
+ Sails::DeltaBuilder.build @wave, @address do |builder|
builder.remove_user params[:who]
end
flash[:notice] = "#{params[:who]} has been removed from the wave."
@@ -81,7 +112,7 @@ def add
elsif !params[:who] || @wave.participants.include?(params[:who])
flash[:error] = "#{params[:who]} is already in this wave."
else
- Sails::Delta.build @remote, @wave, @address do |builder|
+ Sails::DeltaBuilder.build @wave, @address do |builder|
builder.add_user params[:who]
end
flash[:notice] = "#{params[:who]} has been added to the wave."
View
3 rails/app/helpers/application_helper.rb
@@ -13,7 +13,6 @@ def gravatar(email, size=80)
end
def empty?(field)
- return true if field == nil
- return field == ''
+ field.nil? || field.empty?
end
end
View
8 rails/app/views/waves/show.html.erb
@@ -18,18 +18,20 @@
<hr/>
<p><strong><a name="contents">Contents</a></strong></p>
<blockquote id="contents2">
-<% @wave.blips.flatten.each do |blip| -%>
+<%= @html %>
+<!--<% @wave.blips.flatten.each do |blip| -%>
<p><strong><%=h blip %></strong></p>
<p><em>by <%=h @wave.blip(blip).authors.join(', ') %></em></p>
<%= @wave.blip(blip).to_xml %>
<hr/>
-<% end -%>
+<% end -%>-->
</blockquote>
<%= link_to_remote "Update", :update => "contents2", :url => "/waves/#{@wave.name}/ajax" %>
<%= form_remote_tag :url => wave_update_path(@wave.name), :complete => "submitted();", :html => {'id' => 'add-form'} %>
- Message: <%= text_field_tag "message" %><br/>
+ Message: <%= text_field_tag 'message' %><br/>
+ Add under: <%= text_field_tag 'parent' %> (leave blank unless you know what you are doing)<br/>
<%= submit_tag "Add new blip" %>
</form>
View
4 sails.rb
@@ -1,7 +1,7 @@
-# Includes the lib/ files: wave_proto utils delta operations playback provider
+# Includes the lib/ files: wave_proto utils delta delta_builder operations playback provider
# server remote wave blip
-%w{wave_proto utils delta operations playback provider server remote wave blip}.each do |file|
+%w{wave_proto utils delta delta_builder operations playback provider server remote wave blip}.each do |file|
require File.join(File.dirname(__FILE__), 'lib', file)
end
View
4 xmpp_component.rb
@@ -53,9 +53,7 @@
key = Digest::SHA1.hexdigest(id + config['xmpp-password'])
provider.send_data "<handshake>#{key}</handshake>"
-remote = SailsRemote.serve(provider)
-$remote = remote
-puts "DRb server running at #{remote.uri}"
+provider.start_remote
trap("INT") do
remote.stop_service

0 comments on commit 85dbbae

Please sign in to comment.
Something went wrong with that request. Please try again.