Class: Whatup::Server::Server
- Inherits:
-
Object
- Object
- Whatup::Server::Server
- Includes:
- DbInit, Redirection, WhatupLogger
- Defined in:
- lib/whatup/server/server.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
Instance Method Summary collapse
-
#clients_except(client) ⇒ Array<Whatup::Server::Client>
All currently connected clients except for ‘client`.
-
#initialize(ip: 'localhost', port:) ⇒ Whatup::Server::Server
constructor
The created server.
-
#new_room!(clients: [], name:) ⇒ Whatup::Server::Room
The created room.
-
#start ⇒ Object
Starts the server.
Methods included from WhatupLogger
Methods included from Redirection
Methods included from DbInit
Constructor Details
#initialize(ip: 'localhost', port:) ⇒ Whatup::Server::Server
Returns The created server.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/whatup/server/server.rb', line 34 def initialize ip: 'localhost', port: @ip = ip @port = port @address = "#{@ip}:#{@port}" @clients = [] @rooms = [] @pid = Process.pid @pid_file = "#{Dir.home}/.whatup.pid" DbInit.setup_db! end |
Instance Method Details
#clients_except(client) ⇒ Array<Whatup::Server::Client>
Returns All currently connected clients except for ‘client`.
74 75 76 |
# File 'lib/whatup/server/server.rb', line 74 def clients_except client @clients.reject { |c| c == client } end |
#new_room!(clients: [], name:) ⇒ Whatup::Server::Room
Returns The created room.
82 83 84 85 86 |
# File 'lib/whatup/server/server.rb', line 82 def new_room! clients: [], name: Room.create!(name: name, clients: clients).tap do |room| @rooms << room end end |
#start ⇒ Object
Starts the server.
The server continuously loops, and handle each new client in a separate thread.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/whatup/server/server.rb', line 52 def start log.info { "Starting a server with PID:#{@pid} @ #{@address} ... \n" } exit_if_pid_exists! connect_to_socket! write_pid! # Listen for connections, then accept each in a separate thread loop do Thread.new(@socket.accept) do |client| log.info { "Accepted new client: #{client.inspect}" } handle_client client end end rescue SignalException # In case of ^c kill end |