simple-rpc 1.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 07b1ba51457bb2bc5af100d433ef19043e3e415fb5eb9e962a77ac4f3642cb1b
4
+ data.tar.gz: 30f4fb2e32c51c20a3fb5b19305aa7d6ef9bd7f03c449973cdb20ef8a3895721
5
+ SHA512:
6
+ metadata.gz: 930444cc48d0cd3e629b8b71aa1f428a034e7e532cb68bf9b6fd7e8bb48fd5eae4c57d921ddbf6d962c9006bf38d749d3d69db6aa5305efcf5155e83d960a8ee
7
+ data.tar.gz: 798c37ab3ea28f2d8570fc247fc5794b9ac2661084739332aca4fa0aba38443dbd8ac73f2aaf50717e6d12b678b9c6292472fea01692f9f622152de768502488
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/clintmod/simple-rpc" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple-rpc (1.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ rake (~> 10.0)
17
+ simple-rpc!
18
+
19
+ BUNDLED WITH
20
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Clint Modien
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/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # SimpleRPC
2
+
3
+ A simple ruby library for doing machine local interprocess communication using unix sockets.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple-rpc'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple-rpc
20
+
21
+ ## Usage
22
+
23
+ You can clone this repository and open two different console windows and run:
24
+
25
+ Console 1
26
+
27
+ `CHANNEL=/tmp/asdf ./test.rb `
28
+
29
+ Console 2
30
+
31
+ `CHANNEL=/tmp/asdf CHILD=1 ./test.rb`
32
+
33
+
34
+ The `CHILD=1` env var flips the channels in the second process so that they the two processes can send and receive on two local unix sockets as defined by the `CHANNEL` env var.
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/clintmod/simple-rpc.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,81 @@
1
+ require 'socket'
2
+
3
+ class LocalSocket
4
+ attr_reader :connected
5
+ CONNECTION_TEST = "connection_test"
6
+
7
+ def initialize(receive_channel, send_channel, max_message_size=5000, thread_sleep=0.1, socket_timeout=60)
8
+ @receive_channel = receive_channel
9
+ @send_channel = send_channel
10
+ @max_message_size = max_message_size
11
+ @thread_sleep = thread_sleep
12
+ @last_connected_status = nil
13
+ @connected = false
14
+ @receiver_thread = nil
15
+ @connection_thread = nil
16
+ bind
17
+ end
18
+
19
+ def send_msg(msg)
20
+ flag = false
21
+ begin
22
+ @socket.sendmsg_nonblock(msg, 0, @snd_addrInfo)
23
+ flag = true
24
+ rescue => e
25
+ puts "Error: #{e}" unless "#{e}".include? "Connection refused"
26
+ flag = false
27
+ end
28
+ return flag
29
+ end
30
+
31
+ def bind
32
+ begin
33
+ File.unlink @receive_channel
34
+ rescue => e
35
+ puts "#{e}"
36
+ end
37
+ @socket = Socket.new(:UNIX, :DGRAM, 0)
38
+ @snd_addrInfo = Addrinfo.unix(@send_channel)
39
+ @rcv_addrInfo = Addrinfo.unix(@receive_channel)
40
+ @socket.bind(@rcv_addrInfo)
41
+ end
42
+
43
+ def join
44
+ @receiver_thread.join
45
+ end
46
+
47
+
48
+ def message_received
49
+ @receiver_thread = Thread.new do
50
+ loop do
51
+ begin
52
+ result = @socket.recv_nonblock(@max_message_size)
53
+ if result != CONNECTION_TEST
54
+ yield result
55
+ end
56
+ rescue => e
57
+ puts "#{e}" unless "#{e}".include? "would block"
58
+ end
59
+ sleep @thread_sleep
60
+ end
61
+ end
62
+ end
63
+
64
+ def connection_changed
65
+ @connection_thread = Thread.new do
66
+ loop do
67
+ if send_msg(CONNECTION_TEST)
68
+ @connected = true
69
+ else
70
+ @connected = false
71
+ end
72
+ if @last_connected_status != @connected
73
+ yield @connected
74
+ end
75
+ @last_connected_status = @connected
76
+ sleep @thread_sleep
77
+ end
78
+ end
79
+ end
80
+
81
+ end
data/lib/simple-rpc.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ require 'local_socket'
3
+
4
+ class SimpleRPC
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ is_child = ENV['CHILD'] ? true : false
9
+ parent_channel = ENV['CHANNEL'] + '_parent'
10
+ child_channel = ENV['CHANNEL'] + '_child'
11
+ receive_channel = is_child ? parent_channel : child_channel
12
+ send_channel = is_child ? child_channel : parent_channel
13
+ @socket = LocalSocket.new(receive_channel, send_channel)
14
+
15
+ @socket.message_received do | json_array_string |
16
+ array = JSON.parse(json_array_string)
17
+ @client.send(*array)
18
+ end
19
+ @socket.connection_changed do |connected|
20
+ puts "Connection changed: connected = #{connected}"
21
+ end
22
+ end
23
+
24
+ def send_msg(*args)
25
+ msg = JSON.generate(args)
26
+ @socket.send_msg msg
27
+ end
28
+
29
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simple-rpc"
7
+ spec.version = "1.0.2"
8
+ spec.authors = ["Clint M"]
9
+ spec.email = ["cmodien@gmail.com"]
10
+
11
+ spec.summary = "A simple ruby library for doing machine local interprocess communication using unix sockets."
12
+ spec.description = "A simple ruby library for doing machine local interprocess communication using unix sockets."
13
+ spec.homepage = "https://github.com/clintmod/simple-rpc"
14
+
15
+ if not spec.respond_to?(:metadata)
16
+ raise "RubyGems 2.0 or newer is required to protect against " \
17
+ "public gem pushes."
18
+ end
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
data/test.rb ADDED
@@ -0,0 +1,29 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), '/lib'))
4
+
5
+ require 'simple-rpc'
6
+
7
+ class Client
8
+
9
+ def initialize
10
+ @rpc = SimpleRPC.new(self)
11
+ end
12
+
13
+ def say_hello(adjective, sender)
14
+ puts "hello #{adjective} #{sender}"
15
+ end
16
+
17
+ def test
18
+ @rpc.send_msg("say_hello", "awesome", "dude")
19
+ end
20
+
21
+ end
22
+
23
+ client = Client.new
24
+
25
+ puts "Press enter to test"
26
+ loop do
27
+ $stdin.gets.chomp
28
+ client.test
29
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Clint M
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A simple ruby library for doing machine local interprocess communication
42
+ using unix sockets.
43
+ email:
44
+ - cmodien@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - lib/local_socket.rb
56
+ - lib/simple-rpc.rb
57
+ - simple-ruby-rpc.gemspec
58
+ - test.rb
59
+ homepage: https://github.com/clintmod/simple-rpc
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.7.7
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A simple ruby library for doing machine local interprocess communication
82
+ using unix sockets.
83
+ test_files: []