⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientnet.java

📁 短信开发用于文件交换处理转发的类模块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.pub.backserver.net;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.pub.backserver.EntityHead;
import com.pub.backserver.blackList.BlackEntity;
import com.pub.backserver.order.OrderEntity;
import com.pub.backserver.phase.PhaseEntity;

/**
 * 切记:单线程使用
 */
public class ClientNet {

    private static Logger log = Logger.getLogger(ClientNet.class);

    public static final int ERR_NOTCONNECTED = -101;

    public static final int ERR_TIMEOUT = -102;

    public static final int ERR_EXCEPTION = -103;

    public static final int ERR_UNKNOWNEVENT = -104;

    public static final int ERR_CONNECTFAIL = -105;

    public static final int ERR_RECVFAIL = -106;

    public static final int ERR_SERVERFAIL = -1;

    public static final int ERR_NOTFOUND = -3;

    private BlackEntity _be = new BlackEntity();

    private PhaseEntity _pe = new PhaseEntity();

    private OrderEntity _oe = new OrderEntity();

    private InetSocketAddress socketAddress = null;

    private int connectTimeout = 2000; // ms

    private int request(EntityHead entity) throws Exception {
        int ret = ERR_CONNECTFAIL;
        innerNioSocket _socket = Connect();
        if (_socket != null) {
            ret = _socket.queryEntity(entity, this.connectTimeout);
            Disconnect(_socket);
        }
        if (ret < -100) { // try again
            _socket = Connect();
            if (_socket != null) {
                ret = _socket.queryEntity(entity, this.connectTimeout);
                Disconnect(_socket);
            }
        }
        return ret;
    }

    // BlackList process
    public int queryBlackList(String mdn) throws Exception {
        _be.setPhone(mdn);
        _be.setCommandID(EntityHead.QUERY_BLACK);
        int ret = request(_be);
        ret = ret == 0 ? _be.getType() : ret;
        return ret;
    }

    public int queryBlackList(BlackEntity be) throws Exception {
        be.setCommandID(EntityHead.QUERY_BLACK);
        int ret = request(be);
        ret = ret == 0 ? be.getType() : ret;
        return ret;
    }

    public int insertBlackList(String mdn) throws Exception {
        _be.setPhone(mdn);
        _be.setCommandID(EntityHead.INSERT_BLACK);
        int ret = request(_be);
        ret = ret == 0 ? _be.getType() : ret;
        return ret;
    }

    public int insertBlackList(BlackEntity be) throws Exception {
        be.setCommandID(EntityHead.INSERT_BLACK);
        int ret = request(be);
        ret = ret == 0 ? be.getType() : ret;
        return ret;
    }

    public int deleteBlackList(String mdn) throws Exception {
        _be.setPhone(mdn);
        _be.setCommandID(EntityHead.DELETE_BLACK);
        int ret = request(_be);
        ret = ret == 0 ? _be.getType() : ret;
        return ret;
    }

    public int deleteBlackList(BlackEntity be) throws Exception {
        be.setCommandID(EntityHead.DELETE_BLACK);
        int ret = request(be);
        ret = ret == 0 ? be.getType() : ret;
        return ret;
    }

    // Phase process
    public int queryPhaseList(String mdn) throws Exception {
        _pe.setStrPhase(mdn);
        _pe.setCommandID(EntityHead.QUERY_PHASE);
        int ret = request(_pe);
        ret = ret == 0 ? _pe.getProvID() : ret;
        return ret;
    }

    public int queryPhaseList(PhaseEntity pe) throws Exception {
        pe.setCommandID(EntityHead.QUERY_PHASE);
        int ret = request(pe);
        ret = ret == 0 ? pe.getProvID() : ret;
        return ret;
    }

    public int insertPhaseList(String mdn) throws Exception {
        _pe.setStrPhase(mdn);
        _pe.setCommandID(EntityHead.INSERT_PHASE);
        int ret = request(_pe);
        ret = ret == 0 ? _pe.getProvID() : ret;
        return ret;
    }

