Class: Iarm::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/iarm/handle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Handle

Returns a new instance of Handle.



8
9
10
11
12
13
14
15
16
17
# File 'lib/iarm/handle.rb', line 8

def initialize(name)
  @msg_mutex = Mutex.new
  @channels = {}
  @name = name
  @timer = Iarm::Timer.new
  @created_at = Time.now.to_i
  @ttl = 90
  @msgs = []
  self.touch
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



6
7
8
# File 'lib/iarm/handle.rb', line 6

def channels
  @channels
end

#created_atObject (readonly)

Returns the value of attribute created_at.



5
6
7
# File 'lib/iarm/handle.rb', line 5

def created_at
  @created_at
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/iarm/handle.rb', line 5

def name
  @name
end

#timerObject (readonly)

Returns the value of attribute timer.



5
6
7
# File 'lib/iarm/handle.rb', line 5

def timer
  @timer
end

#touched_atObject (readonly)

Returns the value of attribute touched_at.



5
6
7
# File 'lib/iarm/handle.rb', line 5

def touched_at
  @touched_at
end

#ttlObject

Returns the value of attribute ttl.



6
7
8
# File 'lib/iarm/handle.rb', line 6

def ttl
  @ttl
end

Instance Method Details

#depart(channel) ⇒ Object

returns a list of channels that we departed



49
50
51
52
53
54
55
56
57
58
# File 'lib/iarm/handle.rb', line 49

def depart(channel) # returns a list of channels that we departed
  @channels.keys.select do |ch|
    if channel.nil? || ch == channel
      ch.members.delete(self)
      @channels.delete(ch)
      ch.post(self, Msg::Part.new(ch.name, self.name))
      true
    end
  end
end

#join(channel) ⇒ Object



60
61
62
63
64
# File 'lib/iarm/handle.rb', line 60

def join(channel)
  @channels[channel] = channel.members[self] = Time.now.to_i
  send_msg(channel.topic) if channel.topic
  channel.post(self, Msg::Join.new(channel.name, self.name, @channels[channel]))
end

#next_msgObject



23
24
25
# File 'lib/iarm/handle.rb', line 23

def next_msg
  @msg_mutex.synchronize { @msgs.shift }
end

#no_msgs?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/iarm/handle.rb', line 31

def no_msgs?
  @msgs.empty?
end

#pokeObject



74
75
76
# File 'lib/iarm/handle.rb', line 74

def poke
  @timer.poke
end

#push_msg(msg) ⇒ Object



27
28
29
# File 'lib/iarm/handle.rb', line 27

def push_msg(msg)
  @msg_mutex.synchronize { @msgs << msg }
end

#say(channel, msg) ⇒ Object



78
79
80
# File 'lib/iarm/handle.rb', line 78

def say(channel, msg)
  channel.members.each_key {|member| member.send_msg(msg) }
end

#send_msg(msg) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/iarm/handle.rb', line 66

def send_msg(msg)
  if(msg.kind_of?(Msg::Topic) || @name != msg.from)
    #puts "MSG[#{msg.class}] <##{msg.channel}:#{msg.from}> #{@name} : #{msg.data}"
    self.push_msg(msg)
    self.poke
  end
end

#timed_out?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/iarm/handle.rb', line 19

def timed_out?
  (@touched_at + @ttl) < Time.now.to_i
end

#timeoutObject

returns list of channels that this handle was a member of



39
40
41
42
43
44
45
46
# File 'lib/iarm/handle.rb', line 39

def timeout # returns list of channels that this handle was a member of
  @channels.keys.select do |channel|
    channel.members.delete(self) 
    channel.post(self, Msg::Timeout.new(channel.name, self.name))
    @channels.delete(channel)
    true
  end
end

#touchObject



35
36
37
# File 'lib/iarm/handle.rb', line 35

def touch
  @touched_at = Time.now.to_i
end