simple-rpc 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +46 -33
- data/client1.rb +40 -0
- data/client2.rb +32 -0
- data/lib/local_socket.rb +81 -60
- data/lib/local_socket_events.rb +10 -0
- data/lib/local_socket_status.rb +12 -0
- data/lib/simple-rpc.rb +32 -18
- data/simple-ruby-rpc.gemspec +1 -1
- metadata +6 -3
- data/test.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c97c4c362a8cdc9cd7d8dab1152525995fc9956d6b864fa9992bd4bb0901fcf1
|
4
|
+
data.tar.gz: d32bcd8aa8c120c6da3c449cc869284e40a72a53be0535f885e9ac2ff1c931ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac67dd97f74ccabe5e7b3408bc061c8840f90bcfa68f2d966fc161e46dbe5db79633f2a304a6538c142b8db99082c10cb2f9ddb9ec3759f2d4df9c4d2ffcb3a2
|
7
|
+
data.tar.gz: b82bc3f7c87e601a5ba3b45a6c5e68c2f912c42b06445d3ae2af514c1255ccb8b846d844bbad65964e1ab839644fd8355fb28e7f2fd1abe207f3fac8aa6c2af2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -24,17 +24,17 @@ You can clone this repository and open two different console windows and run:
|
|
24
24
|
|
25
25
|
Console 1
|
26
26
|
|
27
|
-
`CHANNEL=/tmp/asdf ./
|
27
|
+
`CHANNEL=/tmp/asdf ./client1.rb `
|
28
28
|
|
29
29
|
Console 2
|
30
30
|
|
31
|
-
`CHANNEL=/tmp/asdf CHILD=1 ./
|
31
|
+
`CHANNEL=/tmp/asdf CHILD=1 ./client2.rb`
|
32
32
|
|
33
33
|
|
34
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
35
|
|
36
36
|
|
37
|
-
Here's the contents of ./
|
37
|
+
Here's the contents of ./client1.rb
|
38
38
|
|
39
39
|
```
|
40
40
|
|
@@ -46,56 +46,69 @@ require 'simple-rpc'
|
|
46
46
|
|
47
47
|
class Client
|
48
48
|
|
49
|
-
def
|
50
|
-
|
49
|
+
def initialize
|
50
|
+
@rpc = SimpleRPC::RPC.new(self)
|
51
|
+
@rpc.add_listener(SimpleRPC::Events::CONNECTION_CHANGED, method(:connection_changed))
|
52
|
+
@rpc.connect
|
51
53
|
end
|
52
54
|
|
53
|
-
def
|
54
|
-
puts "
|
55
|
+
def connection_changed(status)
|
56
|
+
puts "status = #{status}"
|
57
|
+
if status == SimpleRPC::Status::CONNECTED
|
58
|
+
test_all
|
59
|
+
end
|
55
60
|
end
|
56
61
|
|
57
|
-
def
|
58
|
-
|
62
|
+
def test_all
|
63
|
+
@rpc.send_msg("say_hello", "awesome", "dude")
|
64
|
+
@rpc.send_msg("test_hash", {test:"asdf"})
|
59
65
|
end
|
60
66
|
|
61
67
|
end
|
62
68
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
69
|
+
Client.new
|
70
|
+
while true
|
71
|
+
sleep 1
|
72
|
+
end
|
68
73
|
|
69
|
-
|
70
|
-
|
71
|
-
|
74
|
+
```
|
75
|
+
|
76
|
+
and here are the contents of ./client2.rb
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
#! /usr/bin/env ruby
|
81
|
+
|
82
|
+
$:.unshift(File.join(File.dirname(__FILE__), '/lib'))
|
83
|
+
|
84
|
+
require 'simple-rpc'
|
72
85
|
|
73
|
-
|
74
|
-
|
75
|
-
|
86
|
+
class Client
|
87
|
+
|
88
|
+
def initialize
|
89
|
+
@rpc = SimpleRPC::RPC.new(self)
|
90
|
+
@rpc.connect
|
76
91
|
end
|
77
|
-
end
|
78
92
|
|
79
|
-
|
80
|
-
|
93
|
+
def say_hello(adjective, sender)
|
94
|
+
puts "hello #{adjective} #{sender}"
|
95
|
+
end
|
81
96
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
@rpc.send_msg("say_hello", "awesome", "dude")
|
97
|
+
def test_hash(data)
|
98
|
+
puts "hash = #{data}"
|
99
|
+
end
|
86
100
|
|
87
|
-
|
88
|
-
$stdin.gets.chomp
|
89
|
-
@rpc.send_msg("test_hash", {test:"asdf"})
|
101
|
+
end
|
90
102
|
|
91
|
-
|
92
|
-
|
93
|
-
|
103
|
+
Client.new
|
104
|
+
while true
|
105
|
+
sleep 1
|
94
106
|
end
|
95
107
|
|
96
108
|
|
97
109
|
```
|
98
110
|
|
111
|
+
|
99
112
|
## Development
|
100
113
|
|
101
114
|
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.
|
data/client1.rb
ADDED
@@ -0,0 +1,40 @@
|
|
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::RPC.new(self)
|
11
|
+
@rpc.add_listener(SimpleRPC::Events::CONNECTION_CHANGED, method(:connection_changed))
|
12
|
+
@rpc.connect
|
13
|
+
end
|
14
|
+
|
15
|
+
def connection_changed(status)
|
16
|
+
puts "status = #{status}"
|
17
|
+
if status == SimpleRPC::Status::CONNECTED
|
18
|
+
test_all
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_all
|
23
|
+
@rpc.send_msg("say_hello", "awesome", "dude")
|
24
|
+
@rpc.send_msg("test_hash", {test:"asdf"})
|
25
|
+
@rpc.send_msg("test_callback", {test:"asdf"}, "some_method")
|
26
|
+
end
|
27
|
+
|
28
|
+
def some_method(data)
|
29
|
+
puts "some_method called with #{data}"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
client = Client.new
|
35
|
+
puts "Press enter to send again:"
|
36
|
+
while true
|
37
|
+
gets.chomp
|
38
|
+
client.test_all
|
39
|
+
sleep 1
|
40
|
+
end
|
data/client2.rb
ADDED
@@ -0,0 +1,32 @@
|
|
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::RPC.new(self)
|
11
|
+
@rpc.connect
|
12
|
+
end
|
13
|
+
|
14
|
+
def say_hello(adjective, sender)
|
15
|
+
puts "hello #{adjective} #{sender}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_hash(data)
|
19
|
+
puts "hash = #{data}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_callback(data, callback)
|
23
|
+
puts "calling #{callback} with #{data}"
|
24
|
+
@rpc.send_msg callback, data
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
Client.new
|
30
|
+
while true
|
31
|
+
sleep 1
|
32
|
+
end
|
data/lib/local_socket.rb
CHANGED
@@ -1,81 +1,102 @@
|
|
1
|
+
require 'local_socket_events'
|
2
|
+
require 'local_socket_status'
|
3
|
+
require 'set'
|
1
4
|
require 'socket'
|
2
5
|
|
3
|
-
|
4
|
-
attr_reader :connected
|
5
|
-
CONNECTION_TEST = "connection_test"
|
6
|
+
module SimpleRPC
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
8
|
+
class LocalSocket
|
9
|
+
attr_reader :status
|
10
|
+
|
11
|
+
CONNECTION_TEST = "connection_test"
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
13
|
+
def initialize(receive_channel, send_channel, max_message_size=5000, thread_sleep=0.1)
|
14
|
+
@receive_channel = receive_channel
|
15
|
+
@send_channel = send_channel
|
16
|
+
@max_message_size = max_message_size
|
17
|
+
@thread_sleep = thread_sleep
|
18
|
+
@last_status = nil
|
19
|
+
@status = false
|
20
|
+
@receiver_thread = nil
|
21
|
+
@_thread = nil
|
22
|
+
@listeners = {}
|
27
23
|
end
|
28
|
-
return flag
|
29
|
-
end
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
def send_msg(msg)
|
26
|
+
flag = false
|
27
|
+
begin
|
28
|
+
@socket.sendmsg_nonblock(msg, 0, @snd_addrInfo)
|
29
|
+
flag = true
|
30
|
+
rescue => e
|
31
|
+
puts "Error: #{e}" unless "#{e}".include? "Connection refused" or "#{e}".include? "No such file"
|
32
|
+
flag = false
|
33
|
+
end
|
34
|
+
return flag
|
36
35
|
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
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
def connect
|
38
|
+
bind
|
39
|
+
setup_receiver_thread
|
40
|
+
setup_status_thread
|
41
|
+
end
|
46
42
|
|
43
|
+
def bind
|
44
|
+
begin
|
45
|
+
File.unlink @receive_channel
|
46
|
+
rescue => e
|
47
|
+
puts "#{e}" unless "#{e}".include? "No such file"
|
48
|
+
end
|
49
|
+
@socket = Socket.new(:UNIX, :DGRAM, 0)
|
50
|
+
@snd_addrInfo = Addrinfo.unix(@send_channel)
|
51
|
+
@rcv_addrInfo = Addrinfo.unix(@receive_channel)
|
52
|
+
@socket.bind(@rcv_addrInfo)
|
53
|
+
end
|
47
54
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
+
def setup_receiver_thread
|
56
|
+
@receiver_thread = Thread.new do
|
57
|
+
loop do
|
58
|
+
begin
|
59
|
+
result = @socket.recv_nonblock(@max_message_size)
|
60
|
+
if result != CONNECTION_TEST
|
61
|
+
notify(Events::MESSAGE_RECEIVED, result)
|
62
|
+
end
|
63
|
+
rescue => e
|
64
|
+
puts "#{e}" unless "#{e}".include? "would block"
|
55
65
|
end
|
56
|
-
|
57
|
-
puts "#{e}" unless "#{e}".include? "would block"
|
66
|
+
sleep @thread_sleep
|
58
67
|
end
|
59
|
-
sleep @thread_sleep
|
60
68
|
end
|
61
69
|
end
|
62
|
-
end
|
63
70
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
def setup_status_thread
|
72
|
+
@status_thread = Thread.new do
|
73
|
+
# todo:clintmod implement timeout
|
74
|
+
loop do
|
75
|
+
if send_msg(CONNECTION_TEST)
|
76
|
+
@status = Status::CONNECTED
|
77
|
+
else
|
78
|
+
@status = Status::DISCONNECTED
|
79
|
+
end
|
80
|
+
if @last_status != @status
|
81
|
+
notify(Events::CONNECTION_CHANGED, @status)
|
82
|
+
end
|
83
|
+
@last_status = @status
|
84
|
+
sleep @thread_sleep
|
74
85
|
end
|
75
|
-
@last_connected_status = @connected
|
76
|
-
sleep @thread_sleep
|
77
86
|
end
|
78
87
|
end
|
88
|
+
|
89
|
+
def add_listener(event, listener)
|
90
|
+
@listeners[event] ||= Set.new
|
91
|
+
@listeners[event] << listener
|
92
|
+
end
|
93
|
+
|
94
|
+
def notify(event, data)
|
95
|
+
@listeners[event] && @listeners[event].each do |listener|
|
96
|
+
listener.call data
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
79
100
|
end
|
80
101
|
|
81
102
|
end
|
data/lib/simple-rpc.rb
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'local_socket'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
module SimpleRPC
|
5
|
+
|
6
|
+
class RPC
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
is_child = ENV['CHILD'] ? true : false
|
11
|
+
parent_channel = ENV['CHANNEL'] + '_parent'
|
12
|
+
child_channel = ENV['CHANNEL'] + '_child'
|
13
|
+
receive_channel = is_child ? parent_channel : child_channel
|
14
|
+
send_channel = is_child ? child_channel : parent_channel
|
15
|
+
@socket = LocalSocket.new(receive_channel, send_channel)
|
16
|
+
@socket.add_listener(Events::MESSAGE_RECEIVED, method(:message_received))
|
17
|
+
end
|
18
|
+
|
19
|
+
def message_received json_array_string
|
16
20
|
array = JSON.parse(json_array_string)
|
17
21
|
@client.send(*array)
|
18
22
|
end
|
19
|
-
|
20
|
-
|
23
|
+
|
24
|
+
def connect
|
25
|
+
@socket.connect
|
26
|
+
end
|
27
|
+
|
28
|
+
def send_msg(*args)
|
29
|
+
msg = JSON.generate(args)
|
30
|
+
@socket.send_msg msg
|
31
|
+
end
|
32
|
+
|
33
|
+
def status_changed
|
34
|
+
@socket.status_changed
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_listener(event, listener)
|
38
|
+
@socket.add_listener event, listener
|
21
39
|
end
|
22
|
-
end
|
23
40
|
|
24
|
-
def send_msg(*args)
|
25
|
-
msg = JSON.generate(args)
|
26
|
-
@socket.send_msg msg
|
27
41
|
end
|
28
42
|
|
29
43
|
end
|
data/simple-ruby-rpc.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clint M
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,10 +52,13 @@ files:
|
|
52
52
|
- LICENSE
|
53
53
|
- README.md
|
54
54
|
- Rakefile
|
55
|
+
- client1.rb
|
56
|
+
- client2.rb
|
55
57
|
- lib/local_socket.rb
|
58
|
+
- lib/local_socket_events.rb
|
59
|
+
- lib/local_socket_status.rb
|
56
60
|
- lib/simple-rpc.rb
|
57
61
|
- simple-ruby-rpc.gemspec
|
58
|
-
- test.rb
|
59
62
|
homepage: https://github.com/clintmod/simple-rpc
|
60
63
|
licenses: []
|
61
64
|
metadata: {}
|
data/test.rb
DELETED
@@ -1,54 +0,0 @@
|
|
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 say_hello(adjective, sender)
|
10
|
-
puts "hello #{adjective} #{sender}"
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_hash(some_hash)
|
14
|
-
puts "hash = #{some_hash}"
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_class(test_class)
|
18
|
-
puts "test_class = #{test_class}"
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
class TestClass
|
24
|
-
def initialize(a, b)
|
25
|
-
@a = a
|
26
|
-
@b = b
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_json(options = {})
|
30
|
-
{'a' => @a, 'b' => @b}.to_json
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.from_json string
|
34
|
-
data = JSON.load string
|
35
|
-
self.new data['a'], data['b']
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
client = Client.new
|
40
|
-
@rpc = SimpleRPC.new(client)
|
41
|
-
|
42
|
-
loop do
|
43
|
-
puts "Press enter to test strings"
|
44
|
-
$stdin.gets.chomp
|
45
|
-
@rpc.send_msg("say_hello", "awesome", "dude")
|
46
|
-
|
47
|
-
puts "Press enter to test hashes"
|
48
|
-
$stdin.gets.chomp
|
49
|
-
@rpc.send_msg("test_hash", {test:"asdf"})
|
50
|
-
|
51
|
-
puts "Press enter to test class instance"
|
52
|
-
$stdin.gets.chomp
|
53
|
-
@rpc.send_msg("test_class", TestClass.new('asdf1', 'asdf2'))
|
54
|
-
end
|