001
002package tech.aroma.banana.authentication.service;
003
004/*
005 * Copyright 2015 Aroma Tech.
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020
021
022import com.google.inject.Guice;
023import com.google.inject.Injector;
024import java.net.SocketException;
025import org.apache.thrift.protocol.TBinaryProtocol;
026import org.apache.thrift.server.TThreadPoolServer;
027import org.apache.thrift.transport.TServerSocket;
028import org.apache.thrift.transport.TTransportException;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import tech.aroma.banana.authentication.service.data.AuthenticationDataModule;
032import tech.aroma.banana.authentication.service.operations.AuthenticationOperationsModule;
033import tech.aroma.banana.thrift.authentication.service.AuthenticationService;
034import tech.aroma.banana.thrift.authentication.service.AuthenticationServiceConstants;
035import tech.sirwellington.alchemy.annotations.access.Internal;
036
037import static java.util.concurrent.TimeUnit.SECONDS;
038
039/**
040 * This Main Class runs the Authentication Service on a Server Socket.
041 * 
042 * @author SirWellington
043 */
044@Internal
045public final class TcpServer
046{
047
048    private final static Logger LOG = LoggerFactory.getLogger(TcpServer.class);
049    private static final int PORT = AuthenticationServiceConstants.SERVICE_PORT;
050
051    public static void main(String[] args) throws TTransportException, SocketException
052    {
053        Injector injector = Guice.createInjector(new AuthenticationDataModule(),
054                                                 new AuthenticationOperationsModule(),
055                                                 new AuthenticationServiceModule());
056
057        AuthenticationService.Iface bananaService = injector.getInstance(AuthenticationService.Iface.class);
058        AuthenticationService.Processor processor = new AuthenticationService.Processor<>(bananaService);
059
060        TServerSocket socket = new TServerSocket(PORT);
061        socket.getServerSocket().setSoTimeout((int) SECONDS.toMillis(30));
062
063        TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(socket)
064            .protocolFactory(new TBinaryProtocol.Factory())
065            .processor(processor)
066            .requestTimeout(60)
067            .requestTimeoutUnit(SECONDS)
068            .minWorkerThreads(5)
069            .maxWorkerThreads(100);
070        
071        LOG.info("Starting Authentication Service at port {}", PORT);
072        
073        TThreadPoolServer server = new TThreadPoolServer(serverArgs);
074        server.serve();
075        server.stop();
076    }
077}