Class: Lich::Common::SynchronizedSocket

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

Overview

A thread-safe wrapper around a socket that synchronizes access to the underlying socket object using a mutex.

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ SynchronizedSocket

Initializes a new instance of SynchronizedSocket.

Examples:

socket = SynchronizedSocket.new(TCPSocket.new('localhost', 8080))

Parameters:

  • o (Object)

    the socket object to be synchronized.

Raises:

  • (ArgumentError)

    if the provided object is not a valid socket.



16
17
18
19
20
# File 'lib/common/class_exts/synchronizedsocket.rb', line 16

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) {|Block| ... } ⇒ Object

Delegates method calls to the underlying socket object.

Examples:

synchronized_socket.some_method(arg1, arg2)

Parameters:

  • method (Symbol)

    the method name to be called on the delegate.

  • args (Array)

    the arguments to be passed to the method.

Yields:

  • (Block)

    an optional block to be executed.

Returns:

  • (Object)

    the result of the method call on the delegate.



74
75
76
# File 'lib/common/class_exts/synchronizedsocket.rb', line 74

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

Instance Method Details

#puts(*args) {|Block| ... } ⇒ nil

Writes a line to the socket, ensuring thread safety.

Examples:

synchronized_socket.puts("Hello, World!")

Parameters:

  • args (Array)

    the arguments to be written to the socket.

Yields:

  • (Block)

    an optional block to be executed.

Returns:

  • (nil)

    returns nil after writing to the socket.



29
30
31
32
33
# File 'lib/common/class_exts/synchronizedsocket.rb', line 29

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

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

Conditionally writes to the socket based on the result of a block.

Examples:

synchronized_socket.puts_if("Hello, World!") { true }

Parameters:

  • args (Array)

    the arguments to be written to the socket if the block returns true.

Yields:

  • (Block)

    a block that determines whether to write to the socket.

Returns:

  • (Boolean)

    returns true if the block returns true and the write occurs, false otherwise.



42
43
44
45
46
47
48
49
50
51
# File 'lib/common/class_exts/synchronizedsocket.rb', line 42

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

#write(*args) {|Block| ... } ⇒ nil

Writes data to the socket, ensuring thread safety.

Examples:

synchronized_socket.write("Data to send")

Parameters:

  • args (Array)

    the arguments to be written to the socket.

Yields:

  • (Block)

    an optional block to be executed.

Returns:

  • (nil)

    returns nil after writing to the socket.



60
61
62
63
64
# File 'lib/common/class_exts/synchronizedsocket.rb', line 60

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