Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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
 
20
$:.unshift File.dirname(__FILE__) + '/../lib'
21
require 'thrift'
22
$:.unshift File.dirname(__FILE__) + "/gen-rb"
23
require 'benchmark_service'
24
 
25
module Server
26
  include Thrift
27
 
28
  class BenchmarkHandler
29
    # 1-based index into the fibonacci sequence
30
    def fibonacci(n)
31
      seq = [1, 1]
32
      3.upto(n) do
33
        seq << seq[-1] + seq[-2]
34
      end
35
      seq[n-1] # n is 1-based
36
    end
37
  end
38
 
39
  def self.start_server(host, port, serverClass)
40
    handler = BenchmarkHandler.new
41
    processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
42
    transport = ServerSocket.new(host, port)
43
    transport_factory = FramedTransportFactory.new
44
    args = [processor, transport, transport_factory, nil, 20]
45
    if serverClass == NonblockingServer
46
      logger = Logger.new(STDERR)
47
      logger.level = Logger::WARN
48
      args << logger
49
    end
50
    server = serverClass.new(*args)
51
    @server_thread = Thread.new do
52
      server.serve
53
    end
54
    @server = server
55
  end
56
 
57
  def self.shutdown
58
    return if @server.nil?
59
    if @server.respond_to? :shutdown
60
      @server.shutdown
61
    else
62
      @server_thread.kill
63
    end
64
  end
65
end
66
 
67
def resolve_const(const)
68
  const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
69
end
70
 
71
host, port, serverklass = ARGV
72
 
73
Server.start_server(host, port.to_i, resolve_const(serverklass))
74
 
75
# let our host know that the interpreter has started
76
# ideally we'd wait until the server was serving, but we don't have a hook for that
77
Marshal.dump(:started, STDOUT)
78
STDOUT.flush
79
 
80
Marshal.load(STDIN) # wait until we're instructed to shut down
81
 
82
Server.shutdown