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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a137ff202514944b12a1080628bdff8999fe4e74f14cda4252b0e4d0dd306c77
4
- data.tar.gz: f96d2183deb3dd97132469a6d70f17c18c74404679974cac1948b1dd1dac96fb
3
+ metadata.gz: c97c4c362a8cdc9cd7d8dab1152525995fc9956d6b864fa9992bd4bb0901fcf1
4
+ data.tar.gz: d32bcd8aa8c120c6da3c449cc869284e40a72a53be0535f885e9ac2ff1c931ab
5
5
  SHA512:
6
- metadata.gz: 3efcd6a877f4514a9c6bf2289dba4d8b03b1235333e88a619ce1f37092290c0b904874b443ba6b2c0f713283f09af27afddda8b0e5d2123fb5cd06e213fc605e
7
- data.tar.gz: 886ce3111ff8616033e658d5562b30d2b8ed84667fed6db8188a00baa14cf033e77448947700847fb7314aca956436b9f1f1fbd2f4eb4d4f97b07e907d471ae9
6
+ metadata.gz: ac67dd97f74ccabe5e7b3408bc061c8840f90bcfa68f2d966fc161e46dbe5db79633f2a304a6538c142b8db99082c10cb2f9ddb9ec3759f2d4df9c4d2ffcb3a2
7
+ data.tar.gz: b82bc3f7c87e601a5ba3b45a6c5e68c2f912c42b06445d3ae2af514c1255ccb8b846d844bbad65964e1ab839644fd8355fb28e7f2fd1abe207f3fac8aa6c2af2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-rpc (1.0.4)
4
+ simple-rpc (1.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 ./test.rb `
27
+ `CHANNEL=/tmp/asdf ./client1.rb `
28
28
 
29
29
  Console 2
30
30
 
31
- `CHANNEL=/tmp/asdf CHILD=1 ./test.rb`
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 ./test.rb
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 say_hello(adjective, sender)
50
- puts "hello #{adjective} #{sender}"
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 test_hash(some_hash)
54
- puts "hash = #{some_hash}"
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 test_class(test_class)
58
- puts "test_class = #{test_class}"
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
- class TestClass
64
- def initialize(a, b)
65
- @a = a
66
- @b = b
67
- end
69
+ Client.new
70
+ while true
71
+ sleep 1
72
+ end
68
73
 
69
- def to_json(options = {})
70
- {'a' => @a, 'b' => @b}.to_json
71
- end
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
- def self.from_json string
74
- data = JSON.load string
75
- self.new data['a'], data['b']
86
+ class Client
87
+
88
+ def initialize
89
+ @rpc = SimpleRPC::RPC.new(self)
90
+ @rpc.connect
76
91
  end
77
- end
78
92
 
79
- client = Client.new
80
- @rpc = SimpleRPC.new(client)
93
+ def say_hello(adjective, sender)
94
+ puts "hello #{adjective} #{sender}"
95
+ end
81
96
 
82
- loop do
83
- puts "Press enter to test strings"
84
- $stdin.gets.chomp
85
- @rpc.send_msg("say_hello", "awesome", "dude")
97
+ def test_hash(data)
98
+ puts "hash = #{data}"
99
+ end
86
100
 
87
- puts "Press enter to test hashes"
88
- $stdin.gets.chomp
89
- @rpc.send_msg("test_hash", {test:"asdf"})
101
+ end
90
102
 
91
- puts "Press enter to test class instance"
92
- $stdin.gets.chomp
93
- @rpc.send_msg("test_class", TestClass.new('asdf1', 'asdf2'))
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
- class LocalSocket
4
- attr_reader :connected
5
- CONNECTION_TEST = "connection_test"
6
+ module SimpleRPC
6
7
 
7
- def initialize(receive_channel, send_channel, max_message_size=5000, thread_sleep=0.1)
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
8
+ class LocalSocket
9
+ attr_reader :status
10
+
11
+ CONNECTION_TEST = "connection_test"
18
12
 
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" or "#{e}".include? "No such file"
26
- flag = false
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
- def bind
32
- begin
33
- File.unlink @receive_channel
34
- rescue => e
35
- puts "#{e}" unless "#{e}".include? "No such file"
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
- def join
44
- @receiver_thread.join
45
- end
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
- 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
+ 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
- rescue => e
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
- 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
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
@@ -0,0 +1,10 @@
1
+ module SimpleRPC
2
+
3
+ module Events
4
+
5
+ MESSAGE_RECEIVED = "MESSAGE_RECEIVED"
6
+ CONNECTION_CHANGED = "CONNECTION_CHANGED"
7
+
8
+ end
9
+
10
+ end
@@ -0,0 +1,12 @@
1
+ module SimpleRPC
2
+
3
+ module Status
4
+
5
+ CONNECTED = "CONNECTED"
6
+ CONNECTING = "CONNECTING"
7
+ DISCONNECTED = "DISCONNECTED"
8
+ TIMED_OUT = "TIMED_OUT"
9
+
10
+ end
11
+
12
+ end
data/lib/simple-rpc.rb CHANGED
@@ -1,29 +1,43 @@
1
1
  require 'json'
2
2
  require 'local_socket'
3
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 |
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
- @socket.connection_changed do |connected|
20
- puts "Connection changed: connected = #{connected}"
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
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "simple-rpc"
7
- spec.version = "1.0.4"
7
+ spec.version = "1.0.5"
8
8
  spec.authors = ["Clint M"]
9
9
  spec.email = ["cmodien@gmail.com"]
10
10
 
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
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-24 00:00:00.000000000 Z
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