backchat 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  require 'java/smack'
2
2
  require 'java/smackx'
3
3
  java_import 'org.jivesoftware.smack.XMPPConnection'
4
- java_import 'java.util.concurrent.ArrayBlockingQueue'
5
4
  require 'backchat/connection_pool'
6
5
  require 'backchat/version'
7
6
 
@@ -6,34 +6,39 @@ module Backchat
6
6
  host: 'localhost',
7
7
  username: 'admin',
8
8
  password: 'password',
9
- pool_size: 5 }
9
+ pool_size: 5,
10
+ persistent: true }
10
11
 
11
12
  @settings = DEFAULT_SETTINGS
12
13
 
13
14
  def initialize(count = self.class.settings[:pool_size])
14
- @queue = fill(ArrayBlockingQueue.new(count))
15
+ @queue = fill(SizedQueue.new(count))
15
16
  end
16
17
 
17
- def each(&block)
18
- queue.each(&block)
18
+ def count
19
+ queue.length
19
20
  end
20
21
 
21
22
  def take
22
- queue.take
23
+ queue.pop
23
24
  end
24
25
 
25
26
  def add(connection)
26
- queue.add(connection)
27
+ queue.push(connection)
27
28
  end
28
29
 
29
30
  def with_connection
30
31
  connection = take
32
+ connection.connect unless connection.is_connected?
33
+ connection.login(self.class.settings[:username],
34
+ self.class.settings[:password],
35
+ "resource_#{connection.connection_id}") unless connection.is_authenticated?
31
36
  yield connection
32
37
  ensure
38
+ connection.disconnect unless self.class.settings[:persistent]
33
39
  add(connection)
34
40
  end
35
41
 
36
-
37
42
  class << self
38
43
  attr_reader :settings
39
44
 
@@ -58,7 +63,7 @@ module Backchat
58
63
  end
59
64
 
60
65
  private
61
- [:host, :username, :password, :pool_size].each do |key|
66
+ [:host, :username, :password, :pool_size, :persistent].each do |key|
62
67
  define_method("#{key}") do |val|
63
68
  @settings ||= {}
64
69
  @settings[key] = val
@@ -70,13 +75,16 @@ module Backchat
70
75
  attr_reader :queue
71
76
 
72
77
  def fill(queue)
73
- while queue.remaining_capacity > 0
78
+ while queue.length < queue.max
74
79
  conn = XMPPConnection.new(self.class.settings[:host])
75
- conn.connect
76
- conn.login(self.class.settings[:username],
77
- self.class.settings[:password], conn.connection_id)
80
+ if self.class.settings[:persistent]
81
+ conn.connect
82
+ conn.login(self.class.settings[:username],
83
+ self.class.settings[:password],
84
+ "resource_#{conn.connection_id}")
85
+ end
78
86
 
79
- queue.add(conn)
87
+ queue.push(conn)
80
88
  end
81
89
 
82
90
  queue
@@ -1,3 +1,3 @@
1
1
  module Backchat
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -19,7 +19,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
19
19
  assert_equal Backchat::ConnectionPool.settings, { host: 'example.com',
20
20
  username: 'don_draper',
21
21
  password: 'secret',
22
- pool_size: 1 }
22
+ pool_size: 1,
23
+ persistent: true }
23
24
  end
24
25
 
25
26
  def test_configure_accepts_a_hash
@@ -29,7 +30,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
29
30
  assert_equal Backchat::ConnectionPool.settings, { host: 'example.com',
30
31
  username: 'don_draper',
31
32
  password: 'secret',
32
- pool_size: 1 }
33
+ pool_size: 1,
34
+ persistent: true }
33
35
  end
34
36
 
35
37
  def test_configure_allows_partial_settings_in_a_block
@@ -50,8 +52,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
50
52
  assert_includes @pool.instance_variables, :@queue
51
53
  end
52
54
 
53
- def test_queue_is_an_array_blocking_queue
54
- assert_instance_of ArrayBlockingQueue, @pool.instance_variable_get(:@queue)
55
+ def test_queue_is_a_queue
56
+ assert_instance_of SizedQueue, @pool.instance_variable_get(:@queue)
55
57
  end
56
58
 
57
59
  def test_initialize_fills_the_pool_with_connections
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backchat
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.0.2
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Zach Pendleton
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-14 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler