Class: MudServer

Inherits:
Object
  • Object
show all
Defined in:
lib/mud_server.rb

Overview

The server class creates a single instance of a mud server.

Defined Under Namespace

Classes: AbstractController, DefaultController, Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip = "0.0.0.0", port = 4000, environment = 'development') ⇒ MudServer

Returns a new instance of MudServer.



7
8
9
# File 'lib/mud_server.rb', line 7

def initialize(ip = "0.0.0.0", port = 4000, environment = 'development')
  bootstrap_settings(ip, port, environment)
end

Instance Attribute Details

#connection_acceptorObject

Returns the value of attribute connection_acceptor.



4
5
6
# File 'lib/mud_server.rb', line 4

def connection_acceptor
  @connection_acceptor
end

#connection_poolObject

Returns the value of attribute connection_pool.



4
5
6
# File 'lib/mud_server.rb', line 4

def connection_pool
  @connection_pool
end

#environmentObject

Returns the value of attribute environment.



4
5
6
# File 'lib/mud_server.rb', line 4

def environment
  @environment
end

#ipObject

Returns the value of attribute ip.



4
5
6
# File 'lib/mud_server.rb', line 4

def ip
  @ip
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/mud_server.rb', line 4

def port
  @port
end

#tcp_socketObject

Returns the value of attribute tcp_socket.



4
5
6
# File 'lib/mud_server.rb', line 4

def tcp_socket
  @tcp_socket
end

Instance Method Details

#bootstrap_settings(ip, port, environment) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/mud_server.rb', line 11

def bootstrap_settings(ip, port, environment)
  @port            = port
  @ip              = ip
  @environment     = environment
  @connection_pool = [] # This is where we keep reference to all game
                        # connections
end

#startObject



19
20
21
22
23
24
25
26
27
# File 'lib/mud_server.rb', line 19

def start
  @tcp_socket = TCPServer.new @ip , @port
  @connection_acceptor = Thread.new do
    while connection = @tcp_socket.accept
      @connection_pool << MudServer::Session.new(connection)
    end
  end
  return true
end

#stopObject

You probably won’t need this one in production, but it’s a must for testing.



30
31
32
33
34
35
# File 'lib/mud_server.rb', line 30

def stop
  @tcp_socket.close
  @connection_acceptor.kill
  @connection_acceptor = nil
  return true
end