ulms_client 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ulms_client.rb +21 -41
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cce99c2fc29978656d0357a1b8f21ab17d9435b0acfd5405a6c49fac3d68ebf1
|
4
|
+
data.tar.gz: b0cce196c058ca68e613bde9a1d518bd021cdfcfccf5d4ece99cea8083e1b2d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44c9e62cc5a055bff6a5ce546f40dceca0b3884565cd60f15aea70dc544ff64d3149fdecb39627fccbefb2ccd4dfa1262c9f77726e46126369837aded5351f0a
|
7
|
+
data.tar.gz: a497e761a64ea4205cd7d276581ef85651843c99d233c1d41eec75c2d5bd6c114d2ac097d2dc1089e718796695bdb623acfb8605f6cbebee4695f2b176bdb512
|
data/lib/ulms_client.rb
CHANGED
@@ -39,30 +39,17 @@ class Agent
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
class Client
|
43
|
-
attr_reader :version, :mode, :agent
|
44
|
-
|
45
|
-
def initialize(version:, mode:, agent:)
|
46
|
-
@version = version
|
47
|
-
@mode = mode
|
48
|
-
@agent = agent
|
49
|
-
end
|
50
|
-
|
51
|
-
def to_s
|
52
|
-
"#{@version}/#{@mode}/#{@agent}"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
42
|
class Connection
|
57
|
-
OPTIONS = [:
|
43
|
+
OPTIONS = [:password, :clean_session, :keep_alive]
|
58
44
|
|
59
|
-
def initialize(host:, port:,
|
60
|
-
@
|
45
|
+
def initialize(host:, port:, mode:, agent:, **kwargs)
|
46
|
+
@agent = agent
|
61
47
|
|
62
48
|
@mqtt = MQTT::Client.new
|
63
49
|
@mqtt.host = host
|
64
50
|
@mqtt.port = port
|
65
|
-
@mqtt.
|
51
|
+
@mqtt.username = "v2::#{mode}"
|
52
|
+
@mqtt.client_id = agent.to_s
|
66
53
|
|
67
54
|
OPTIONS.each do |option|
|
68
55
|
@mqtt.send("#{option}=", kwargs[option]) if kwargs[option] != nil
|
@@ -72,13 +59,13 @@ class Connection
|
|
72
59
|
# Establish the connection.
|
73
60
|
def connect
|
74
61
|
@mqtt.connect
|
75
|
-
LOG.info("#{@
|
62
|
+
LOG.info("#{@agent} connected")
|
76
63
|
end
|
77
64
|
|
78
65
|
# Disconnect from the broker.
|
79
66
|
def disconnect
|
80
67
|
@mqtt.disconnect
|
81
|
-
LOG.info("#{@
|
68
|
+
LOG.info("#{@agent} disconnected")
|
82
69
|
end
|
83
70
|
|
84
71
|
# Publish a message to the `topic`.
|
@@ -97,7 +84,7 @@ class Connection
|
|
97
84
|
@mqtt.publish(topic, JSON.dump(envelope), retain, qos)
|
98
85
|
|
99
86
|
LOG.info <<~EOF
|
100
|
-
#{@
|
87
|
+
#{@agent} published to #{topic} (q#{qos}, r#{retain ? 1 : 0}):
|
101
88
|
Payload: #{JSON.pretty_generate(payload)}
|
102
89
|
Properties: #{JSON.pretty_generate(properties)}
|
103
90
|
EOF
|
@@ -109,7 +96,7 @@ class Connection
|
|
109
96
|
# - `qos`: Subscriptions QoS. An interger 0..2.
|
110
97
|
def subscribe(topic, qos: 0)
|
111
98
|
@mqtt.subscribe([topic, qos])
|
112
|
-
LOG.info("#{@
|
99
|
+
LOG.info("#{@agent} subscribed to #{topic} (q#{qos})")
|
113
100
|
end
|
114
101
|
|
115
102
|
# Waits for an incoming message.
|
@@ -126,7 +113,7 @@ class Connection
|
|
126
113
|
message = IncomingMessage.new(topic, payload, envelope['properties'])
|
127
114
|
|
128
115
|
LOG.info <<~EOF
|
129
|
-
#{@
|
116
|
+
#{@agent} received a message from topic #{topic}:
|
130
117
|
Payload: #{JSON.pretty_generate(message.payload)}
|
131
118
|
Properties: #{JSON.pretty_generate(message.properties)}
|
132
119
|
EOF
|
@@ -148,20 +135,21 @@ class Connection
|
|
148
135
|
# Options:
|
149
136
|
# - `to`: the destination service `Account` (required).
|
150
137
|
# - `payload`: the publish message payload (required).
|
138
|
+
# - `api_version`: service API version.
|
151
139
|
# - `properties`: additional MQTT properties hash.
|
152
140
|
# - `qos`: Publish QoS. An integer 0..2.
|
153
141
|
# - `timeout`: Timeout for the response awaiting.
|
154
|
-
def make_request(method, to:, payload:, properties: {}, qos: 0, timeout: DEFAULT_TIMEOUT)
|
142
|
+
def make_request(method, to:, payload:, api_version: 'v1', properties: {}, qos: 0, timeout: DEFAULT_TIMEOUT)
|
155
143
|
correlation_data = SecureRandom.hex
|
156
144
|
|
157
145
|
properties.merge!({
|
158
146
|
type: 'request',
|
159
147
|
method: method,
|
160
148
|
correlation_data: correlation_data,
|
161
|
-
response_topic: "agents/#{@
|
149
|
+
response_topic: "agents/#{@agent}/api/#{api_version}/in/#{to}"
|
162
150
|
})
|
163
151
|
|
164
|
-
topic = "agents/#{@
|
152
|
+
topic = "agents/#{@agent}/api/#{api_version}/out/#{to}"
|
165
153
|
publish(topic, payload: payload, properties: properties, qos: qos)
|
166
154
|
|
167
155
|
receive(timeout) do |msg|
|
@@ -203,28 +191,20 @@ def account(label, audience)
|
|
203
191
|
Account.new(label, audience)
|
204
192
|
end
|
205
193
|
|
206
|
-
# Builds a `Client` instance.
|
207
|
-
#
|
208
|
-
# Options:
|
209
|
-
# - `mode`: Connection mode (required). Available values: `agents`, `service-agents`, `bridge-agents`, `observer-agents`.
|
210
|
-
# - `version`: Always `v1` for now.
|
211
|
-
def client(agent, mode:, version: 'v1')
|
212
|
-
Client.new(version: version, mode: mode, agent: agent)
|
213
|
-
end
|
214
|
-
|
215
194
|
# Connects to the broker and subscribes to the client's inbox topics.
|
216
195
|
#
|
217
196
|
# Options:
|
218
197
|
# - `host`: The broker's host (required).
|
219
198
|
# - `port`: The broker's TCP port for MQTT connections (required).
|
220
|
-
# - `
|
221
|
-
# - `
|
222
|
-
# - `
|
199
|
+
# - `agent`: The `Agent` object (required).
|
200
|
+
# - `mode`: Connection mode: default | service | bridge | observer.
|
201
|
+
# - `api_version`: agent's API version.
|
202
|
+
# - `password`: If the broker has authn enalbed this requires the password for the `agent`'s account.
|
223
203
|
# - `clean_session`: A boolean indicating whether the broker has to clean the previos session.
|
224
204
|
# - `keep_alive`: Keep alive time in seconds.
|
225
|
-
def connect(host: 'localhost', port: 1883,
|
226
|
-
conn = Connection.new(host: host, port: port,
|
205
|
+
def connect(host: 'localhost', port: 1883, mode: 'default', agent:, api_version: 'v1', **kwargs)
|
206
|
+
conn = Connection.new(host: host, port: port, mode: mode, agent: agent, **kwargs)
|
227
207
|
conn.connect
|
228
|
-
conn.subscribe("agents/#{
|
208
|
+
conn.subscribe("agents/#{agent}/api/#{api_version}/in/#")
|
229
209
|
conn
|
230
210
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ulms_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timofey Martynov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt
|