David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 20 | $:.unshift File.dirname(__FILE__) + '/../lib' |
| 21 | require 'thrift' |
| 22 | require 'thrift/server/nonblockingserver' |
| 23 | $:.unshift File.dirname(__FILE__) + "/gen-rb" |
| 24 | require 'BenchmarkService' |
| 25 | |
| 26 | module Server |
| 27 | include Thrift |
| 28 | |
| 29 | class BenchmarkHandler |
| 30 | # 1-based index into the fibonacci sequence |
| 31 | def fibonacci(n) |
| 32 | seq = [1, 1] |
| 33 | 3.upto(n) do |
| 34 | seq << seq[-1] + seq[-2] |
| 35 | end |
| 36 | seq[n-1] # n is 1-based |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | def self.start_server(host, port, serverClass) |
| 41 | handler = BenchmarkHandler.new |
| 42 | processor = ThriftBenchmark::BenchmarkService::Processor.new(handler) |
| 43 | transport = ServerSocket.new(host, port) |
| 44 | transportFactory = FramedTransportFactory.new |
| 45 | args = [processor, transport, transportFactory, nil, 20] |
| 46 | if serverClass == NonblockingServer |
| 47 | logger = Logger.new(STDERR) |
| 48 | logger.level = Logger::WARN |
| 49 | args << logger |
| 50 | end |
| 51 | server = serverClass.new(*args) |
| 52 | @server_thread = Thread.new do |
| 53 | server.serve |
| 54 | end |
| 55 | @server = server |
| 56 | end |
| 57 | |
| 58 | def self.shutdown |
| 59 | return if @server.nil? |
| 60 | if @server.respond_to? :shutdown |
| 61 | @server.shutdown |
| 62 | else |
| 63 | @server_thread.kill |
| 64 | end |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | def resolve_const(const) |
| 69 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 70 | end |
| 71 | |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 72 | host, port, serverklass = ARGV |
| 73 | |
| 74 | Server.start_server(host, port.to_i, resolve_const(serverklass)) |
| 75 | |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 76 | # let our host know that the interpreter has started |
| 77 | # ideally we'd wait until the server was serving, but we don't have a hook for that |
| 78 | Marshal.dump(:started, STDOUT) |
Kevin Clark | 1e8f520 | 2008-06-18 01:19:33 +0000 | [diff] [blame] | 79 | STDOUT.flush |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 80 | |
Kevin Clark | b679131 | 2008-06-18 01:19:23 +0000 | [diff] [blame] | 81 | Marshal.load(STDIN) # wait until we're instructed to shut down |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 82 | |
| 83 | Server.shutdown |