Class: Meshchat::Network::Remote::Relay

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/network/remote/relay.rb

Constant Summary collapse

CHANNEL =
'MeshRelayChannel'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, message_dispatcher, connected: nil) ⇒ Relay



16
17
18
19
20
21
22
23
24
# File 'lib/meshchat/network/remote/relay.rb', line 16

def initialize(url, message_dispatcher, connected: nil)
  @_url = url
  @_request_processor = Incoming::RequestProcessor.new(
    network: NETWORK_RELAY,
    location: url,
    message_dispatcher: message_dispatcher)

  setup(connected: connected)
end

Instance Attribute Details

#_clientObject (readonly)

Returns the value of attribute _client.



12
13
14
# File 'lib/meshchat/network/remote/relay.rb', line 12

def _client
  @_client
end

#_connectedObject

Returns the value of attribute _connected.



13
14
15
# File 'lib/meshchat/network/remote/relay.rb', line 13

def _connected
  @_connected
end

#_request_processorObject (readonly)

Returns the value of attribute _request_processor.



12
13
14
# File 'lib/meshchat/network/remote/relay.rb', line 12

def _request_processor
  @_request_processor
end

#_urlObject (readonly)

Returns the value of attribute _url.



12
13
14
# File 'lib/meshchat/network/remote/relay.rb', line 12

def _url
  @_url
end

Instance Method Details

#chat_message_received(message) ⇒ Object



88
89
90
91
92
93
# File 'lib/meshchat/network/remote/relay.rb', line 88

def chat_message_received(message)
  _request_processor.process(message)
rescue => e
  ap e.message
  puts e.backtrace
end

#connected?Boolean



56
57
58
# File 'lib/meshchat/network/remote/relay.rb', line 56

def connected?
  _connected
end

#error_message_received(message) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/meshchat/network/remote/relay.rb', line 95

def error_message_received(message)
  Display.info message['error']
  if message['status'] == 404
    uid = message['uid']
    mark_as_offline(uid)
  end
end

#mark_as_offline(uid) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/meshchat/network/remote/relay.rb', line 103

def mark_as_offline(uid)
  node = Node.find_by_uid(uid)
  if node
    Display.info "#{node.alias_name} has ventured offline"
    node.update(on_relay: false)
  else
    Display.info 'someone directly sent you a fake offline message'
  end
end

#process_error(message) ⇒ Object

TODO: what does an error message look like? TODO: what are situations in which we receive an error message?



84
85
86
# File 'lib/meshchat/network/remote/relay.rb', line 84

def process_error(message)
  Display.alert(message)
end

#process_message(message) ⇒ Object

example messages: “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“message”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>“error”=>“hi”} “identifier”=>“{"channel":"MeshRelayChannel"”, “type”=>“confirm_subscription”} “identifier”=>“{"channel":"MeshRelayChannel"”, “message”=>with UID user2 could not be found”}



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/meshchat/network/remote/relay.rb', line 65

def process_message(message)
  Debug.received_message_from_relay(message, _url)

  _, type, message = message.values_at('identifier', 'type', 'message')

  # do we want to do anything here?
  return if type == 'confirm_subscription'
  # are there any other types of websocket messages?
  return unless message

  if message['message']
    chat_message_received(message)
  elsif message['error']
    error_message_received(message)
  end
end

#setup(connected: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meshchat/network/remote/relay.rb', line 26

def setup(connected: nil)
  path = "#{_url}?uid=#{APP_CONFIG.user['uid']}"
  @_client = ActionCableClient.new(path, CHANNEL)

  # don't output anything upon connecting
  _client.connected { self._connected = true }

  # If there are errors, report them!
  _client.errored do |message|
    process_error(message)
  end

  _client.subscribed do
    Debug.subscribed_to_relay
    connected.call if connected
  end

  # forward the encrypted messages to our RequestProcessor
  # so that they can be decrypted
  _client.received do |message|
    process_message(message)
  end

  _client.disconnected do
    self._connected = false
  end

  _client
end