Sha256: ced54d4c99c5e494ef7ba548d7786de85dac4ca8b8540ee5af4248181c991432

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Grenache
  class Http < Grenache::Base
    def listen(key, port,  opts={}, &block)
      start_http_service(port,&block)

      announce(key, port, opts) do |res|
        puts "#{key} announced #{res}"
      end
    end

    def start_http_service(port, &block)
      EM.defer {
        app = -> (env) {
          req = ServiceMessage.parse(env['rack.input'].read)
          err, payload = block.call(req)
          [200,nil, ServiceMessage.new(payload, err, req.rid).to_json]
        }
        server = Thin::Server.start('0.0.0.0', port, app, {signals: false})
      }
    end

    def request(key, payload)
      services = lookup(key)
      if services.size > 0
        json = ServiceMessage.new(payload).to_json
        service = services.sample.sub("tcp://","http://")
        service.prepend("http://") unless service.start_with?("http://")
        resp = HTTParty.post(service,{body: json})
        msg = ServiceMessage.parse(resp.body)
        return [msg.err, msg.payload]
      else
        return ["NoPeerFound",nil]
      end
    rescue Exception => e
      return [e, nil]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grenache-ruby-http-0.2.0 lib/grenache/http.rb