blob: 37e33584b2ca5ec35f7e3efe09fb3aef0276feb9 [file] [log] [blame]
Kevin Clarkd3cee022008-06-18 01:19:09 +00001$:.unshift File.dirname(__FILE__) + '/../lib'
2require 'thrift'
3require 'thrift/server/nonblockingserver'
4$:.unshift File.dirname(__FILE__) + "/gen-rb"
5require 'BenchmarkService'
6
7module 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
47end
48
49def resolve_const(const)
50 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
51end
52
Kevin Clarkfdc9c972008-06-18 01:19:46 +000053host, port, serverklass = ARGV
54
55Server.start_server(host, port.to_i, resolve_const(serverklass))
56
Kevin Clark1a95a1d2008-06-18 01:19:28 +000057# 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
59Marshal.dump(:started, STDOUT)
Kevin Clark1e8f5202008-06-18 01:19:33 +000060STDOUT.flush
Kevin Clark1a95a1d2008-06-18 01:19:28 +000061
Kevin Clarkb6791312008-06-18 01:19:23 +000062Marshal.load(STDIN) # wait until we're instructed to shut down
Kevin Clarkd3cee022008-06-18 01:19:09 +000063
64Server.shutdown