All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.net.tcp.SimpleTcpServer Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp;

import com.firefly.net.SecureSessionFactory;
import com.firefly.net.Server;
import com.firefly.net.Session;
import com.firefly.net.tcp.aio.AsynchronousTcpServer;
import com.firefly.utils.function.Action1;
import com.firefly.utils.function.Action2;
import com.firefly.utils.lang.AbstractLifeCycle;

public class SimpleTcpServer extends AbstractLifeCycle {

    // accept TCP connection callback
    private Action1 accept;
    private Action2 failedAcceptance;

    private Server server;
    private TcpServerConfiguration config;

    public SimpleTcpServer() {
        this(new TcpServerConfiguration());
    }

    public SimpleTcpServer(TcpServerConfiguration config) {
        this(new AsynchronousTcpServer(config));
        this.config = config;
    }

    public SimpleTcpServer(Server server) {
        this.server = server;
    }

    public SimpleTcpServer accept(Action1 accept) {
        this.accept = accept;
        return this;
    }

    public SimpleTcpServer accept(Action1 accept, Action2 failed) {
        this.accept = accept;
        this.failedAcceptance = failed;
        return this;
    }

    public void listen(String host, int port) {
        config.setHost(host);
        config.setPort(port);
        start();
    }

    public abstract class AbstractHandler extends AbstractSimpleHandler {

        @Override
        public void failedAcceptingSession(Integer sessionId, Throwable t) throws Throwable {
            if (failedAcceptance != null) {
                failedAcceptance.call(sessionId, t);
            }
        }

    }

    @Override
    protected void init() {
        if (!config.isSecureConnectionEnabled()) {
            config.setDecoder(AbstractSimpleHandler.decoder);
            config.setHandler(new AbstractHandler() {

                @Override
                public void sessionOpened(Session session) throws Throwable {
                    TcpConnectionImpl c = new TcpConnectionImpl(session);
                    session.attachObject(c);
                    if (accept != null) {
                        accept.call(c);
                    }
                }

            });
        } else {
            config.setDecoder(AbstractSimpleHandler.sslDecoder);
            config.setHandler(new AbstractHandler() {

                @Override
                public void sessionOpened(Session session) throws Throwable {
                    SecureSessionFactory factory = config.getSecureSessionFactory();
                    session.attachObject(new SecureTcpConnectionImpl(session, factory.create(session, false, ssl -> {
                        Object o = session.getAttachment();
                        if (o != null && o instanceof SecureTcpConnectionImpl) {
                            SecureTcpConnectionImpl c = (SecureTcpConnectionImpl) o;
                            if (accept != null) {
                                accept.call(c);
                            }
                        }
                    })));
                }

            });
        }
        server.listen(config.getHost(), config.getPort());
    }

    @Override
    protected void destroy() {
        server.stop();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy