Class: Messaging::Server
- Inherits:
-
Object
- Object
- Messaging::Server
- Defined in:
- lib/rdb/messaging.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #broadcast ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #listen(host, port) ⇒ Object
Constructor Details
#initialize ⇒ Server
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
#broadcast ⇒ Object
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 { (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 = Messaging::Message.unpack(payload) # TODO: Hack. socket = client.instance_eval { @connection.socket } @clients_lock.synchronize { if @sockets.add?(socket) @clients[socket] = client end } (client, ) rescue => e puts ">>> Error: #{e}\n#{e.backtrace}" end end end |