Sha256: d3dafcbe84bf384803c31c2c7e343a14bbad060eaf6b8ab2254607c29b4e112f

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

module SubZero
  class Socket
    module Server

      def run &handle_message
        context do |c|
          socket c do |s|
            poll s do |m|
              handle_message[m]
            end
          end
        end
      rescue => e
        error e, 'failed while running server, going to retry'
        retry
      end

      def up!
        send_server_verb 'UP'
      end

      def down!
        send_server_verb 'DOWN'
      end

      def ping
        send_server_verb 'PING'
      end

      def send_server_verb verb
        context do |c|
          socket c do |s|
            send_message s, Message.new(type: 'SRV', verb: verb)
          end
        end
      end

      def poll socket
        ZMQ::Poller.new.tap do |p|
          p.register socket, ZMQ::POLLIN
          p.poll 1

          p.readables.each do |s|
            s.recv_strings source = []

            begin
              message = Message.parse(source)
            rescue => e
              error e, 'error parsing message'
            end

            yield message if message
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sub_zero-0.0.9 lib/sub_zero/socket/server.rb
sub_zero-0.0.8 lib/sub_zero/socket/server.rb
sub_zero-0.0.7 lib/sub_zero/socket/server.rb
sub_zero-0.0.6 lib/sub_zero/socket/server.rb
sub_zero-0.0.5 lib/sub_zero/socket/server.rb