Class: Messaging::Server

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

Direct Known Subclasses

CommandServer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



44
45
46
47
48
# File 'lib/rdb/messaging.rb', line 44

def initialize
  @sockets = Set.new
  @clients = {}
  @clients_lock = Mutex.new
end

Class Method Details

.inherited(klass) ⇒ Object



40
41
42
# File 'lib/rdb/messaging.rb', line 40

def self.inherited(klass)
  klass.send(:extend, RemoteAnnotation)
end

Instance Method Details

#broadcastObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rdb/messaging.rb', line 77

def broadcast
  @broadcast ||= Class.new do
    def initialize(server)
      @server = server
    end

    def method_missing(symbol, *args, &block)
      @server.instance_exec {
        broadcast_message(symbol.to_s, args)
      }
    end
  end.new(self)
end

#listen(host, port) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdb/messaging.rb', line 50

def listen(host, port)
  channel = Cod.tcp_server("#{host}:#{port}")

  loop do
    begin
      payload, client = channel.get_ext
    rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNRESET
      next
    end

    begin
      message = Messaging::Message.unpack(payload)
      # TODO: Hack.
      socket = client.instance_eval { @connection.socket }
      @clients_lock.synchronize {
        if @sockets.add?(socket)
          @clients[socket] = client
        end
      }

      handle_message(client, message)
    rescue => e
      puts ">>> Error: #{e}\n#{e.backtrace}"
    end
  end
end