Class: Nova::Starbound::Server
- Inherits:
-
Object
- Object
- Nova::Starbound::Server
- Defined in:
- lib/nova/starbound/server.rb
Overview
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
-
#options ⇒ Hash
readonly
The options passed to the server on initialization.
-
#run ⇒ Boolean
Whether or not to run.
-
#thread_list ⇒ Array<Thread>
readonly
The list of active threads that are running.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Server
constructor
Initialize the server with the given options.
-
#listen ⇒ void
Listen for clients.
-
#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.
-
#server ⇒ Object
Returns the server type that this server should run as, already instantized.
-
#shutdown ⇒ void
Shuts down the server.
-
#with_client {|protocol| ... } ⇒ Proc
How to handle the client.
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.
48 49 50 51 52 53 |
# File 'lib/nova/starbound/server.rb', line 48 def initialize( = {}) = DEFAULT_OPTIONS.merge = (.delete(:protocol) || {}).dup @run = true @thread_list = [] end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The options passed to the server on initialization.
14 15 16 |
# File 'lib/nova/starbound/server.rb', line 14 def end |
#run ⇒ Boolean
Whether or not to run. Once this is set to false, the server stops listening for clients.
28 29 30 |
# File 'lib/nova/starbound/server.rb', line 28 def run @run end |
#thread_list ⇒ Array<Thread> (readonly)
The list of active threads that are running.
33 34 35 |
# File 'lib/nova/starbound/server.rb', line 33 def thread_list @thread_list end |
Instance Method Details
#listen ⇒ void
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.
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 |
#server ⇒ Object
Returns the server type that this server should run as, already instantized.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/nova/starbound/server.rb', line 115 def server @_server ||= case .fetch(:type, :tcp) when :tcp TCPServer.new .fetch(:host, "127.0.0.1"), .fetch(:port, 2010) when :unix UNIXServer.new .fetch(:path) when :pipe FakeServer.new .fetch(:pipe) end end |
#shutdown ⇒ void
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.
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 |