This commit is contained in:
RedKale
2016-02-19 14:43:37 +08:00
parent b323d71a80
commit 5d4d23672f
10 changed files with 62 additions and 42 deletions

View File

@@ -8,8 +8,10 @@ package org.redkale.net;
import java.io.*; import java.io.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import java.util.logging.*; import java.util.logging.*;
import org.redkale.util.*;
/** /**
* *
@@ -21,12 +23,18 @@ import java.util.logging.*;
* @param <R> Request的子类型 * @param <R> Request的子类型
* @param <P> Response的子类型 * @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 executeCounter = new AtomicLong(); //执行请求次数
protected final AtomicLong illRequestCounter = 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 { public final void prepare(final ByteBuffer buffer, final R request, final P response) throws IOException {
executeCounter.incrementAndGet(); executeCounter.incrementAndGet();
final int rs = request.readHeader(buffer); 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;
}
} }

View File

@@ -24,7 +24,7 @@ import org.redkale.watch.*;
* *
* @author zhangjx * @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"; public static final String RESNAME_SERVER_ROOT = "SERVER_ROOT";
@@ -37,7 +37,9 @@ public abstract class Server {
protected final String protocol; protected final String protocol;
protected final PrepareServlet prepare; protected final PrepareServlet<K, C, R, P> prepare;
protected C context;
protected AnyValue config; protected AnyValue config;
@@ -45,8 +47,6 @@ public abstract class Server {
protected InetSocketAddress address; protected InetSocketAddress address;
protected Context context;
protected int backlog; protected int backlog;
protected ProtocolServer serverChannel; protected ProtocolServer serverChannel;
@@ -69,7 +69,7 @@ public abstract class Server {
private ScheduledThreadPoolExecutor scheduler; 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.serverStartTime = serverStartTime;
this.protocol = protocol; this.protocol = protocol;
this.prepare = servlet; this.prepare = servlet;
@@ -116,6 +116,10 @@ public abstract class Server {
return this.logger; 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 { public void start() throws IOException {
this.context = this.createContext(); this.context = this.createContext();
this.prepare.init(this.context, config); this.prepare.init(this.context, config);
@@ -133,7 +137,7 @@ public abstract class Server {
+ ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms"); + ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms");
} }
protected abstract Context createContext(); protected abstract C createContext();
public void shutdown() throws IOException { public void shutdown() throws IOException {
long s = System.currentTimeMillis(); long s = System.currentTimeMillis();

View File

@@ -20,6 +20,8 @@ import java.io.IOException;
*/ */
public abstract class Servlet<C extends Context, R extends Request<C>, P extends Response<C, R>> { 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) { public void init(C context, AnyValue config) {
} }

View File

