Class: SelfSDK::MessagingClient
- Inherits:
-
Object
- Object
- SelfSDK::MessagingClient
- Defined in:
- lib/messaging.rb
Constant Summary collapse
- DEFAULT_DEVICE =
"1"
- DEFAULT_AUTO_RECONNECT =
true
- DEFAULT_STORAGE_DIR =
"./.self_storage"
- ON_DEMAND_CLOSE_CODE =
3999
- PRIORITIES =
{ "chat.invite": SelfSDK::Messages::PRIORITY_VISIBLE, "chat.join": SelfSDK::Messages::PRIORITY_INVISIBLE, "chat.message": SelfSDK::Messages::PRIORITY_VISIBLE, "chat.message.delete": SelfSDK::Messages::PRIORITY_INVISIBLE, "chat.message.delivered": SelfSDK::Messages::PRIORITY_INVISIBLE, "chat.message.edit": SelfSDK::Messages::PRIORITY_INVISIBLE, "chat.message.read": SelfSDK::Messages::PRIORITY_INVISIBLE, "chat.remove": SelfSDK::Messages::PRIORITY_INVISIBLE, "document.sign.req": SelfSDK::Messages::PRIORITY_VISIBLE, "identities.authenticate.req": SelfSDK::Messages::PRIORITY_VISIBLE, "identities.connections.req": SelfSDK::Messages::PRIORITY_VISIBLE, "identities.facts.query.req": SelfSDK::Messages::PRIORITY_VISIBLE, "identities.facts.issue": SelfSDK::Messages::PRIORITY_VISIBLE, "identities.notify": SelfSDK::Messages::PRIORITY_VISIBLE }
Instance Attribute Summary collapse
-
#ack_timeout ⇒ Object
Returns the value of attribute ack_timeout.
-
#client ⇒ Object
Returns the value of attribute client.
-
#device_id ⇒ Object
Returns the value of attribute device_id.
-
#encryption_client ⇒ Object
Returns the value of attribute encryption_client.
-
#jwt ⇒ Object
Returns the value of attribute jwt.
-
#source ⇒ Object
Returns the value of attribute source.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#type_observer ⇒ Object
Returns the value of attribute type_observer.
-
#uuid_observer ⇒ Object
Returns the value of attribute uuid_observer.
Instance Method Summary collapse
-
#add_acl_rule(payload) ⇒ Object
Allows incomming messages from the given identity.
- #clean_observers ⇒ Object
- #close ⇒ Object
-
#initialize(url, client, storage_key, options = {}) ⇒ MessagingClient
constructor
RestClient initializer.
-
#list_acl_rules ⇒ Object
Lists acl rules.
-
#notify_observer(message) ⇒ Object
Notify the type observer for the given message.
-
#remove_acl_rule(payload) ⇒ Object
Blocks incoming messages from specified identities.
-
#send_and_wait_for_response(msgs, original) ⇒ Object
Sends a message and waits for the response.
-
#send_custom(recipients, request_body) ⇒ Object
Send custom mmessage.
-
#send_message(msg) ⇒ Object
Send a message through self network.
- #session?(identifier, device) ⇒ Boolean
- #set_observer(original, options = {}, &block) ⇒ Object
-
#share_information(recipient, recipient_device, request) ⇒ Object
Responds a request information request.
- #stop ⇒ Object
- #subscribe(type, &block) ⇒ Object
-
#wait_for(uuid, msg = nil) ⇒ Object
Executes the given block and waits for the message id specified on the uuid.
Constructor Details
#initialize(url, client, storage_key, options = {}) ⇒ MessagingClient
RestClient initializer
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/messaging.rb', line 48 def initialize(url, client, storage_key, = {}) @mon = Monitor.new @url = url @messages = {} @acks = {} @type_observer = {} @uuid_observer = {} @jwt = client.jwt @client = client @ack_timeout = 60 # seconds @timeout = 120 # seconds @auth_id = SecureRandom.uuid @device_id = .fetch(:device_id, DEFAULT_DEVICE) @auto_reconnect = .fetch(:auto_reconnect, DEFAULT_AUTO_RECONNECT) @raw_storage_dir = .fetch(:storage_dir, DEFAULT_STORAGE_DIR) @storage_dir = "#{@raw_storage_dir}/apps/#{@jwt.id}/devices/#{@device_id}" FileUtils.mkdir_p @storage_dir unless File.exist? @storage_dir @offset_file = "#{@storage_dir}/#{@jwt.id}:#{@device_id}.offset" @offset = read_offset @source = SelfSDK::Sources.new("#{__dir__}/sources.json") migrate_old_storage_format unless .include? :no_crypto crypto_path = "#{@storage_dir}/keys" FileUtils.mkdir_p crypto_path unless File.exist? crypto_path @encryption_client = Crypto.new(@client, @device_id, crypto_path, storage_key) end if .include? :ws @ws = [:ws] else start end end |
Instance Attribute Details
#ack_timeout ⇒ Object
Returns the value of attribute ack_timeout.
37 38 39 |
# File 'lib/messaging.rb', line 37 def ack_timeout @ack_timeout end |
#client ⇒ Object
Returns the value of attribute client.
37 38 39 |
# File 'lib/messaging.rb', line 37 def client @client end |
#device_id ⇒ Object
Returns the value of attribute device_id.
37 38 39 |
# File 'lib/messaging.rb', line 37 def device_id @device_id end |
#encryption_client ⇒ Object
Returns the value of attribute encryption_client.
37 38 39 |
# File 'lib/messaging.rb', line 37 def encryption_client @encryption_client end |
#jwt ⇒ Object
Returns the value of attribute jwt.
37 38 39 |
# File 'lib/messaging.rb', line 37 def jwt @jwt end |
#source ⇒ Object
Returns the value of attribute source.
37 38 39 |
# File 'lib/messaging.rb', line 37 def source @source end |
#timeout ⇒ Object
Returns the value of attribute timeout.
37 38 39 |
# File 'lib/messaging.rb', line 37 def timeout @timeout end |
#type_observer ⇒ Object
Returns the value of attribute type_observer.
37 38 39 |
# File 'lib/messaging.rb', line 37 def type_observer @type_observer end |
#uuid_observer ⇒ Object
Returns the value of attribute uuid_observer.
37 38 39 |
# File 'lib/messaging.rb', line 37 def uuid_observer @uuid_observer end |
Instance Method Details
#add_acl_rule(payload) ⇒ Object
Allows incomming messages from the given identity
165 166 167 168 169 170 171 172 |
# File 'lib/messaging.rb', line 165 def add_acl_rule(payload) a = SelfMsg::Acl.new a.id = SecureRandom.uuid a.command = SelfMsg::AclCommandPERMIT a.payload = payload a end |
#clean_observers ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/messaging.rb', line 265 def clean_observers live = {} @uuid_observer.clone.each do |id, msg| if msg[:timeout] < SelfSDK::Time.now = SelfSDK::Messages::Base.new(self) .status = "errored" @uuid_observer[id][:block].call() @uuid_observer.delete(id) else live[id] = msg end end @uuid_observer = live end |
#close ⇒ Object
93 94 95 |
# File 'lib/messaging.rb', line 93 def close @ws.close(ON_DEMAND_CLOSE_CODE, "connection closed by the client") end |
#list_acl_rules ⇒ Object
Lists acl rules
187 188 189 190 191 192 193 194 195 |
# File 'lib/messaging.rb', line 187 def list_acl_rules wait_for 'acl_list' do a = SelfMsg::Acl.new a.id = SecureRandom.uuid a.command = SelfMsg::AclCommandLIST send_raw a end end |
#notify_observer(message) ⇒ Object
Notify the type observer for the given message
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/messaging.rb', line 282 def notify_observer() if @uuid_observer.include? .id observer = @uuid_observer[.id] .validate!(observer[:original_message]) if observer.include?(:original_message) Thread.new do @uuid_observer[.id][:block].call() @uuid_observer.delete(.id) end return end # Return if there is no observer setup for this kind of message return unless @type_observer.include? .typ Thread.new do @type_observer[.typ][:block].call() end end |
#remove_acl_rule(payload) ⇒ Object
Blocks incoming messages from specified identities
177 178 179 180 181 182 183 184 |
# File 'lib/messaging.rb', line 177 def remove_acl_rule(payload) a = SelfMsg::Acl.new a.id = SecureRandom.uuid a.command = SelfMsg::AclCommandREVOKE a.payload = payload a end |
#send_and_wait_for_response(msgs, original) ⇒ Object
Sends a message and waits for the response
200 201 202 203 204 205 206 207 |
# File 'lib/messaging.rb', line 200 def send_and_wait_for_response(msgs, original) SelfSDK.logger.info "sending/wait for #{msgs.first.id}" wait_for msgs.first.id, original do msgs.each do |msg| msg end end end |
#send_custom(recipients, request_body) ⇒ Object
Send custom mmessage
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/messaging.rb', line 124 def send_custom(recipients, request_body) # convert argument into an array if is a string recipients = [recipients] if recipients.is_a? String # send to current identity devices except the current one. recipients |= [@jwt.id] # build recipients list recs = [] recipients.each do |r| @client.devices(r).each do |to_device| recs << { id: r, device_id: to_device } end end SelfSDK.logger.info "sending custom message #{request_body.to_json}" current_device = "#{@jwt.id}:#{@device_id}" recs.each do |r| next if current_device == "#{r[:id]}:#{r[:device_id]}" request_body[:sub] = r[:id] request_body[:aud] = r[:id] unless request_body.key?(:aud) ciphertext = @encryption_client.encrypt(@jwt.prepare(request_body), recs) m = SelfMsg::Message.new m.id = SecureRandom.uuid m.sender = current_device m.recipient = "#{r[:id]}:#{r[:device_id]}" m.ciphertext = ciphertext m. = r[:typ] m.priority = select_priority(r[:typ]) SelfSDK.logger.info " -> to #{m.recipient}" m end end |
#send_message(msg) ⇒ Object
Send a message through self network
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/messaging.rb', line 242 def (msg) uuid = msg.id @mon.synchronize do @acks[uuid] = { waiting_cond: @mon.new_cond, waiting: true, timeout: SelfSDK::Time.now + @ack_timeout, } end send_raw(msg) SelfSDK.logger.info "waiting for acknowledgement #{uuid}" @mon.synchronize do @acks[uuid][:waiting_cond].wait_while do @acks[uuid][:waiting] end end SelfSDK.logger.info "acknowledged #{uuid}" true ensure @acks.delete(uuid) false end |
#session?(identifier, device) ⇒ Boolean
114 115 116 117 |
# File 'lib/messaging.rb', line 114 def session?(identifier, device) path = @encryption_client.session_path(identifier, device) File.file?(path) end |
#set_observer(original, options = {}, &block) ⇒ Object
301 302 303 304 |
# File 'lib/messaging.rb', line 301 def set_observer(original, = {}, &block) request_timeout = .fetch(:timeout, @timeout) @uuid_observer[original.id] = { original_message: original, block: block, timeout: SelfSDK::Time.now + request_timeout } end |
#share_information(recipient, recipient_device, request) ⇒ Object
Responds a request information request
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/messaging.rb', line 102 def share_information(recipient, recipient_device, request) m = SelfMsg::Message.new m.id = SecureRandom.uuid m.sender = "#{@jwt.id}:#{@device_id}" m.recipient = "#{recipient}:#{recipient_device}" m. = "identities.facts.query.resp" m.priority = select_priority(m.) m.ciphertext = @jwt.prepare(request) m end |
#stop ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/messaging.rb', line 83 def stop @acks.each do |k, _v| mark_as_acknowledged(k) end @messages.each do |k, _v| mark_as_acknowledged(k) mark_as_arrived(k) end end |
#subscribe(type, &block) ⇒ Object
306 307 308 309 |
# File 'lib/messaging.rb', line 306 def subscribe(type, &block) type = @source.(type) if type.is_a? Symbol @type_observer[type] = { block: block } end |
#wait_for(uuid, msg = nil) ⇒ Object
Executes the given block and waits for the message id specified on the uuid.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/messaging.rb', line 213 def wait_for(uuid, msg = nil) SelfSDK.logger.info "sending #{uuid}" @mon.synchronize do @messages[uuid] = { waiting_cond: @mon.new_cond, waiting: true, timeout: SelfSDK::Time.now + @timeout, original_message: msg, } end yield SelfSDK.logger.info "waiting for client to respond #{uuid}" @mon.synchronize do @messages[uuid][:waiting_cond].wait_while do @messages[uuid][:waiting] end end SelfSDK.logger.info "response received for #{uuid}" @messages[uuid][:response] ensure @messages.delete(uuid) end |