sumac 0.0.0

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 (61) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +2 -0
  4. data/lib/core_extensions.rb +207 -0
  5. data/lib/sumac.rb +94 -0
  6. data/lib/sumac/adapter.rb +5 -0
  7. data/lib/sumac/adapter/closed.rb +7 -0
  8. data/lib/sumac/argument_error.rb +5 -0
  9. data/lib/sumac/call_dispatcher.rb +70 -0
  10. data/lib/sumac/call_processor.rb +93 -0
  11. data/lib/sumac/closed_error.rb +5 -0
  12. data/lib/sumac/closer.rb +53 -0
  13. data/lib/sumac/connection.rb +94 -0
  14. data/lib/sumac/exposed_object.rb +131 -0
  15. data/lib/sumac/exposed_object_child.rb +158 -0
  16. data/lib/sumac/forgoten.rb +5 -0
  17. data/lib/sumac/handshake.rb +48 -0
  18. data/lib/sumac/id_allocator.rb +99 -0
  19. data/lib/sumac/local_reference.rb +16 -0
  20. data/lib/sumac/local_references.rb +80 -0
  21. data/lib/sumac/message.rb +19 -0
  22. data/lib/sumac/message/exchange.rb +30 -0
  23. data/lib/sumac/message/exchange/base.rb +15 -0
  24. data/lib/sumac/message/exchange/call_request.rb +96 -0
  25. data/lib/sumac/message/exchange/call_response.rb +83 -0
  26. data/lib/sumac/message/exchange/compatibility_notification.rb +53 -0
  27. data/lib/sumac/message/exchange/forget_notification.rb +77 -0
  28. data/lib/sumac/message/exchange/id.rb +30 -0
  29. data/lib/sumac/message/exchange/initialization_notification.rb +52 -0
  30. data/lib/sumac/message/exchange/notification.rb +9 -0
  31. data/lib/sumac/message/exchange/shutdown_notification.rb +27 -0
  32. data/lib/sumac/message/object.rb +72 -0
  33. data/lib/sumac/message/object/array.rb +57 -0
  34. data/lib/sumac/message/object/base.rb +21 -0
  35. data/lib/sumac/message/object/boolean.rb +45 -0
  36. data/lib/sumac/message/object/exception.rb +66 -0
  37. data/lib/sumac/message/object/exposed.rb +79 -0
  38. data/lib/sumac/message/object/exposed_child.rb +86 -0
  39. data/lib/sumac/message/object/float.rb +45 -0
  40. data/lib/sumac/message/object/hash_table.rb +75 -0
  41. data/lib/sumac/message/object/integer.rb +45 -0
  42. data/lib/sumac/message/object/native_exception.rb +56 -0
  43. data/lib/sumac/message/object/null.rb +44 -0
  44. data/lib/sumac/message/object/string.rb +45 -0
  45. data/lib/sumac/message_error.rb +5 -0
  46. data/lib/sumac/messenger.rb +65 -0
  47. data/lib/sumac/native_error.rb +17 -0
  48. data/lib/sumac/no_method_error.rb +9 -0
  49. data/lib/sumac/reference.rb +68 -0
  50. data/lib/sumac/remote_entry.rb +42 -0
  51. data/lib/sumac/remote_object.rb +39 -0
  52. data/lib/sumac/remote_object_child.rb +38 -0
  53. data/lib/sumac/remote_reference.rb +16 -0
  54. data/lib/sumac/remote_references.rb +70 -0
  55. data/lib/sumac/scheduler.rb +56 -0
  56. data/lib/sumac/shutdown.rb +32 -0
  57. data/lib/sumac/stale_object_error.rb +5 -0
  58. data/lib/sumac/unexposable_object_error.rb +9 -0
  59. data/lib/sumac/worker_pool.rb +35 -0
  60. data/test/test_id_allocator.rb +25 -0
  61. metadata +145 -0
