sgslib 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sgs/rpc.rb +84 -0
- data/lib/sgs/version.rb +1 -1
- data/lib/sgslib.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bea8bcf38f3b977b335ab31aeeff1cb18596c85
|
4
|
+
data.tar.gz: 2160754a21d95a718a24483cc4efe322a27e1648
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6574269e3640039aa280efbfb5fa908346fdcd31916051632b3b574ac8385e3613c78c76d02c59e4815ef8d2cbc7da3fe20526f2336965118557f82126919507
|
7
|
+
data.tar.gz: cc6e4e3f58a463c4c729c9ce35d9aae0fdae77b59b95c2223d695f5ee9e2e0be625b3961a2d333e401a115eebc350b084e6eb2275be9860cfb36506bb2d4ba38
|
data/lib/sgs/rpc.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2018, Kalopa Research. All rights reserved. This is free
|
3
|
+
# software; you can redistribute it and/or modify it under the terms of the
|
4
|
+
# GNU General Public License as published by the Free Software Foundation;
|
5
|
+
# either version 2, or (at your option) any later version.
|
6
|
+
#
|
7
|
+
# It is distributed in the hope that it will be useful, but WITHOUT
|
8
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
9
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
10
|
+
# for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License along
|
13
|
+
# with this product; see the file COPYING. If not, write to the Free
|
14
|
+
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY KALOPA RESEARCH "AS IS" AND ANY EXPRESS OR
|
17
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
18
|
+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
19
|
+
# IN NO EVENT SHALL KALOPA RESEARCH BE LIABLE FOR ANY DIRECT, INDIRECT,
|
20
|
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
21
|
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
22
|
+
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
23
|
+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
#
|
27
|
+
|
28
|
+
##
|
29
|
+
# Routines for sending and receiving messages using Redis. Sourced from
|
30
|
+
# Anthoni Scotti, https://128bit.io/2014/08/31/rpc-using-redis/
|
31
|
+
#
|
32
|
+
require 'redis'
|
33
|
+
require 'securerandom'
|
34
|
+
require 'msgpack'
|
35
|
+
|
36
|
+
module SGS
|
37
|
+
class RPCClient
|
38
|
+
def initialize(channel)
|
39
|
+
@channel = channel.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(name, *args)
|
43
|
+
uuid = SecureRandom.uuid
|
44
|
+
request = {
|
45
|
+
'id' => uuid,
|
46
|
+
'jsonrpc' => '2.0',
|
47
|
+
'method' => name,
|
48
|
+
'params' => args
|
49
|
+
}
|
50
|
+
SGS::RedisBase.redis.lpush(@channel, request.to_msgpack)
|
51
|
+
channel, response = SGS::RedisBase.redis.brpop(uuid, timeout=60)
|
52
|
+
MessagePack.unpack(response)['result']
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class RPCServer
|
57
|
+
def initialize(channel, klass)
|
58
|
+
@channel = channel.to_s
|
59
|
+
@klass = klass
|
60
|
+
end
|
61
|
+
|
62
|
+
def start
|
63
|
+
puts "Starting RPC server for #{@channel}"
|
64
|
+
loop do
|
65
|
+
channel, request = SGS::RedisBase.redis.brpop(@channel)
|
66
|
+
request = MessagePack.unpack(request)
|
67
|
+
|
68
|
+
puts "Working on request: #{request['id']}"
|
69
|
+
|
70
|
+
args = request['params'].unshift(request['method'])
|
71
|
+
result = @klass.send *args
|
72
|
+
|
73
|
+
reply = {
|
74
|
+
'jsonrpc' => '2.0',
|
75
|
+
'result' => result,
|
76
|
+
'id' => request['id']
|
77
|
+
}
|
78
|
+
|
79
|
+
SGS::RedisBase.redis.rpush(request['id'], MessagePack.pack(reply))
|
80
|
+
SGS::RedisBase.redis.expire(request['id'], 30)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/sgs/version.rb
CHANGED
data/lib/sgslib.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sgslib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dermot Tynan
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- lib/sgs/nmea.rb
|
96
96
|
- lib/sgs/otto.rb
|
97
97
|
- lib/sgs/redis_base.rb
|
98
|
+
- lib/sgs/rpc.rb
|
98
99
|
- lib/sgs/timing.rb
|
99
100
|
- lib/sgs/version.rb
|
100
101
|
- lib/sgs/waypoint.rb
|