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

io.higgs.events.EventServer Maven / Gradle / Ivy

The newest version!
package io.higgs.events;

import io.higgs.core.HiggsServer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalChannel;
import io.netty.channel.local.LocalEventLoopGroup;
import io.netty.channel.local.LocalServerChannel;

/**
 * @author Courtney Robinson 
 */
public class EventServer extends HiggsServer {
    protected final LocalAddress address;

    public EventServer(LocalAddress address) {
        this.address = address;
        bossGroup = new LocalEventLoopGroup();
    }

    /**
     * Start the server causing it to bind to the provided {@link #port}
     *
     * @throws UnsupportedOperationException if the server's already started
     */
    public void start() {
        if (channel != null) {
            throw new UnsupportedOperationException("Server already started");
        }
        try {
            bootstrap
                    .group(bossGroup)
                    .channel(LocalServerChannel.class)
                    .handler(new ChannelInitializer() {
                        public void initChannel(LocalServerChannel ch) throws Exception {
                            // ch.pipeline().addLast(new LoggingHandler(LogLevel.ERROR));
                        }
                    })
                    .childHandler(new ChannelInitializer() {
                        @Override
                        public void initChannel(LocalChannel ch) throws Exception {
                            ch.pipeline().addLast(new EventHandler(methods));
                        }
                    });

            // Bind and start to accept incoming connections.
            channel = bootstrap.bind(address).sync().channel();
        } catch (Throwable t) {
            log.warn("Error starting server", t);
        }
    }

    public  void registerMethod(FunctionEventMethod method) {
        methods.add(method);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy