All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jizcode.netty.server.Client Maven / Gradle / Ivy

package jizcode.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import jizcode.base.util.JsonUtils;
import jizcode.netty.contract.RequestStatus;
import jizcode.netty.contract.RtDataFromClient;
import jizcode.netty.contract.RtDataFromServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.net.InetSocketAddress;
import java.util.Date;

public class Client {
    protected Log logger = LogFactory.getLog(this.getClass());
    private String id;

    private Channel channel;

    private Date latestActiveTime;

    /**
     * 获取客户端最后一次活跃时间
     * @return
     */
    public Date getLatestActiveTime(){
        return latestActiveTime;
    }

    /**
     * 判断客户端是否是激活状态
     * @return
     */
    public boolean isActived(){
        if(this.channel!=null && this.channel.isActive() && this.channel.isOpen()){
            return true;
        }
        return false;
    }

    public Client(String id, Channel channel) {
        this.id = id;
        this.channel = channel;
        this.latestActiveTime = new Date();
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the channel
     */
    public Channel getChannel() {
        return channel;
    }

    /**
     * 获取客户端ip
     * @return
     */
    public String getIp() {
        InetSocketAddress inetSocketAddress = (InetSocketAddress) this.channel.remoteAddress();
        if (inetSocketAddress == null) {
            return "0.0.0.0";
        }
        return inetSocketAddress.getAddress().getHostAddress();
    }

    /**
     * @param channel the channel to set
     */
    public void setChannel(Channel channel) {
        this.channel = channel;
        this.latestActiveTime = new Date();
    }

    private ClientRequestCollection clientRequestCollection = new ClientRequestCollection();

    public synchronized ClientRequest request(RtDataFromServer data) {
        data.oneway = false;
        ClientRequest clientRequest = null;
        clientRequest = clientRequestCollection.findRequestAndRemoveCompleted(data);
        if (clientRequest != null) {
            return clientRequest;
        }
        clientRequest = new ClientRequest(data);
        clientRequestCollection.addRequest(clientRequest);
        clientRequest.begin();
        //logger.info("client request:" + data.getUniqueId());

        String msg = JsonUtils.tryToJson(data);
        byte[] bytes = String.format("%s%s", msg,System.getProperty("line.separator")).getBytes();
        ByteBuf byteBuf = Unpooled.buffer(bytes.length);
        byteBuf.writeBytes(bytes);
        this.channel.writeAndFlush(byteBuf);
        this.latestActiveTime = new Date();
        return clientRequest;
    }

    public void requestOnway(RtDataFromServer data) {
        data.oneway = true;
        //logger.info("client request:" + data.getUniqueId());

        String msg = JsonUtils.tryToJson(data);
        byte[] bytes = String.format("%s%s", msg,System.getProperty("line.separator")).getBytes();
        ByteBuf byteBuf = Unpooled.buffer(bytes.length);
        byteBuf.writeBytes(bytes);
        this.channel.writeAndFlush(byteBuf);
        this.latestActiveTime = new Date();
    }

    public boolean response(RtDataFromClient data) {
        ClientRequest clientRequest = clientRequestCollection.findRequest(data);
        if (clientRequest == null) {
            //logger.info("data is time out client id:" + data.getOuCode());
            return false;
        } else {
            //logger.info("找到客户端clientRequest");
            this.latestActiveTime = new Date();
            clientRequest.response(RequestStatus.OK, data.getJson());
            return true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy