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

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

package jizcode.netty.server;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import jizcode.netty.contract.RequestStatus;
import jizcode.netty.contract.RtDataFromServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ClientRequest {
    protected Log logger = LogFactory.getLog(this.getClass());
    private int cmd;
    private String md5;
    private long uniqueId;
    private RequestStatus status;
    private String json;
    private boolean isCompleted;
    private CountDownLatch countDownLatch;

    public ClientRequest(RtDataFromServer rtDataFromServer) {
        this.setMd5(rtDataFromServer.getMd5());
        this.setCmd(rtDataFromServer.getCmd());
        this.setUniqueId(rtDataFromServer.getUniqueId());
        isCompleted = false;
    }

    /**
     * @return the uniqueId
     */
    public long getUniqueId() {
        return uniqueId;
    }

    /**
     * @param uniqueId the uniqueId to set
     */
    public void setUniqueId(long uniqueId) {
        this.uniqueId = uniqueId;
    }

    /**
     * @return the md5
     */
    public String getMd5() {
        return md5;
    }

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

    public RequestStatus getStatus() {
        return this.status;
    }

    public void setStatus(RequestStatus status){
        this.status = status;
    }

    /**
     * @return the cmd
     */
    public int getCmd() {
        return cmd;
    }

    /**
     * @param cmd the cmd to set
     */
    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public static ClientRequest NotFound(RtDataFromServer dataFromServer){
        ClientRequest request = new ClientRequest(dataFromServer);
        request.setStatus(RequestStatus.NotFound);
        request.isCompleted = true;
        return request;
    }

    public synchronized void response(RequestStatus status, String json){
        if(this.isCompleted == false){
            this.json = json;
            this.status = status;
            this.isCompleted = true;
            this.countDownLatch.countDown();
            ////logger.info("找到客户端clientRequest.countDownLatch.countDown()");
        }
    }

    public String getJson(){
        return this.json;
    }
    public boolean getIsCompleted(){
        return this.isCompleted;
    }

    public void begin(){
        countDownLatch = new CountDownLatch(1);
    }

    /**
     * 默认等待30秒钟
     * @throws InterruptedException
     */
    public void await() throws InterruptedException {
        this.await(30);
    }

    public void await(long timeoutSeconds) throws InterruptedException {
        if(this.isCompleted){
            return;
        }
        this.countDownLatch.await(timeoutSeconds, TimeUnit.SECONDS);
        if(this.isCompleted == false){
            this.status = RequestStatus.NotFound;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy