Class: Nova::Starbound::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/nova/starbound/server.rb

Overview

TODO:

Default proc.

A Starbound server.

Defined Under Namespace

Classes: FakeServer

Constant Summary collapse

DEFAULT_OPTIONS =

The default options.

{
  :type  => :tcp,
  :host  => "127.0.0.1",
  :port  => 2010,
  :path  => "/tmp/sock"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Initialize the server with the given options. If the options has a :protocol key, it is removed from the options and set as the protocol options.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :type (Symbol)

    the type of server. Can be any of :tcp or :unix. Defaults to :tcp.

  • :host (String)

    the host to bind to. Defaults to “127.0.0.1”. Only used if :type is :tcp.

  • :port (Numeric)

    the port to bind to. Defaults to 2010. Only used if :type is :tcp.

  • :path (String)

    the path to the unix socket. Only used if :type is :unix.



48
49
50
51
52
53
# File 'lib/nova/starbound/server.rb', line 48

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge options
  @protocol_options = (options.delete(:protocol) || {}).dup
  @run = true
  @thread_list = []
end

Instance Attribute Details

#optionsHash (readonly)

The options passed to the server on initialization.

Returns:

  • (Hash)


14
15
16
# File 'lib/nova/starbound/server.rb', line 14

def options
  @options
end

#runBoolean

Whether or not to run. Once this is set to false, the server stops listening for clients.

Returns:

  • (Boolean)


28
29
30
# File 'lib/nova/starbound/server.rb', line 28

def run
  @run
end

#thread_listArray<Thread> (readonly)

The list of active threads that are running.

Returns:

  • (Array<Thread>)


33
34
35
# File 'lib/nova/starbound/server.rb', line 33

def thread_list
  @thread_list
end

Instance Method Details

#listenvoid

This method returns an undefined value.

Listen for clients. Calls #handle_client when a client is found.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nova/starbound/server.rb', line 85

def listen
  Nova.logger.info { "Server started." }
  while run
    next unless IO.select [server], nil, nil, 1
    thread_list << Thread.start(server.accept) do |client|
      Nova.logger.info { "Client accepted." }

      handle_client client

      thread_list.delete(Thread.current)
      Nova.logger.info { "Client disconnected." }
    end
  end
end

#read_client_file(file) ⇒ Object

Reads a ruby file in the instance of this server, so that it can build a #with_client block for it.

Parameters:

  • file (String)

    the file to read from.



75
76
77
78
79
# File 'lib/nova/starbound/server.rb', line 75

def read_client_file(file)
  return unless file && File.exists?(file)

  instance_eval File.open(file, "r").read, file, 1
end

#serverObject

Returns the server type that this server should run as, already instantized.

Returns:

  • (Object)


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nova/starbound/server.rb', line 115

def server
  @_server ||= case options.fetch(:type, :tcp)
  when :tcp
    TCPServer.new options.fetch(:host, "127.0.0.1"),
      options.fetch(:port, 2010)
  when :unix
    UNIXServer.new options.fetch(:path)
  when :pipe
    FakeServer.new options.fetch(:pipe)
  end
end

#shutdownvoid

This method returns an undefined value.

Shuts down the server.



103
104
105
106
107
108
109
# File 'lib/nova/starbound/server.rb', line 103

def shutdown
  puts "shutting down"
  @run = false
  thread_list.each do |thread|
    thread.raise ExitError
  end
end

#with_client {|protocol| ... } ⇒ Proc

How to handle the client. Accepts a block, which is used in #listen when a client connects. If no block is given, the a block is returned.

Yield Parameters:

  • protocol (Protocol)

    the protocol instance that is used to handle the client. By the time it is yielded, the client has already done a handshake with the server.

Returns:

  • (Proc)


63
64
65
66
67
68
69
# File 'lib/nova/starbound/server.rb', line 63

def with_client(&block)
  if block_given?
    @with_client = block
  else
    @with_client ||= proc {}
  end
end