module DirectchatMesh
Constants
- BASE_MESH_PORT
- PORT_SPAN
Public Instance Methods
Source
# File lib/directchat/mesh.rb, line 81 def apply_remote_session(network:, session:, store: RabbotDb) payload = DirectchatSession.symbolize_session( DirectchatSession.stringify_session(session) ) return nil unless payload DirectchatSession.save(network: network, session: payload, store: store) payload end
Source
# File lib/directchat/mesh.rb, line 91 def handle_peer_relay(session_id:, from:, text:, network:, bot_nick:, store: RabbotDb) session = DirectchatSession.load(network: network, session_id: session_id, store: store) return { ignored: true } unless session return { ignored: true } unless session[:status].to_s == "active" assignment = DirectchatSession.assignment_for_bot(session: session, bot_nick: bot_nick) return { ignored: true } unless assignment line = DirectchatSession.format_relay_line(from: from, text: text) { local_deliveries: [ { participant: assignment[:participant], line: line } ] } end
Source
# File lib/directchat/mesh.rb, line 107 def handle_wire_message(line, network:, bot_nick:, store: RabbotDb) frame = DirectchatWire.decode(line) return { ignored: true } unless frame case frame[:type] when :session apply_remote_session(network: network, session: frame[:session], store: store) { session_synced: true, session: frame[:session] } when :relay handle_peer_relay( session_id: frame[:session_id], from: frame[:from], text: frame[:text], network: network, bot_nick: bot_nick, store: store ) when :stop DirectchatSession.close(network: network, session_id: frame[:session_id], store: store) { session_stopped: true, session_id: frame[:session_id] } when :hello { hello: frame[:bot] } else { ignored: true } end end
Source
# File lib/directchat/mesh.rb, line 20 def mesh_host ENV.fetch("RABBOT_MESH_HOST", "127.0.0.1") end
Source
# File lib/directchat/mesh.rb, line 24 def mesh_port(bot_nick) key = Normalize.user_key(bot_nick) offset = Zlib.crc32(key) % PORT_SPAN BASE_MESH_PORT + offset end
Source
# File lib/directchat/mesh.rb, line 39 def mesh_targets(session:, exclude_bot:, host: mesh_host) peer_bots(session: session, bot_nick: exclude_bot).map do |bot| { bot: bot, host: host, port: mesh_port(bot) } end end
Source
# File lib/directchat/mesh.rb, line 30 def peer_bots(session:, bot_nick:) origin_key = Normalize.user_key(bot_nick) session[:assignments] .map { |entry| entry[:bot] } .reject { |nick| Normalize.user_key(nick) == origin_key } .uniq .sort_by { |nick| Normalize.user_key(nick) } end
Source
# File lib/directchat/mesh.rb, line 134 def relay_frames(peer_relays:) peer_relays.map do |relay| DirectchatWire.encode_relay( session_id: relay[:session_id], from: relay[:from], text: relay[:text] ) end end
Source
# File lib/directchat/mesh.rb, line 49 def split_relay(session:, from_participant:, text:, origin_bot:) targets = DirectchatSession.relay_targets( session: session, from_participant: from_participant, text: text ) return { ignored: true } if targets.nil? origin_key = Normalize.user_key(origin_bot) local_deliveries = [] peer_relays = [] targets.each do |target| if Normalize.user_key(target[:bot]) == origin_key local_deliveries << { participant: target[:participant], line: DirectchatSession.format_relay_line(from: target[:from], text: target[:text]) } else peer_relays << { bot: target[:bot], participant: target[:participant], session_id: session[:id], from: target[:from], text: target[:text] } end end { local_deliveries: local_deliveries, peer_relays: peer_relays } end
Source
# File lib/directchat/mesh.rb, line 144 def stop_frames(session_id:) [DirectchatWire.encode_stop(session_id: session_id)] end