Class: CarbonMU::Server

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO, Celluloid::Logger, Celluloid::ZMQ
Defined in:
lib/core/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/core/server.rb', line 15

def initialize
  info "*** Starting CarbonMU game server to connect to edge router port #{CarbonMU.edge_router_receive_port}."
  CarbonMU.server = Actor.current

  @ipc_reader = ReadSocket.new
  @ipc_writer = WriteSocket.new(CarbonMU.edge_router_receive_port)
  send_server_started_to_edge_router

  Server.initialize_database
  Server.create_starter_objects

  async.run
  retrieve_existing_connections
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections


13
14
15
# File 'lib/core/server.rb', line 13

def connections
  @connections
end

Class Method Details

.create_starter_objectsObject


104
105
106
107
108
# File 'lib/core/server.rb', line 104

def self.create_starter_objects
  ensure_special_exists(:starting_room, Room, {name: "Starting Room", description: "This is the starting room for newly-created players. Feel free to rename and re-describe it."})
  ensure_special_exists(:lostandfound_room, Room, {name: "Lost & Found Room", description: "This is the room where objects and players go if the thing that was holding them gets destroyed."})
  ensure_special_exists(:superadmin_player, Player, {name: "Superadmin", description: "Obviously the most powerful of his race, it could kill us all."})
end

.ensure_special_exists(special, klass, attributes) ⇒ Object


110
111
112
# File 'lib/core/server.rb', line 110

def self.ensure_special_exists(special, klass, attributes)
  klass.create!(attributes.merge(_special: special)) if klass.where(_special: special).count == 0
end

.initialize_databaseObject


98
99
100
101
102
# File 'lib/core/server.rb', line 98

def self.initialize_database
  Mongoid.logger.level = ::Logger::DEBUG
  Mongoid.load!("mongoid.yml", ENV["MONGOID_ENV"] || :production)
  ::Mongoid::Tasks::Database.create_indexes
end

.trigger_rebootObject


76
77
78
# File 'lib/core/server.rb', line 76

def self.trigger_reboot
  Actor[:server].send_reboot_message_to_edge_router
end

Instance Method Details

#add_connection(connection_id) ⇒ Object


40
41
42
43
# File 'lib/core/server.rb', line 40

def add_connection(connection_id)
  @connections ||= []
  @connections << connection_id
end

#handle_command(input, connection_id) ⇒ Object


49
50
51
# File 'lib/core/server.rb', line 49

def handle_command(input, connection_id)
  Parser.parse_and_execute(connection_id, input)
end

#handle_edge_router_message(input) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/core/server.rb', line 57

def handle_edge_router_message(input)
  message = IPCMessage.unserialize(input)
  debug "SERVER RECEIVE: #{message}" if CarbonMU.configuration.log_ipc_traffic
  case message.op
  when :command
    handle_command(message.command, message.connection_id)
  when :connect
    add_connection(message.connection_id)
  when :disconnect
    remove_connection(message.connection_id)
  else
    raise ArgumentError, "Unsupported operation '#{message.op}' received from edge router."
  end
end

#remove_connection(connection_id) ⇒ Object


45
46
47
# File 'lib/core/server.rb', line 45

def remove_connection(connection_id)
  @connections.delete(connection_id)
end

#retrieve_existing_connectionsObject


72
73
74
# File 'lib/core/server.rb', line 72

def retrieve_existing_connections
  send_message_to_edge_router(:retrieve_existing_connections)
end

#runObject


30
31
32
33
34
# File 'lib/core/server.rb', line 30

def run
  loop do
    async.handle_edge_router_message(@ipc_reader.read)
  end
end

#send_message_to_edge_router(op, params = {}) ⇒ Object


92
93
94
95
96
# File 'lib/core/server.rb', line 92

def send_message_to_edge_router(op, params={})
  message = IPCMessage.new(op, params)
  debug "SERVER SEND: #{message}" if CarbonMU.configuration.log_ipc_traffic
  @ipc_writer.send message.serialize
end

#send_reboot_message_to_edge_routerObject


80
81
82
# File 'lib/core/server.rb', line 80

def send_reboot_message_to_edge_router
  send_message_to_edge_router(:reboot)
end

#send_server_started_to_edge_routerObject


53
54
55
# File 'lib/core/server.rb', line 53

def send_server_started_to_edge_router
  send_message_to_edge_router(:started, port: @ipc_reader.port_number, pid: Process.pid)
end

#shutdownObject


36
37
38
# File 'lib/core/server.rb', line 36

def shutdown
  error "Terminating server!"
end

#write_to_all_connections(str) ⇒ Object


84
85
86
# File 'lib/core/server.rb', line 84

def write_to_all_connections(str)
  connections.each {|c| write_to_connection(c, str) }
end

#write_to_connection(connection_id, str) ⇒ Object


88
89
90
# File 'lib/core/server.rb', line 88

def write_to_connection(connection_id, str)
  send_message_to_edge_router(:write, connection_id: connection_id, output: str)
end