io.github.vastframework.pubsub.PubSubClient.java Source code

Java tutorial

Introduction

Here is the source code for io.github.vastframework.pubsub.PubSubClient.java

Source

/*
 * Copyright 2016 The vast-pubsub authors
 *
 * Licensed 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 io.github.vastframework.pubsub;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.function.Consumer;

public class PubSubClient implements PubSub {

    private EventLoopGroup eventLoopGroup;
    private String host;
    private int port;

    public PubSubClient(String host, int port) {
        this.port = port;
        this.host = host;
    }

    public void connect(String apiKey) {
        Bootstrap bootstrap = new Bootstrap();
        Class<? extends Channel> channelClazz;

        if (Epoll.isAvailable()) {
            channelClazz = EpollSocketChannel.class;
            eventLoopGroup = new EpollEventLoopGroup();
        } else {
            channelClazz = NioSocketChannel.class;
            eventLoopGroup = new NioEventLoopGroup();
        }

        bootstrap.group(eventLoopGroup).channel(channelClazz).option(ChannelOption.SO_KEEPALIVE, true)
                // TODO: add function to get data class by topic and add handler
                .remoteAddress(host, port).connect();
    }

    @Override
    public <T> void subscribe(String topic, Class<T> responseClass, Consumer<T> value) {

    }

    @Override
    public void unsubscribe(String topic) {

    }

    @Override
    public void publish(String topic, Object data) {

    }
}