    public int insertPhaseList(PhaseEntity pe) throws Exception {
        pe.setCommandID(EntityHead.INSERT_PHASE);
        int ret = request(pe);
        ret = ret == 0 ? pe.getProvID() : ret;
        return ret;
    }

    public int deletePhaseList(String mdn) throws Exception {
        _pe.setStrPhase(mdn);
        _pe.setCommandID(EntityHead.DELETE_PHASE);
        int ret = request(_pe);
        ret = ret == 0 ? _pe.getProvID() : ret;
        return ret;
    }

    public int deletePhaseList(PhaseEntity pe) throws Exception {
        pe.setCommandID(EntityHead.DELETE_PHASE);
        int ret = request(pe);
        ret = ret == 0 ? pe.getProvID() : ret;
        return ret;
    }

    public int queryPhaseList(String mobile, int gwId, int serviceCodeId)
            throws Exception {
        _oe.setMobile(mobile);
        _oe.setGwId(gwId);
        _oe.setServiceCodeId(serviceCodeId);
        _oe.setCommandID(EntityHead.QUERY_ORDER);
        int ret = request(_pe);
        ret = ret == 0 ? _pe.getProvID() : ret;
        return ret;
    }

    // Order Process
    public int queryOrderList(OrderEntity oe) throws Exception {
        oe.setCommandID(EntityHead.QUERY_ORDER);
        int ret = request(oe);
        ret = ret == 0 ? oe.getProdId() : ret;
        return ret;
    }

    public int insertOrderList(String mobile, int prodId, int serviceCodeId)
            throws Exception {
        _oe.setMobile(mobile);
        _oe.setProdId(prodId);
        _oe.setServiceCodeId(serviceCodeId);
        _oe.setCommandID(EntityHead.INSERT_ORDER);
        int ret = request(_oe);
        ret = ret == 0 ? _oe.getProdId() : ret;
        return ret;
    }

    public int insertOrderList(OrderEntity oe) throws Exception {
        oe.setCommandID(EntityHead.INSERT_ORDER);
        int ret = request(oe);
        ret = ret == 0 ? oe.getProdId() : ret;
        return ret;
    }

    public int deleteOrderList(String mobile, int gwId, int serviceCode)
            throws Exception {
        _oe.setMobile(mobile);
        _oe.setGwId(gwId);
        _oe.setServiceCodeId(serviceCode);
        _oe.setCommandID(EntityHead.DELETE_ORDER);
        int ret = request(_oe);
        ret = ret == 0 ? _oe.getProdId() : ret;
        return ret;
    }

    public int deleteOrderList(OrderEntity oe) throws Exception {
        oe.setCommandID(EntityHead.DELETE_ORDER);
        int ret = request(oe);
        ret = ret == 0 ? oe.getProdId() : ret;
        return ret;
    }

    public int queryOrderList(String mobile, int prodId, int serviceCodeId)
            throws Exception {
        _oe.setCommandID(EntityHead.QUERY_ORDER);
        _oe.setMobile(mobile);
        _oe.setProdId(prodId);
        _oe.setServiceCodeId(serviceCodeId);
        _oe.setCommandID(EntityHead.QUERY_ORDER);
//        log.debug(_oe);
        int ret = request(_oe);
        ret = ret == 0 ? _oe.getProdId() : ret;
        return ret;
    }

    class innerNioSocket {
        private Selector selector = null;

        private SelectionKey sk = null;

        private boolean inPool = true;

        private boolean using = false;

        private ByteBuffer buffer = ByteBuffer.allocate(0x100);

        public innerNioSocket() {
            // socketAddress = new InetSocketAddress(ip, (port & 0xffff));
            try {
                selector = Selector.open();
            } catch (Throwable ex) {
                if (log.isEnabledFor(Priority.ERROR)) {
                    log.error(null, ex);
                }
                selector = null;
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -