radlib 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 53cad957b6b70eb6e093ad71f1d690d1c00c87a3b7934f9b05f27e478dd1334f
4
+ data.tar.gz: 420525a7429e5bf0e8fe234cfcf8a6b2c28d177a0488b4aa3972e6a3ddf42e6f
5
+ SHA512:
6
+ metadata.gz: 788629155ba7b2b042e78b35386b0cdb9f04fa706651d5ca9e279d42ba67c43da0c4728fb57ee390ccbcd17024ea4c8829652b375f7c0ed52bc81b17882ad07b
7
+ data.tar.gz: 365b69f8248baa9176744066ab62765cfec805dcf49fd39d4aec728567e221f5dc4c0ee39ad1632013a707af864a10652945f7667f2020c1e4877cfd261acf23
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in radlib.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "standard", "~> 1.3"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Nathan Hessler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Radlib
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/radlib`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nhessler/radlib.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,4 @@
1
+ module Actor
2
+ def self.actor(&block)
3
+
4
+ end
@@ -0,0 +1,49 @@
1
+ module Radlib
2
+ class Agent < Ractor
3
+ private_class_method :new
4
+
5
+ def self.start(start_state)
6
+ new(start_state) do |state|
7
+
8
+ setup(state)
9
+
10
+ loop do
11
+ case Ractor.receive
12
+ in :set, new_state
13
+ do_set(new_state)
14
+ in :get, asker
15
+ parsel = do_get()
16
+ asker.send([Ractor.current, parsel])
17
+ in message
18
+ puts "Unknown Message: '#{message}' was received"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def set(new_state)
25
+ self.send([:set, new_state])
26
+ end
27
+
28
+ def get()
29
+ asker = Ractor.current
30
+ self.send([:get, asker])
31
+ message = Ractor.receive_if{|msg| msg.first == self}
32
+ message.last
33
+ end
34
+
35
+ private
36
+
37
+ def setup(state)
38
+ self[:state] = state
39
+ end
40
+
41
+ def do_set(new_state)
42
+ self[:state] = new_state
43
+ end
44
+
45
+ def do_get()
46
+ self[:state]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,73 @@
1
+ module Radlib
2
+ class Counter < Ractor
3
+ private_class_method :new
4
+
5
+ def self.inc(counter, num)
6
+ counter.send([:inc, num])
7
+ end
8
+
9
+ def self.dec(counter, num)
10
+ counter.send([:dec, num])
11
+ end
12
+
13
+ def self.get(counter)
14
+ counter.send(:get)
15
+ message = receive_if{|msg| msg.first == counter}
16
+ message.last
17
+ end
18
+
19
+ def self.start(start_count = 0)
20
+ new(start_count) do |count|
21
+
22
+ setup(count)
23
+
24
+ loop do
25
+ case Ractor.receive
26
+ in :inc, by
27
+ do_inc(by)
28
+ in :dec, by
29
+ do_dec(by)
30
+ in :get, asker
31
+ parsel = do_get()
32
+ asker.send([Ractor.current, parsel])
33
+ in msg
34
+ "unknown message '#{msg}' was received"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def inc(by = 1)
41
+ self.send([:inc, by])
42
+ end
43
+
44
+ def dec(by = 1)
45
+ self.send([:dec, by])
46
+ end
47
+
48
+ def get()
49
+ asker = Ractor.current
50
+ self.send([:get, asker])
51
+ message = Ractor.receive_if{ |msg| msg.first == self }
52
+ message.last
53
+ end
54
+
55
+ private
56
+
57
+ def setup(count)
58
+ self[:count] = count
59
+ end
60
+
61
+ def do_inc(by)
62
+ self[:count] = self[:count] + by
63
+ end
64
+
65
+ def do_dec(by)
66
+ self[:count] = self[:count] - by
67
+ end
68
+
69
+ def do_get()
70
+ self[:count]
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,24 @@
1
+ module Radlib
2
+ module TCP
3
+ class Client < Ractor
4
+ private_class_method :new
5
+ def self.async(id, server_address, server_port, server_requests = 1)
6
+ server_params = {address: server_address, port: server_port, requests: server_requests}
7
+ new(server_params, name: "client-#{id}") do |params|
8
+ params[:requests].times do |i|
9
+ server = TCPSocket.open(params[:address], params[:port])
10
+
11
+ request = "HELLO from #{self.name}"
12
+ server.puts(request)
13
+
14
+ response = server.gets
15
+
16
+ puts "#{Time.now} - Request #{i}: #{response.strip}"
17
+
18
+ server.close
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ module Radlib
2
+ module TCP
3
+ class RequestQueue < Ractor
4
+ private_class_method :new
5
+
6
+ def self.start(worker_count = 4)
7
+ new(worker_count) do |count|
8
+ setup(count)
9
+
10
+ loop do
11
+ case Ractor.receive
12
+ in [:process, client]
13
+ do_process(client)
14
+ in [:ready, worker]
15
+ do_ready(worker)
16
+ in message
17
+ puts "Unknown Message: '#{message}' was received"
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ def process(client)
24
+ self.send([:process, client], move: true)
25
+ end
26
+
27
+ def ready(worker)
28
+ self.send([:ready, worker])
29
+ end
30
+
31
+ private
32
+
33
+ def setup(count)
34
+ self[:workers] = count.times.map do |i|
35
+ ractor = RequestWorker.start(Ractor.current, i)
36
+ {id: i, ractor: ractor, ready: true}
37
+ end
38
+
39
+ self[:clients] = []
40
+ end
41
+
42
+ def workers
43
+ self[:workers]
44
+ end
45
+
46
+ def clients
47
+ self[:clients]
48
+ end
49
+
50
+ def do_process(client)
51
+ clients.append(client)
52
+ delegate_work
53
+ end
54
+
55
+ def do_ready(ractor)
56
+ worker = workers.find{|w| w[:ractor] == ractor}
57
+ worker[:ready] = true
58
+ delegate_work
59
+ end
60
+
61
+ def delegate_work
62
+ while any_clients? and any_workers?
63
+ worker = workers.find{|w| w[:ready] }
64
+ client = clients.shift
65
+ worker[:ractor].work(client)
66
+ worker[:ready] = false
67
+ end
68
+ end
69
+
70
+ def any_workers?
71
+ workers.any?{|w| w[:ready]}
72
+ end
73
+
74
+ def any_clients?
75
+ not clients.empty?
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,46 @@
1
+ module Radlib
2
+ module TCP
3
+ class RequestWorker < Ractor
4
+ private_class_method :new
5
+
6
+ def self.start(worker_queue, id)
7
+ new(worker_queue, name: "worker-#{id}") do |queue|
8
+ loop do
9
+ case Ractor.receive
10
+ in :work, client
11
+ do_work(client)
12
+ queue.ready(Ractor.current)
13
+ in message
14
+ puts "Unknown Message; '#{message}' was received"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def work(request)
21
+ self.send([:work, request], move: true)
22
+ end
23
+
24
+ private
25
+
26
+ def do_work(client)
27
+ request = client.gets
28
+
29
+ if result = request.match(/^HELLO from client-(.*?)$/)
30
+ client_id = result[1]
31
+ pause()
32
+ response = "HEY, client-#{client_id}! I'm #{self.name}."
33
+
34
+ client.puts(response)
35
+ end
36
+
37
+ client.close
38
+ end
39
+
40
+ def pause()
41
+ secs = [1, 2, 3].sample
42
+ sleep secs
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ module Radlib
2
+ module TCP
3
+ class Server < Ractor
4
+ private_class_method :new
5
+
6
+ def self.start(server_port, queue_size = 4)
7
+ new(server_port, queue_size) do |port, size|
8
+ setup(port, size)
9
+
10
+ take_requests()
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def setup(port, size)
17
+ self[:server] = TCPServer.new(port)
18
+ self[:queue] = RequestQueue.start(size)
19
+ end
20
+
21
+ def take_requests()
22
+ loop do
23
+ request = self[:server].accept
24
+ self[:queue].process(request)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ require 'socket'
2
+
3
+ require_relative 't_c_p/client'
4
+ require_relative 't_c_p/server'
5
+ require_relative 't_c_p/request_queue'
6
+ require_relative 't_c_p/request_worker'
7
+
8
+ module Radlib
9
+ module TCP
10
+
11
+ end
12
+ end
@@ -0,0 +1,101 @@
1
+ module Radlib
2
+ module Tarai
3
+ class SeqTarai
4
+ def call (x, y, z)
5
+ if x <= y
6
+ y
7
+ else
8
+ call(
9
+ call(x - 1, y, z),
10
+ call(y - 1, z, x),
11
+ call(z - 1, x, y))
12
+ end
13
+ end
14
+ end
15
+
16
+ class ParTarai < Ractor
17
+ private_class_method :new
18
+
19
+ def self.async(start_x, start_y, start_z)
20
+ new(start_x, start_y, start_z) do |x, y, z|
21
+ result = call(x, y, z)
22
+
23
+ loop do
24
+ case Ractor.receive
25
+ in :await, asker
26
+ asker.send([self, result])
27
+ break
28
+ in message
29
+ puts "Unknown Message: '#{message}' was received"
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def await()
36
+ self.send([:await, Ractor.current])
37
+ message = Ractor.receive_if{|msg| msg.first == self}
38
+ message.last
39
+ end
40
+
41
+ private
42
+
43
+ def call(x, y, z)
44
+ if x <= y
45
+ y
46
+ else
47
+ call(
48
+ call(x - 1, y, z),
49
+ call(y - 1, z, x),
50
+ call(z - 1, x, y))
51
+ end
52
+ end
53
+ end
54
+
55
+ class RecTarai < Ractor
56
+ private_class_method :new
57
+
58
+ def self.async(start_x, start_y, start_z)
59
+ new(start_x, start_y, start_z) do |x, y, z|
60
+ result = call(x, y, z)
61
+
62
+ loop do
63
+ case Ractor.receive
64
+ in :await, asker
65
+ asker.send([self, result])
66
+ break
67
+ in message
68
+ puts "Unknown Message: '#{message}' was received"
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ def await()
75
+ self.send([:await, Ractor.current])
76
+ message = Ractor.receive_if{|msg| msg.first == self}
77
+ message.last
78
+ end
79
+
80
+ private
81
+
82
+ def call(x, y, z)
83
+ if x <= y
84
+ y
85
+ else
86
+ rtx = RecTarai.async(x - 1, y, z)
87
+ rty = RecTarai.async(y - 1, z, x)
88
+ rtz = RecTarai.async(z - 1, x, y)
89
+
90
+ new_x = rtx.await
91
+ new_y = rty.await
92
+ new_z = rtz.await
93
+
94
+ rtt = RecTarai.async(new_x, new_y, new_z)
95
+
96
+ rtt.await
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Radlib
4
+ VERSION = "0.1.0"
5
+ end
data/lib/radlib.rb ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "radlib/version"
4
+
5
+ require_relative "radlib/agent"
6
+ require_relative "radlib/counter"
7
+ require_relative "radlib/tarai"
8
+ require_relative "radlib/t_c_p"
9
+
10
+ module Radlib
11
+ class Error < StandardError; end
12
+
13
+ # actor "Agent" do |a|
14
+
15
+ # a.setup do
16
+ # 0
17
+ # end
18
+
19
+ # a.tell :set, state, move: true do
20
+ # state = state
21
+ # end
22
+
23
+ # a.ask :get do
24
+
25
+ # end
26
+ # end
27
+ end
data/radlib.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/radlib/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "radlib"
7
+ spec.version = Radlib::VERSION
8
+ spec.authors = ["Nathan Hessler"]
9
+ spec.email = ["xmilestegx@gmail.com"]
10
+
11
+ spec.summary = "an exploration of ruby's ractors"
12
+ spec.description = "an exploration of ruby's ractors and potention apis or abstractions to make it more approachable"
13
+ spec.homepage = "https://github.com/nhessler/radlib"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/nhessler/radlib"
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
data/sig/radlib.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Radlib
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radlib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hessler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: an exploration of ruby's ractors and potention apis or abstractions to
14
+ make it more approachable
15
+ email:
16
+ - xmilestegx@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".standard.yml"
22
+ - Gemfile
23
+ - LICENSE
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/radlib.rb
28
+ - lib/radlib/actor.rb
29
+ - lib/radlib/agent.rb
30
+ - lib/radlib/counter.rb
31
+ - lib/radlib/t_c_p.rb
32
+ - lib/radlib/t_c_p/client.rb
33
+ - lib/radlib/t_c_p/request_queue.rb
34
+ - lib/radlib/t_c_p/request_worker.rb
35
+ - lib/radlib/t_c_p/server.rb
36
+ - lib/radlib/tarai.rb
37
+ - lib/radlib/version.rb
38
+ - radlib.gemspec
39
+ - sig/radlib.rbs
40
+ homepage: https://github.com/nhessler/radlib
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/nhessler/radlib
45
+ source_code_uri: https://github.com/nhessler/radlib
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.4.10
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: an exploration of ruby's ractors
65
+ test_files: []