rb_banyan 0.1.2.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,188 @@
1
+ ##############################################################################
2
+ # Copyright (c) 2017 Alan Yorinks - All Rights Reserved.
3
+ #
4
+ # This file is part of Ruby Banyan.
5
+ #
6
+ # Ruby Banyan is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
8
+ # Version 3 as published by the Free Software Foundation; either
9
+ # or (at your option) any later version.
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
16
+ # along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+ #
19
+ # banyan_base.rb
20
+ ###############################################################################
21
+
22
+ require 'rubygems'
23
+ # noinspection RubyResolve
24
+ require 'ffi-rzmq'
25
+ require 'socket'
26
+ # noinspection RubyResolve
27
+ require 'msgpack'
28
+ require 'optparse'
29
+
30
+ # This class is used as a base class to create a Banyan compatible
31
+ # component that connects to a single Banyan Backplane.
32
+ # A banyan component inherits and extends this class
33
+ # to encapsulate zeromq and message pack functionality.
34
+ # Its methods may be overridden by the user in the derived class
35
+ # to meet the needs of the component.
36
+ # noinspection ALL
37
+ class BanyanBase
38
+ ## Parameters:
39
+ # back_plane_ip_address: banyan_base back_planeIP Address.
40
+ # If not specified, it will be set to the
41
+ # IP address of local computer.
42
+ # subscriber_port: banyan_base back plane subscriber port.
43
+ # This must match that of the banyan_base backplane
44
+ # publisher_port: banyan_base back plane publisher port.
45
+ # This must match that of the banyan_base backplane
46
+ # process_name: Component identifier
47
+ # loop_time: receive loop sleep time.
48
+ #
49
+ # Example Usage:
50
+ #
51
+ ## require 'rubygems'
52
+ ## require 'rb_banyan/banyan_base.rb'
53
+ ## class Myexample < BanyanBase
54
+ #
55
+ # def initialize
56
+ # super(process_name: 'MyExample')
57
+ # end
58
+ ## end
59
+ #
60
+ ## MyExample.new
61
+ #
62
+ ## => This banner appears in the console
63
+ ##
64
+ ## ***********************************************************************
65
+ ## ***** Reminder: Make Sure That The Backplane is Already Running *****
66
+ ## ***********************************************************************
67
+ ##
68
+ ## ************************************************************
69
+ ## MyExample using BackPlane IP address: 192.168.2.194
70
+ ## Subscriber Port = 43125
71
+ ## Publisher Port = 43124
72
+ ## Loop Time = 0.1 seconds
73
+ ## ************************************************************
74
+
75
+
76
+ def initialize(back_plane_ip_address: nil, subscriber_port: '43125',
77
+ publisher_port: '43124', process_name: 'Unnamed',
78
+ loop_time: 0.1)
79
+ # If no back plane address was specified, determine the IP address
80
+ # of the local machine
81
+ if back_plane_ip_address.nil?
82
+ @back_plane_ip_address = Socket.ip_address_list[1].ip_address
83
+ else
84
+ @back_plane_ip_address = back_plane_ip_address
85
+ end
86
+ @subscriber_port = subscriber_port
87
+ @publisher_port = publisher_port
88
+ @process_name = process_name
89
+ @loop_time = loop_time
90
+
91
+ puts('***********************************************************************')
92
+ puts('***** Reminder: Make Sure That The Backplane is Already Running *****')
93
+ puts('***********************************************************************')
94
+ puts
95
+
96
+ puts('************************************************************')
97
+ # noinspection RubyResolve
98
+ puts(@process_name + ' using BackPlane IP address: ' +
99
+ @back_plane_ip_address)
100
+ puts('Subscriber Port = ' + @subscriber_port)
101
+ puts('Publisher Port = ' + @publisher_port)
102
+ puts('Loop Time = ' + (@loop_time.to_s) + ' seconds')
103
+ puts('************************************************************')
104
+
105
+ # establish the zeromq sub and pub sockets and connect to the backplane
106
+ @context = ZMQ::Context.create
107
+ @subscriber = @context.socket ZMQ::SUB
108
+ # noinspection RubyResolve
109
+ @subscriber.connect 'tcp://' + @back_plane_ip_address + ':' + @subscriber_port
110
+
111
+ @publisher = @context.socket ZMQ::PUB
112
+ @publisher.connect 'tcp://' + @back_plane_ip_address + ':' + @publisher_port
113
+
114
+ trap('INT') {'puts Control C detected - bye bye.'; @publisher.close; @subscriber.close; @context.terminate; exit}
115
+ end
116
+
117
+ # This method sets a subscriber topic.
118
+ # Parameters:
119
+ # topic: Message topic - must be in the form of a string
120
+ def set_subscriber_topic(topic)
121
+
122
+ # You can subscribe to multiple topics by calling this method for
123
+ # each topic.
124
+
125
+ if topic.is_a? String
126
+ @subscriber.setsockopt ZMQ::SUBSCRIBE, topic
127
+ else
128
+ raise TypeError, 'Subscriber topic must be a string'
129
+ end
130
+ end
131
+
132
+ # This method will publish a python_banyan payload and its associated topic
133
+ # Parameters:
134
+ # payload: Message payload in the form of a hash
135
+ # topic: Message topic - must be in the form of a string
136
+ def publish_payload(payload, topic)
137
+ if topic.is_a? String
138
+ message = payload.to_msgpack
139
+ pub_envelope = topic.encode('UTF-8')
140
+ @publisher.send_strings([pub_envelope, message], flags = ZMQ::DONTWAIT)
141
+ else
142
+ raise TypeError, 'Subscriber topic must be a string'
143
+ end
144
+
145
+ end
146
+
147
+ # receive_loop
148
+ #
149
+ # This is the receive loop for zmq messages.
150
+ #
151
+ # This method may be overwritten to meet the needs
152
+ # of the application before handling received messages.
153
+ def receive_loop
154
+ loop do
155
+ list = []
156
+ p = @subscriber.recv_strings(list, ZMQ::DONTWAIT)
157
+ begin
158
+ if p == -1
159
+ if ZMQ::Util.errno == ZMQ::EAGAIN
160
+ sleep(@loop_time)
161
+ else
162
+ sleep(@loop_time)
163
+ puts ZMQ::Util.errno
164
+ end
165
+ else
166
+ incoming_message_processing(list[0], MessagePack.unpack(list[1]))
167
+ end
168
+ rescue Interrupt
169
+ clean_up
170
+ end
171
+ end
172
+ end
173
+
174
+ # This method should be overwritten to process incoming messages.
175
+ # Parameters:
176
+ # topic: Message topic - must be in the form of a string
177
+ # payload: Message payload in the form of a hash
178
+ def incoming_message_processing(topic, payload)
179
+ puts(topic, payload)
180
+ end
181
+
182
+ # This method closes all zmq connections.
183
+ def clean_up
184
+ @publisher.close
185
+ @subscriber.close
186
+ @context.terminate
187
+ end
188
+ end
@@ -0,0 +1,3 @@
1
+ module RbBanyan
2
+ VERSION = "0.1.2.pre"
3
+ end
data/lib/rb_banyan.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rb_banyan/version"
2
+
3
+ module RbBanyan
4
+ # Your code goes here...
5
+ end
data/rb_banyan.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rb_banyan/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rb_banyan"
8
+ spec.version = RbBanyan::VERSION
9
+ spec.authors = ["Alan Yorinks"]
10
+ spec.email = ["MrYsLab@gmail.com"]
11
+ spec.licenses = ['AGPL-3.0']
12
+ spec.summary = %q{BanyanBase is the base class used to derive Banyan compatible components.}
13
+ spec.description = %q{This gem contains the base class used to create Banyan compatible components
14
+ requiring connection to a single Banyan backplane. In addition to the base class,
15
+ executables for rb_backplane and rb_monitor are installed on the executable
16
+ path.}
17
+ spec.homepage = "https://github.com/MrYsLab/rb_banyan."
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency('ffi-rzmq', '~> 2.0')
27
+ spec.add_runtime_dependency('msgpack', '~> 1.1')
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.15"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb_banyan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2.pre
5
+ platform: ruby
6
+ authors:
7
+ - Alan Yorinks
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi-rzmq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: msgpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |-
84
+ This gem contains the base class used to create Banyan compatible components
85
+ requiring connection to a single Banyan backplane. In addition to the base class,
86
+ executables for rb_backplane and rb_monitor are installed on the executable
87
+ path.
88
+ email:
89
+ - MrYsLab@gmail.com
90
+ executables:
91
+ - rb_backplane
92
+ - rb_monitor
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".idea/misc.xml"
98
+ - ".idea/modules.xml"
99
+ - ".idea/rb_banyan.iml"
100
+ - ".idea/workspace.xml"
101
+ - ".rspec"
102
+ - ".travis.yml"
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - bin/console
108
+ - bin/setup
109
+ - examples/banyan_base/simple_echo_client.rb
110
+ - examples/banyan_base/simple_echo_server.rb
111
+ - examples/coverage/.last_run.json
112
+ - examples/coverage/.resultset.json.lock
113
+ - exe/rb_backplane
114
+ - exe/rb_monitor
115
+ - lib/rb_banyan.rb
116
+ - lib/rb_banyan/banyan_base.rb
117
+ - lib/rb_banyan/version.rb
118
+ - rb_banyan.gemspec
119
+ homepage: https://github.com/MrYsLab/rb_banyan.
120
+ licenses:
121
+ - AGPL-3.0
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">"
135
+ - !ruby/object:Gem::Version
136
+ version: 1.3.1
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.5.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: BanyanBase is the base class used to derive Banyan compatible components.
143
+ test_files: []