io.flood.rpc.FloodServer.java Source code

Java tutorial

Introduction

Here is the source code for io.flood.rpc.FloodServer.java

Source

/*
 * Copyright 2015 The RPC Project
 *
 * The RPC Project 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 io.flood.rpc;

import io.flood.common.Resource;
import io.flood.rpc.network.AcceptorImpl;
import com.google.protobuf.BlockingService;
import com.google.protobuf.Service;
import io.flood.rpc.registry.ResourceRegistry;
import io.netty.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

import static io.flood.common.utils.LogHelper.format;

public class FloodServer extends AcceptorImpl {

    private final FloodDispatcher dispatcher;
    private static final Logger LOG = LoggerFactory.getLogger(FloodServer.class);
    private volatile boolean bindCompleted = false;
    private ResourceRegistry resourceRegistry;

    FloodServer(ResourceRegistry resourceRegistry, FloodDispatcher dispatcher) {
        super(dispatcher);
        this.resourceRegistry = resourceRegistry;
        this.dispatcher = dispatcher;
    }

    @Override
    public void onBindCompleted(Future<? super Void> completefuture) {
        if (completefuture.isSuccess()) {
            bindCompleted = true;
            Map<String, Service> serviceMap = dispatcher.getServices();
            for (Map.Entry<String, Service> entry : serviceMap.entrySet()) {
                try {
                    registServiceToRemote(entry.getValue());
                } catch (Exception e) {
                    LOG.warn(format(getId(), "buid service instance faild({})",
                            entry.getValue().getDescriptorForType().getFullName()));
                }
            }

            Map<String, BlockingService> blockingServiceMap = dispatcher.getBlockingServices();
            for (Map.Entry<String, BlockingService> entry : blockingServiceMap.entrySet()) {
                try {
                    registServiceToRemote(entry.getValue());
                } catch (Exception e) {
                    LOG.warn(format(getId(), "buid service instance faild({})",
                            entry.getValue().getDescriptorForType().getFullName()));
                }
            }

        } else {
            LOG.error(format(getId(), "start rpc server faild({})", completefuture.cause().getMessage()));
        }
    }

    public void registService(Service service) throws Exception {
        if (bindCompleted) {
            dispatcher.registService(service);
            registServiceToRemote(service);
        } else {
            dispatcher.registService(service);
        }
    }

    public void registService(BlockingService service) throws Exception {
        if (bindCompleted) {
            dispatcher.registService(service);
            registServiceToRemote(service);
        } else {
            dispatcher.registService(service);
        }
    }

    private void registServiceToRemote(Service service) throws Exception {
        Resource resource = Resource.newBuilder().setPort(getPort())
                .setName(service.getDescriptorForType().getFullName()).setHost(getHost())
                .setCreateTime(System.currentTimeMillis()).build();
        dispatcher.registService(service);
        resourceRegistry.register(resource);

        //dispatcher.registService(service);
        //        ServiceInstanceBuilder<FloodInfo> builder = ServiceInstance.builder();
        //        builder.name(service.getDescriptorForType().getFullName());
        //        builder.serviceType(ServiceType.DYNAMIC);
        //        builder.port(getPort());
        //        builder.address(getHost());
        //        FloodInfo info=new FloodInfo();
        //        info.setWeight(getWeigth());
        //        builder.payload(info);
        //        ServiceInstance<FloodInfo> inst= builder.build();
        //        registry.registerService(inst);
        //        LOG.info(format(getId(),"regist service to zookeeper({},{}:{}:{}:{})",inst.getId(),getHost(),getPort(),getWeigth(),inst.getName()));
    }

    private void registServiceToRemote(BlockingService service) throws Exception {

        Resource resource = Resource.newBuilder().setPort(getPort())
                .setName(service.getDescriptorForType().getFullName()).setHost(getHost())
                .setCreateTime(System.currentTimeMillis()).build();
        dispatcher.registService(service);
        resourceRegistry.register(resource);

        //       // dispatcher.registService(service);
        //        ServiceInstanceBuilder<FloodInfo> builder = ServiceInstance.builder();
        //        builder.name(service.getDescriptorForType().getFullName());
        //        builder.serviceType(ServiceType.DYNAMIC);
        //        builder.port(getPort());
        //        builder.address(getHost());
        //        FloodInfo info=new FloodInfo();
        //        info.setWeight(getWeigth());
        //        builder.payload(info);
        //        ServiceInstance<FloodInfo> inst= builder.build();
        //        registry.registerService(inst);
        //        LOG.info(format(getId(),"regist service to zookeeper({},{}:{}:{}:{})",inst.getId(),getHost(),getPort(),getWeigth(),inst.getName()));
    }

}