bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
BinaryListener.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.thrift.server;
2 
3 import java.security.NoSuchAlgorithmException;
4 import java.util.concurrent.TimeUnit;
5 
6 import javax.net.ssl.SSLContext;
7 import javax.net.ssl.SSLSocketFactory;
8 
9 import org.apache.logging.log4j.LogManager;
10 import org.apache.logging.log4j.Logger;
11 import org.apache.thrift.protocol.TProtocolFactory;
12 import org.apache.thrift.server.THsHaServer;
13 import org.apache.thrift.server.TServer;
14 import org.apache.thrift.server.TThreadPoolServer;
15 import org.apache.thrift.transport.TNonblockingServerSocket;
16 import org.apache.thrift.transport.TNonblockingServerTransport;
17 import org.apache.thrift.transport.TSSLTransportFactory;
18 import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
19 import org.apache.thrift.transport.TServerTransport;
20 import org.apache.thrift.transport.TTransportException;
21 import org.apache.thrift.transport.layered.TFramedTransport;
22 import org.openslx.bwlp.thrift.iface.MasterServer;
24 import org.openslx.thrifthelper.TBinaryProtocolSafe;
25 
26 public class BinaryListener implements Runnable
27 {
28  private static final int MAX_MSG_LEN = 30 * 1000 * 1000;
29 
34  private static final int CLIENT_TIMEOUT_MS = 120_000;
35 
36  private final MasterServer.Processor<MasterServerHandler> processor = new MasterServer.Processor<MasterServerHandler>(
37  new MasterServerHandler() );
38  final TProtocolFactory protFactory = new TBinaryProtocolSafe.Factory( true, true );
39 
40  private static Logger log = LogManager.getLogger( BinaryListener.class );
41  final TServer server;
42 
43  @Override
44  public void run()
45  {
46  log.info( "Starting Binary Thrift" );
47  server.serve();
48  log.info( "Stopped Binary Thrift" );
49  System.exit( 1 ); // Exit so the server can fully restart
50  }
51 
52  public BinaryListener( int port, boolean secure ) throws TTransportException, NoSuchAlgorithmException
53  {
54  if ( secure )
55  server = initSecure( port );
56  else
57  server = initNormal( port );
58  }
59 
69  private TServer initSecure( int port ) throws NoSuchAlgorithmException, TTransportException
70  {
71  SSLContext context = SSLContext.getDefault();
72  SSLSocketFactory sf = context.getSocketFactory();
73  String[] cipherSuites = sf.getSupportedCipherSuites();
74  // TODO: Remove insecure ones
75  final TSSLTransportParameters params = new TSSLTransportParameters( "TLSv1.2", cipherSuites );
76  params.setKeyStore( Globals.getSslKeystoreFile(), Globals.getSslKeystorePassword() );
77  TServerTransport serverTransport;
78  try {
79  serverTransport = TSSLTransportFactory.getServerSocket( port, CLIENT_TIMEOUT_MS, null, params );
80  } catch ( TTransportException e ) {
81  log.fatal( "Could not listen on port " + port );
82  throw e;
83  }
84  TThreadPoolServer.Args args = new TThreadPoolServer.Args( serverTransport );
85  args.protocolFactory( protFactory );
86  args.processor( processor );
87  args.minWorkerThreads( 4 ).maxWorkerThreads( 256 );
88  args.stopTimeoutVal( 2 ).stopTimeoutUnit( TimeUnit.MINUTES );
89  args.transportFactory( new TFramedTransport.Factory( MAX_MSG_LEN ) );
90  return new TThreadPoolServer( args );
91  }
92 
100  public TServer initNormal( int port ) throws TTransportException
101  {
102  final TNonblockingServerTransport serverTransport;
103  try {
104  serverTransport = new TNonblockingServerSocket( port );
105  } catch ( TTransportException e ) {
106  log.fatal( "Could not listen on port " + port );
107  throw e;
108  }
109  THsHaServer.Args args = new THsHaServer.Args( serverTransport );
110  args.protocolFactory( protFactory );
111  args.processor( processor );
112  args.minWorkerThreads( 2 ).maxWorkerThreads( 6 );
113  args.maxReadBufferBytes = MAX_MSG_LEN;
114  return new THsHaServer( args );
115  }
116 
117 }
TServer initSecure(int port)
Listen with TLS wrapping - has to use the thread pool server, since encrypted servers cannot use nonb...
static final int CLIENT_TIMEOUT_MS
How long a client connection can be idle before we close it to free up resources. ...
Class to hold global constants and properties from 'config/global.properties'.
Definition: Globals.java:16
static String getSslKeystoreFile()
Definition: Globals.java:139
TServer initNormal(int port)
Create normal plain server, no encryption.
static String getSslKeystorePassword()
Definition: Globals.java:149
final MasterServer.Processor< MasterServerHandler > processor