From 2052ad7cef9df43b2fd9dbcf782d0e6b92ee158e Mon Sep 17 00:00:00 2001 From: "G. Gibson" Date: Fri, 5 Sep 2025 17:42:50 -0700 Subject: [PATCH] federation fixes --- gxg/web/gxg.rb | 6 ++- gxg/web/gxg_comm.rb | 15 ++++-- gxg/web/gxg_events.rb | 105 ++++++++++++++++++++++++++++++++++++++---- gxg/web/gxg_xhr.rb | 3 -- 4 files changed, 113 insertions(+), 16 deletions(-) diff --git a/gxg/web/gxg.rb b/gxg/web/gxg.rb index a4be588..749e6a9 100644 --- a/gxg/web/gxg.rb +++ b/gxg/web/gxg.rb @@ -10,6 +10,10 @@ $object_space = {:registry => {}} class Object private # + def logged_in?() + GxG::DISPLAY_DETAILS[:logged_in] + end + # def bytes(*args) GxG::ByteArray::try_convert(args) end @@ -17,7 +21,7 @@ class Object def new_message(*args) unless @uuid @uuid = ::GxG::uuid_generate.to_s.to_sym - ::GxG::CHANNELS.create_channel(@uuid) + ::GxG::CHANNELS.create_channel(@uuid,self) end ::GxG::Events::Message.new({:sender => @uuid, :subject => args[1], :body => args[0]}) end diff --git a/gxg/web/gxg_comm.rb b/gxg/web/gxg_comm.rb index 9fd7d14..0cc8c30 100644 --- a/gxg/web/gxg_comm.rb +++ b/gxg/web/gxg_comm.rb @@ -145,9 +145,6 @@ module GxG end # XHR Connector: module Networking - class Channel - # - end # class Connector # @@ -973,6 +970,10 @@ module GxG if the_response.is_a?(::Hash) if the_response[:status] == "credentialed" GxG::DISPLAY_DETAILS[:logged_in] = true + GXG_FEDERATION[:connections].keys.each do |the_uuid| + socket.send({ :open_channel => the_uuid.to_s }.to_json.encode64, :text) + end + # end end the_handler.call(the_response) @@ -999,6 +1000,9 @@ module GxG if response.is_a?(::Hash) # FIXME: possible conflict with proper credential setting. if response[:status] == "uncredentialed" + GXG_FEDERATION[:connections].keys.each do |the_uuid| + socket.send({ :close_channel => the_uuid.to_s }.to_json.encode64, :text) + end GxG::DISPLAY_DETAILS[:logged_in] = false if the_handler.respond_to?(:call) the_handler.call(response) @@ -1087,7 +1091,6 @@ module GxG @csrf = nil @the_connector = self # Make introduction to host and aquire csrf token - ::GxG::CHANNELS.create_channel(@uuid) the_introduction = new_message({:introduction => @uuid.to_s}) the_introduction[:sender] = @uuid.to_s the_introduction[:to] = @host_prefix.to_s @@ -1150,6 +1153,10 @@ module GxG the_message[:sender] = @uuid the_message[:to] = @remote_uuid # Review : add fail/succeed code blocks?? + # if succeed do this: ::GxG::CHANNELS.create_channel(@uuid) + the_message.on(:success) do |response| + ::GxG::CHANNELS.create_channel(@uuid) + end GxG::CONNECTION.push(the_message) end end diff --git a/gxg/web/gxg_events.rb b/gxg/web/gxg_events.rb index 8cc0c0b..bf61c35 100644 --- a/gxg/web/gxg_events.rb +++ b/gxg/web/gxg_events.rb @@ -3,6 +3,77 @@ module GxG GXG_FEDERATION = {:title => "Untitled", :uuid => nil, :available => {}, :connections => {}} # ::GxG::SOCKET_MONITOR module Messages + class Channel + # + def initialize(the_uuid=nil, the_object=nil) + @uuid = the_uuid + @inbox = [] + @outbox = [] + @socket = nil + @remote = nil + @channel_secret = nil + @object = the_object + self + end + # + def uuid() + @uuid + end + # + def object() + @object + end + # + def socket() + @socket + end + # + def socket=(the_socket=nil) + @socket = the_socket + end + # + def remote() + @remote + end + # + def remote=(the_remote=nil) + @remote = the_remote + end + # + def secret() + @channel_secret + end + # + def secret=(the_secret=nil) + @channel_secret = the_secret + end + # + def inbox_size() + @inbox.size + end + # + def next_message() + @inbox.unshift + end + # + def outbox_size() + @outbox.size + end + # + def send_message(the_message) + @outbox << the_message + end + # + def read() + @outbox.unshift + end + # + def write(the_message) + @inbox << the_message + end + # + end + # class ChannelManager # def update_channels @@ -33,11 +104,17 @@ module GxG channel = self.fetch_channel(destination.to_s.to_sym) if channel channel.write(the_message) + result = true else # Send format: channel.socket.send({ :payload => the_message.export.to_s.encrypt(channel.secret).encode64 }.to_json.encode64, :text) - socket.send({ :payload => the_message.export.to_s.encrypt(connector.secret).encode64 }.to_json.encode64, :text) + if GxG::DISPLAY_DETAILS[:logged_in] + socket.send({ :payload => the_message.export.to_s.encrypt(connector.secret).encode64 }.to_json.encode64, :text) + result = true + else + log_warn("Message dropped --> login and resend") + result = false + end end - result = true end # email address -- TODO end @@ -48,19 +125,26 @@ module GxG GXG_FEDERATION[:connections][(the_uuid)] end # - def create_channel(the_uuid) - GXG_FEDERATION[:connections][(the_uuid)] = ::GxG::Messages::Channel.new(the_uuid) + def create_channel(the_uuid=nil,the_object=nil) + GXG_FEDERATION[:connections][(the_uuid)] = ::GxG::Messages::Channel.new(the_uuid,the_object) + if GxG::DISPLAY_DETAILS[:logged_in] + socket.send({ :open_channel => the_uuid }.to_json.encode64, :text) + end end # def destroy_channel(the_uuid) channel = self.fetch_channel(the_uuid) if channel + channel.outbox_size.times do |indexer| the_message = channel.read() if the_message self.dispatch_message(the_message) end end + if GxG::DISPLAY_DETAILS[:logged_in] + socket.send({ :close_channel => the_uuid }.to_json.encode64, :text) + end end GXG_FEDERATION[:connections].delete(the_uuid) end @@ -694,7 +778,7 @@ class Object def send_message(the_message) unless @uuid @uuid = ::GxG::uuid_generate.to_s.to_sym - ::GxG::CHANNELS.create_channel(@uuid) + ::GxG::CHANNELS.create_channel(@uuid,self) end ::GxG::CHANNELS.send_message(@uuid, the_message) true @@ -703,7 +787,7 @@ class Object def next_message() unless @uuid @uuid = ::GxG::uuid_generate.to_s.to_sym - ::GxG::CHANNELS.create_channel(@uuid) + ::GxG::CHANNELS.create_channel(@uuid,self) end ::GxG::CHANNELS.next_message(@uuid) end @@ -711,11 +795,16 @@ class Object def post(the_message) unless @uuid @uuid = ::GxG::uuid_generate.to_s.to_sym - ::GxG::CHANNELS.create_channel(@uuid) + ::GxG::CHANNELS.create_channel(@uuid,self) end channel = ::GxG::CHANNELS.fetch_channel(@uuid) if channel channel.write(the_message) end true - end \ No newline at end of file + end + # + def process_message(the_message=nil, options = {}) + # override to use + end +end \ No newline at end of file diff --git a/gxg/web/gxg_xhr.rb b/gxg/web/gxg_xhr.rb index dab594f..5f16c8f 100644 --- a/gxg/web/gxg_xhr.rb +++ b/gxg/web/gxg_xhr.rb @@ -3,9 +3,6 @@ # module GxG module Networking - class Channel - # - end # class Connector #