blob: 153eb0f0a2d0527b9143f96743405c85927776c7 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Clarkd3cee022008-06-18 01:19:09 +000020$:.unshift File.dirname(__FILE__) + '/../lib'
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050021$:.unshift File.dirname(__FILE__) + '/../ext'
Kevin Clarkd3cee022008-06-18 01:19:09 +000022require 'thrift'
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050023require 'openssl'
Kevin Clarkd3cee022008-06-18 01:19:09 +000024$:.unshift File.dirname(__FILE__) + "/gen-rb"
Bryan Duxbury0bbef922009-04-07 22:23:40 +000025require 'benchmark_service'
Kevin Clarkd3cee022008-06-18 01:19:09 +000026
27module Server
28 include Thrift
29
30 class BenchmarkHandler
31 # 1-based index into the fibonacci sequence
32 def fibonacci(n)
33 seq = [1, 1]
34 3.upto(n) do
35 seq << seq[-1] + seq[-2]
36 end
37 seq[n-1] # n is 1-based
38 end
39 end
40
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050041 def self.start_server(host, port, serverClass, tls)
Kevin Clarkd3cee022008-06-18 01:19:09 +000042 handler = BenchmarkHandler.new
43 processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050044 transport = if tls
45 ssl_context = OpenSSL::SSL::SSLContext.new.tap do |ctx|
46 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
47 ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
48
49 keys_dir = File.expand_path("../../../test/keys", __dir__)
50 ctx.ca_file = File.join(keys_dir, "CA.pem")
51 ctx.cert = OpenSSL::X509::Certificate.new(File.open(File.join(keys_dir, "server.crt")))
52 ctx.cert_store = OpenSSL::X509::Store.new
53 ctx.cert_store.add_file(File.join(keys_dir, 'client.pem'))
54 ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keys_dir, "server.key")))
55 end
56
57 Thrift::SSLServerSocket.new(host, port, ssl_context)
58 else
59 ServerSocket.new(host, port)
60 end
Bryan Duxburyd1d15422009-04-04 00:58:03 +000061 transport_factory = FramedTransportFactory.new
62 args = [processor, transport, transport_factory, nil, 20]
Kevin Clarkd3cee022008-06-18 01:19:09 +000063 if serverClass == NonblockingServer
64 logger = Logger.new(STDERR)
65 logger.level = Logger::WARN
66 args << logger
67 end
68 server = serverClass.new(*args)
69 @server_thread = Thread.new do
70 server.serve
71 end
72 @server = server
73 end
74
75 def self.shutdown
76 return if @server.nil?
77 if @server.respond_to? :shutdown
78 @server.shutdown
79 else
80 @server_thread.kill
81 end
82 end
83end
84
85def resolve_const(const)
86 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
87end
88
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050089tls = true if ARGV[0] == '-tls' and ARGV.shift
90
Kevin Clarkfdc9c972008-06-18 01:19:46 +000091host, port, serverklass = ARGV
92
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050093Server.start_server(host, port.to_i, resolve_const(serverklass), tls)
Kevin Clarkfdc9c972008-06-18 01:19:46 +000094
Kevin Clark1a95a1d2008-06-18 01:19:28 +000095# let our host know that the interpreter has started
96# ideally we'd wait until the server was serving, but we don't have a hook for that
97Marshal.dump(:started, STDOUT)
Kevin Clark1e8f5202008-06-18 01:19:33 +000098STDOUT.flush
Kevin Clark1a95a1d2008-06-18 01:19:28 +000099
Kevin Clarkb6791312008-06-18 01:19:23 +0000100Marshal.load(STDIN) # wait until we're instructed to shut down
Kevin Clarkd3cee022008-06-18 01:19:09 +0000101
102Server.shutdown