package io.vertx.core.net.impl;
import io.netty.channel.EventLoop;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
public class HandlerManager<T> {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(HandlerManager.class);
private final VertxEventLoopGroup availableWorkers;
private final ConcurrentMap<EventLoop, Handlers<T>> handlerMap = new ConcurrentHashMap<>();
private volatile boolean hasHandlers;
public HandlerManager(VertxEventLoopGroup availableWorkers) {
this.availableWorkers = availableWorkers;
}
public boolean hasHandlers() {
return hasHandlers;
}
public synchronized List<T> handlers() {
return handlerMap.values().stream()
.flatMap(handlers -> handlers.list.stream())
.map(holder -> holder.handler)
.collect(Collectors.toList());
}
public HandlerHolder<T> chooseHandler(EventLoop worker) {
Handlers<T> handlers = handlerMap.get(worker);
return handlers == null ? null : handlers.chooseHandler();
}
public synchronized void addHandler(T handler, ContextInternal context) {
EventLoop worker = context.nettyEventLoop();
availableWorkers.addWorker(worker);
Handlers<T> handlers = new Handlers<>();
Handlers<T> prev = handlerMap.putIfAbsent(worker, handlers);
if (prev != null) {
handlers = prev;
}
handlers.addHandler(new HandlerHolder<>(context, handler));
hasHandlers = true;
}
public synchronized void removeHandler(T handler, ContextInternal context) {
EventLoop worker = context.nettyEventLoop();
Handlers<T> handlers = handlerMap.get(worker);
if (!handlers.removeHandler(new HandlerHolder<>(context, handler))) {
throw new IllegalStateException("Can't find handler");
}
if (handlers.isEmpty()) {
handlerMap.remove(worker);
}
if (handlerMap.isEmpty()) {
hasHandlers = false;
}
availableWorkers.removeWorker(worker);
}
private static final class Handlers<T> {
private int pos;
private final List<HandlerHolder<T>> list = new CopyOnWriteArrayList<>();
HandlerHolder<T> chooseHandler() {
HandlerHolder<T> handler = list.get(pos);
pos++;
checkPos();
return handler;
}
void addHandler(HandlerHolder<T> handler) {
list.add(handler);
}
boolean removeHandler(HandlerHolder<T> handler) {
if (list.remove(handler)) {
checkPos();
return true;
} else {
return false;
}
}
boolean isEmpty() {
return list.isEmpty();
}
void checkPos() {
if (pos == list.size()) {
pos = 0;
}
}
}
}