Class: Agent::Pop

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/pop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pop

Returns a new instance of Pop.



7
8
9
10
11
12
13
14
15
16
# File 'lib/agent/pop.rb', line 7

def initialize(options={})
  @object        = nil
  @uuid          = options[:uuid] || UUID.generate
  @blocking_once = options[:blocking_once]
  @notifier      = options[:notifier]
  @mutex         = Mutex.new
  @cvar          = ConditionVariable.new
  @received      = false
  @closed        = false
end

Instance Attribute Details

#blocking_onceObject (readonly)

Returns the value of attribute blocking_once.



5
6
7
# File 'lib/agent/pop.rb', line 5

def blocking_once
  @blocking_once
end

#notifierObject (readonly)

Returns the value of attribute notifier.



5
6
7
# File 'lib/agent/pop.rb', line 5

def notifier
  @notifier
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/agent/pop.rb', line 5

def object
  @object
end

#uuidObject (readonly)

Returns the value of attribute uuid.



5
6
7
# File 'lib/agent/pop.rb', line 5

def uuid
  @uuid
end

Instance Method Details

#closeObject



59
60
61
62
63
64
65
66
# File 'lib/agent/pop.rb', line 59

def close
  @mutex.synchronize do
    return if @received
    @closed = true
    @cvar.broadcast
    @notifier.notify(self) if @notifier
  end
end

#closed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/agent/pop.rb', line 22

def closed?
  @closed
end

#received?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/agent/pop.rb', line 18

def received?
  @received
end

#sendObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/agent/pop.rb', line 36

def send
  @mutex.synchronize do
    if @blocking_once
      _, error = @blocking_once.perform do
        @object = yield unless @closed
        @received = true
        @cvar.signal
        @notifier.notify(self) if @notifier
      end

      return error
    else
      begin
        @object = yield unless @closed
        @received = true
        @cvar.signal
        @notifier.notify(self) if @notifier
      rescue Errors::Rollback
      end
    end
  end
end

#waitObject



26
27
28
29
30
31
32
33
34
# File 'lib/agent/pop.rb', line 26

def wait
  @mutex.synchronize do
    until @received || @closed
      @cvar.wait(@mutex)
    end
    return false if @closed
    received?
  end
end