Skip to content

Commit

Permalink
Add some more broadcast stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed May 3, 2012
1 parent 4b59474 commit 4d56044
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/torchat/session.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
require 'torchat/session/buddies' require 'torchat/session/buddies'
require 'torchat/session/file_transfers' require 'torchat/session/file_transfers'
require 'torchat/session/group_chats' require 'torchat/session/group_chats'
require 'torchat/session/broadcast' require 'torchat/session/broadcasts'


class Torchat class Torchat


class Session class Session
attr_reader :config, :id, :name, :description, :status, :buddies, :file_transfers, :group_chats attr_reader :config, :id, :name, :description, :status, :buddies, :file_transfers, :group_chats, :broadcasts
attr_writer :client, :version attr_writer :client, :version
attr_accessor :connection_timeout attr_accessor :connection_timeout


Expand All @@ -44,6 +44,7 @@ def initialize (config)
@buddies = Buddies.new(self) @buddies = Buddies.new(self)
@file_transfers = FileTransfers.new(self) @file_transfers = FileTransfers.new(self)
@group_chats = GroupChats.new(self) @group_chats = GroupChats.new(self)
@broadcasts = Broadcasts.new(self)


@callbacks = Hash.new { |h, k| h[k] = [] } @callbacks = Hash.new { |h, k| h[k] = [] }
@before = Hash.new { |h, k| h[k] = [] } @before = Hash.new { |h, k| h[k] = [] }
Expand Down Expand Up @@ -402,11 +403,11 @@ def initialize (config)


# broadcast support # broadcast support
on_packet :broadcast, :message do |e| on_packet :broadcast, :message do |e|
buddies.each_online {|buddy| broadcasts.received e.to_str
buddy.send_packet [:broadcast, :message], e.to_str end
}


fire :broadcast, message: Broadcast::Message.parse(e.to_str) set_interval 360 do
broadcasts.flush! 360
end end


yield self if block_given? yield self if block_given?
Expand Down
34 changes: 30 additions & 4 deletions lib/torchat/session/broadcast/message.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,45 @@
#--
# Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
#
# This file is part of torchat for ruby.
#
# torchat for ruby is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# torchat for ruby is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with torchat for ruby. If not, see <http://www.gnu.org/licenses/>.
#++

class Torchat; class Session; module Broadcast class Torchat; class Session; module Broadcast


class Message class Message
def self.parse (text) def self.parse (text)
new text, text.scan(/#([^ ]+)/) new text, text.scan(/#([^ ]+)/)
end end


attr_reader :message, :tags attr_reader :at, :tags


def initialize (message, *tags) def initialize (message, *tags)
@message = message @at = Time.new
@tags = tags.flatten.compact.uniq.map(&:to_sym) @internal = message
@tags = tags.flatten.compact.uniq.map(&:to_sym)
end

def to_s
@internal
end end


alias to_str to_s

def inspect def inspect
"#<Torchat::Broadcast::Message(#{tags.join ' '}): #{message}>" "#<Torchat::Broadcast::Message(#{tags.join ' '}): #{to_s}>"
end end
end end


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,3 +18,39 @@
#++ #++


require 'torchat/session/broadcast/message' require 'torchat/session/broadcast/message'

class Torchat; class Session

class Broadcasts < Array
attr_reader :session

def initialize (session)
@session = session
end

def received? (message)
any? { |m| m.to_s == message }
end

def received (message)
return if received? message

message = Broadcast::Message.parse(message)

push message

session.buddies.each_online {|buddy|
buddy.send_packet [:broadcast, :message], message.to_s
}

session.fire :broadcast, message: message
end

def flush! (time)
delete_if {|message|
(Time.now.to_i - message.at.to_i) > time
}
end
end

end; end

0 comments on commit 4d56044

Please sign in to comment.