Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 1 | $:.unshift File.dirname(__FILE__) + '/../lib' |
| 2 | require 'thrift' |
| 3 | require 'thrift/server/nonblockingserver' |
| 4 | $:.unshift File.dirname(__FILE__) + "/gen-rb" |
| 5 | require 'BenchmarkService' |
| 6 | |
| 7 | module Server |
| 8 | include Thrift |
| 9 | |
| 10 | class BenchmarkHandler |
| 11 | # 1-based index into the fibonacci sequence |
| 12 | def fibonacci(n) |
| 13 | seq = [1, 1] |
| 14 | 3.upto(n) do |
| 15 | seq << seq[-1] + seq[-2] |
| 16 | end |
| 17 | seq[n-1] # n is 1-based |
| 18 | end |
| 19 | end |
| 20 | |
| 21 | def self.start_server(host, port, serverClass) |
| 22 | handler = BenchmarkHandler.new |
| 23 | processor = ThriftBenchmark::BenchmarkService::Processor.new(handler) |
| 24 | transport = ServerSocket.new(host, port) |
| 25 | transportFactory = FramedTransportFactory.new |
| 26 | args = [processor, transport, transportFactory, nil, 20] |
| 27 | if serverClass == NonblockingServer |
| 28 | logger = Logger.new(STDERR) |
| 29 | logger.level = Logger::WARN |
| 30 | args << logger |
| 31 | end |
| 32 | server = serverClass.new(*args) |
| 33 | @server_thread = Thread.new do |
| 34 | server.serve |
| 35 | end |
| 36 | @server = server |
| 37 | end |
| 38 | |
| 39 | def self.shutdown |
| 40 | return if @server.nil? |
| 41 | if @server.respond_to? :shutdown |
| 42 | @server.shutdown |
| 43 | else |
| 44 | @server_thread.kill |
| 45 | end |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | def resolve_const(const) |
| 50 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 51 | end |
| 52 | |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame^] | 53 | host, port, serverklass = ARGV |
| 54 | |
| 55 | Server.start_server(host, port.to_i, resolve_const(serverklass)) |
| 56 | |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 57 | # let our host know that the interpreter has started |
| 58 | # ideally we'd wait until the server was serving, but we don't have a hook for that |
| 59 | Marshal.dump(:started, STDOUT) |
Kevin Clark | 1e8f520 | 2008-06-18 01:19:33 +0000 | [diff] [blame] | 60 | STDOUT.flush |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 61 | |
Kevin Clark | b679131 | 2008-06-18 01:19:23 +0000 | [diff] [blame] | 62 | Marshal.load(STDIN) # wait until we're instructed to shut down |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 63 | |
| 64 | Server.shutdown |