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 package org.apache.thrift.test;
21 
22 import org.apache.thrift.server.ServerTestBase.TestHandler;
23 import org.apache.thrift.server.THsHaServer;
24 import org.apache.thrift.server.THsHaServer.Args;
25 import org.apache.thrift.server.TNonblockingServer;
26 import org.apache.thrift.server.TServer;
27 import org.apache.thrift.transport.TNonblockingServerSocket;
28 import thrift.test.ThriftTest;
29 
30 public class TestNonblockingServer extends TestServer {
main(String[] args)31   public static void main(String[] args) {
32     try {
33       int port = 9090;
34       boolean hsha = false;
35 
36       for (int i = 0; i < args.length; i++) {
37         if (args[i].equals("-p")) {
38           port = Integer.valueOf(args[i++]);
39         } else if (args[i].equals("-hsha")) {
40           hsha = true;
41         }
42       }
43       // @TODO add other protocol and transport types
44 
45       // Processor
46       TestHandler testHandler = new TestHandler();
47       ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
48 
49       // Transport
50       TNonblockingServerSocket tServerSocket =
51           new TNonblockingServerSocket(
52               new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs().port(port));
53 
54       TServer serverEngine;
55 
56       if (hsha) {
57         // HsHa Server
58         serverEngine = new THsHaServer(new Args(tServerSocket).processor(testProcessor));
59       } else {
60         // Nonblocking Server
61         serverEngine = new TNonblockingServer(new Args(tServerSocket).processor(testProcessor));
62       }
63 
64       // Run it
65       System.out.println("Starting the server on port " + port + "...");
66       serverEngine.serve();
67 
68     } catch (Exception x) {
69       x.printStackTrace();
70     }
71     System.out.println("done.");
72   }
73 }
74