@@ -23,11 +23,7 @@ import org.redkale.watch.*;
* *
* @author zhangjx * @author zhangjx
*/ */
public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRequest, HttpResponse> { public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse> {
private final List<HttpServlet> servlets = new ArrayList<>();
private final Map<String, HttpServlet> strmaps = new HashMap<>();
private SimpleEntry<Predicate<String>, HttpServlet>[] regArray = new SimpleEntry[0]; 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) { public void init(HttpContext context, AnyValue config) {
this.servlets.stream().forEach(s -> { this.servlets.stream().forEach(s -> {
if (s instanceof WebSocketServlet) { if (s instanceof WebSocketServlet) {
((WebSocketServlet) s).preInit(context, s._conf); ((WebSocketServlet) s).preInit(context, getServletConf(s));
} else if (s instanceof BasedHttpServlet) { } 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(); final WatchFactory watch = context.getWatchFactory();
if (watch != null) { if (watch != null) {
@@ -71,7 +67,7 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
public void execute(HttpRequest request, HttpResponse response) throws IOException { public void execute(HttpRequest request, HttpResponse response) throws IOException {
try { try {
final String uri = request.getRequestURI(); 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) { if (servlet == null && this.regArray != null) {
for (SimpleEntry<Predicate<String>, HttpServlet> en : regArray) { for (SimpleEntry<Predicate<String>, HttpServlet> en : regArray) {
if (en.getKey().test(uri)) { 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 = ""; if (prefix == null) prefix = "";
HttpServlet servlet = (HttpServlet) servlet0;
for (String mapping : mappings) { for (String mapping : mappings) {
if (!prefix.isEmpty()) mapping = prefix + mapping; if (!prefix.toString().isEmpty()) mapping = prefix + mapping;
if (contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式)) if (contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
if (mapping.charAt(0) != '^') mapping = '^' + mapping; if (mapping.charAt(0) != '^') mapping = '^' + mapping;
if (mapping.endsWith("/*")) { 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); regArray[regArray.length - 1] = new SimpleEntry<>(Pattern.compile(mapping).asPredicate(), servlet);
} }
} else if (mapping != null && !mapping.isEmpty()) { } else if (mapping != null && !mapping.isEmpty()) {
strmaps.put(mapping, servlet); this.mappings.put(mapping, servlet);
} }
} }
servlet._conf = conf; setServletConf(servlet, conf);
servlet._prefix = prefix == null ? "" : prefix; servlet._prefix = prefix == null ? "" : prefix.toString();
this.servlets.add(servlet); this.servlets.add(servlet);
} }
@@ -135,11 +133,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRe
public void destroy(HttpContext context, AnyValue config) { public void destroy(HttpContext context, AnyValue config) {
this.resourceHttpServlet.destroy(context, config); this.resourceHttpServlet.destroy(context, config);
this.servlets.stream().forEach(s -> { this.servlets.stream().forEach(s -> {
s.destroy(context, s._conf); s.destroy(context, getServletConf(s));
if (s instanceof WebSocketServlet) { if (s instanceof WebSocketServlet) {
((WebSocketServlet) s).postDestroy(context, s._conf); ((WebSocketServlet) s).postDestroy(context, getServletConf(s));
} else if (s instanceof BasedHttpServlet) { } else if (s instanceof BasedHttpServlet) {
((BasedHttpServlet) s).postDestroy(context, s._conf); ((BasedHttpServlet) s).postDestroy(context, getServletConf(s));
} }
}); });
} }

View File

@@ -20,7 +20,7 @@ import org.redkale.watch.*;
* *
* @author zhangjx * @author zhangjx
*/ */
public final class HttpServer extends Server { public final class HttpServer extends Server<String, HttpContext, HttpRequest, HttpResponse> {
public HttpServer() { public HttpServer() {
this(System.currentTimeMillis(), null); 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) { 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 @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Context createContext() { protected HttpContext createContext() {
final int port = this.address.getPort(); final int port = this.address.getPort();
AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.creatCounter"); AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.creatCounter");
AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.cycleCounter"); AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Buffer.cycleCounter");

View File

@@ -6,7 +6,6 @@
package org.redkale.net.http; package org.redkale.net.http;
import org.redkale.net.Servlet; 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> { public abstract class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse> {
AnyValue _conf; //当前HttpServlet的配置
String _prefix = ""; //当前HttpServlet的path前缀 String _prefix = ""; //当前HttpServlet的path前缀
@Override @Override

View File

@@ -51,8 +51,7 @@ public final class SncpDynServlet extends SncpServlet {
private Supplier<ByteBuffer> bufferSupplier; private Supplier<ByteBuffer> bufferSupplier;
public SncpDynServlet(final BsonConvert convert, final String serviceName, final Class<? extends Service> type, final Service service, final AnyValue conf) { public SncpDynServlet(final BsonConvert convert, final String serviceName, final Class<? extends Service> type, final Service service) {
this.conf = conf;
this.serviceName = serviceName; this.serviceName = serviceName;
this.type = type; this.type = type;
this.nameid = Sncp.hash(serviceName); this.nameid = Sncp.hash(serviceName);

View File

@@ -10,6 +10,7 @@ import org.redkale.util.AnyValue;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.*; import java.util.*;
import org.redkale.net.*;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
@@ -19,7 +20,7 @@ import org.redkale.util.*;
* *
* @author zhangjx * @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(); 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<>(); 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) { if (servlet.getNameid() == DLong.ZERO) {
synchronized (singlemaps) { synchronized (singlemaps) {
singlemaps.put(servlet.getServiceid(), servlet); singlemaps.put(servlet.getServiceid(), servlet);
@@ -54,7 +61,7 @@ public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest,
public void init(SncpContext context, AnyValue config) { public void init(SncpContext context, AnyValue config) {
Collection<Map<DLong, SncpServlet>> values = this.maps.values(); Collection<Map<DLong, SncpServlet>> values = this.maps.values();
values.stream().forEach((en) -> { 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) { public void destroy(SncpContext context, AnyValue config) {
Collection<Map<DLong, SncpServlet>> values = this.maps.values(); Collection<Map<DLong, SncpServlet>> values = this.maps.values();
values.stream().forEach((en) -> { values.stream().forEach((en) -> {
en.values().stream().forEach(s -> s.destroy(context, s.conf)); en.values().stream().forEach(s -> s.destroy(context, getServletConf(s)));
}); });
} }

View File

@@ -21,7 +21,7 @@ import org.redkale.watch.*;
* *
* @author zhangjx * @author zhangjx
*/ */
public final class SncpServer extends Server { public final class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResponse> {
public SncpServer() { public SncpServer() {
this(System.currentTimeMillis(), null); this(System.currentTimeMillis(), null);
@@ -37,8 +37,8 @@ public final class SncpServer extends Server {
} }
public SncpDynServlet addService(ServiceWrapper entry) { public SncpDynServlet addService(ServiceWrapper entry) {
SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), entry.getName(), entry.getType(), entry.getService(), entry.getConf()); SncpDynServlet sds = new SncpDynServlet(BsonFactory.root().getConvert(), entry.getName(), entry.getType(), entry.getService());
((SncpPrepareServlet) this.prepare).addSncpServlet(sds); this.prepare.addServlet(sds, null, entry.getConf());
return sds; return sds;
} }
@@ -48,7 +48,7 @@ public final class SncpServer extends Server {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Context createContext() { protected SncpContext createContext() {
final int port = this.address.getPort(); final int port = this.address.getPort();
AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.creatCounter"); AtomicLong createBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.creatCounter");
AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.cycleCounter"); AtomicLong cycleBufferCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Buffer.cycleCounter");

View File

@@ -17,8 +17,6 @@ import org.redkale.util.*;
*/ */
public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse> implements Comparable<SncpServlet> { public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse> implements Comparable<SncpServlet> {
AnyValue conf;
public abstract DLong getNameid(); public abstract DLong getNameid();
public abstract DLong getServiceid(); public abstract DLong getServiceid();