raktr 0.0.1

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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1 -0
  3. data/LICENSE.md +29 -0
  4. data/README.md +77 -0
  5. data/Rakefile +53 -0
  6. data/lib/raktr/connection/callbacks.rb +71 -0
  7. data/lib/raktr/connection/error.rb +120 -0
  8. data/lib/raktr/connection/peer_info.rb +90 -0
  9. data/lib/raktr/connection/tls.rb +164 -0
  10. data/lib/raktr/connection.rb +339 -0
  11. data/lib/raktr/global.rb +24 -0
  12. data/lib/raktr/iterator.rb +249 -0
  13. data/lib/raktr/queue.rb +89 -0
  14. data/lib/raktr/tasks/base.rb +57 -0
  15. data/lib/raktr/tasks/delayed.rb +33 -0
  16. data/lib/raktr/tasks/one_off.rb +30 -0
  17. data/lib/raktr/tasks/periodic.rb +58 -0
  18. data/lib/raktr/tasks/persistent.rb +29 -0
  19. data/lib/raktr/tasks.rb +105 -0
  20. data/lib/raktr/version.rb +13 -0
  21. data/lib/raktr.rb +707 -0
  22. data/spec/raktr/connection/tls_spec.rb +348 -0
  23. data/spec/raktr/connection_spec.rb +74 -0
  24. data/spec/raktr/iterator_spec.rb +203 -0
  25. data/spec/raktr/queue_spec.rb +91 -0
  26. data/spec/raktr/tasks/base.rb +8 -0
  27. data/spec/raktr/tasks/delayed_spec.rb +71 -0
  28. data/spec/raktr/tasks/one_off_spec.rb +66 -0
  29. data/spec/raktr/tasks/periodic_spec.rb +57 -0
  30. data/spec/raktr/tasks/persistent_spec.rb +54 -0
  31. data/spec/raktr/tasks_spec.rb +155 -0
  32. data/spec/raktr_spec.rb +20 -0
  33. data/spec/raktr_tls_spec.rb +20 -0
  34. data/spec/spec_helper.rb +17 -0
  35. data/spec/support/fixtures/handlers/echo_client.rb +34 -0
  36. data/spec/support/fixtures/handlers/echo_client_tls.rb +10 -0
  37. data/spec/support/fixtures/handlers/echo_server.rb +12 -0
  38. data/spec/support/fixtures/handlers/echo_server_tls.rb +8 -0
  39. data/spec/support/fixtures/pems/cacert.pem +37 -0
  40. data/spec/support/fixtures/pems/client/cert.pem +37 -0
  41. data/spec/support/fixtures/pems/client/foo-cert.pem +39 -0
  42. data/spec/support/fixtures/pems/client/foo-key.pem +51 -0
  43. data/spec/support/fixtures/pems/client/key.pem +51 -0
  44. data/spec/support/fixtures/pems/server/cert.pem +37 -0
  45. data/spec/support/fixtures/pems/server/key.pem +51 -0
  46. data/spec/support/helpers/paths.rb +23 -0
  47. data/spec/support/helpers/utilities.rb +135 -0
  48. data/spec/support/lib/server_option_parser.rb +29 -0
  49. data/spec/support/lib/servers/runner.rb +13 -0
  50. data/spec/support/lib/servers.rb +133 -0
  51. data/spec/support/servers/echo.rb +14 -0
  52. data/spec/support/servers/echo_tls.rb +22 -0
  53. data/spec/support/servers/echo_unix.rb +14 -0
  54. data/spec/support/servers/echo_unix_tls.rb +22 -0
  55. data/spec/support/shared/connection.rb +696 -0
  56. data/spec/support/shared/raktr.rb +834 -0
  57. data/spec/support/shared/task.rb +21 -0
  58. metadata +140 -0
