This commit is contained in:
RedKale
2016-02-22 10:46:49 +08:00
parent 736f6b54e3
commit e632280d44
12 changed files with 42 additions and 113 deletions

View File

@@ -23,17 +23,17 @@ import org.redkale.util.*;
* @param <R> Request的子类型
* @param <P> Response的子类型
*/
public abstract class PrepareServlet<K extends Serializable, 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>, S extends Servlet<C, R, P>> 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 Set<S> servlets = new HashSet<>();
protected final Map<K, Servlet<C, R, P>> mappings = new HashMap<>();
protected final Map<K, S> mappings = new HashMap<>();
public abstract <S extends Servlet<C, R, P>> void addServlet(S servlet, Object attachment, AnyValue conf, K... mappings);
public abstract 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();

View File

@@ -24,7 +24,7 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public abstract class Server<K extends Serializable, C extends Context, R extends Request<C>, P extends Response<C, R>> {
public abstract class Server<K extends Serializable, C extends Context, R extends Request<C>, P extends Response<C, R>, S extends Servlet<C, R, P>> {
public static final String RESNAME_SERVER_ROOT = "SERVER_ROOT";
@@ -37,7 +37,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
protected final String protocol;
protected final PrepareServlet<K, C, R, P> prepare;
protected final PrepareServlet<K, C, R, P, S> prepare;
protected C context;
@@ -69,7 +69,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
private ScheduledThreadPoolExecutor scheduler;
protected Server(long serverStartTime, String protocol, PrepareServlet<K, C, R, P> servlet, final WatchFactory watch) {
protected Server(long serverStartTime, String protocol, PrepareServlet<K, C, R, P, S> servlet, final WatchFactory watch) {
this.serverStartTime = serverStartTime;
this.protocol = protocol;
this.prepare = servlet;
@@ -116,7 +116,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
return this.logger;
}
public <S extends Servlet<C, R, P>> void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
this.prepare.addServlet(servlet, attachment, conf, mappings);
}

View File

@@ -23,7 +23,7 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse> {
public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse, HttpServlet> {
private SimpleEntry<Predicate<String>, HttpServlet>[] regArray = new SimpleEntry[0];
@@ -31,7 +31,7 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
@Override
public void init(HttpContext context, AnyValue config) {
this.servlets.stream().forEach(s -> {
this.servlets.forEach(s -> {
if (s instanceof WebSocketServlet) {
((WebSocketServlet) s).preInit(context, getServletConf(s));
} else if (s instanceof BasedHttpServlet) {
@@ -41,7 +41,7 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
});
final WatchFactory watch = context.getWatchFactory();
if (watch != null) {
this.servlets.stream().forEach(s -> {
this.servlets.forEach(s -> {
watch.inject(s);
});
}
@@ -85,9 +85,8 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
}
@Override
public <S extends Servlet<HttpContext, HttpRequest, HttpResponse>> void addServlet(S servlet0, Object prefix, AnyValue conf, String... mappings) {
public void addServlet(HttpServlet servlet, Object prefix, AnyValue conf, String... mappings) {
if (prefix == null) prefix = "";
HttpServlet servlet = (HttpServlet) servlet0;
for (String mapping : mappings) {
if (!prefix.toString().isEmpty()) mapping = prefix + mapping;
if (contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
@@ -132,7 +131,7 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
@Override
public void destroy(HttpContext context, AnyValue config) {
this.resourceHttpServlet.destroy(context, config);
this.servlets.stream().forEach(s -> {
this.servlets.forEach(s -> {
s.destroy(context, getServletConf(s));
if (s instanceof WebSocketServlet) {
((WebSocketServlet) s).postDestroy(context, getServletConf(s));

View File

@@ -20,7 +20,7 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public final class HttpServer extends Server<String, HttpContext, HttpRequest, HttpResponse> {
public final class HttpServer extends Server<String, HttpContext, HttpRequest, HttpResponse, HttpServlet> {
public HttpServer() {
this(System.currentTimeMillis(), null);

View File

@@ -33,12 +33,6 @@ import org.redkale.service.DynRemote;
*/
public abstract class Sncp {
private static final java.lang.reflect.Type GROUPS_TYPE1 = new TypeToken<Set<String>>() {
}.getType();
private static final java.lang.reflect.Type GROUPS_TYPE2 = new TypeToken<String[]>() {
}.getType();
static final String LOCALPREFIX = "_DynLocal";
static final String REMOTEPREFIX = "_DynRemote";
@@ -725,7 +719,7 @@ public abstract class Sncp {
try {
Field e = newClazz.getDeclaredField("_client");
e.setAccessible(true);
client = new SncpClient(name, executor, hash(serviceClass), false, newClazz, clientAddress);
client = new SncpClient(name, serviceClass, executor, false, newClazz, clientAddress);
e.set(rs, client);
} catch (NoSuchFieldException ne) {
}
@@ -734,7 +728,7 @@ public abstract class Sncp {
StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name).append("'");
if (client != null) {
sb.append(", nameid = ").append(client.getNameid()).append(", serviceid = ").append(client.getServiceid());
sb.append(", serviceid = ").append(client.getServiceid());
sb.append(", action.size = ").append(client.getActionCount());
List<String> groups = new ArrayList<>();
if (sameGroupTransport != null) groups.add(sameGroupTransport.getName());
@@ -857,7 +851,7 @@ public abstract class Sncp {
final String anyValueDesc = Type.getDescriptor(AnyValue.class);
ClassLoader loader = Sncp.class.getClassLoader();
String newDynName = supDynName.substring(0, supDynName.lastIndexOf('/') + 1) + REMOTEPREFIX + serviceClass.getSimpleName();
final SncpClient client = new SncpClient(name, executor, hash(serviceClass), true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
final SncpClient client = new SncpClient(name, serviceClass, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
try {
Class newClazz = Class.forName(newDynName.replace('/', '.'));
T rs = (T) newClazz.newInstance();
@@ -870,7 +864,7 @@ public abstract class Sncp {
{
StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name);
sb.append("', nameid = ").append(client.getNameid()).append(", serviceid = ").append(client.getServiceid());
sb.append("', serviceid = ").append(client.getServiceid());
sb.append(", action.size = ").append(client.getActionCount());
sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName());
sb.append(", remoteaddrs = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses()));
@@ -1089,7 +1083,7 @@ public abstract class Sncp {
{
StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name);
sb.append("', nameid = ").append(client.getNameid()).append(", serviceid = ").append(client.getServiceid());
sb.append("', serviceid = ").append(client.getServiceid());
sb.append(", action.size = ").append(client.getActionCount());
sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName());
sb.append(", remotes = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses()));

View File

@@ -135,21 +135,18 @@ public final class SncpClient {
protected final DLong serviceid;
protected final DLong nameid;
protected final SncpAction[] actions;
protected final Consumer<Runnable> executor;
public SncpClient(final String serviceName, final Consumer<Runnable> executor, final DLong serviceid, boolean remote,
public <T extends Service> SncpClient(final String serviceName, final Class<T> serviceType, final Consumer<Runnable> executor, boolean remote,
final Class serviceClass, final InetSocketAddress clientAddress) {
this.remote = remote;
this.executor = executor;
this.serviceClass = serviceClass;
this.clientAddress = clientAddress;
this.name = serviceName;
this.nameid = Sncp.hash(serviceName);
this.serviceid = serviceid;
this.serviceid = Sncp.hash(serviceType.getName() + ':' + serviceName);
final List<SncpAction> methodens = new ArrayList<>();
//------------------------------------------------------------------------------
for (java.lang.reflect.Method method : parseMethod(serviceClass)) {
@@ -165,10 +162,6 @@ public final class SncpClient {
return clientAddress;
}
public DLong getNameid() {
return nameid;
}
public DLong getServiceid() {
return serviceid;
}
@@ -181,8 +174,8 @@ public final class SncpClient {
public String toString() {
String service = serviceClass.getName();
if (remote) service = service.replace(Sncp.LOCALPREFIX, Sncp.REMOTEPREFIX);
return this.getClass().getSimpleName() + "(service = " + service + ", serviceid = " + serviceid + ", nameid = " + nameid
+ ", name = '" + name + "', address = " + (clientAddress == null ? "" : (clientAddress.getHostString() + ":" + clientAddress.getPort()))
return this.getClass().getSimpleName() + "(service = " + service + ", serviceid = " + serviceid + ", name = '" + name
+ "', address = " + (clientAddress == null ? "" : (clientAddress.getHostString() + ":" + clientAddress.getPort()))
+ ", actions.size = " + actions.length + ")";
}
@@ -449,8 +442,6 @@ public final class SncpClient {
if (buffer.getChar() != HEADER_SIZE) throw new RuntimeException("sncp(" + action.method + ") buffer receive header.length not " + HEADER_SIZE);
DLong rserviceid = DLong.read(buffer);
if (!rserviceid.equals(serviceid)) throw new RuntimeException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid);
DLong rnameid = DLong.read(buffer);
if (!rnameid.equals(nameid)) throw new RuntimeException("sncp(" + action.method + ") response.nameid = " + nameid + ", but receive nameid =" + rnameid);
DLong raction = DLong.read(buffer);
if (!action.actionid.equals(raction)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")");
buffer.getInt(); //地址
@@ -464,7 +455,6 @@ public final class SncpClient {
buffer.putLong(seqid); //序列号
buffer.putChar((char) HEADER_SIZE); //header长度
DLong.write(buffer, this.serviceid);
DLong.write(buffer, this.nameid);
DLong.write(buffer, actionid);
buffer.put(addrBytes);
buffer.putChar((char) this.addrPort);

View File

@@ -45,8 +45,6 @@ public final class SncpDynServlet extends SncpServlet {
private final DLong serviceid;
private final DLong nameid;
private final HashMap<DLong, SncpServletAction> actions = new HashMap<>();
private Supplier<ByteBuffer> bufferSupplier;
@@ -54,8 +52,7 @@ public final class SncpDynServlet extends SncpServlet {
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);
this.serviceid = Sncp.hash(type);
this.serviceid = Sncp.hash(type.getName() + ':' + serviceName);
Set<DLong> actionids = new HashSet<>();
for (java.lang.reflect.Method method : service.getClass().getMethods()) {
if (method.isSynthetic()) continue;
@@ -90,14 +87,10 @@ public final class SncpDynServlet extends SncpServlet {
for (int i = 0; i < maxNameLength - serviceName.length(); i++) {
sb.append(' ');
}
sb.append(", nameid=").append(nameid).append(", actions.size=").append(actions.size() > 9 ? "" : " ").append(actions.size()).append(")");
sb.append(", actions.size=").append(actions.size() > 9 ? "" : " ").append(actions.size()).append(")");
return sb.toString();
}
@Override
public DLong getNameid() {
return nameid;
}
@Override
public DLong getServiceid() {

View File

@@ -10,7 +10,6 @@ import org.redkale.util.AnyValue;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import org.redkale.net.*;
import org.redkale.util.*;
/**
@@ -20,57 +19,37 @@ import org.redkale.util.*;
*
* @author zhangjx
*/
public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpRequest, SncpResponse> {
public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpRequest, SncpResponse, SncpServlet> {
private static final ByteBuffer pongBuffer = ByteBuffer.wrap("PONG".getBytes()).asReadOnlyBuffer();
private final Map<DLong, Map<DLong, SncpServlet>> maps = new HashMap<>();
private final Map<DLong, SncpServlet> singlemaps = new HashMap<>();
@Override
public <S extends Servlet<SncpContext, SncpRequest, SncpResponse>> void addServlet(S servlet, Object attachment, AnyValue conf, DLong... mappings) {
public void addServlet(SncpServlet 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);
}
} else {
synchronized (maps) {
Map<DLong, SncpServlet> m = maps.get(servlet.getServiceid());
if (m == null) {
m = new HashMap<>();
maps.put(servlet.getServiceid(), m);
}
m.put(servlet.getNameid(), servlet);
}
synchronized (mappings) {
mappings.put(servlet.getServiceid(), servlet);
servlets.add(servlet);
}
}
public List<SncpServlet> getSncpServlets() {
ArrayList<SncpServlet> list = new ArrayList<>(singlemaps.values());
maps.values().forEach(x -> list.addAll(x.values()));
ArrayList<SncpServlet> list = new ArrayList<>(servlets.size());
servlets.forEach(x -> list.add((SncpServlet) x));
return list;
}
@Override
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, getServletConf(s)));
});
servlets.forEach(s -> s.init(context, getServletConf(s)));
}
@Override
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, getServletConf(s)));
});
servlets.forEach(s -> s.destroy(context, getServletConf(s)));
}
@Override
@@ -79,23 +58,9 @@ public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpR
response.finish(pongBuffer.duplicate());
return;
}
SncpServlet servlet;
if (request.getNameid() == DLong.ZERO) {
servlet = singlemaps.get(request.getServiceid());
if (servlet == null) {
response.finish(SncpResponse.RETCODE_ILLSERVICEID, null); //无效serviceid
return;
}
} else {
Map<DLong, SncpServlet> m = maps.get(request.getServiceid());
if (m == null) {
response.finish(SncpResponse.RETCODE_ILLSERVICEID, null); //无效serviceid
return;
}
servlet = m.get(request.getNameid());
}
SncpServlet servlet = (SncpServlet) mappings.get(request.getServiceid());
if (servlet == null) {
response.finish(SncpResponse.RETCODE_ILLNAMEID, null); //无效nameid
response.finish(SncpResponse.RETCODE_ILLSERVICEID, null); //无效serviceid
} else {
servlet.execute(request, response);
}

View File

@@ -13,12 +13,14 @@ import org.redkale.util.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public final class SncpRequest extends Request<SncpContext> {
public static final int HEADER_SIZE = 72;
public static final int HEADER_SIZE = 56;
public static final byte[] DEFAULT_HEADER = new byte[HEADER_SIZE];
@@ -28,8 +30,6 @@ public final class SncpRequest extends Request<SncpContext> {
private DLong serviceid;
private DLong nameid;
private DLong actionid;
private int bodylength;
@@ -60,7 +60,6 @@ public final class SncpRequest extends Request<SncpContext> {
return -1;
}
this.serviceid = DLong.read(buffer);
this.nameid = DLong.read(buffer);
this.actionid = DLong.read(buffer);
buffer.get(bufferbytes);
this.bodylength = buffer.getInt();
@@ -102,7 +101,6 @@ public final class SncpRequest extends Request<SncpContext> {
protected void recycle() {
this.seqid = 0;
this.serviceid = null;
this.nameid = null;
this.actionid = null;
this.bodylength = 0;
this.bodyoffset = 0;
@@ -128,10 +126,6 @@ public final class SncpRequest extends Request<SncpContext> {
return serviceid;
}
public DLong getNameid() {
return nameid;
}
public DLong getActionid() {
return actionid;
}

View File

@@ -23,9 +23,7 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
public static final int RETCODE_ILLSERVICEID = (1 << 10); //无效serviceid
public static final int RETCODE_ILLNAMEID = (1 << 11); //无效nameid
public static final int RETCODE_ILLACTIONID = (1 << 12); //无效actionid
public static final int RETCODE_ILLACTIONID = (1 << 11); //无效actionid
public static final int RETCODE_THROWEXCEPTION = (1 << 30); //内部异常
@@ -39,7 +37,6 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
public static String getRetCodeInfo(int retcode) {
if (retcode == RETCODE_ILLSERVICEID) return "serviceid is invalid";
if (retcode == RETCODE_ILLNAMEID) return "nameid is invalid";
if (retcode == RETCODE_ILLACTIONID) return "actionid is invalid";
if (retcode == RETCODE_THROWEXCEPTION) return "Inner exception";
return null;
@@ -71,7 +68,6 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
buffer.putLong(request.getSeqid());
buffer.putChar((char) SncpRequest.HEADER_SIZE);
DLong.write(buffer, request.getServiceid());
DLong.write(buffer, request.getNameid());
DLong.write(buffer, request.getActionid());
buffer.put(addrBytes);
buffer.putChar((char) this.addrPort);

View File

@@ -21,7 +21,7 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public final class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResponse> {
public final class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResponse, SncpServlet> {
public SncpServer() {
this(System.currentTimeMillis(), null);

View File

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