Class: Textbringer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/commands/server.rb,
sig/lib/textbringer/commands/server.rbs

Defined Under Namespace

Classes: ExistError, FrontObject

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Server

Returns a new instance of Server.

Parameters:

  • (String)


34
35
36
# File 'lib/textbringer/commands/server.rb', line 34

def initialize(uri)
  @uri = uri
end

Instance Method Details

#startDRb::DRbServer #startnil

Overloads:

  • #startDRb::DRbServer

    Returns:

    • (DRb::DRbServer)
  • #startnil

    Returns:

    • (nil)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/textbringer/commands/server.rb', line 38

def start
  front = FrontObject.new
  default_options = unix_domain? ? { UNIXFileMode: 0600 } : {}
  options = default_options.merge(CONFIG[:server_options] || {})
  begin
    DRb.start_service(@uri, front, options)
  rescue Errno::EADDRINUSE
    if unix_domain? && !unix_domain_server_alive?
      # Remove the socket file in case another server died unexpectedly before.
      File.unlink(unix_domain_socket_path)
      DRb.start_service(@uri, front, options)
    else
      raise ExistError, "There is an existing Textbringer server"
    end
  end
end

#unix_domain?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/textbringer/commands/server.rb', line 57

def unix_domain?
  @uri.start_with?("drbunix:")
end

#unix_domain_server_alive?Integer #unix_domain_server_alive?Boolean

Overloads:

  • #unix_domain_server_alive?Integer

    Returns:

    • (Integer)
  • #unix_domain_server_alive?Boolean

    Returns:

    • (Boolean)

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/textbringer/commands/server.rb', line 65

def unix_domain_server_alive?
  socket = Socket.new(:UNIX, :STREAM)
  sockaddr = Socket.sockaddr_un(unix_domain_socket_path)
  begin
    socket.connect_nonblock(sockaddr)
  rescue Errno::EINPROGRESS
    return true
  rescue Errno::ECONNREFUSED
    return false
  ensure
    socket.close
  end
end

#unix_domain_socket_pathString

Returns:

  • (String)


61
62
63
# File 'lib/textbringer/commands/server.rb', line 61

def unix_domain_socket_path
  @uri.sub(/\Adrbunix:/, "")
end