This commit is contained in:
Redkale
2016-09-24 18:22:01 +08:00
parent 6f056bbcb6
commit afa45498d4
4 changed files with 48 additions and 13 deletions

View File

@@ -781,7 +781,7 @@ public abstract class Sncp {
try { try {
Field e = newClazz.getDeclaredField(FIELDPREFIX + "_client"); Field e = newClazz.getDeclaredField(FIELDPREFIX + "_client");
e.setAccessible(true); e.setAccessible(true);
client = new SncpClient(name, serviceClass, executor, false, newClazz, clientAddress); client = new SncpClient(name, serviceClass, rs, executor, false, newClazz, clientAddress);
e.set(rs, client); e.set(rs, client);
} catch (NoSuchFieldException ne) { } catch (NoSuchFieldException ne) {
} }
@@ -791,6 +791,7 @@ public abstract class Sncp {
sb.append(newClazz.getName()).append("{name = '").append(name).append("'"); sb.append(newClazz.getName()).append("{name = '").append(name).append("'");
if (client != null) { if (client != null) {
sb.append(", serviceid = ").append(client.getServiceid()); sb.append(", serviceid = ").append(client.getServiceid());
sb.append(", serviceversion = ").append(client.getServiceversion());
sb.append(", action.size = ").append(client.getActionCount()); sb.append(", action.size = ").append(client.getActionCount());
List<String> groups = new ArrayList<>(); List<String> groups = new ArrayList<>();
if (sameGroupTransport != null) groups.add(sameGroupTransport.getName()); if (sameGroupTransport != null) groups.add(sameGroupTransport.getName());
@@ -917,10 +918,10 @@ public abstract class Sncp {
final String anyValueDesc = Type.getDescriptor(AnyValue.class); final String anyValueDesc = Type.getDescriptor(AnyValue.class);
ClassLoader loader = Sncp.class.getClassLoader(); ClassLoader loader = Sncp.class.getClassLoader();
String newDynName = supDynName.substring(0, supDynName.lastIndexOf('/') + 1) + REMOTEPREFIX + serviceClass.getSimpleName(); String newDynName = supDynName.substring(0, supDynName.lastIndexOf('/') + 1) + REMOTEPREFIX + serviceClass.getSimpleName();
final SncpClient client = new SncpClient(name, serviceClass, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
try { try {
Class newClazz = Class.forName(newDynName.replace('/', '.')); Class newClazz = Class.forName(newDynName.replace('/', '.'));
T rs = (T) newClazz.newInstance(); T rs = (T) newClazz.newInstance();
SncpClient client = new SncpClient(name, serviceClass, rs, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_client"); Field c = newClazz.getDeclaredField(FIELDPREFIX + "_client");
c.setAccessible(true); c.setAccessible(true);
c.set(rs, client); c.set(rs, client);
@@ -930,7 +931,8 @@ public abstract class Sncp {
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name); sb.append(newClazz.getName()).append("{name = '").append(name);
sb.append("', serviceid = ").append(client.getServiceid()); sb.append("', serviceid = '").append(client.getServiceid());
sb.append("', serviceversion = ").append(client.getServiceversion());
sb.append(", action.size = ").append(client.getActionCount()); sb.append(", action.size = ").append(client.getActionCount());
sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName()); sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName());
sb.append(", remoteaddrs = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses())); sb.append(", remoteaddrs = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses()));
@@ -1045,7 +1047,7 @@ public abstract class Sncp {
mv.visitEnd(); mv.visitEnd();
} }
int i = -1; int i = -1;
for (final SncpAction entry : client.actions) { for (final SncpAction entry : SncpClient.getSncpActions(realed ? createLocalServiceClass(name, serviceClass) : serviceClass)) {
final int index = ++i; final int index = ++i;
final java.lang.reflect.Method method = entry.method; final java.lang.reflect.Method method = entry.method;
{ {
@@ -1156,6 +1158,7 @@ public abstract class Sncp {
T rs = (T) newClazz.newInstance(); T rs = (T) newClazz.newInstance();
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_client"); Field c = newClazz.getDeclaredField(FIELDPREFIX + "_client");
c.setAccessible(true); c.setAccessible(true);
SncpClient client = new SncpClient(name, serviceClass, rs, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
c.set(rs, client); c.set(rs, client);
Field t = newClazz.getDeclaredField(FIELDPREFIX + "_transport"); Field t = newClazz.getDeclaredField(FIELDPREFIX + "_transport");
t.setAccessible(true); t.setAccessible(true);
@@ -1164,6 +1167,7 @@ public abstract class Sncp {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name); sb.append(newClazz.getName()).append("{name = '").append(name);
sb.append("', serviceid = ").append(client.getServiceid()); sb.append("', serviceid = ").append(client.getServiceid());
sb.append(", serviceversion = ").append(client.getServiceversion());
sb.append(", action.size = ").append(client.getActionCount()); sb.append(", action.size = ").append(client.getActionCount());
sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName()); sb.append(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName());
sb.append(", remotes = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses())); sb.append(", remotes = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses()));

View File

@@ -120,15 +120,18 @@ public final class SncpClient {
protected final DLong serviceid; protected final DLong serviceid;
protected final int serviceversion;
protected final SncpAction[] actions; protected final SncpAction[] actions;
protected final Consumer<Runnable> executor; protected final Consumer<Runnable> executor;
public <T extends Service> SncpClient(final String serviceName, final Class<T> serviceType, final Consumer<Runnable> executor, public <T extends Service> SncpClient(final String serviceName, final Class<T> serviceType, final T service, final Consumer<Runnable> executor,
final boolean remote, final Class serviceClass, final InetSocketAddress clientAddress) { final boolean remote, final Class serviceClass, final InetSocketAddress clientAddress) {
this.remote = remote; this.remote = remote;
this.executor = executor; this.executor = executor;
this.serviceClass = serviceClass; this.serviceClass = serviceClass;
this.serviceversion = service.version();
this.clientAddress = clientAddress; this.clientAddress = clientAddress;
this.name = serviceName; this.name = serviceName;
this.serviceid = Sncp.hash(serviceType.getName() + ':' + serviceName); this.serviceid = Sncp.hash(serviceType.getName() + ':' + serviceName);
@@ -142,6 +145,15 @@ public final class SncpClient {
this.addrPort = clientAddress == null ? 0 : clientAddress.getPort(); this.addrPort = clientAddress == null ? 0 : clientAddress.getPort();
} }
static List<SncpAction> getSncpActions(final Class serviceClass) {
final List<SncpAction> actions = new ArrayList<>();
//------------------------------------------------------------------------------
for (java.lang.reflect.Method method : parseMethod(serviceClass)) {
actions.add(new SncpAction(method, Sncp.hash(method)));
}
return actions;
}
public InetSocketAddress getClientAddress() { public InetSocketAddress getClientAddress() {
return clientAddress; return clientAddress;
} }
@@ -150,6 +162,10 @@ public final class SncpClient {
return serviceid; return serviceid;
} }
public int getServiceversion() {
return serviceversion;
}
public int getActionCount() { public int getActionCount() {
return actions.length; return actions.length;
} }
@@ -158,7 +174,7 @@ public final class SncpClient {
public String toString() { public String toString() {
String service = serviceClass.getName(); String service = serviceClass.getName();
if (remote) service = service.replace(Sncp.LOCALPREFIX, Sncp.REMOTEPREFIX); if (remote) service = service.replace(Sncp.LOCALPREFIX, Sncp.REMOTEPREFIX);
return this.getClass().getSimpleName() + "(service = " + service + ", serviceid = " + serviceid + ", name = '" + name return this.getClass().getSimpleName() + "(service = " + service + ", serviceid = " + serviceid + ", serviceversion = " + serviceversion + ", name = '" + name
+ "', address = " + (clientAddress == null ? "" : (clientAddress.getHostString() + ":" + clientAddress.getPort())) + "', address = " + (clientAddress == null ? "" : (clientAddress.getHostString() + ":" + clientAddress.getPort()))
+ ", actions.size = " + actions.length + ")"; + ", actions.size = " + actions.length + ")";
} }
@@ -411,7 +427,9 @@ public final class SncpClient {
if (rseqid != seqid) throw new RuntimeException("sncp(" + action.method + ") response.seqid = " + seqid + ", but request.seqid =" + rseqid); if (rseqid != seqid) throw new RuntimeException("sncp(" + action.method + ") response.seqid = " + seqid + ", but request.seqid =" + rseqid);
if (buffer.getChar() != HEADER_SIZE) throw new RuntimeException("sncp(" + action.method + ") buffer receive header.length not " + HEADER_SIZE); if (buffer.getChar() != HEADER_SIZE) throw new RuntimeException("sncp(" + action.method + ") buffer receive header.length not " + HEADER_SIZE);
DLong rserviceid = DLong.read(buffer); DLong rserviceid = DLong.read(buffer);
if (!rserviceid.equals(serviceid)) throw new RuntimeException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid); if (!rserviceid.equals(this.serviceid)) throw new RuntimeException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid);
int version = buffer.getInt();
if (version != this.serviceversion) throw new RuntimeException("sncp(" + action.method + ") response.serviceversion = " + serviceversion + ", but request.serviceversion =" + version);
DLong raction = DLong.read(buffer); DLong raction = DLong.read(buffer);
if (!action.actionid.equals(raction)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")"); if (!action.actionid.equals(raction)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")");
buffer.getInt(); //地址 buffer.getInt(); //地址
@@ -425,6 +443,7 @@ public final class SncpClient {
buffer.putLong(seqid); //序列号 buffer.putLong(seqid); //序列号
buffer.putChar((char) HEADER_SIZE); //header长度 buffer.putChar((char) HEADER_SIZE); //header长度
DLong.write(buffer, this.serviceid); DLong.write(buffer, this.serviceid);
buffer.putInt(this.serviceversion);
DLong.write(buffer, actionid); DLong.write(buffer, actionid);
buffer.put(addrBytes); buffer.put(addrBytes);
buffer.putChar((char) this.addrPort); buffer.putChar((char) this.addrPort);

View File

@@ -20,7 +20,7 @@ import org.redkale.util.*;
*/ */
public final class SncpRequest extends Request<SncpContext> { public final class SncpRequest extends Request<SncpContext> {
public static final int HEADER_SIZE = 56; public static final int HEADER_SIZE = 60;
public static final byte[] DEFAULT_HEADER = new byte[HEADER_SIZE]; public static final byte[] DEFAULT_HEADER = new byte[HEADER_SIZE];
@@ -28,6 +28,8 @@ public final class SncpRequest extends Request<SncpContext> {
private long seqid; private long seqid;
private int serviceversion;
private DLong serviceid; private DLong serviceid;
private DLong actionid; private DLong actionid;
@@ -60,6 +62,7 @@ public final class SncpRequest extends Request<SncpContext> {
return -1; return -1;
} }
this.serviceid = DLong.read(buffer); this.serviceid = DLong.read(buffer);
this.serviceversion = buffer.getInt();
this.actionid = DLong.read(buffer); this.actionid = DLong.read(buffer);
buffer.get(bufferbytes); buffer.get(bufferbytes);
this.bodylength = buffer.getInt(); this.bodylength = buffer.getInt();
@@ -92,15 +95,16 @@ public final class SncpRequest extends Request<SncpContext> {
@Override @Override
public String toString() { public String toString() {
return SncpRequest.class.getSimpleName() + "{seqid=" + this.seqid return SncpRequest.class.getSimpleName() + "{seqid=" + this.seqid
+ ",serviceid=" + this.serviceid + ",actionid=" + this.actionid + ",serviceversion=" + this.serviceversion + ",serviceid=" + this.serviceid
+ ",bodylength=" + this.bodylength + ",bodyoffset=" + this.bodyoffset + ",actionid=" + this.actionid + ",bodylength=" + this.bodylength
+ ",remoteAddress=" + getRemoteAddress() + "}"; + ",bodyoffset=" + this.bodyoffset + ",remoteAddress=" + getRemoteAddress() + "}";
} }
@Override @Override
protected void recycle() { protected void recycle() {
this.seqid = 0; this.seqid = 0;
this.serviceid = null; this.serviceid = null;
this.serviceversion = 0;
this.actionid = null; this.actionid = null;
this.bodylength = 0; this.bodylength = 0;
this.bodyoffset = 0; this.bodyoffset = 0;
@@ -122,6 +126,10 @@ public final class SncpRequest extends Request<SncpContext> {
return seqid; return seqid;
} }
public int getServiceversion() {
return serviceversion;
}
public DLong getServiceid() { public DLong getServiceid() {
return serviceid; return serviceid;
} }

View File

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