mirror of
https://github.com/mistergibson/gxg-web-client.git
synced 2026-08-15 03:26:00 -07:00
Federation Scaffolding
This commit is contained in:
parent
e473b70c75
commit
c1b8397275
3 changed files with 164 additions and 3 deletions
|
|
@ -57,10 +57,11 @@ module GxG
|
|||
# Expects: String or Hash
|
||||
if the_data.is_any?(::Hash, ::GxG::Database::DetachedHash)
|
||||
if the_data.is_a?(::Hash)
|
||||
the_data = JSON.generate(the_data).encode64
|
||||
# ??? Where to encrypt : encrypt(connector().secret)
|
||||
the_data = JSON.generate(the_data).to_s.encode64
|
||||
else
|
||||
# Discouraged: no context of an operation this way. Not good. (deprecate?)
|
||||
the_data = JSON.generate(the_data.export).encode64
|
||||
the_data = JSON.generate(the_data.export).to_s.encode64
|
||||
end
|
||||
end
|
||||
thesocket = @socket
|
||||
|
|
@ -99,6 +100,7 @@ module GxG
|
|||
the_url = "ws://#{the_location}"
|
||||
end
|
||||
begin
|
||||
# Unencrypted Preamble
|
||||
greeting = {:attach_display => true}.to_json.encode64
|
||||
the_socket = `new WebSocket(the_url)`
|
||||
%x{
|
||||
|
|
@ -1033,6 +1035,10 @@ module GxG
|
|||
@display_path
|
||||
end
|
||||
#
|
||||
def secret()
|
||||
@csrf
|
||||
end
|
||||
#
|
||||
def initialize()
|
||||
if (`window.location['href']`).include?("https://")
|
||||
@host_prefix = ("https://" + `window.location['host']`)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,98 @@
|
|||
# Events Section:
|
||||
module GxG
|
||||
GXG_FEDERATION = {:title => "Untitled", :uuid => nil, :available => {}, :connections => {}}
|
||||
# ::GxG::SOCKET_MONITOR
|
||||
module Messages
|
||||
class ChannelManager
|
||||
#
|
||||
def update_channels
|
||||
result = false
|
||||
channels = []
|
||||
GXG_FEDERATION[:connections].values.each do |the_channel|
|
||||
channels << the_channel
|
||||
end
|
||||
channels.each do |the_channel|
|
||||
the_message = the_channel.read()
|
||||
while the_message do
|
||||
self.dispatch_message(the_message)
|
||||
the_message = the_channel.read()
|
||||
end
|
||||
end
|
||||
if channels.size > 0
|
||||
result = true
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
def dispatch_message(the_message=nil)
|
||||
result = false
|
||||
if the_message.is_a?(::GxG::Events::Message)
|
||||
destination = the_message[:to]
|
||||
# uuid
|
||||
if ::GxG::valud_uuid?(destination.to_s)
|
||||
channel = self.fetch_channel(destination.to_s.to_sym)
|
||||
if channel
|
||||
channel.write(the_message)
|
||||
result = true
|
||||
end
|
||||
end
|
||||
# email address -- TODO
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
def fetch_channel(the_uuid)
|
||||
GXG_FEDERATION[:connections][(the_uuid)]
|
||||
end
|
||||
#
|
||||
def create_channel(the_uuid)
|
||||
GXG_FEDERATION[:connections][(the_uuid)] = ::GxG::Messages::Channel.new(the_uuid)
|
||||
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
|
||||
end
|
||||
GXG_FEDERATION[:connections].delete(the_uuid)
|
||||
end
|
||||
#
|
||||
def next_message(the_uuid)
|
||||
result = nil
|
||||
channel = self.fetch_channel(the_uuid)
|
||||
if channel
|
||||
result = channel.next_message()
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
def send_message(the_uuid, the_message)
|
||||
channel = self.fetch_channel(the_uuid)
|
||||
if channel
|
||||
channel.send_message(the_message)
|
||||
end
|
||||
end
|
||||
#
|
||||
def initialize
|
||||
self
|
||||
end
|
||||
#
|
||||
end
|
||||
end
|
||||
# ###
|
||||
module Events
|
||||
#
|
||||
class Message
|
||||
def self.import(the_data=nil)
|
||||
the_message = new_message {}
|
||||
the_message.import(the_data)
|
||||
the_message
|
||||
end
|
||||
#
|
||||
def initialize(*args)
|
||||
# MUST provide a hash with at least :sender message field set
|
||||
|
|
@ -93,6 +183,33 @@ module GxG
|
|||
def set_at_path(*args)
|
||||
@data.set_at_path(*args)
|
||||
end
|
||||
#
|
||||
def import(the_data=nil)
|
||||
# Requires gxg_export+JSON String or gxg_export Hash
|
||||
unless the_data.is_any?(::String, ::Hash)
|
||||
raise Exception.new("You MUST supply a JSON string or a Hash, you supplied: #{the_data.class.inspect}")
|
||||
end
|
||||
result = false
|
||||
if the_data.is_a?(::String)
|
||||
the_data = JSON.parse(the_data, {:symbolize_names => true})
|
||||
end
|
||||
if the_data.is(::Hash)
|
||||
@data.merge(::Hash::gxg_import(the_data))
|
||||
result = true
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
def export()
|
||||
@data.gxg_export.to_json.to_s
|
||||
end
|
||||
def to_s()
|
||||
self.export.to_s
|
||||
end
|
||||
def to_json
|
||||
self.to_s
|
||||
end
|
||||
#
|
||||
end
|
||||
#
|
||||
class LoggerDB
|
||||
|
|
@ -564,4 +681,38 @@ module GxG
|
|||
DISPATCHER.shutdown
|
||||
end
|
||||
end
|
||||
#
|
||||
#
|
||||
|
||||
module GxG
|
||||
CHANNELS = ::GxG::Messages::ChannelManager.new
|
||||
end
|
||||
class Object
|
||||
#
|
||||
def send_message(the_message)
|
||||
unless @uuid
|
||||
@uuid = ::GxG::uuid_generate.to_s.to_sym
|
||||
::GxG::CHANNELS.create_channel(@uuid)
|
||||
end
|
||||
::GxG::CHANNELS.send_message(@uuid, the_message)
|
||||
true
|
||||
end
|
||||
#
|
||||
def next_message()
|
||||
unless @uuid
|
||||
@uuid = ::GxG::uuid_generate.to_s.to_sym
|
||||
::GxG::CHANNELS.create_channel(@uuid)
|
||||
end
|
||||
::GxG::CHANNELS.next_message(@uuid)
|
||||
end
|
||||
#
|
||||
def post(the_message)
|
||||
unless @uuid
|
||||
@uuid = ::GxG::uuid_generate.to_s.to_sym
|
||||
::GxG::CHANNELS.create_channel(@uuid)
|
||||
end
|
||||
channel = ::GxG::CHANNELS.fetch_channel(@uuid)
|
||||
if channel
|
||||
channel.write(the_message)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
|
@ -89,6 +89,10 @@ class Object
|
|||
::GxG::CONNECTION
|
||||
end
|
||||
#
|
||||
def socket()
|
||||
::GxG::DISPLAY_DETAILS[:socket]
|
||||
end
|
||||
#
|
||||
def sockets()
|
||||
::GxG::SOCKET_MONITOR
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue