This commit is contained in:
@@ -8,8 +8,10 @@ package org.redkale.net;
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -21,12 +23,18 @@ import java.util.logging.*;
|
||||
* @param <R> Request的子类型
|
||||
* @param <P> Response的子类型
|
||||
*/
|
||||
public abstract class PrepareServlet<C extends Context, R extends Request<C>, P extends Response<C, R>> extends Servlet<C, R, P> {
|
||||
public abstract class PrepareServlet<K extends Serializable, C extends Context, R extends Request<C>, P extends Response<C, R>> extends Servlet<C, R, P> {
|
||||
|
||||
protected final AtomicLong executeCounter = new AtomicLong(); //执行请求次数
|
||||
|
||||
protected final AtomicLong illRequestCounter = new AtomicLong(); //错误请求次数
|
||||
|
||||
protected final List<Servlet<C, R, P>> servlets = new ArrayList<>();
|
||||
|
||||
protected final Map<K, Servlet<C, R, P>> mappings = new HashMap<>();
|
||||
|
||||
public abstract <S extends Servlet<C, R, P>> void addServlet(S servlet, Object attachment, AnyValue conf, K... mappings);
|
||||
|
||||
public final void prepare(final ByteBuffer buffer, final R request, final P response) throws IOException {
|
||||
executeCounter.incrementAndGet();
|
||||
final int rs = request.readHeader(buffer);
|
||||
@@ -74,4 +82,11 @@ public abstract class PrepareServlet<C extends Context, R extends Request<C>, P
|
||||
}
|
||||
}
|
||||
|
||||
protected AnyValue getServletConf(Servlet servlet) {
|
||||
return servlet._conf;
|
||||
}
|
||||
|
||||
protected void setServletConf(Servlet servlet, AnyValue conf) {
|
||||
servlet._conf = conf;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public abstract class Server {
|
||||
public abstract class Server<K extends Serializable, C extends Context, R extends Request<C>, P extends Response<C, R>> {
|
||||
|
||||
public static final String RESNAME_SERVER_ROOT = "SERVER_ROOT";
|
||||
|
||||
@@ -37,7 +37,9 @@ public abstract class Server {
|
||||
|
||||
protected final String protocol;
|
||||
|
||||
protected final PrepareServlet prepare;
|
||||
protected final PrepareServlet<K, C, R, P> prepare;
|
||||
|
||||
protected C context;
|
||||
|
||||
protected AnyValue config;
|
||||
|
||||
@@ -45,8 +47,6 @@ public abstract class Server {
|
||||
|
||||
protected InetSocketAddress address;
|
||||
|
||||
protected Context context;
|
||||
|
||||
protected int backlog;
|
||||
|
||||
protected ProtocolServer serverChannel;
|
||||
@@ -69,7 +69,7 @@ public abstract class Server {
|
||||
|
||||
private ScheduledThreadPoolExecutor scheduler;
|
||||
|
||||
protected Server(long serverStartTime, String protocol, PrepareServlet servlet, final WatchFactory watch) {
|
||||
protected Server(long serverStartTime, String protocol, PrepareServlet<K, C, R, P> servlet, final WatchFactory watch) {
|
||||
this.serverStartTime = serverStartTime;
|
||||
this.protocol = protocol;
|
||||
this.prepare = servlet;
|
||||
@@ -116,6 +116,10 @@ public abstract class Server {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
public <S extends Servlet<C, R, P>> void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
|
||||
this.prepare.addServlet(servlet, attachment, conf, mappings);
|
||||
}
|
||||
|
||||
public void start() throws IOException {
|
||||
this.context = this.createContext();
|
||||
this.prepare.init(this.context, config);
|
||||
@@ -133,7 +137,7 @@ public abstract class Server {
|
||||
+ ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms");
|
||||
}
|
||||
|
||||
protected abstract Context createContext();
|
||||
protected abstract C createContext();
|
||||
|
||||
public void shutdown() throws IOException {
|
||||
long s = System.currentTimeMillis();
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.io.IOException;
|
||||
*/
|
||||
public abstract class Servlet<C extends Context, R extends Request<C>, P extends Response<C, R>> {
|
||||
|
||||
AnyValue _conf; //当前Servlet的配置
|
||||
|
||||
public void init(C context, AnyValue config) {
|
||||
}
|
||||
|
||||
|
||||
@@ -23,11 +23,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRequest, HttpResponse> {
|
||||
|
||||
private final List<HttpServlet> servlets = new ArrayList<>();
|
||||
|
||||
private final Map<String, HttpServlet> strmaps = new HashMap<>();
|
||||
public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse> {
|
||||
|
||||
private SimpleEntry<Predicate<String>, HttpServlet>[] regArray = new SimpleEntry[0];
|
||||
|
||||
@@ -37,11 +33,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
|
||||
public void init(HttpContext context, AnyValue config) {
|
||||
this.servlets.stream().forEach(s -> {
|
||||
if (s instanceof WebSocketServlet) {
|
||||
((WebSocketServlet) s).preInit(context, s._conf);
|
||||
((WebSocketServlet) s).preInit(context, getServletConf(s));
|
||||
} else if (s instanceof BasedHttpServlet) {
|
||||
((BasedHttpServlet) s).preInit(context, s._conf);
|
||||
((BasedHttpServlet) s).preInit(context, getServletConf(s));
|
||||
}
|
||||
s.init(context, s._conf);
|
||||
s.init(context, getServletConf(s));
|
||||
});
|
||||
final WatchFactory watch = context.getWatchFactory();
|
||||
if (watch != null) {
|
||||
@@ -71,7 +67,7 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
|
||||
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
||||
try {
|
||||
final String uri = request.getRequestURI();
|
||||
HttpServlet servlet = this.strmaps.isEmpty() ? null : this.strmaps.get(uri);
|
||||
Servlet<HttpContext, HttpRequest, HttpResponse> servlet = this.mappings.isEmpty() ? null : this.mappings.get(uri);
|
||||
if (servlet == null && this.regArray != null) {
|
||||
for (SimpleEntry<Predicate<String>, HttpServlet> en : regArray) {
|
||||
if (en.getKey().test(uri)) {
|
||||
@@ -88,10 +84,12 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
|
||||
}
|
||||
}
|
||||
|
||||
public void addHttpServlet(HttpServlet servlet, String prefix, AnyValue conf, String... mappings) {
|
||||
@Override
|
||||
public <S extends Servlet<HttpContext, HttpRequest, HttpResponse>> void addServlet(S servlet0, Object prefix, AnyValue conf, String... mappings) {
|
||||
if (prefix == null) prefix = "";
|
||||
HttpServlet servlet = (HttpServlet) servlet0;
|
||||
for (String mapping : mappings) {
|
||||
if (!prefix.isEmpty()) mapping = prefix + mapping;
|
||||
if (!prefix.toString().isEmpty()) mapping = prefix + mapping;
|
||||
if (contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
|
||||
if (mapping.charAt(0) != '^') mapping = '^' + mapping;
|
||||
if (mapping.endsWith("/*")) {
|
||||
@@ -107,11 +105,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
|
||||
regArray[regArray.length - 1] = new SimpleEntry<>(Pattern.compile(mapping).asPredicate(), servlet);
|
||||
}
|
||||
} else if (mapping != null && !mapping.isEmpty()) {
|
||||
strmaps.put(mapping, servlet);
|
||||
this.mappings.put(mapping, servlet);
|
||||
}
|
||||
}
|
||||
servlet._conf = conf;
|
||||
servlet._prefix = prefix == null ? "" : prefix;
|
||||
setServletConf(servlet, conf);
|
||||
servlet._prefix = prefix == null ? "" : prefix.toString();
|
||||
this.servlets.add(servlet);
|
||||
}
|
||||
|
||||
@@ -135,11 +133,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
|
||||
public void destroy(HttpContext context, AnyValue config) {
|
||||
this.resourceHttpServlet.destroy(context, config);
|
||||
this.servlets.stream().forEach(s -> {
|
||||
s.destroy(context, s._conf);
|
||||
s.destroy(context, getServletConf(s));
|
||||
if (s instanceof WebSocketServlet) {
|
||||
((WebSocketServlet) s).postDestroy(context, s._conf);
|
||||
((WebSocketServlet) s).postDestroy(context, getServletConf(s));
|
||||
} else if (s instanceof BasedHttpServlet) {
|
||||
((BasedHttpServlet) s).postDestroy(context, s._conf);
|
||||
((BasedHttpServlet) s).postDestroy(context, getServletConf(s));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class HttpServer extends Server {
|
||||
public final class HttpServer extends Server<String, HttpContext, HttpRequest, HttpResponse> {
|
||||
|
||||
public HttpServer() {
|
||||
this(System.currentTimeMillis(), null);
|
||||
@@ -37,12 +37,12 @@ public final class HttpServer extends Server {
|
||||
}
|
||||
|
||||
public void addHttpServlet(HttpServlet servlet, final String prefix, AnyValue conf, String... mappings) {
|
||||
((HttpPrepareServlet) this.prepare).addHttpServlet(servlet, prefix, conf, mappings);
|
||||
this.prepare.addServlet(servlet, prefix, conf, mappings);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Context createContext() {
|
||||
protected HttpContext createContext() {
|
||||
final int port = this.address.getPort();
|
||||
AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.creatCounter");
|
||||
AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.cycleCounter");
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package org.redkale.net.http;
|
||||
|
||||
import org.redkale.net.Servlet;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -17,8 +16,6 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public abstract class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse> {
|
||||
|
||||
AnyValue _conf; //当前HttpServlet的配置
|
||||
|
||||
String _prefix = ""; //当前HttpServlet的path前缀
|
||||
|
||||
@Override
|
||||
|
||||
@@ -51,8 +51,7 @@ public final class SncpDynServlet extends SncpServlet {
|
||||
|
||||
private Supplier<ByteBuffer> bufferSupplier;
|
||||
|
||||
public SncpDynServlet(final BsonConvert convert, final String serviceName, final Class<? extends Service> type, final Service service, final AnyValue conf) {
|
||||
this.conf = conf;
|
||||
public SncpDynServlet(final BsonConvert convert, final String serviceName, final Class<? extends Service> type, final Service service) {
|
||||
this.serviceName = serviceName;
|
||||
this.type = type;
|
||||
this.nameid = Sncp.hash(serviceName);
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.redkale.util.AnyValue;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -19,7 +20,7 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest, SncpResponse> {
|
||||
public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpRequest, SncpResponse> {
|
||||
|
||||
private static final ByteBuffer pongBuffer = ByteBuffer.wrap("PONG".getBytes()).asReadOnlyBuffer();
|
||||
|
||||
@@ -27,7 +28,13 @@ public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest,
|
||||
|
||||
private final Map<DLong, SncpServlet> singlemaps = new HashMap<>();
|
||||
|
||||
public void addSncpServlet(SncpServlet servlet) {
|
||||
@Override
|
||||
public <S extends Servlet<SncpContext, SncpRequest, SncpResponse>> void addServlet(S servlet, Object attachment, AnyValue conf, DLong... mappings) {
|
||||
addServlet((SncpServlet) servlet, conf);
|
||||
}
|
||||
|
||||
public void addServlet(SncpServlet servlet, AnyValue conf) {
|
||||
setServletConf(servlet, conf);
|
||||
if (servlet.getNameid() == DLong.ZERO) {
|
||||
synchronized (singlemaps) {
|
||||
singlemaps.put(servlet.getServiceid(), servlet);
|
||||
@@ -54,7 +61,7 @@ public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest,
|
||||
public void init(SncpContext context, AnyValue config) {
|
||||
Collection<Map<DLong, SncpServlet>> values = this.maps.values();
|
||||
values.stream().forEach((en) -> {
|
||||
en.values().stream().forEach(s -> s.init(context, s.conf));
|
||||
en.values().stream().forEach(s -> s.init(context, getServletConf(s)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -62,7 +69,7 @@ public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest,
|
||||
public void destroy(SncpContext context, AnyValue config) {
|
||||
Collection<Map<DLong, SncpServlet>> values = this.maps.values();
|
||||
values.stream().forEach((en) -> {
|
||||
en.values().stream().forEach(s -> s.destroy(context, s.conf));
|
||||
en.values().stream().forEach(s -> s.destroy(context, getServletConf(s)));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.redkale.watch.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class SncpServer extends Server {
|
||||
public final class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResponse> {
|
||||
|
||||
public SncpServer() {
|
||||
this(System.currentTimeMillis(), null);
|
||||
@@ -37,8 +37,8 @@ public final class SncpServer extends Server {
|
||||
}
|
||||
|
||||
public SncpDynServlet addService(ServiceWrapper entry) {
|
||||
SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), entry.getName(), entry.getType(), entry.getService(), entry.getConf());
|
||||
((SncpPrepareServlet) this.prepare).addSncpServlet(sds);
|
||||
SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), entry.getName(), entry.getType(), entry.getService());
|
||||
this.prepare.addServlet(sds, null, entry.getConf());
|
||||
return sds;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public final class SncpServer extends Server {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Context createContext() {
|
||||
protected SncpContext createContext() {
|
||||
final int port = this.address.getPort();
|
||||
AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.creatCounter");
|
||||
AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.cycleCounter");
|
||||
|
||||
@@ -17,8 +17,6 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse> implements Comparable<SncpServlet> {
|
||||
|
||||
AnyValue conf;
|
||||
|
||||
public abstract DLong getNameid();
|
||||
|
||||
public abstract DLong getServiceid();
|
||||
|
||||
Reference in New Issue
Block a user