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 {
Field e = newClazz.getDeclaredField(FIELDPREFIX + "_client");
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);
} catch (NoSuchFieldException ne) {
}
@@ -791,6 +791,7 @@ public abstract class Sncp {
sb.append(newClazz.getName()).append("{name = '").append(name).append("'");
if (client != null) {
sb.append(", serviceid = ").append(client.getServiceid());
sb.append(", serviceversion = ").append(client.getServiceversion());
sb.append(", action.size = ").append(client.getActionCount());
List<String> groups = new ArrayList<>();
if (sameGroupTransport != null) groups.add(sameGroupTransport.getName());
@@ -917,10 +918,10 @@ 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, serviceClass, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
try {
Class newClazz = Class.forName(newDynName.replace('/', '.'));
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");
c.setAccessible(true);
c.set(rs, client);
@@ -930,7 +931,8 @@ public abstract class Sncp {
{
StringBuilder sb = new StringBuilder();
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(", address = ").append(clientAddress).append(", groups = ").append(transport == null ? null : transport.getName());
sb.append(", remoteaddrs = ").append(transport == null ? null : Arrays.asList(transport.getRemoteAddresses()));
@@ -1045,7 +1047,7 @@ public abstract class Sncp {
mv.visitEnd();
}
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 java.lang.reflect.Method method = entry.method;
{
@@ -1156,6 +1158,7 @@ public abstract class Sncp {
T rs = (T) newClazz.newInstance();
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_client");
c.setAccessible(true);
SncpClient client = new SncpClient(name, serviceClass, rs, executor, true, realed ? createLocalServiceClass(name, serviceClass) : serviceClass, clientAddress);
c.set(rs, client);
Field t = newClazz.getDeclaredField(FIELDPREFIX + "_transport");
t.setAccessible(true);
@@ -1164,6 +1167,7 @@ public abstract class Sncp {
StringBuilder sb = new StringBuilder();
sb.append(newClazz.getName()).append("{name = '").append(name);
sb.append("', serviceid = ").append(client.getServiceid());
sb.append(", serviceversion = ").append(client.getServiceversion());
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

@@ -120,15 +120,18 @@ public final class SncpClient {
protected final DLong serviceid;
protected final int serviceversion;
protected final SncpAction[] actions;
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) {
this.remote = remote;
this.executor = executor;
this.serviceClass = serviceClass;
this.serviceversion = service.version();
this.clientAddress = clientAddress;
this.name = serviceName;
this.serviceid = Sncp.hash(serviceType.getName() + ':' + serviceName);
@@ -142,6 +145,15 @@ public final class SncpClient {
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() {
return clientAddress;
}
@@ -150,6 +162,10 @@ public final class SncpClient {
return serviceid;
}
public int getServiceversion() {
return serviceversion;
}
public int getActionCount() {
return actions.length;
}
@@ -158,7 +174,7 @@ 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 + ", name = '" + name
return this.getClass().getSimpleName() + "(service = " + service + ", serviceid = " + serviceid + ", serviceversion = " + serviceversion + ", name = '" + name
+ "', address = " + (clientAddress == null ? "" : (clientAddress.getHostString() + ":" + clientAddress.getPort()))
+ ", 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 (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);
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);
if (!action.actionid.equals(raction)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")");
buffer.getInt(); //地址
@@ -425,6 +443,7 @@ public final class SncpClient {
buffer.putLong(seqid); //序列号
buffer.putChar((char) HEADER_SIZE); //header长度
DLong.write(buffer, this.serviceid);
buffer.putInt(this.serviceversion);
DLong.write(buffer, actionid);
buffer.put(addrBytes);
buffer.putChar((char) this.addrPort);

View File

@@ -20,7 +20,7 @@ import org.redkale.util.*;
*/
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];
@@ -28,6 +28,8 @@ public final class SncpRequest extends Request<SncpContext> {
private long seqid;
private int serviceversion;
private DLong serviceid;
private DLong actionid;
@@ -60,6 +62,7 @@ public final class SncpRequest extends Request<SncpContext> {
return -1;
}
this.serviceid = DLong.read(buffer);
this.serviceversion = buffer.getInt();
this.actionid = DLong.read(buffer);
buffer.get(bufferbytes);
this.bodylength = buffer.getInt();
@@ -92,15 +95,16 @@ public final class SncpRequest extends Request<SncpContext> {
@Override
public String toString() {
return SncpRequest.class.getSimpleName() + "{seqid=" + this.seqid
+ ",serviceid=" + this.serviceid + ",actionid=" + this.actionid
+ ",bodylength=" + this.bodylength + ",bodyoffset=" + this.bodyoffset
+ ",remoteAddress=" + getRemoteAddress() + "}";
+ ",serviceversion=" + this.serviceversion + ",serviceid=" + this.serviceid
+ ",actionid=" + this.actionid + ",bodylength=" + this.bodylength
+ ",bodyoffset=" + this.bodyoffset + ",remoteAddress=" + getRemoteAddress() + "}";
}
@Override
protected void recycle() {
this.seqid = 0;
this.serviceid = null;
this.serviceversion = 0;
this.actionid = null;
this.bodylength = 0;
this.bodyoffset = 0;
@@ -122,6 +126,10 @@ public final class SncpRequest extends Request<SncpContext> {
return seqid;
}
public int getServiceversion() {
return serviceversion;
}
public DLong getServiceid() {
return serviceid;
}
@@ -133,7 +141,7 @@ public final class SncpRequest extends Request<SncpContext> {
public InetSocketAddress getRemoteAddress() {
if (bufferbytes[0] == 0) return null;
return new InetSocketAddress((0xff & bufferbytes[0]) + "." + (0xff & bufferbytes[1]) + "." + (0xff & bufferbytes[2]) + "." + (0xff & bufferbytes[3]),
((0xff00 & (bufferbytes[4] << 8)) | (0xff & bufferbytes[5])));
((0xff00 & (bufferbytes[4] << 8)) | (0xff & bufferbytes[5])));
}
}

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_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); //内部异常
@@ -37,6 +39,7 @@ 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_ILLSERVICEVER) return "serviceversion is invalid";
if (retcode == RETCODE_ILLACTIONID) return "actionid is invalid";
if (retcode == RETCODE_THROWEXCEPTION) return "Inner exception";
return null;
@@ -68,6 +71,7 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
buffer.putLong(request.getSeqid());
buffer.putChar((char) SncpRequest.HEADER_SIZE);
DLong.write(buffer, request.getServiceid());
buffer.putInt(request.getServiceversion());
DLong.write(buffer, request.getActionid());
buffer.put(addrBytes);
buffer.putChar((char) this.addrPort);