Class: Adhd::UpdateNotifierConnection

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

Overview

In practice we will have two types of connections: Replicate and Notify.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_url, couchdb_server_port, db_name, sync_block) ⇒ UpdateNotifierConnection

Returns a new instance of UpdateNotifierConnection.



97
98
99
100
101
102
103
104
105
106
# File 'lib/adhd/reactor.rb', line 97

def initialize(node_url, couchdb_server_port, db_name, sync_block)
  @node_url = node_url
  @couchdb_server_port = couchdb_server_port
  @db_name = db_name
  @sync_block = sync_block
  @status = "NOTRUNNING"
  @base_url = "http://#{@node_url}:#{@couchdb_server_port}"
  @name = @base_url +"/"+ @db_name
  @keep_alive = true      
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



95
96
97
# File 'lib/adhd/reactor.rb', line 95

def base_url
  @base_url
end

#connection_insideObject

Returns the value of attribute connection_inside.



95
96
97
# File 'lib/adhd/reactor.rb', line 95

def connection_inside
  @connection_inside
end

#db_nameObject

Returns the value of attribute db_name.



95
96
97
# File 'lib/adhd/reactor.rb', line 95

def db_name
  @db_name
end

#nameObject

Returns the value of attribute name.



95
96
97
# File 'lib/adhd/reactor.rb', line 95

def name
  @name
end

Instance Method Details

#close_handlerObject



125
126
127
128
# File 'lib/adhd/reactor.rb', line 125

def close_handler
  puts "Closed abnormally #{reason}"
  @status = "NOTRUNNING"
end

#down_for_good(reason) ⇒ Object



130
131
132
133
134
# File 'lib/adhd/reactor.rb', line 130

def down_for_good(reason)
  if reason
    puts "Closed for good #{reason}"
  end
end

#event_handler(data) ⇒ Object



118
119
120
121
122
123
# File 'lib/adhd/reactor.rb', line 118

def event_handler data
  # puts "||#{data}||nn"
  puts "Run a crazy sync on db #{@db_name}"
  #@db_obj_for_sync.sync
  @sync_block.call(data)
end

#is_closed?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/adhd/reactor.rb', line 153

def is_closed?
  (@status == "NOTRUNNING")
end

#keep_alive?Boolean

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/adhd/reactor.rb', line 136

def keep_alive?
  # Returns the truth value of the predicate
  @keep_alive
end

#keep_alive_or_kill!Object



141
142
143
144
145
146
147
# File 'lib/adhd/reactor.rb', line 141

def keep_alive_or_kill!
  if ! keep_alive?
    # Schedule this connection for close
    connection_inside.close_connection_after_writing
    @status = "NOTRUNNING"
  end
end

#killObject



108
109
110
# File 'lib/adhd/reactor.rb', line 108

def kill
  @keep_alive = false
end

#should_start?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/adhd/reactor.rb', line 149

def should_start?
  !(@status == "RUNNING")
end

#startObject



112
113
114
115
116
# File 'lib/adhd/reactor.rb', line 112

def start
  puts "Register the connection for #{@db_name}"
  EM.connect @node_url, @couchdb_server_port, Adhd::DbUpdateNotifier, @db_name, self
  @status = "RUNNING"
end