Module: Adhd::ReplicationNotifier

Defined in:
lib/adhd/models/replication_connection.rb

Instance Method Summary collapse

Instance Method Details

#initialize(conn_obj) ⇒ Object



7
8
9
10
11
12
# File 'lib/adhd/models/replication_connection.rb', line 7

def initialize(conn_obj)      
  @conn_obj = conn_obj
  @buffer = ""
  conn_obj.connection_inside = self # We tell the outer object who we are
  # pending_connect_timeout= 5.0
end

#post_initObject

Makes a long-running request to a CouchDB instance Implement PULL replication, as it is most efficient in CouchDB 0.9



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/adhd/models/replication_connection.rb', line 18

def post_init 
  # Build a JSON representation
  r = {:source => "#{@conn_obj.remote_db}",
       :target => "#{@conn_obj.our_db}"}
       #, :continuous => true }
  r_json = r.to_json

  # Create the HTTP request
  req = "POST /_replicate HTTP/1.1\r\n"
  req += "Content-Length: #{r_json.length}\r\n\r\n"      
  req += "#{r_json}"
  
  # Push it to the network
  send_data req
end

#receive_data(data) ⇒ Object

Shoots replication events from CouchDB to the @conn. Buffers data until a JSON object is detected



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/adhd/models/replication_connection.rb', line 37

def receive_data data

  @buffer += data # Add the data to the current buffer
  updates = []
  if @buffer =~ /(\{[^\n]+\}\n)/
    updates += ($~.to_a)[1..-1]
    # Trim the buffer to $_.end(0)
    @buffer = @buffer[$~.end(0)..-1]
  end
 
  # Regexp for JSON updates is /\{[\n]\}+/      
  updates.each do |json_event|
    @conn_obj.event_handler(json_event) unless data == "\n"
  end
end

#unbindObject



53
54
55
56
# File 'lib/adhd/models/replication_connection.rb', line 53

def unbind
  # TODO: detect when the remote node is down and update their 
  #       status
end