com.mpush.core.server.GatewayUDPConnector.java Source code

Java tutorial

Introduction

Here is the source code for com.mpush.core.server.GatewayUDPConnector.java

Source

/*
 * (C) Copyright 2015-2016 the original author or 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.
 *
 * Contributors:
 *   ohun@live.cn ()
 */

package com.mpush.core.server;

import com.mpush.api.connection.Connection;
import com.mpush.api.protocol.Command;
import com.mpush.common.MessageDispatcher;
import com.mpush.core.handler.GatewayKickUserHandler;
import com.mpush.core.handler.GatewayPushHandler;
import com.mpush.netty.udp.UDPChannelHandler;
import com.mpush.netty.udp.NettyUDPConnector;
import com.mpush.tools.Utils;
import com.mpush.tools.config.CC;
import com.mpush.tools.config.CC.mp.net.rcv_buf;
import com.mpush.tools.config.CC.mp.net.snd_buf;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption;

import static com.mpush.common.MessageDispatcher.POLICY_LOG;

/**
 * Created by ohun on 2015/12/30.
 *
 * @author ohun@live.cn
 */
public final class GatewayUDPConnector extends NettyUDPConnector {

    private static GatewayUDPConnector I;

    private UDPChannelHandler channelHandler;

    public static GatewayUDPConnector I() {
        if (I == null) {
            synchronized (GatewayUDPConnector.class) {
                if (I == null) {
                    I = new GatewayUDPConnector();
                }
            }
        }
        return I;
    }

    private GatewayUDPConnector() {
        super(CC.mp.net.gateway_server_port);
    }

    @Override
    public void init() {
        super.init();
        MessageDispatcher receiver = new MessageDispatcher(POLICY_LOG);
        receiver.register(Command.GATEWAY_PUSH, new GatewayPushHandler());
        receiver.register(Command.GATEWAY_KICK, new GatewayKickUserHandler());
        channelHandler = new UDPChannelHandler(receiver);
        channelHandler.setMulticastAddress(Utils.getInetAddress(CC.mp.net.gateway_server_multicast));
        channelHandler.setNetworkInterface(Utils.getLocalNetworkInterface());
    }

    @Override
    protected void initOptions(Bootstrap b) {
        super.initOptions(b);
        b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, true);//?????IP???IP_MULTICAST_LOOP????
        b.option(ChannelOption.IP_MULTICAST_TTL, 255);//IP_MULTICAST_TTL?TTL0255
        //b.option(ChannelOption.IP_MULTICAST_IF, null);//IP_MULTICAST_IF???????,?addr?IP?INADDR_ANY???
        //b.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 1024 * 1024));
        if (snd_buf.gateway_server > 0)
            b.option(ChannelOption.SO_SNDBUF, snd_buf.gateway_server);
        if (rcv_buf.gateway_server > 0)
            b.option(ChannelOption.SO_RCVBUF, rcv_buf.gateway_server);
    }

    @Override
    public ChannelHandler getChannelHandler() {
        return channelHandler;
    }

    public Connection getConnection() {
        return channelHandler.getConnection();
    }
}