com.spotify.heroic.consumer.collectd.Server.java Source code

Java tutorial

Introduction

Here is the source code for com.spotify.heroic.consumer.collectd.Server.java

Source

/*
 * Copyright (c) 2015 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.spotify.heroic.consumer.collectd;

import eu.toolchain.async.AsyncFramework;
import eu.toolchain.async.AsyncFuture;
import eu.toolchain.async.ResolvableFuture;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.Data;

import java.net.InetAddress;

@Data
public class Server {
    private final AsyncFramework async;
    private final Channel channel;

    public AsyncFuture<Void> start() {
        return async.resolved();
    }

    public AsyncFuture<Void> shutdown() {
        return async.resolved();
    }

    public static AsyncFuture<Server> setup(final AsyncFramework async, final CollectdChannelHandler handler,
            final InetAddress host, final int port) {
        final EventLoopGroup group = new NioEventLoopGroup();
        final Bootstrap b = new Bootstrap();

        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(handler);

        final ResolvableFuture<Server> future = async.future();

        b.bind(host, port).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(final ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                    future.resolve(new Server(async, f.channel()));
                } else {
                    future.fail(f.cause() != null ? f.cause() : new RuntimeException("Failed to bind"));
                }
            }
        });

        return future;
    }
}