<commit_before>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)
          fingerprint = extract_fingerprint(env['rack.peer_cert'])
          e, payload = block.call(req, fingerprint)
          err = e.kind_of?(Exception) ? e.message : e
          [200,nil, ServiceMessage.new(payload, err, req.rid).to_json]
        }
        server = Thin::Server.new config.service_host, port, {signals: false}, app

        if config.thin_threaded
          server.threaded = true
          server.threadpool_size = config.thin_threadpool_size
        end

        if tls?
          server.ssl = true
          server.ssl_options = {
            private_key_file: config.key,
            cert_chain_file: config.cert_pem,
            verify_peer: true
          }
        end
        server.start
      }
    end

    def request(key, payload, params={})
      services = lookup(key)
      if services.size > 0
        json = ServiceMessage.new(payload,key).to_json
        service = get_random_service services
        resp = http_client.request service, json, params
        msg = ServiceMessage.parse(resp.body)
        return [msg.err, msg.payload]
      else
        return ["NoPeerFound",nil]
      end
    rescue Exception => e
      return [e, nil]
    end<commit_msg>fix bug when services is nil<commit_after>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)
          fingerprint = extract_fingerprint(env['rack.peer_cert'])
          e, payload = block.call(req, fingerprint)
          err = e.kind_of?(Exception) ? e.message : e
          [200,nil, ServiceMessage.new(payload, err, req.rid).to_json]
        }
        server = Thin::Server.new config.service_host, port, {signals: false}, app

        if config.thin_threaded
          server.threaded = true
          server.threadpool_size = config.thin_threadpool_size
        end

        if tls?
          server.ssl = true
          server.ssl_options = {
            private_key_file: config.key,
            cert_chain_file: config.cert_pem,
            verify_peer: true
          }
        end
        server.start
      }
    end

    def request(key, payload, params={})
      services = lookup(key)
      if services && services.size > 0
        json = ServiceMessage.new(payload,key).to_json
        service = get_random_service services
        resp = http_client.request service, json, params
        msg = ServiceMessage.parse(resp.body)
        return [msg.err, msg.payload]
      else
        return ["NoPeerFound",nil]
      end
    rescue Exception => e
      return [e, nil]
    end
{'fraction_non_alphanumeric': 0.10378243201000313, 'fraction_numerical': 0.0025007814942169426, 'mean_word_length': 2.071017274472169, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807724', 'n_tokens_mistral': 964, 'n_tokens_neox': 941, 'n_words': 219}