Sha256: 04dc63e998b6b501676cd1c83900637de3225e6c9ad2eafc4d04065bb3caebc0

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

#!/usr/bin/env ruby
require 'configliere'
require 'wukong'
require 'eventmachine'
require 'em-synchrony'
require 'multi_json'

Settings({
  host: "localhost",
  port: 9500
})

Settings.use :commandline
Settings.resolve!

# Load the file on the command line
wu_file = ARGV.shift
load wu_file
$processor = File.basename(wu_file, '.rb').to_sym

class Wukong::Server
  def prepare(options = {})
    dataflow_class = Wukong.dataflow(:server){ send(options[:processor]) }
    flow_builder = Wukong.registry.retrieve(:server)
    flow = flow_builder.build(processor: $processor)
    
    @buffer = []
    @processor = flow.stages.values.first
    @processor.emitter = ->(value){ @buffer << value }
  end
  
  def process(record)
    @process.process(record)
  end
  
  def cleanup
  end
end


# EventMachine server

class WukongMachine < EM::Protocols::LineAndTextProtocol
  def post_init
    puts "[server] Client connected"
    @wukong = WukongInterface.new
    @wukong.prepare(processor: $processor)
  end

  def receive_data(data)
    @buffer = []
    input = MultiJson.load data
    
    op = proc { @wukong.process(input) }
    callback = proc { send_data MultiJson.dump(@buffer) + "\n" }
    EM.defer(op, callback)
      
  rescue MultiJson::DecodeError => ex
    STDERR.puts "[server] Dropped: Malformed request"
  end

  def unbind
    puts "[server] Client disconnected."
  end
end

EM::run {
  EM::start_server(Settings[:host], Settings[:port], WukongMachine)
  puts "Listening on #{Settings[:host]}:#{Settings[:port]}"
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wukong-3.0.0.pre2 bin/wu-server