From bfc6b04a30899ec3c23218a40c8a07473d7dd1e8 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 4 May 2018 08:50:17 +0800 Subject: [PATCH] --- src/org/redkale/net/Context.java | 60 ++++++++++++++++++++++ src/org/redkale/net/http/HttpContext.java | 18 +++---- src/org/redkale/net/http/HttpResponse.java | 57 +++++++++++++------- src/org/redkale/net/http/HttpServer.java | 51 ++++++++++++------ src/org/redkale/net/sncp/SncpContext.java | 19 +++---- src/org/redkale/net/sncp/SncpServer.java | 23 +++++++-- 6 files changed, 167 insertions(+), 61 deletions(-) diff --git a/src/org/redkale/net/Context.java b/src/org/redkale/net/Context.java index b1ed9af3b..eceab2250 100644 --- a/src/org/redkale/net/Context.java +++ b/src/org/redkale/net/Context.java @@ -79,6 +79,13 @@ public class Context { //依赖注入工厂类 protected final ResourceFactory resourceFactory; + public Context(ContextConfig config) { + this(config.serverStartTime, config.logger, config.executor, config.sslContext, + config.bufferCapacity, config.bufferPool, config.responsePool, config.maxbody, + config.charset, config.address, config.resourceFactory, config.prepare, + config.aliveTimeoutSeconds, config.readTimeoutSeconds, config.writeTimeoutSeconds); + } + public Context(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext, int bufferCapacity, ObjectPool bufferPool, ObjectPool responsePool, final int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory, @@ -188,4 +195,57 @@ public class Context { public BsonConvert getBsonConvert() { return bsonFactory.getConvert(); } + + public static class ContextConfig { + + //服务启动时间 + public long serverStartTime; + + //Server的线程池 + public ThreadPoolExecutor executor; + + //SSL + public SSLContext sslContext; + + //ByteBuffer的容量,默认8K + public int bufferCapacity; + + //ByteBuffer对象池 + public ObjectPool bufferPool; + + //Response对象池 + public ObjectPool responsePool; + + //服务的根Servlet + public PrepareServlet prepare; + + //服务的监听地址 + public InetSocketAddress address; + + //字符集 + public Charset charset; + + //请求内容的大小上限, 默认64K + public int maxbody; + + //keep alive IO读取的超时时间 + public int aliveTimeoutSeconds; + + //IO读取的超时时间 + public int readTimeoutSeconds; + + //IO写入的超时时间 + public int writeTimeoutSeconds; + + //日志Logger + public Logger logger; + + //依赖注入工厂类 + public ResourceFactory resourceFactory; + + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } + } } diff --git a/src/org/redkale/net/http/HttpContext.java b/src/org/redkale/net/http/HttpContext.java index ca669b756..ea0309473 100644 --- a/src/org/redkale/net/http/HttpContext.java +++ b/src/org/redkale/net/http/HttpContext.java @@ -6,14 +6,9 @@ package org.redkale.net.http; import org.redkale.asm.MethodDebugVisitor; -import java.net.*; -import java.nio.*; import java.nio.channels.CompletionHandler; -import java.nio.charset.*; import java.security.*; import java.util.concurrent.*; -import java.util.logging.*; -import javax.net.ssl.SSLContext; import org.redkale.asm.*; import static org.redkale.asm.Opcodes.*; import org.redkale.net.*; @@ -33,13 +28,8 @@ public class HttpContext extends Context { protected final ConcurrentHashMap asyncHandlerCreators = new ConcurrentHashMap<>(); - public HttpContext(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext, - final int bufferCapacity, final ObjectPool bufferPool, ObjectPool responsePool, - int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory, - PrepareServlet prepare, int aliveTimeoutSeconds, int readTimeoutSeconds, int writeTimeoutSeconds) { - super(serverStartTime, logger, executor, sslContext, bufferCapacity, bufferPool, responsePool, - maxbody, charset, address, resourceFactory, prepare, aliveTimeoutSeconds, readTimeoutSeconds, writeTimeoutSeconds); - + public HttpContext(HttpContextConfig config) { + super(config); random.setSeed(Math.abs(System.nanoTime())); } @@ -169,4 +159,8 @@ public class HttpContext extends Context { }.loadClass(newDynName.replace('/', '.'), bytes); return (Creator) Creator.create(newHandlerClazz); } + + public static class HttpContextConfig extends ContextConfig { + + } } diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index 792767c2e..03c69fc57 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -136,7 +136,7 @@ public class HttpResponse extends Response { private final Supplier dateSupplier; - private final HttpCookie defcookie; + private final HttpCookie defaultCookie; private final List renders; @@ -148,21 +148,18 @@ public class HttpResponse extends Response { return new ObjectPool<>(creatCounter, cycleCounter, max, creator, (x) -> ((HttpResponse) x).prepare(), (x) -> ((HttpResponse) x).recycle()); } - public HttpResponse(HttpContext context, HttpRequest request, - String plainContentType, String jsonContentType, - String[][] defaultAddHeaders, String[][] defaultSetHeaders, - HttpCookie defcookie, boolean autoOptions, Supplier dateSupplier, List< HttpRender> renders) { + public HttpResponse(HttpContext context, HttpRequest request, HttpResponseConfig config) { super(context, request); - this.plainContentType = plainContentType == null || plainContentType.isEmpty() ? "text/plain; charset=utf-8" : plainContentType; - this.jsonContentType = jsonContentType == null || jsonContentType.isEmpty() ? "application/json; charset=utf-8" : jsonContentType; + this.plainContentType = config.plainContentType == null || config.plainContentType.isEmpty() ? "text/plain; charset=utf-8" : config.plainContentType; + this.jsonContentType = config.jsonContentType == null || config.jsonContentType.isEmpty() ? "application/json; charset=utf-8" : config.jsonContentType; this.plainContentTypeBytes = ("Content-Type: " + this.plainContentType + "\r\n").getBytes(); this.jsonContentTypeBytes = ("Content-Type: " + this.jsonContentType + "\r\n").getBytes(); - this.defaultAddHeaders = defaultAddHeaders; - this.defaultSetHeaders = defaultSetHeaders; - this.defcookie = defcookie; - this.autoOptions = autoOptions; - this.dateSupplier = dateSupplier; - this.renders = renders; + this.defaultAddHeaders = config.defaultAddHeaders; + this.defaultSetHeaders = config.defaultSetHeaders; + this.defaultCookie = config.defaultCookie; + this.autoOptions = config.autoOptions; + this.dateSupplier = config.dateSupplier; + this.renders = config.renders; this.hasRender = renders != null && !renders.isEmpty(); this.onlyoneHttpRender = renders != null && renders.size() == 1 ? renders.get(0) : null; this.contentType = this.plainContentType; @@ -907,13 +904,13 @@ public class HttpResponse extends Response { buffer.put((en.name + ": " + en.getValue() + "\r\n").getBytes()); } if (request.newsessionid != null) { - String domain = defcookie == null ? null : defcookie.getDomain(); + String domain = defaultCookie == null ? null : defaultCookie.getDomain(); if (domain == null) { domain = ""; } else { domain = "Domain=" + domain + "; "; } - String path = defcookie == null ? null : defcookie.getPath(); + String path = defaultCookie == null ? null : defaultCookie.getPath(); if (path == null || path.isEmpty()) path = "/"; if (request.newsessionid.isEmpty()) { buffer.put(("Set-Cookie: " + HttpRequest.SESSIONID_NAME + "=; " + domain + "Path=" + path + "; Max-Age=0; HttpOnly\r\n").getBytes()); @@ -924,9 +921,9 @@ public class HttpResponse extends Response { if (this.cookies != null) { for (HttpCookie cookie : this.cookies) { if (cookie == null) continue; - if (defcookie != null) { - if (defcookie.getDomain() != null && cookie.getDomain() == null) cookie.setDomain(defcookie.getDomain()); - if (defcookie.getPath() != null && cookie.getPath() == null) cookie.setPath(defcookie.getPath()); + if (defaultCookie != null) { + if (defaultCookie.getDomain() != null && cookie.getDomain() == null) cookie.setDomain(defaultCookie.getDomain()); + if (defaultCookie.getPath() != null && cookie.getPath() == null) cookie.setPath(defaultCookie.getPath()); } buffer.put(("Set-Cookie: " + genString(cookie) + "\r\n").getBytes()); } @@ -1173,4 +1170,28 @@ public class HttpResponse extends Response { } } + + public static class HttpResponseConfig { + + public String plainContentType; + + public String jsonContentType; + + public String[][] defaultAddHeaders; + + public String[][] defaultSetHeaders; + + public HttpCookie defaultCookie; + + public boolean autoOptions; + + public Supplier dateSupplier; + + public List< HttpRender> renders; + + @Override + public String toString() { + return JsonConvert.root().convertTo(this); + } + } } diff --git a/src/org/redkale/net/http/HttpServer.java b/src/org/redkale/net/http/HttpServer.java index 0e4e28bc2..151d57603 100644 --- a/src/org/redkale/net/http/HttpServer.java +++ b/src/org/redkale/net/http/HttpServer.java @@ -17,6 +17,8 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.Supplier; import java.util.logging.Level; import org.redkale.net.*; +import org.redkale.net.http.HttpContext.HttpContextConfig; +import org.redkale.net.http.HttpResponse.HttpResponseConfig; import org.redkale.net.sncp.Sncp; import org.redkale.service.Service; import org.redkale.util.*; @@ -391,15 +393,10 @@ public class HttpServer extends Server dateSupplier0 = null; + Supplier dateSupplier = null; if (datePeriod == 0) { final ZoneId gmtZone = ZoneId.of("GMT"); - dateSupplier0 = () -> ("Date: " + RFC_1123_DATE_TIME.format(java.time.ZonedDateTime.now(gmtZone)) + "\r\n").getBytes(); + dateSupplier = () -> ("Date: " + RFC_1123_DATE_TIME.format(java.time.ZonedDateTime.now(gmtZone)) + "\r\n").getBytes(); } else if (datePeriod > 0) { if (this.dateScheduler == null) { this.dateScheduler = new ScheduledThreadPoolExecutor(1, (Runnable r) -> { @@ -413,21 +410,45 @@ public class HttpServer extends Server { currDateBytes = ("Date: " + gmtDateFormat.format(new Date()) + "\r\n").getBytes(); }, 1000 - System.currentTimeMillis() % 1000, datePeriod, TimeUnit.MILLISECONDS); - dateSupplier0 = () -> currDateBytes; + dateSupplier = () -> currDateBytes; } } - final HttpCookie defCookie = defaultCookie; final String addrHeader = remoteAddrHeader; - final Supplier dateSupplier = dateSupplier0; + + final HttpResponseConfig respConfig = new HttpResponseConfig(); + respConfig.plainContentType = plainContentType; + respConfig.jsonContentType = jsonContentType; + respConfig.defaultAddHeaders = defaultAddHeaders.isEmpty() ? null : defaultAddHeaders.toArray(new String[defaultAddHeaders.size()][]); + respConfig.defaultSetHeaders = defaultSetHeaders.isEmpty() ? null : defaultSetHeaders.toArray(new String[defaultSetHeaders.size()][]); + respConfig.defaultCookie = defaultCookie; + respConfig.autoOptions = autoOptions; + respConfig.dateSupplier = dateSupplier; + respConfig.renders = ((HttpPrepareServlet) prepare).renders; + AtomicLong createResponseCounter = new AtomicLong(); AtomicLong cycleResponseCounter = new AtomicLong(); ObjectPool responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null); - HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, this.sslContext, - rcapacity, bufferPool, responsePool, this.maxbody, this.charset, this.address, this.resourceFactory, - this.prepare, this.aliveTimeoutSeconds, this.readTimeoutSeconds, this.writeTimeoutSeconds); - responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), - plainType, jsonType, addHeaders, setHeaders, defCookie, options, dateSupplier, ((HttpPrepareServlet) prepare).renders)); + + final HttpContextConfig contextConfig = new HttpContextConfig(); + contextConfig.serverStartTime = this.serverStartTime; + contextConfig.logger = this.logger; + contextConfig.executor = this.executor; + contextConfig.sslContext = this.sslContext; + contextConfig.bufferCapacity = rcapacity; + contextConfig.bufferPool = bufferPool; + contextConfig.responsePool = responsePool; + contextConfig.maxbody = this.maxbody; + contextConfig.charset = this.charset; + contextConfig.address = this.address; + contextConfig.prepare = this.prepare; + contextConfig.resourceFactory = this.resourceFactory; + contextConfig.aliveTimeoutSeconds = this.aliveTimeoutSeconds; + contextConfig.readTimeoutSeconds = this.readTimeoutSeconds; + contextConfig.writeTimeoutSeconds = this.writeTimeoutSeconds; + + HttpContext httpcontext = new HttpContext(contextConfig); + responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), respConfig)); return httpcontext; } } diff --git a/src/org/redkale/net/sncp/SncpContext.java b/src/org/redkale/net/sncp/SncpContext.java index dfce67911..5a58b914e 100644 --- a/src/org/redkale/net/sncp/SncpContext.java +++ b/src/org/redkale/net/sncp/SncpContext.java @@ -5,14 +5,7 @@ */ package org.redkale.net.sncp; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.logging.Logger; -import javax.net.ssl.SSLContext; import org.redkale.net.*; -import org.redkale.util.*; /** *

@@ -22,11 +15,11 @@ import org.redkale.util.*; */ public class SncpContext extends Context { - public SncpContext(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext, - int bufferCapacity, ObjectPool bufferPool, ObjectPool responsePool, - int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory, - PrepareServlet prepare, int aliveTimeoutSeconds, int readTimeoutSeconds, int writeTimeoutSeconds) { - super(serverStartTime, logger, executor, sslContext, bufferCapacity, bufferPool, responsePool, - maxbody, charset, address, resourceFactory, prepare, aliveTimeoutSeconds, readTimeoutSeconds, writeTimeoutSeconds); + public SncpContext(SncpContextConfig config) { + super(config); + } + + public static class SncpContextConfig extends ContextConfig { + } } diff --git a/src/org/redkale/net/sncp/SncpServer.java b/src/org/redkale/net/sncp/SncpServer.java index d8e535550..53c7965d6 100644 --- a/src/org/redkale/net/sncp/SncpServer.java +++ b/src/org/redkale/net/sncp/SncpServer.java @@ -10,6 +10,7 @@ import java.util.*; import java.util.concurrent.atomic.*; import org.redkale.convert.bson.*; import org.redkale.net.*; +import org.redkale.net.sncp.SncpContext.SncpContextConfig; import org.redkale.service.Service; import org.redkale.util.*; @@ -106,9 +107,25 @@ public class SncpServer extends Server responsePool = SncpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null); - SncpContext sncpcontext = new SncpContext(this.serverStartTime, this.logger, executor, this.sslContext, - rcapacity, bufferPool, responsePool, this.maxbody, this.charset, this.address, this.resourceFactory, - this.prepare, this.aliveTimeoutSeconds, this.readTimeoutSeconds, this.writeTimeoutSeconds); + + final SncpContextConfig contextConfig = new SncpContextConfig(); + contextConfig.serverStartTime = this.serverStartTime; + contextConfig.logger = this.logger; + contextConfig.executor = this.executor; + contextConfig.sslContext = this.sslContext; + contextConfig.bufferCapacity = rcapacity; + contextConfig.bufferPool = bufferPool; + contextConfig.responsePool = responsePool; + contextConfig.maxbody = this.maxbody; + contextConfig.charset = this.charset; + contextConfig.address = this.address; + contextConfig.prepare = this.prepare; + contextConfig.resourceFactory = this.resourceFactory; + contextConfig.aliveTimeoutSeconds = this.aliveTimeoutSeconds; + contextConfig.readTimeoutSeconds = this.readTimeoutSeconds; + contextConfig.writeTimeoutSeconds = this.writeTimeoutSeconds; + + SncpContext sncpcontext = new SncpContext(contextConfig); responsePool.setCreator((Object... params) -> new SncpResponse(sncpcontext, new SncpRequest(sncpcontext))); return sncpcontext; }