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

com.hibegin.http.server.handler.CheckRequestRunnable Maven / Gradle / Ivy

Go to download

Simple, flexible, less dependent, more extended. Less memory footprint, can quickly build Web project. Can quickly run embedded, Android devices

There is a newer version: 0.3.162
Show newest version
package com.hibegin.http.server.handler;

import com.hibegin.http.server.api.HttpRequestDeCoder;
import com.hibegin.http.server.config.ServerConfig;

import java.io.IOException;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

public class CheckRequestRunnable implements Runnable {

    private final Map channelHttpRequestHandlerThreadMap;
    private final ServerConfig serverConfig;
    private final Map httpDeCoderMap;


    public CheckRequestRunnable(ServerConfig serverConfig, Map httpDeCoderMap) {
        this.channelHttpRequestHandlerThreadMap = new ConcurrentHashMap<>();
        this.serverConfig  = serverConfig;
        this.httpDeCoderMap = httpDeCoderMap;
    }

    private int getRequestTimeout() {
        return serverConfig.getTimeout();
    }

    @Override
    public void run() {
        clearRequestListener(getClosedRequestSocketSet());
        clearRequestDecode(getClosedDecodedSocketSet());
    }

    private void clearRequestListener(Set removeHttpRequestList) {
        for (SocketChannel socket : removeHttpRequestList) {
            try {
                socket.socket().close();
                socket.close();
            } catch (IOException e) {
                //ignore
            }
            if (channelHttpRequestHandlerThreadMap.get(socket) != null) {
                channelHttpRequestHandlerThreadMap.get(socket).close();
                channelHttpRequestHandlerThreadMap.remove(socket);
            }
        }
    }

    private Set getClosedRequestSocketSet() {
        Set socketChannels = new CopyOnWriteArraySet<>();
        for (Map.Entry entry : channelHttpRequestHandlerThreadMap.entrySet()) {
            SocketChannel socketChannel = entry.getKey();
            if (!socketChannel.isOpen()) {
                socketChannels.add(entry.getKey());
                continue;
            }
            if (getRequestTimeout() > 0) {
                if (System.currentTimeMillis() - entry.getValue().getRequest().getCreateTime() > getRequestTimeout() * 1000L) {
                    entry.getValue().getResponse().renderCode(504);
                    socketChannels.add(entry.getKey());
                }
            }
        }
        return socketChannels;
    }

    private Set getClosedDecodedSocketSet() {
        Set removeHttpRequestList = new CopyOnWriteArraySet<>();
        for (Map.Entry entry : httpDeCoderMap.entrySet()) {
            Socket socket = entry.getKey();
            if (socket.isClosed() || !socket.isConnected()) {
                removeHttpRequestList.add(socket);
            }
        }
        return removeHttpRequestList;
    }

    private void clearRequestDecode(Set removeHttpRequestList) {
        for (Socket socket : removeHttpRequestList) {
            httpDeCoderMap.remove(socket);
        }
    }

    public Map getChannelHttpRequestHandlerThreadMap() {
        return channelHttpRequestHandlerThreadMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy