devp2p 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,88 +0,0 @@
1
- # -*- encoding : ascii-8bit -*-
2
- require 'hashie'
3
-
4
- module DEVp2p
5
- class BaseApp
6
-
7
- extend Configurable
8
- add_config(
9
- default_config: {
10
- client_version_string: "ruby-devp2p #{VersionString}",
11
- deactivated_services: []
12
- }
13
- )
14
-
15
- attr :config, :services
16
-
17
- def initialize(config=default_config)
18
- @config = Utils.update_config_with_defaults config, default_config
19
- @container = Celluloid::Supervision::Container.new
20
- @services = Hashie::Mash.new
21
- end
22
-
23
- ##
24
- # Registers protocol with app, which will be accessible as
25
- # `app.services.<protocol_name>` (e.g. `app.services.p2p` or
26
- # `app.services.eth`)
27
- #
28
- def register_service(klass, *args)
29
- raise ArgumentError, "service must be instance of BaseService" unless klass.instance_of?(Class) && klass < BaseService
30
- raise ArgumentError, "service #{klass.name} already registered" if services.has_key?(klass.name)
31
-
32
- logger.info "registering service", service: klass.name
33
- @container.add type: klass, as: get_actor_name(klass.name), args: args
34
- services[klass.name] = actor(klass.name)
35
-
36
- klass
37
- end
38
-
39
- ##
40
- # Terminate service instance, remove it from registry.
41
- #
42
- def deregister_service(klass)
43
- raise ArgumentError, "service must be instance of BaseService" unless klass.instance_of?(Class) && klass < BaseService
44
- raise ArgumentError, "service #{klass.name} not registered" unless services.has_key?(klass.name)
45
-
46
- logger.info "deregistering service", service: klass.name
47
- @container.remove actor(klass.name)
48
- services.delete klass.name
49
-
50
- klass
51
- end
52
-
53
- def start
54
- services.each_value do |service|
55
- service.start if service.stopped?
56
- end
57
- end
58
-
59
- def stop
60
- services.each_value do |service|
61
- service.stop if service.alive?
62
- end
63
-
64
- #@container.shutdown
65
- end
66
-
67
- def join
68
- @services.each_value do |service|
69
- Celluloid::Actor.join service
70
- end
71
- end
72
-
73
- private
74
-
75
- def logger
76
- @logger ||= Logger.new 'app'
77
- end
78
-
79
- def actor(name)
80
- Celluloid::Actor[get_actor_name(name)]
81
- end
82
-
83
- def get_actor_name(service_name)
84
- "#{object_id}_#{service_name}"
85
- end
86
-
87
- end
88
- end
@@ -1,55 +0,0 @@
1
- # -*- encoding : ascii-8bit -*-
2
- module DEVp2p
3
-
4
- ##
5
- # Service instances are added to the application under
6
- # `app.services.<service_name>`.
7
- #
8
- # App should be passed to the service in order to query other services.
9
- #
10
- # Services must be an actor. If a service spawns additional services, it's
11
- # responsible to stop them.
12
- #
13
- class BaseService
14
- include Celluloid
15
- #finalizer :stop
16
- include Control
17
-
18
- extend Configurable
19
- add_config(
20
- name: '',
21
- default_config: {name: {}},
22
- required_services: []
23
- )
24
-
25
- class <<self
26
- ##
27
- # Services know best how to initiate themselves. Create a service
28
- # instance, probably based on `app.config` and `app.services`.
29
- #
30
- def register_with_app(app)
31
- app.register_service self, app
32
- end
33
- end
34
-
35
- attr :app, :config
36
-
37
- def initialize(app)
38
- @app = app
39
- @config = Utils.update_config_with_defaults app.config, default_config
40
-
41
- initialize_control
42
-
43
- available_services = app.services.each_value.map(&:class)
44
- required_services.each do |r|
45
- raise MissingRequiredServiceError, "require service #{r}" unless available_services.include?(r)
46
- end
47
- end
48
-
49
- def to_s
50
- "<Service #{name}##{object_id}>"
51
- end
52
- alias inspect to_s
53
-
54
- end
55
- end
@@ -1,32 +0,0 @@
1
- module DEVp2p
2
- module Control
3
-
4
- def initialize_control
5
- @stopped = true
6
- @killed = false
7
- end
8
-
9
- def run
10
- _run
11
- end
12
-
13
- def start
14
- @stopped = false
15
- async.run unless killed?
16
- end
17
-
18
- def stop
19
- @stopped = true
20
- @killed = true
21
- end
22
-
23
- def stopped?
24
- @stopped
25
- end
26
-
27
- def killed?
28
- @killed
29
- end
30
-
31
- end
32
- end