class Strum::Server::Endpoint

Public Class Methods

new() click to toggle source

Called when a new Endpoint object is created.

Calls superclass method Strum::Internal::Component::new
# File lib/strum/server/endpoint.rb, line 38
def initialize
  super(:Container)
  self[:API] = Strum::Internal::Listener.instance
  # Spawn our endpoint
  begin
    self[:Server] = Async::IO::TCPServer.new(local_host = ENV['HOST'],
                                               local_port = ENV['PORT'].to_i)
    # Trigger event.
    self[:API].trigger_events(Strum::Internal::Events::EndpointSpawnEvent)
  rescue SocketError => e
    report "Fatal Error during Endpoint initialization!\n#{e}"
  end
  self[:API].trigger_events(Strum::Internal::Events::EndpointInitializationEvent)
end

Public Instance Methods

listen(&block) click to toggle source

Listens for connections

# File lib/strum/server/endpoint.rb, line 14
def listen(&block)
  report "Listening on #{ENV['HOST']}:#{ENV['PORT']}"
  ##
  # Call our pending events for this EndpointListenEvent
  self[:API].trigger_events(Strum::Internal::Events::EndpointListenEvent)
  Async do |task|
    # binding.pry
    ##
    # Start our listen loop
    loop do

      self[:Server].accept(task: task) do |client|
        self[:Manager].receive(client)
      end
      
    end
    task.async do
      block.call(self)  if block_given?
    end
  end
end