Class: Lich::Common::SynchronizedSocket

Inherits:
Object
  • Object
show all
Defined in:
documented/common/class_exts/synchronizedsocket.rb

Overview

A socket wrapper that synchronizes access to a delegate socket.

This class ensures that all operations on the delegate socket are thread-safe.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ void

Initializes a new SynchronizedSocket instance.

Parameters:

  • o (Object)

    the delegate socket to synchronize access to



14
15
16
17
18
# File 'documented/common/class_exts/synchronizedsocket.rb', line 14

def initialize(o)
  @delegate = o
  @mutex = Mutex.new
  # self # removed by robocop, needs broad testing
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) {|Proc| ... } ⇒ Object

Handles calls to methods that are not defined in this class.

This method delegates the call to the underlying socket.

Parameters:

  • method (Symbol)

    the name of the method being called

  • args (Array)

    the arguments to be passed to the method

Yields:

  • (Proc)

    an optional block to be executed

Returns:

  • (Object)

    the result of the delegated method call



68
69
70
# File 'documented/common/class_exts/synchronizedsocket.rb', line 68

def method_missing(method, *args, &block)
  @delegate.__send__ method, *args, &block
end

Instance Method Details

#puts(*args) {|Proc| ... } ⇒ void

This method returns an undefined value.

Writes a line to the delegate socket.

This method is thread-safe and will synchronize access to the delegate.

Parameters:

  • args (Array)

    the arguments to be passed to the delegate's puts method

Yields:

  • (Proc)

    an optional block to be executed



26
27
28
29
30
# File 'documented/common/class_exts/synchronizedsocket.rb', line 26

def puts(*args, &block)
  @mutex.synchronize {
    @delegate.puts(*args, &block)
  }
end

#puts_if(*args) {|Proc| ... } ⇒ Boolean

Conditionally writes a line to the delegate socket based on the block's return value.

This method is thread-safe and will synchronize access to the delegate.

Parameters:

  • args (Array)

    the arguments to be passed to the delegate's puts method

Yields:

  • (Proc)

    a block that determines whether to write to the socket

Returns:

  • (Boolean)

    true if the line was written, false otherwise



38
39
40
41
42
43
44
45
46
47
# File 'documented/common/class_exts/synchronizedsocket.rb', line 38

def puts_if(*args)
  @mutex.synchronize {
    if yield
      @delegate.puts(*args)
      return true
    else
      return false
    end
  }
end

#write(*args) {|Proc| ... } ⇒ void

This method returns an undefined value.

Writes data to the delegate socket.

This method is thread-safe and will synchronize access to the delegate.

Parameters:

  • args (Array)

    the arguments to be passed to the delegate's write method

Yields:

  • (Proc)

    an optional block to be executed



55
56
57
58
59
# File 'documented/common/class_exts/synchronizedsocket.rb', line 55

def write(*args, &block)
  @mutex.synchronize {
    @delegate.write(*args, &block)
  }
end