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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7ee0414fad3641eb50bf386b9e05b2b4290ac3d
4
- data.tar.gz: aec3efc5d970a7705bc44b90f26e060dd286650a
3
+ metadata.gz: 6bea8bcf38f3b977b335ab31aeeff1cb18596c85
4
+ data.tar.gz: 2160754a21d95a718a24483cc4efe322a27e1648
5
5
  SHA512:
6
- metadata.gz: cf487f698bae00c8c37a2dfd588ab7388bc122afcb312d2a441ba8fc7887cb80a1da80d7d3e2977ed31964fe37c1a6943e40de5931553a62c942b0cbaabc130c
7
- data.tar.gz: db0e9bbfc4fdcd778885dbb30fc2a7f98a8594b5933fcd9e8c681f6853df2f28a0bc99097d41e4b675776d3aff1c04a8f506cc0592bea97e83a28c0cf14fafb6
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
@@ -1,3 +1,3 @@
1
1
  module SGS
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
data/lib/sgslib.rb CHANGED
@@ -30,6 +30,7 @@
30
30
  require "sgs/version"
31
31
  require 'sgs/redis_base'
32
32
  require 'sgs/config'
33
+ require 'sgs/rpc'
33
34
  require 'sgs/location'
34
35
  require 'sgs/nmea'
35
36
  require 'sgs/gps'
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.6
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