@@ -0,0 +1,72 @@
1
+ class Sumac
2
+ class Message
3
+ class Object < Message
4
+
5
+ def self.from_json_structure(connection, json_structure)
6
+ raise MessageError unless json_structure.is_a?(::Hash) && json_structure['message_type'] == 'object'
7
+ object_class =
8
+ case json_structure['object_type']
9
+ when 'null'
10
+ Null
11
+ when 'boolean'
12
+ Boolean
13
+ when 'exception'
14
+ Exception
15
+ when 'native_exception'
16
+ NativeException
17
+ when 'integer'
18
+ Integer
19
+ when 'float'
20
+ Float
21
+ when 'string'
22
+ String
23
+ when 'array'
24
+ Array
25
+ when 'hash_table'
26
+ HashTable
27
+ when 'exposed'
28
+ Exposed
29
+ when 'exposed_child'
30
+ ExposedChild
31
+ else
32
+ raise MessageError
33
+ end
34
+ object = object_class.from_json_structure(connection, json_structure)
35
+ object
36
+ end
37
+
38
+ def self.from_native_object(connection, native_object)
39
+ object_class =
40
+ case
41
+ when native_object.is_a?(RemoteObject) || (native_object.respond_to?(:__sumac_exposed_object__) && native_object.respond_to?(:__native_id__))
42
+ Exposed
43
+ when native_object.is_a?(RemoteObjectChild) || (native_object.respond_to?(:__sumac_exposed_object__) && native_object.respond_to?(:__parent__))
44
+ ExposedChild
45
+ when native_object == nil
46
+ Null
47
+ when native_object == true || native_object == false
48
+ Boolean
49
+ when Exception.map.transpose[1].any? { |klass| native_object.is_a?(klass) }
50
+ Exception
51
+ when native_object.is_a?(::Exception)
52
+ NativeException
53
+ when native_object.is_a?(::Integer)
54
+ Integer
55
+ when native_object.is_a?(::Float)
56
+ Float
57
+ when native_object.is_a?(::String)
58
+ String
59
+ when native_object.is_a?(::Array)
60
+ Array
61
+ when native_object.is_a?(::Hash)
62
+ HashTable
63
+ else
64
+ raise UnexposableObjectError
65
+ end
66
+ object = object_class.from_native_object(connection, native_object)
67
+ object
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,57 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Array < Base
5
+
6
+ def initialize(connection)
7
+ super
8
+ @elements = nil
9
+ end
10
+
11
+ def parse_json_structure(json_structure)
12
+ raise MessageError unless json_structure.is_a?(::Hash) &&
13
+ json_structure['message_type'] == 'object' &&
14
+ json_structure['object_type'] == 'array'
15
+ raise MessageError unless json_structure['elements'].is_a?(::Array)
16
+ @elements = json_structure['elements'].map do |element|
17
+ Object.from_json_structure(@connection, element)
18
+ end
19
+ nil
20
+ end
21
+
22
+ def parse_native_object(native_object)
23
+ raise MessageError unless native_object.is_a?(::Array)
24
+ @elements = native_object.map { |element| Object.from_native_object(@connection, element) }
25
+ nil
26
+ end
27
+
28
+ def to_json_structure
29
+ raise MessageError unless setup?
30
+ {
31
+ 'message_type' => 'object',
32
+ 'object_type' => 'array',
33
+ 'elements' => @elements.map(&:to_json_structure)
34
+ }
35
+ end
36
+
37
+ def to_native_object
38
+ raise MessageError unless setup?
39
+ @elements.map(&:to_native_object)
40
+ end
41
+
42
+ def invert_orgin
43
+ raise MessageError unless setup?
44
+ @elements.each { |element| element.invert_orgin if element.respond_to?(:invert_orgin) }
45
+ nil
46
+ end
47
+
48
+ private
49
+
50
+ def setup?
51
+ @elements != nil
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Base < Object
5
+
6
+ def self.from_json_structure(connection, json_structure)
7
+ object = new(connection)
8
+ object.parse_json_structure(json_structure)
9
+ object
10
+ end
11
+
12
+ def self.from_native_object(connection, native_object)
13
+ object = new(connection)
14
+ object.parse_native_object(native_object)
15
+ object
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Boolean < Base
5
+
6
+ def initialize(connection)
7
+ super
8
+ @value = nil
9
+ end
10
+
11
+ def parse_json_structure(json_structure)
12
+ raise MessageError unless json_structure.is_a?(::Hash) &&
13
+ json_structure['message_type'] == 'object' &&
14
+ json_structure['object_type'] == 'boolean'
15
+ raise MessageError unless json_structure['value'] == true || json_structure['value'] == false
16
+ @value = json_structure['value']
17
+ nil
18
+ end
19
+
20
+ def parse_native_object(native_object)
21
+ raise MessageError unless native_object == true || native_object == false
22
+ @value = native_object
23
+ nil
24
+ end
25
+
26
+ def to_json_structure
27
+ raise MessageError unless setup?
28
+ {'message_type' => 'object', 'object_type' => 'boolean', 'value' => @value}
29
+ end
30
+
31
+ def to_native_object
32
+ raise MessageError unless setup?
33
+ @value
34
+ end
35
+
36
+ private
37
+
38
+ def setup?
39
+ @value != nil
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,66 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Exception < Base
5
+
6
+ def self.map
7
+ @map ||=
8
+ [
9
+ ['no_method_exception', NoMethodError],
10
+ ['argument_exception', ArgumentError],
11
+ ['stale_object_exception', StaleObjectError],
12
+ ['unexposable_object_exception', UnexposableObjectError]
13
+ ]
14
+ end
15
+
16
+ def initialize(connection)
17
+ super
18
+ @type = nil
19
+ @message = nil
20
+ end
21
+
22
+ def parse_json_structure(json_structure)
23
+ raise MessageError unless json_structure.is_a?(::Hash) &&
24
+ json_structure['message_type'] == 'object' &&
25
+ json_structure['object_type'] == 'exception'
26
+ raise MessageError if self.class.map.assoc(json_structure['type']) == nil
27
+ @type = json_structure['type']
28
+ if json_structure['message']
29
+ raise MessageError unless json_structure['message'].is_a?(::String)
30
+ @message = json_structure['message']
31
+ end
32
+ nil
33
+ end
34
+
35
+ def parse_native_object(native_object)
36
+ raise MessageError if self.class.map.rassoc(native_object.class) == nil
37
+ @type = self.class.map.rassoc(native_object.class)[0]
38
+ @message = native_object.message
39
+ nil
40
+ end
41
+
42
+ def to_json_structure
43
+ raise MessageError unless setup?
44
+ {
45
+ 'message_type' => 'object',
46
+ 'object_type' => 'exception',
47
+ 'type' => @type,
48
+ 'message' => @message
49
+ }
50
+ end
51
+
52
+ def to_native_object
53
+ raise MessageError unless setup?
54
+ self.class.map.assoc(@type)[1].new(@message)
55
+ end
56
+
57
+ private
58
+
59
+ def setup?
60
+ @type != nil
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,79 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Exposed < Base
5
+
6
+ def initialize(connection)
7
+ super
8
+ @orgin = nil
9
+ @id = nil
10
+ end
11
+
12
+ def parse_json_structure(json_structure)
13
+ raise MessageError unless json_structure.is_a?(::Hash) &&
14
+ json_structure['message_type'] == 'object' &&
15
+ json_structure['object_type'] == 'exposed'
16
+ raise MessageError unless json_structure['orgin'] == 'local' || json_structure['orgin'] == 'remote'
17
+ @orgin = json_structure['orgin']
18
+ raise MessageError unless json_structure['id'].is_a?(::Integer)
19
+ @id = json_structure['id']
20
+ nil
21
+ end
22
+
23
+ def parse_native_object(native_object)
24
+ case
25
+ when native_object.respond_to?(:__sumac_exposed_object__)
26
+ @orgin = 'local'
27
+ @id = @connection.local_references.from_object(native_object).exposed_id
28
+ when native_object.is_a?(RemoteObject)
29
+ @orgin = 'remote'
30
+ @id = @connection.remote_references.from_object(native_object).exposed_id
31
+ else
32
+ raise MessageError
33
+ end
34
+ nil
35
+ end
36
+
37
+ def to_json_structure
38
+ raise MessageError unless setup?
39
+ {
40
+ 'message_type' => 'object',
41
+ 'object_type' => 'exposed',
42
+ 'orgin' => @orgin,
43
+ 'id' => @id
44
+ }
45
+ end
46
+
47
+ def to_native_object
48
+ raise MessageError unless setup?
49
+ case @orgin
50
+ when 'local'
51
+ native_object = @connection.local_references.from_id(@id).exposed_object
52
+ raise MessageError unless native_object
53
+ native_object
54
+ when 'remote'
55
+ @connection.remote_references.from_id(@id).remote_object
56
+ end
57
+ end
58
+
59
+ def invert_orgin
60
+ raise MessageError unless setup?
61
+ case @orgin
62
+ when 'local'
63
+ @orgin = 'remote'
64
+ when 'remote'
65
+ @orgin = 'local'
66
+ end
67
+ nil
68
+ end
69
+
70
+ private
71
+
72
+ def setup?
73
+ @orgin != nil && @id != nil
74
+ end
75
+
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,86 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class ExposedChild < Base
5
+
6
+ def initialize(connection)
7
+ super
8
+ @parent = nil
9
+ @key = nil
10
+ end
11
+
12
+ def parse_json_structure(json_structure)
13
+ raise MessageError unless json_structure.is_a?(::Hash) &&
14
+ json_structure['message_type'] == 'object' &&
15
+ json_structure['object_type'] == 'exposed_child'
16
+ @parent = Exposed.from_json_structure(@connection, json_structure['parent'])
17
+ key = json_structure['key']
18
+ raise MessageError unless key.is_a?(::String) || key.is_a?(::Float) || key.is_a?(::Integer)
19
+ @key = key
20
+ nil
21
+ end
22
+
23
+ def parse_native_object(native_object)
24
+ unless native_object.is_a?(RemoteObjectChild) ||
25
+ (native_object.respond_to?(:__sumac_exposed_object__) && native_object.respond_to?(:__parent__))
26
+ raise MessageError
27
+ end
28
+ begin
29
+ native_parent = native_object.__parent__
30
+ rescue
31
+ raise MessageError
32
+ end
33
+ @parent = Exposed.from_native_object(@connection, native_parent)
34
+ begin
35
+ key = native_object.__key__
36
+ rescue
37
+ raise MessageError
38
+ end
39
+ raise unless key.is_a?(::String) || key.is_a?(::Float) || key.is_a?(::Integer)
40
+ @key = key
41
+ nil
42
+ end
43
+
44
+ def to_json_structure
45
+ raise MessageError unless setup?
46
+ {
47
+ 'message_type' => 'object',
48
+ 'object_type' => 'exposed_child',
49
+ 'parent' => @parent.to_json_structure,
50
+ 'key' => @key
51
+ }
52
+ end
53
+
54
+ def to_native_object
55
+ raise MessageError unless setup?
56
+ native_parent = @parent.to_native_object
57
+ case native_parent
58
+ when ExposedObject
59
+ begin
60
+ native_child = native_parent.__child__(@key)
61
+ rescue
62
+ raise MessageError
63
+ end
64
+ raise unless native_child.is_a?(ExposedObjectChild)
65
+ when RemoteObject
66
+ native_child = RemoteObjectChild.new(@connection, native_parent, @key)
67
+ end
68
+ native_child
69
+ end
70
+
71
+ def invert_orgin
72
+ raise MessageError unless setup?
73
+ @parent.invert_orgin
74
+ nil
75
+ end
76
+
77
+ private
78
+
79
+ def setup?
80
+ @parent != nil && @key != nil
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,45 @@
1
+ class Sumac
2
+ class Message
3
+ class Object
4
+ class Float < Base
5
+
6
+ def initialize(connection)
7
+ super
8
+ @value = nil
9
+ end
10
+
11
+ def parse_json_structure(json_structure)
12
+ raise MessageError unless json_structure.is_a?(::Hash) &&
13
+ json_structure['message_type'] == 'object' &&
14
+ json_structure['object_type'] == 'float'
15
+ raise MessageError unless json_structure['value'].is_a?(::Numeric)
16
+ @value = json_structure['value'].to_f
17
+ nil
18
+ end
19
+
20
+ def parse_native_object(native_object)
21
+ raise MessageError unless native_object.is_a?(::Float)
22
+ @value = native_object
23
+ nil
24
+ end
25
+
26
+ def to_json_structure
27
+ raise MessageError unless setup?
28
+ {'message_type' => 'object', 'object_type' => 'float', 'value' => @value}
29
+ end
30
+
31
+ def to_native_object
32
+ raise MessageError unless setup?
33
+ @value
34
+ end
35
+
36
+ private
37
+
38
+ def setup?
39
+ @value != nil
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end