@@ -0,0 +1,57 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+ class Tasks
11
+
12
+ # {#call Callable} task.
13
+ #
14
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
15
+ class Base
16
+
17
+ # @return [Tasks]
18
+ # List managing this task.
19
+ attr_accessor :owner
20
+
21
+ # @param [Block] task
22
+ def initialize( &task )
23
+ fail ArgumentError, 'Missing block.' if !block_given?
24
+
25
+ @task = task
26
+ end
27
+
28
+ # Calls the {#initialize configured} task and passes `args` and self` to it.
29
+ #
30
+ # @abstract
31
+ def call( *args )
32
+ fail NotImplementedError
33
+ end
34
+
35
+ # {Tasks#delete Removes} the task from the {#owner}'s list.
36
+ def done
37
+ @owner.delete self
38
+ end
39
+
40
+ def to_proc
41
+ @task
42
+ end
43
+
44
+ def hash
45
+ @task.hash
46
+ end
47
+
48
+ private
49
+
50
+ def call_task( *args )
51
+ @task.call *([self] + args)
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+ class Tasks
11
+
12
+ # @note {#interval Time} accuracy cannot be guaranteed.
13
+ #
14
+ # {Base Task} occurring after {#interval} seconds.
15
+ #
16
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
17
+ class Delayed < Periodic
18
+
19
+ # @note Will call {#done} right after.
20
+ #
21
+ # @return [Object, nil]
22
+ # Return value of the configured task or `nil` if it's not
23
+ # {#interval time} yet.
24
+ def call( *args )
25
+ return if !call?
26
+
27
+ call_task( *args ).tap { done }
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+ class Tasks
11
+
12
+ # {Base Task} occurring at the next tick and then marking itself as {#done}.
13
+ #
14
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
15
+ class OneOff < Base
16
+
17
+ # Performs the task and marks it as {#done}.
18
+ #
19
+ # @return [Object]
20
+ # Return value of the task.
21
+ def call( *args )
22
+ call_task( *args )
23
+ ensure
24
+ done
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+ class Tasks
11
+
12
+ # @note {#interval Time} accuracy cannot be guaranteed.
13
+ #
14
+ # {Base Task} occurring every {#interval} seconds.
15
+ #
16
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
17
+ class Periodic < Persistent
18
+
19
+ # @return [Float]
20
+ attr_reader :interval
21
+
22
+ # @param [Float] interval
23
+ # Needs to be greater than `0.0`.
24
+ # @param [#call] task
25
+ def initialize( interval, &task )
26
+ interval = interval.to_f
27
+ fail ArgumentError, 'Interval needs to be greater than 0.' if interval <= 0
28
+
29
+ super( &task )
30
+
31
+ @interval = interval
32
+ calculate_next
33
+ end
34
+
35
+ # @return [Object, nil]
36
+ # Return value of the configured task or `nil` if it's not
37
+ # {#interval time} yet.
38
+ def call( *args )
39
+ return if !call?
40
+ calculate_next
41
+
42
+ super( *args )
43
+ end
44
+
45
+ private
46
+
47
+ def call?
48
+ Time.now >= @next
49
+ end
50
+
51
+ def calculate_next
52
+ @next = Time.now + @interval
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+ class Tasks
11
+
12
+ # {Base Task} which does not cancel itself once called and occurs at every
13
+ # tick.
14
+ #
15
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
16
+ class Persistent < Base
17
+
18
+ # Performs the task and marks it as {#done}.
19
+ #
20
+ # @return [Object]
21
+ # Return value of the task.
22
+ def call( *args )
23
+ call_task( *args )
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,105 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ require 'mutex_m'
10
+
11
+ require_relative 'tasks/base'
12
+ require_relative 'tasks/persistent'
13
+ require_relative 'tasks/one_off'
14
+ require_relative 'tasks/periodic'
15
+ require_relative 'tasks/delayed'
16
+
17
+ class Raktr
18
+
19
+ # {Tasks::Base Task} list.
20
+ #
21
+ # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
22
+ class Tasks
23
+ include Mutex_m
24
+
25
+ def initialize
26
+ super
27
+
28
+ @tasks = []
29
+ end
30
+
31
+ # @note Only {Base#hash unique} tasks will be included.
32
+ # @note Will assign `self` as the task's {Base#owner owner}.
33
+ #
34
+ # @param [Base] task
35
+ # Task to add to the list.
36
+ # @return [Tasks] `self`
37
+ def <<( task )
38
+ synchronize do
39
+ task.owner = self
40
+ @tasks << task
41
+ end
42
+
43
+ self
44
+ end
45
+
46
+ # @param [Base] task
47
+ # Task to check.
48
+ # @return [Bool]
49
+ def include?( task )
50
+ @tasks.include? task
51
+ end
52
+
53
+ # @param [Base] task
54
+ # Task to remove from the list.
55
+ # @return [Base,nil]
56
+ # The task if it was included, `nil` otherwise.
57
+ def delete( task )
58
+ synchronize do
59
+ task = @tasks.delete( task )
60
+ task.owner = nil if task
61
+ task
62
+ end
63
+ end
64
+
65
+ # @return [Integer]
66
+ def size
67
+ @tasks.size
68
+ end
69
+
70
+ # @return [Bool]
71
+ def empty?
72
+ @tasks.empty?
73
+ end
74
+
75
+ # @return [Bool]
76
+ def any?
77
+ !empty?
78
+ end
79
+
80
+ # Removes all tasks.
81
+ #
82
+ # @return [Tasks] `self`
83
+ def clear
84
+ synchronize do
85
+ @tasks.clear
86
+ end
87
+
88
+ self
89
+ end
90
+
91
+ # {Base#call Calls} all tasks.
92
+ #
93
+ # @return [Tasks] `self`
94
+ def call( *args )
95
+ @tasks.dup.each { |t| t.call *args }
96
+ self
97
+ end
98
+
99
+ def hash
100
+ @tasks.hash
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,13 @@
1
+ =begin
2
+
3
+ This file is part of the Raktr project and may be subject to
4
+ redistribution and commercial restrictions. Please see the Raktr
5
+ web site for more information on licensing and terms of use.
6
+
7
+ =end
8
+
9
+ class Raktr
10
+
11
+ VERSION = '0.0.1'
12
+
13
+ end