diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f3ab91 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# tinyrpc-java diff --git a/pom.xml b/pom.xml index 5ea21c4..4de6fe8 100644 --- a/pom.xml +++ b/pom.xml @@ -43,16 +43,6 @@ netty-all 4.1.86.Final - - org.apache.logging.log4j - log4j-api - 2.19.0 - - - org.apache.logging.log4j - log4j-core - 2.19.0 - com.lmax disruptor @@ -73,6 +63,11 @@ protobuf-java 3.19.4 + + org.reflections + reflections + 0.9.11 + diff --git a/src/main/java/com/iker/tinyrpc/TinyRpcInitializer.java b/src/main/java/com/iker/tinyrpc/TinyRpcInitializer.java new file mode 100644 index 0000000..1a0082f --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/TinyRpcInitializer.java @@ -0,0 +1,33 @@ +package com.iker.tinyrpc; + +import com.iker.tinyrpc.util.SpringContextUtil; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Arrays; + +@Component +@Order(value = 1) +@Slf4j +public class TinyRpcInitializer implements CommandLineRunner { + + @Getter + @Resource + private AnnotationConfigApplicationContext applicationContext; + + @Override + public void run(String... args) throws Exception { + SpringContextUtil.setApplicationContext(applicationContext); + log.info(String.valueOf(applicationContext.getClass())); + String[] beans = applicationContext.getBeanDefinitionNames(); + Arrays.sort(beans); + for (String bean : beans) { + System.out.println(bean + " of Type :: " + applicationContext.getBean(bean).getClass()); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/iker/tinyrpc/TinyRpcJavaApplication.java b/src/main/java/com/iker/tinyrpc/TinyRpcJavaApplication.java index 69805f2..989cbcc 100644 --- a/src/main/java/com/iker/tinyrpc/TinyRpcJavaApplication.java +++ b/src/main/java/com/iker/tinyrpc/TinyRpcJavaApplication.java @@ -15,31 +15,14 @@ @SpringBootApplication @Slf4j -public class TinyRpcJavaApplication implements CommandLineRunner { +public class TinyRpcJavaApplication { public static void main(String[] args) { log.info("TinyRpcJavaApplication run begin"); - ConfigurableApplicationContext context = SpringApplication.run(TinyRpcJavaApplication.class, args); - SpringContextUtil.setApplicationContext(context); + SpringApplication.run(TinyRpcJavaApplication.class, args); log.info("TinyRpcJavaApplication run end"); - } - - @Getter - @Resource - private ApplicationContext applicationContext; - /** - * @param args incoming main method arguments - * @throws Exception - */ - @Override - public void run(String... args) throws Exception { -// String[] beans = applicationContext.getBeanDefinitionNames(); -// Arrays.sort(beans); -// for (String bean : beans) -// { -// System.out.println(bean + " of Type :: " + applicationContext.getBean(bean).getClass()); -// } } + } diff --git a/src/main/java/com/iker/tinyrpc/annotation/AnnotationContextHandler.java b/src/main/java/com/iker/tinyrpc/annotation/AnnotationContextHandler.java new file mode 100644 index 0000000..e6cd982 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/annotation/AnnotationContextHandler.java @@ -0,0 +1,45 @@ +package com.iker.tinyrpc.annotation; + +import lombok.Getter; +import org.reflections.Reflections; + +import java.lang.annotation.Annotation; +import java.util.Set; + +public class AnnotationContextHandler { + + @Getter + private final String packageName; + + private final Reflections reflections; + + public AnnotationContextHandler(String packageName) { + this.packageName = packageName; + reflections = new Reflections(packageName); + } + + public Set> scanAnnotation(Class clazz) { + return reflections.getTypesAnnotatedWith(clazz); + +// for (Class item : classSet) { +// TinyPBService annotation = item.getAnnotation(TinyPBService.class); +// String name = Optional.ofNullable(annotation).orElseThrow( +// () -> { throw new RuntimeException("get TinyPBService annotation null"); } +// ).name(); +// +// if (name.isEmpty()) { +// name = item.getSuperclass().getSimpleName(); +// } +// +// try { +// SpringContextUtil.getBean("rpcServiceFactory", ProtobufRpcServiceFactory.class).registerService(name, item.newInstance()); +// } catch (InstantiationException | IllegalAccessException e) { +// throw new RuntimeException(e); +// } +// +// } + } + + + +} diff --git a/src/main/java/com/iker/tinyrpc/annotation/TinyPBService.java b/src/main/java/com/iker/tinyrpc/annotation/TinyPBService.java new file mode 100644 index 0000000..743ee5c --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/annotation/TinyPBService.java @@ -0,0 +1,10 @@ +package com.iker.tinyrpc.annotation; + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface TinyPBService { + String serviceName() default ""; +} diff --git a/src/main/java/com/iker/tinyrpc/controller/TinyPBRpcController.java b/src/main/java/com/iker/tinyrpc/controller/TinyPBRpcController.java deleted file mode 100644 index caeb7c7..0000000 --- a/src/main/java/com/iker/tinyrpc/controller/TinyPBRpcController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.iker.tinyrpc.controller; - -public class TinyPBRpcController { -} diff --git a/src/main/java/com/iker/tinyrpc/net/TcpClient.java b/src/main/java/com/iker/tinyrpc/net/TcpClient.java index d461ac4..41f379a 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpClient.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpClient.java @@ -1,6 +1,6 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.protocol.AbstractProtocol; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; import com.iker.tinyrpc.util.TinyRpcSystemException; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -12,8 +12,9 @@ import lombok.extern.slf4j.Slf4j; import java.net.InetSocketAddress; +import java.util.Optional; -import static com.iker.tinyrpc.util.TinyPBErrorCode.ERROR_FAILED_CONNECT; +import static com.iker.tinyrpc.util.TinyRpcErrorCode.ERROR_FAILED_CONNECT; @Slf4j public class TcpClient { @@ -32,64 +33,61 @@ public class TcpClient { private ChannelFuture connectChannelFuture; - public TcpClient(EventLoopGroup eventLoopGroup) { - this.eventLoopGroup = eventLoopGroup; - } - public TcpClient(InetSocketAddress peerAddress, EventLoopGroup eventLoopGroup) { this.peerAddress = peerAddress; this.eventLoopGroup = eventLoopGroup; } - public void connect() throws InterruptedException, TinyRpcSystemException { - if (this.peerAddress == null) { - throw new TinyRpcSystemException("connect failed, not set peerAddress"); - } - connect(this.peerAddress); - } - - public void connect(InetSocketAddress peerAddress) throws TinyRpcSystemException { - if (this.peerAddress != null) { - throw new TinyRpcSystemException("init client failed, peerAddress has already set"); - } - this.peerAddress = peerAddress; - + public ChannelFuture connect() { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup) .channel(NioSocketChannel.class) - .remoteAddress(peerAddress) + .remoteAddress(Optional.ofNullable(peerAddress).orElseThrow( + () -> { throw new TinyRpcSystemException(ERROR_FAILED_CONNECT, "peer address is null"); } + )) .handler(new TcpClientChannelInitializer()); - connectChannelFuture = bootstrap.connect(); channel = connectChannelFuture.channel(); connectChannelFuture.addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { - this.peerAddress = (InetSocketAddress) future.channel().remoteAddress(); - this.localAddress = (InetSocketAddress) future.channel().localAddress(); + peerAddress = (InetSocketAddress) future.channel().remoteAddress(); + localAddress = (InetSocketAddress) future.channel().localAddress(); log.info(String.format("success connect to remoteAddr[%s:%d], localAddr[%s:%d]", - this.peerAddress.getHostName(), this.peerAddress.getPort(), - this.localAddress.getHostName(), this.localAddress.getPort())); + peerAddress.getHostName(), peerAddress.getPort(), + localAddress.getHostName(), localAddress.getPort())); } else { - log.error("connect failed, stack info:"); - future.cause().printStackTrace(); - throw new TinyRpcSystemException(future.cause().getMessage()); + log.error(String.format("connect failed, peer addr[%s:%d]", peerAddress.getHostName(), peerAddress.getPort())); + log.error("exception: ", future.cause()); } }); + return connectChannelFuture; } - public ChannelFuture sendMessage(AbstractProtocol protocol) { + public ChannelFuture sendMessage(RpcProtocol protocol) { if (!connectChannelFuture.isDone()) { try { connectChannelFuture.sync(); } catch (InterruptedException e) { - throw new RuntimeException(e); + log.error("exception ", e); + throw new TinyRpcSystemException(e.getMessage()); } } if (!channel.isActive()) { - throw new TinyRpcSystemException(ERROR_FAILED_CONNECT, "sendMessage error, connection is not active"); + throw new TinyRpcSystemException(ERROR_FAILED_CONNECT, String.format("sendMessage error, connection[%s:%d] is not active", peerAddress.getHostName(), peerAddress.getPort())); } - return channel.writeAndFlush(protocol); + return channel.writeAndFlush(protocol).addListener( future -> { + if (future.isSuccess()) { + log.info(String.format("success send protocol message[%s] to remoteAddr[%s:%d]", protocol.getMsgReq(), peerAddress.getHostName(), peerAddress.getPort())); + } else { + log.error(String.format("failed send protocol message[%s] to remoteAddr[%s:%d]", protocol.getMsgReq(), peerAddress.getHostName(), peerAddress.getPort())); + log.error("exception: ", future.cause()); + } + }); + } + + public void awaitResponseWithTimeout(String msgReq, int timeout) throws InterruptedException { +// channel. } } diff --git a/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInboundHandler.java b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInboundHandler.java index 774ab46..2ecc50a 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInboundHandler.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInboundHandler.java @@ -1,9 +1,14 @@ package com.iker.tinyrpc.net; +import com.iker.tinyrpc.net.rpc.RpcFutureMap; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; +import com.iker.tinyrpc.util.SpringContextUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j; +import java.util.Optional; + @Slf4j public class TcpClientChannelInboundHandler extends ChannelInboundHandlerAdapter { @@ -49,6 +54,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.debug("channelInactive{}", ctx.channel().remoteAddress()); super.channelInactive(ctx); } @@ -59,37 +65,16 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.debug("channelRead{}", ctx.channel().remoteAddress().toString()); + Optional.ofNullable((RpcProtocol) msg).ifPresent( + (protocol) -> { + log.info(String.format("success get reply protocol of msgReq [%s]", protocol.getMsgReq())); + SpringContextUtil.getApplicationContext().getBean(RpcFutureMap.class).getFuture(protocol.getMsgReq()).invoke(protocol); + } + ); super.channelRead(ctx, msg); } - /** - * @param ctx - * @throws Exception - */ - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - super.channelReadComplete(ctx); - } - - /** - * @param ctx - * @param evt - * @throws Exception - */ - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - super.userEventTriggered(ctx, evt); - } - - /** - * @param ctx - * @throws Exception - */ - @Override - public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { - super.channelWritabilityChanged(ctx); - } - /** * @param ctx * @param cause diff --git a/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInitializer.java b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInitializer.java index 5a842fc..c33a3a1 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInitializer.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelInitializer.java @@ -1,7 +1,7 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.codec.TinyPBDecoder; -import com.iker.tinyrpc.codec.TinyPBEncoder; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBDecoder; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBEncoder; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; @@ -15,9 +15,10 @@ public class TcpClientChannelInitializer extends ChannelInitializer { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(75, 75, 75, TimeUnit.SECONDS)) - .addLast("decoder", new TinyPBDecoder()) - .addLast("inboundHandlerAdapter", new TcpClientChannelInboundHandler()) - .addLast("encoder", new TinyPBEncoder()) + .addLast("tinyPBDecoder", new TinyPBDecoder()) + .addLast("inBoundHandler", new TcpClientChannelInboundHandler()) + .addLast("tinyPBEncoder", new TinyPBEncoder()) + .addLast("outBoundHandler", new TcpClientChannelOutboundHandler()) .addLast("exceptionHandler", new TcpServerExceptionHandler()); } } diff --git a/src/main/java/com/iker/tinyrpc/net/TcpClientChannelOutboundHandler.java b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelOutboundHandler.java new file mode 100644 index 0000000..82335d6 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/TcpClientChannelOutboundHandler.java @@ -0,0 +1,57 @@ +package com.iker.tinyrpc.net; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; +import lombok.extern.slf4j.Slf4j; + +import java.net.SocketAddress; + +@Slf4j +public class TcpClientChannelOutboundHandler extends ChannelOutboundHandlerAdapter { + public TcpClientChannelOutboundHandler() { + super(); + } + + @Override + public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { + super.bind(ctx, localAddress, promise); + } + + @Override + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception { + super.connect(ctx, remoteAddress, localAddress, promise); + } + + @Override + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + super.disconnect(ctx, promise); + } + + @Override + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + super.close(ctx, promise); + } + + @Override + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + super.deregister(ctx, promise); + } + + @Override + public void read(ChannelHandlerContext ctx) throws Exception { + super.read(ctx); + } + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("begin to write"); + super.write(ctx, msg, promise); + log.info("end to write"); + } + + @Override + public void flush(ChannelHandlerContext ctx) throws Exception { + super.flush(ctx); + } +} diff --git a/src/main/java/com/iker/tinyrpc/net/TcpServer.java b/src/main/java/com/iker/tinyrpc/net/TcpServer.java index 3b7f0c3..3dbc230 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpServer.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpServer.java @@ -1,6 +1,9 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.util.TinyRpcSystemException; +import com.iker.tinyrpc.annotation.AnnotationContextHandler; +import com.iker.tinyrpc.annotation.TinyPBService; +import com.iker.tinyrpc.net.rpc.protobuf.ProtobufRpcServiceFactory; +import com.iker.tinyrpc.util.SpringContextUtil; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -11,64 +14,91 @@ import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.stereotype.Component; -import javax.annotation.Resource; import java.net.InetSocketAddress; +import java.util.Optional; +import java.util.Set; @Slf4j +@Component public class TcpServer { @Getter - @Setter private EventLoopGroup mainLoopGroup; // mainReactor @Getter - @Setter private EventLoopGroup workerLoopGroup; // io subReactors @Getter - private InetSocketAddress localAddress; + @Setter + private InetSocketAddress localAddress; // listen addr + + public void initMainLoopGroup(int size) { + mainLoopGroup = new NioEventLoopGroup(size); + } - public void start(InetSocketAddress localAddress) throws InterruptedException, TinyRpcSystemException { - try { - if(this.localAddress != null) { - throw new TinyRpcSystemException("TinyRPC TcpServer start error, local address has set."); + public void initWorkerLoopGroup(int size) { + workerLoopGroup = new NioEventLoopGroup(size); + } + + public void registerService() { + AnnotationContextHandler annotationContextHandler = new AnnotationContextHandler("com.iker.tinyrpc"); + Set> classSet = annotationContextHandler.scanAnnotation(TinyPBService.class); + for (Class item : classSet) { + TinyPBService annotation = item.getAnnotation(TinyPBService.class); + String name = Optional.ofNullable(annotation).orElseThrow( + () -> { throw new RuntimeException("get TinyPBService annotation null"); } + ).serviceName(); + + // register name must be same as the service's name in protobuf file + if (name.isEmpty()) { + name = item.getSuperclass().getSimpleName(); } - this.localAddress = localAddress; - - workerLoopGroup = new NioEventLoopGroup(4); - mainLoopGroup = new NioEventLoopGroup(1); - - ServerBootstrap serverBootstrap = new ServerBootstrap() - .group(mainLoopGroup, workerLoopGroup) - .option(ChannelOption.SO_BACKLOG, 128) - .channel(NioServerSocketChannel.class) - .childHandler(new TcpServerChannelInitializer()) - .localAddress(localAddress); - - ChannelFuture channelFuture = serverBootstrap.bind().sync(); - channelFuture.addListener((ChannelFutureListener) future -> { - if (future.isSuccess()) { - InetSocketAddress address = (InetSocketAddress)future.channel().localAddress(); - assert (address != null); - log.info(String.format("TinyRPC TcpServer start success, listen on [%s:%d]", getLocalAddress().getHostString(), getLocalAddress().getPort())); - } else { - log.error("TinyRPC TcpServer start error"); - future.cause().printStackTrace(); - throw new TinyRpcSystemException(future.cause().getMessage()); - } - }); - - // wait until close this channel - channelFuture.channel().closeFuture().sync(); - log.info("TinyRPC quit success"); - } catch (InterruptedException e) { - throw e; - } finally { - mainLoopGroup.shutdownGracefully().sync(); - workerLoopGroup.shutdownGracefully().sync(); + + // 1. register service to BeanFactory + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); + beanDefinition.setBeanClass(item); + SpringContextUtil.getBeanFactory().registerBeanDefinition(item.getName(), beanDefinition); + + // 2. get this bean, then register to RpcServiceFactory + SpringContextUtil.getBean(ProtobufRpcServiceFactory.class).registerService(name, SpringContextUtil.getBean(item)); + } + } + + public void start() throws InterruptedException { + + registerService(); + + ServerBootstrap serverBootstrap = new ServerBootstrap() + .group(mainLoopGroup, workerLoopGroup) + .option(ChannelOption.SO_BACKLOG, 128) + .channel(NioServerSocketChannel.class) + .childHandler(new TcpServerChannelInitializer()) + .localAddress(localAddress); + ChannelFuture channelFuture = serverBootstrap.bind().sync(); + channelFuture.addListener((ChannelFutureListener) future -> { + if(future.isSuccess()){ + InetSocketAddress address = (InetSocketAddress) future.channel().localAddress(); + assert (address != null); + log.info(String.format("TinyRPC TcpServer start success, listen on [%s:%d]", getLocalAddress().getHostString(), getLocalAddress().getPort())); + } else { + log.error("TinyRPC TcpServer start error"); + log.error("exception:", future.cause()); + throw new RuntimeException(future.cause().getMessage()); + } + }); + + // wait until close this channel + channelFuture.channel().closeFuture().sync(); + log.info("TinyRPC quit success"); + + mainLoopGroup.shutdownGracefully().sync(); + workerLoopGroup.shutdownGracefully().sync(); } + + } diff --git a/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInboundHandler.java b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInboundHandler.java index c1d9484..b8afa2a 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInboundHandler.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInboundHandler.java @@ -1,13 +1,14 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.protocol.TinyPBProtocol; -import com.iker.tinyrpc.util.TinyRpcSystemException; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBRpcDispatcher; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBProtocol; +import com.iker.tinyrpc.util.SpringContextUtil; import io.netty.channel.*; import lombok.extern.slf4j.Slf4j; import java.net.InetSocketAddress; - -import static com.iker.tinyrpc.util.TinyPBErrorCode.ERROR_FAILED_DECODE; +import java.util.Optional; +; @Slf4j @ChannelHandler.Sharable @@ -90,13 +91,13 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { InetSocketAddress address = (InetSocketAddress)ctx.channel().remoteAddress(); log.info("channelRead, remote addr: " + address.getHostString()); - TinyPBProtocol protocol = (TinyPBProtocol) msg; - if (protocol != null) { - log.info(String.format("get protocol of msgReq [%s]", protocol.getMsgReq())); - } else { - throw new TinyRpcSystemException(ERROR_FAILED_DECODE, "failed get object"); - } -// super.channelRead(ctx, msg); + + Optional.ofNullable((TinyPBProtocol) msg).ifPresent( + (protocol) -> { + log.info(String.format("get protocol of msgReq [%s]", protocol.getMsgReq())); + SpringContextUtil.getApplicationContext().getBean(TinyPBRpcDispatcher.class).dispatch(protocol, ctx.channel()); + } + ); } @@ -157,8 +158,8 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - super.exceptionCaught(ctx, cause); InetSocketAddress address = (InetSocketAddress)ctx.channel().remoteAddress(); log.info("exceptionCaught, remote addr: " + address.getHostString()); + super.exceptionCaught(ctx, cause); } } diff --git a/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInitializer.java b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInitializer.java index c64047d..8c0f372 100644 --- a/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInitializer.java +++ b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelInitializer.java @@ -1,7 +1,7 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.codec.TinyPBDecoder; -import com.iker.tinyrpc.codec.TinyPBEncoder; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBDecoder; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBEncoder; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; @@ -17,8 +17,9 @@ public class TcpServerChannelInitializer extends ChannelInitializer { protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(75, 75, 75, TimeUnit.SECONDS)) .addLast("tinyPBDecoder", new TinyPBDecoder()) + .addLast("inBoundHandler", new TcpServerChannelInboundHandler()) .addLast("tinyPBEncoder", new TinyPBEncoder()) - .addLast("inboundHandler", new TcpServerChannelInboundHandler()) + .addLast("outBoundHandler", new TcpServerChannelOutboundHandler()) .addLast("exceptionHandler", new TcpServerExceptionHandler()); } } diff --git a/src/main/java/com/iker/tinyrpc/net/TcpServerChannelOutboundHandler.java b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelOutboundHandler.java new file mode 100644 index 0000000..cf1b804 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/TcpServerChannelOutboundHandler.java @@ -0,0 +1,17 @@ +package com.iker.tinyrpc.net; + +import io.netty.channel.*; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TcpServerChannelOutboundHandler extends ChannelOutboundHandlerAdapter { + public TcpServerChannelOutboundHandler() { + super(); + } + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + super.write(ctx, msg, promise); + } + +} diff --git a/src/main/java/com/iker/tinyrpc/net/future/RpcFuture.java b/src/main/java/com/iker/tinyrpc/net/future/RpcFuture.java new file mode 100644 index 0000000..1608ca3 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/future/RpcFuture.java @@ -0,0 +1,10 @@ +package com.iker.tinyrpc.net.future; + +import java.util.concurrent.*; + +public interface RpcFuture extends Future { + String getId(); + + // to invoke the future who is call get() + void invoke(T object); +} diff --git a/src/main/java/com/iker/tinyrpc/net/future/RpcSyncFuture.java b/src/main/java/com/iker/tinyrpc/net/future/RpcSyncFuture.java new file mode 100644 index 0000000..75b8276 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/future/RpcSyncFuture.java @@ -0,0 +1,144 @@ +package com.iker.tinyrpc.net.future; + +import com.iker.tinyrpc.net.rpc.protobuf.DefaultRpcCallback; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; + +import java.util.Optional; +import java.util.concurrent.*; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +@Slf4j +public class RpcSyncFuture implements RpcFuture { + + private static final int DEFAULT_TIMEOUT = 2000; // 默认超时时间,2000 ms + + private final String id; + + private final Lock lock = new ReentrantLock(); + private final Condition condition = lock.newCondition(); + + private final DefaultRpcCallback callback; + + @Setter + private boolean done = false; + + @Setter + private boolean canceled = false; + + @Setter + private boolean needInvoke = false; + + @Setter + private RpcProtocol reply; + + public RpcSyncFuture(String id, DefaultRpcCallback callback) { + this.id = id; + this.callback = callback; + } + + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + if (canceled) { + return true; + } + canceled = true; + return true; + } + + @Override + public boolean isCancelled() { + return canceled; + } + + @Override + public boolean isDone() { + return done; + } + + @Override + public RpcProtocol get() throws InterruptedException, ExecutionException { + return get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); + } + + + public RpcProtocol get(long timeout) throws InterruptedException, ExecutionException { + return get(timeout, TimeUnit.MILLISECONDS); + } + + @Override + public RpcProtocol get(long timeout, TimeUnit unit) { + if (done) { + return reply; + } + + lock.lock(); + needInvoke = true; + if (reply != null) { + return reply; + } + try { + while (!done) { + if(condition.await(timeout, unit)){ + // invoked by other thread + log.info("RpcFuture:{} invoked", id); + } else { + // timeout + log.info("RpcFuture:{} timeout", id); + } + } + } catch (InterruptedException e) { + log.error("InterruptedException", e); + } finally { + lock.unlock(); + } + + runCallback(); + + return reply; + } + + @Override + public String getId() { + return id; + } + + @Override + public void invoke(RpcProtocol object) { + if (done) { + return; + } + // 是否需要 notify 原线程 + // 如果主调线程调用了 get, 就需要 notify + // 否则直接当前线程执行回调即可 + if (isNeedInvoke()){ + lock.lock(); + + reply = object; + + setDone(true); + + condition.signal(); + + lock.unlock(); + + } else { + runCallback(); + } + } + + private boolean isNeedInvoke () { + return needInvoke; + } + + private void runCallback() { + Optional.ofNullable(callback).ifPresent( + DefaultRpcCallback::run + ); + } +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/AbstractRpcDispatcher.java b/src/main/java/com/iker/tinyrpc/net/rpc/RpcDispatcher.java similarity index 62% rename from src/main/java/com/iker/tinyrpc/net/rpc/AbstractRpcDispatcher.java rename to src/main/java/com/iker/tinyrpc/net/rpc/RpcDispatcher.java index 3600567..f630fdf 100644 --- a/src/main/java/com/iker/tinyrpc/net/rpc/AbstractRpcDispatcher.java +++ b/src/main/java/com/iker/tinyrpc/net/rpc/RpcDispatcher.java @@ -1,13 +1,14 @@ package com.iker.tinyrpc.net.rpc; -import com.iker.tinyrpc.protocol.AbstractProtocol; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; import com.iker.tinyrpc.util.TinyRpcSystemException; +import io.netty.channel.Channel; -public abstract class AbstractRpcDispatcher { +public interface RpcDispatcher { /** * @param protocol protocol object, such as TinyPBProtocol * @throws TinyRpcSystemException * To dispatch rpc request according protocol, so that can server call designated method do business things, and reply to client */ - abstract void dispatch(AbstractProtocol protocol) throws TinyRpcSystemException; + void dispatch(RpcProtocol protocol, Channel channel) throws TinyRpcSystemException; } diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/RpcFutureMap.java b/src/main/java/com/iker/tinyrpc/net/rpc/RpcFutureMap.java new file mode 100644 index 0000000..a06fd9d --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/RpcFutureMap.java @@ -0,0 +1,40 @@ +package com.iker.tinyrpc.net.rpc; + +import com.iker.tinyrpc.net.future.RpcFuture; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class RpcFutureMap { + + private final Map> rpcResponseMap = new HashMap<>(); + + public void addFuture(RpcFuture future) { + synchronized (rpcResponseMap) { + rpcResponseMap.put(future.getId(), future); + } + + } + + public RpcFuture getFuture(String key) { + synchronized (rpcResponseMap) { + if (rpcResponseMap.containsKey(key)) { + return rpcResponseMap.get(key); + } + return null; + } + } + + public boolean deleteFuture(String key) { + synchronized (rpcResponseMap) { + if (rpcResponseMap.containsKey(key)) { + rpcResponseMap.remove(key); + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/RpcServiceFactory.java b/src/main/java/com/iker/tinyrpc/net/rpc/RpcServiceFactory.java deleted file mode 100644 index 91d7464..0000000 --- a/src/main/java/com/iker/tinyrpc/net/rpc/RpcServiceFactory.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.iker.tinyrpc.net.rpc; - -import com.iker.tinyrpc.util.TinyRpcSystemException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import java.util.Map; -import java.util.Optional; - -@Component -@Slf4j -public class RpcServiceFactory { - - @Resource - private Map rpcServiceMap; - - public void registerService(String key, Object object) { - if (rpcServiceMap.containsKey(key)) { - throw new TinyRpcSystemException(String.format("registerService error, key %s exist", key)); - } - rpcServiceMap.put(key, object); - log.info(String.format("register %s success", key)); - } - - public Optional getService(String key) { - return Optional.ofNullable(rpcServiceMap.get(key)); - } - - -} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcDispatcher.java b/src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcDispatcher.java deleted file mode 100644 index 1f077a0..0000000 --- a/src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcDispatcher.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.iker.tinyrpc.net.rpc; - -import com.google.protobuf.*; -import com.iker.tinyrpc.protocol.AbstractProtocol; -import com.iker.tinyrpc.protocol.TinyPBProtocol; -import com.iker.tinyrpc.util.SpringContextUtil; -import com.iker.tinyrpc.util.TinyRpcSystemException; - -import com.iker.tinyrpc.util.TinyPBErrorCode; - -import java.util.Optional; - - -public class TinyPBRpcDispatcher extends AbstractRpcDispatcher { - - @Override - void dispatch(AbstractProtocol protocol) throws TinyRpcSystemException { - TinyPBProtocol tinyPBProtocol = (TinyPBProtocol) protocol; - if (tinyPBProtocol == null) { - throw new TinyRpcSystemException(TinyPBErrorCode.ERROR_FAILED_DESERIALIZE, "failed to deserialize TinyPB protocol object, get null object"); - } - String serviceName = ""; - String methodName = ""; - - // get RPC protobuf service object - Service service = (Service) SpringContextUtil.getBean("rpcServiceFactory", RpcServiceFactory.class).getService(serviceName).orElseThrow( - () -> { - throw new TinyRpcSystemException(TinyPBErrorCode.ERROR_SERVICE_NOT_FOUND, String.format("not found service name of [%s]", serviceName)); - } - ); - - // find method by methodName - Descriptors.MethodDescriptor methodDescriptor = Optional.ofNullable(service.getDescriptorForType().findMethodByName(methodName)).orElseThrow( - () -> { - throw new TinyRpcSystemException(TinyPBErrorCode.ERROR_METHOD_NOT_FOUND, String.format("not found service name of [%s]", serviceName)); - } - ); - - try { - Message request = service.getRequestPrototype(methodDescriptor).newBuilderForType().mergeFrom(ByteString.copyFromUtf8(tinyPBProtocol.getPbData())).build(); - TinyPBRpcController rpcController = new TinyPBRpcController(); - rpcController.setMsgReq(tinyPBProtocol.getMsgReq()); - rpcController.setMethodFullName(tinyPBProtocol.getServiceName()); - rpcController.setMethodName(methodName); - -// service.callMethod(methodDescriptor, rpcController, request, ); - - } catch (InvalidProtocolBufferException e) { - throw new TinyRpcSystemException(TinyPBErrorCode.ERROR_FAILED_DESERIALIZE, "failed deserialize protobuf data from request"); - } - - - - } -} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/AbstractRpcChannel.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/AbstractRpcChannel.java new file mode 100644 index 0000000..c6bb8dc --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/AbstractRpcChannel.java @@ -0,0 +1,162 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import com.google.protobuf.*; +import com.iker.tinyrpc.net.TcpClient; +import com.iker.tinyrpc.net.TcpServer; +import com.iker.tinyrpc.net.future.RpcSyncFuture; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBProtocol; +import com.iker.tinyrpc.util.SpringContextUtil; +import com.iker.tinyrpc.util.TinyRpcErrorCode; +import com.iker.tinyrpc.util.TinyRpcSystemException; +import io.netty.channel.ChannelFuture; +import io.netty.channel.nio.NioEventLoopGroup; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +@Slf4j +public abstract class AbstractRpcChannel implements DefaultRpcChannel { + protected final InetSocketAddress peerAddr; + + protected TinyPBProtocol sendProtocol; + + protected TinyPBProtocol replyProtocol; + + protected TinyRpcController tinyRpcController; + + protected TcpClient tcpClient; + + protected RpcCallback done; + + protected Message request; + + protected Message responsePrototype; + + @Getter + protected RpcSyncFuture replyFuture; + + public AbstractRpcChannel(InetSocketAddress peerAddr) { + this.peerAddr = peerAddr; + } + + + @Override + public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback done) { + try { + init(controller, request, responsePrototype, done); + + fillRpcController(method); + + generateTinyPBProtocol(request); + + + ioHandler(); + + } catch (TinyRpcSystemException e) { + log.error(String.format("call rpc occur TinyRpcSystemException, error code [%d], error info [%s]", e.getErrorCode().ordinal(), e.getErrorInfo())); + log.error("exception: ", e); + tinyRpcController.setErrCode(e.getErrorCode()); + tinyRpcController.setFailed(e.getErrorInfo()); + callBack(); + } +// } catch (InvalidProtocolBufferException e) { +// log.error(String.format("call rpc occur InvalidProtocolBufferException, error info [%s]", e.getMessage())); +// log.error("exception: ", e); +// tinyRpcController.setErrCode(TinyRpcErrorCode.ERROR_FAILED_DESERIALIZE); +// tinyRpcController.setFailed("failed to deserialize response protobuf data"); +// callBack(); +// } + } + + private void init(RpcController controller, Message request, Message responsePrototype, RpcCallback done) { + this.request = request; + this.responsePrototype = responsePrototype; + this.done = done; + tinyRpcController = (TinyRpcController) controller; + } + + protected void fillRpcController(Descriptors.MethodDescriptor method) { + + tinyRpcController.setMethodFullName(method.getFullName()); + tinyRpcController.setMethodName(method.getName()); + tinyRpcController.setPeerAddr(peerAddr); + + if (tinyRpcController.getMsgReq().isEmpty()) { + tinyRpcController.setMsgReq("123456789"); + } + + } + + protected Message parseResponse(Message responsePrototype) { + try { + RpcProtocol protocol = replyFuture.get(); + if (protocol != null) { + TinyPBProtocol tinyPBProtocol = (TinyPBProtocol) (protocol); + return responsePrototype.toBuilder().mergeFrom(ByteString.copyFrom(tinyPBProtocol.getPbData(), StandardCharsets.ISO_8859_1)).build(); + } + } catch (InterruptedException | ExecutionException | InvalidProtocolBufferException e) { + throw new RuntimeException(e); + } + return null; + } + + + protected void generateTinyPBProtocol(Message request){ + + sendProtocol = new TinyPBProtocol(); + + sendProtocol.setMsgReq(tinyRpcController.getMsgReq()); + try { + sendProtocol.setPbData(request.toByteString().toString("ISO-8859-1")); + } catch (UnsupportedEncodingException e) { + throw new TinyRpcSystemException(TinyRpcErrorCode.ERROR_FAILED_DESERIALIZE, "failed to deserialize request data"); + } + sendProtocol.setServiceName(tinyRpcController.getMethodFullName()); + sendProtocol.resetPackageLen(); + + replyProtocol = new TinyPBProtocol(); + replyProtocol.setMsgReq(sendProtocol.getMsgReq()); + replyProtocol.setServiceName(sendProtocol.getServiceName()); + + } + + protected ChannelFuture asyncConnect() { + tcpClient = new TcpClient(peerAddr, Optional.ofNullable(SpringContextUtil.getBean(TcpServer.class).getWorkerLoopGroup()).orElse( + new NioEventLoopGroup(1) + ).next()); + + return tcpClient.connect(); + } + + protected ChannelFuture asyncSendMessage() { + return tcpClient.sendMessage(sendProtocol); + } + + protected void setError(TinyRpcErrorCode errorCode, String errorInfo) { + tinyRpcController.setErrCode(errorCode); + tinyRpcController.setErrInfo(errorInfo); + tinyRpcController.setFailed(errorInfo); + } + + protected void callBack() { + if (done != null) { + log.info("now callback"); + done.run(responsePrototype); + } + } + + + /** + * send protocol and get reply at there, you can do it by yourself, such as: + * 1. sync: send data, then blocking wait until get peer server's reply + * 2. async: send data, and set callback, then do other things. callback will run when get peer server's reply + */ + protected abstract void ioHandler() throws TinyRpcSystemException; + +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcCallback.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcCallback.java new file mode 100644 index 0000000..249e143 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcCallback.java @@ -0,0 +1,9 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Message; +import com.google.protobuf.RpcCallback; + +public interface DefaultRpcCallback { + void run(); +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcChannel.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcChannel.java new file mode 100644 index 0000000..d0d6e03 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/DefaultRpcChannel.java @@ -0,0 +1,7 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import com.google.protobuf.RpcChannel; + +public interface DefaultRpcChannel extends RpcChannel { + +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/ProtobufRpcServiceFactory.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/ProtobufRpcServiceFactory.java new file mode 100644 index 0000000..8c4b906 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/ProtobufRpcServiceFactory.java @@ -0,0 +1,43 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Component +@Slf4j +public class ProtobufRpcServiceFactory { + + private final Map rpcServiceMap = new HashMap<>(); + + public void registerService(Object object) throws RuntimeException { + String key = Optional.ofNullable(object).orElseThrow( + () -> { + throw new RuntimeException("register error, service object null"); + } + ).getClass().getSuperclass().getSimpleName(); + registerService(key, object); + } + + public void registerService(String key, Object object) throws RuntimeException { + Optional.ofNullable(key).orElseThrow( + () -> { + throw new RuntimeException("register error, key is null"); + } + ); + + if (rpcServiceMap.containsKey(key)) { + throw new RuntimeException(String.format("registerService error, key %s exist", key)); + } + rpcServiceMap.put(key, object); + log.info(String.format("register %s success", key)); + } + + public Optional getService(String key) { + return Optional.ofNullable(rpcServiceMap.get(key)); + } + +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcAsyncChannel.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcAsyncChannel.java new file mode 100644 index 0000000..1029ed0 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcAsyncChannel.java @@ -0,0 +1,99 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import com.iker.tinyrpc.net.future.RpcSyncFuture; +import com.iker.tinyrpc.net.rpc.RpcFutureMap; +import com.iker.tinyrpc.util.SpringContextUtil; +import com.iker.tinyrpc.util.TinyRpcErrorCode; +import com.iker.tinyrpc.util.TinyRpcSystemException; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +import java.net.InetSocketAddress; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +/** + * An async RpcChannel to do rpc, it will not be blocking + */ +@Slf4j +public class TinyRpcAsyncChannel extends AbstractRpcChannel { + + public TinyRpcAsyncChannel(InetSocketAddress peerAddr) { + super(peerAddr); + } + + + public void sync() throws ExecutionException, InterruptedException { + replyFuture.get(); + } + + private void registerFuture() { + // 首先注册 future 对象。 无论成功或者失败都会调用 callback + // 因此主调方需要在 callback 判断 rpc 调用是否成功 + String id = sendProtocol.getMsgReq(); + replyFuture = new RpcSyncFuture(id, ()-> { + log.info("{}|{}|RpcCallBack, peer addr {}", sendProtocol.getMsgReq(), sendProtocol.getServiceName(), peerAddr.toString()); + + if (tinyRpcController.failed()) { + log.error("{}|{}|rpc failed, peer addr: {}, errorCode:{}, errorInfo: {}", + replyProtocol.getMsgReq(), replyProtocol.getServiceName(), peerAddr.toString(), + tinyRpcController.getErrCode().ordinal(), tinyRpcController.getErrInfo()); + } else { + log.info("{}|{}|rpc success, peer addr: {}, total get replay {} bytes", + replyProtocol.getMsgReq(), replyProtocol.getServiceName(), peerAddr.toString(), + replyProtocol.getPkLen()); + } + Optional.ofNullable(done).ifPresent( + (func) -> { + func.run(parseResponse(responsePrototype)); + } + ); + // invoke 最后,需要删除 future 对象,不然会内存泄漏 + SpringContextUtil.getBean(RpcFutureMap.class).deleteFuture(id); + }); + + // future 对象注册到全局 map 里面 + SpringContextUtil.getBean(RpcFutureMap.class).addFuture(replyFuture); + log.info("register RpcFuture {} success", id); + } + + private void invokeFuture(TinyRpcErrorCode errorCode, String errorInfo) { + log.error("{}|rpc failed, peer addr: {}, errorCode:{}, errorInfo: {}", sendProtocol.getMsgReq(), peerAddr.toString(), errorCode.ordinal(), errorInfo); + tinyRpcController.setErrCode(errorCode); + tinyRpcController.setErrInfo(errorInfo); + replyProtocol.setErrInfo(errorInfo); + replyProtocol.setErrCode(errorCode.ordinal()); + + replyFuture.invoke(replyProtocol); + } + + /** + * send protocol and get reply at there, you can do it by yourself, such as: + * 1. sync: send data, then blocking wait until get peer server's reply + * 2. async: send data, and set callback, then do other things. callback will run when get peer server's reply + */ + @Override + protected void ioHandler() throws TinyRpcSystemException { + registerFuture(); + + asyncConnect().addListener( future -> { + if (future.isSuccess()) { + log.info("{}|connect to peer addr {} success", sendProtocol.getMsgReq(), peerAddr.toString()); + asyncSendMessage().addListener( future1 -> { + if (future1.isSuccess()) { + log.info("{}|send message to peer addr {} success, send total {} bytes", sendProtocol.getMsgReq(), peerAddr.toString(), sendProtocol.getPkLen()); + } else { + log.error("{}|send message to peer addr {} failed", sendProtocol.getMsgReq(), peerAddr.toString()); + // 发送包失败,直接唤醒 replyFuture,通知失败 + invokeFuture(TinyRpcErrorCode.ERROR_SEND_MESSAGE, "send message, peer addr: " + peerAddr.toString()); + } + }); + } else { + log.error("{}|connect to peer addr {} failed", sendProtocol.getMsgReq(), peerAddr.toString()); + // 连接失败,直接唤醒 replyFuture,通知失败 + invokeFuture(TinyRpcErrorCode.ERROR_FAILED_CONNECT, "connect error, peer addr: " + peerAddr.toString()); + } + }); + } + +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcController.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcController.java similarity index 71% rename from src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcController.java rename to src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcController.java index abbb0ce..1365115 100644 --- a/src/main/java/com/iker/tinyrpc/net/rpc/TinyPBRpcController.java +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcController.java @@ -1,48 +1,49 @@ -package com.iker.tinyrpc.net.rpc; +package com.iker.tinyrpc.net.rpc.protobuf; import com.google.protobuf.RpcCallback; import com.google.protobuf.RpcController; +import com.iker.tinyrpc.util.TinyRpcErrorCode; import lombok.Getter; import lombok.Setter; import java.net.InetSocketAddress; -public class TinyPBRpcController implements RpcController { +public class TinyRpcController implements RpcController { /** * error_code, identify one specific error */ @Getter @Setter - private int errCode; + private TinyRpcErrorCode errCode; /** * error_info, details description of error */ @Getter @Setter - private String errInfo; + private String errInfo = ""; /** * msg_req, identify once rpc request and response */ @Getter @Setter - private String msgReq; + private String msgReq = ""; /** * methodName of rpc call, such as queryName */ @Getter @Setter - private String methodName; + private String methodName = ""; /** * methodFullName of rpc call, such as QueryService.queryName */ @Getter @Setter - private String methodFullName; + private String methodFullName = ""; /** * localAddr of rpc call @@ -62,20 +63,25 @@ public class TinyPBRpcController implements RpcController { /** * show rpc progress is failed */ - @Getter - @Setter private boolean isFailed; /** * show is cancel rpc progress */ - @Getter @Setter private boolean isCanceled; @Override public void reset() { - + isCanceled = false; + isFailed = false; + peerAddr = null; + localAddr = null; + methodName = null; + methodFullName = null; + msgReq = null; + errCode = null; + errInfo = null; } @Override @@ -95,7 +101,8 @@ public void startCancel() { @Override public void setFailed(String reason) { - + errInfo = reason; + isFailed = true; } @Override diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcSyncChannel.java b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcSyncChannel.java new file mode 100644 index 0000000..6f79522 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protobuf/TinyRpcSyncChannel.java @@ -0,0 +1,37 @@ +package com.iker.tinyrpc.net.rpc.protobuf; + +import com.iker.tinyrpc.util.TinyRpcSystemException; + +import java.net.InetSocketAddress; + +public class TinyRpcSyncChannel extends AbstractRpcChannel { + + public TinyRpcSyncChannel(InetSocketAddress peerAddr) { + super(peerAddr); + } + + /** + * send protocol and get reply at there, you can do it by yourself, such as: + * 1. sync: send data, then blocking wait until get peer server's reply + * 2. async: send data, and set callback, then do other things. callback will run when get peer server's reply + */ + @Override + protected void ioHandler() throws TinyRpcSystemException { + + asyncConnect(); + +// RpcSyncFuture syncFuture = new RpcSyncFuture(sendProtocol.getMsgReq()); +// SpringContextUtil.getBean(RpcFutureMap.class).addFuture(syncFuture); +// +// tcpClient.sendMessage(sendProtocol); + +// try { +// replyProtocol = syncFuture.get(); +// } catch (InterruptedException | ExecutionException e) { +// throw new TinyRpcSystemException(TinyRpcErrorCode.ERROR_UNKNOWN, String.format("unknown error: %s", e.getMessage())); +// } + + + } + +} diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protocol/RpcProtocol.java b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/RpcProtocol.java new file mode 100644 index 0000000..c43b40a --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/RpcProtocol.java @@ -0,0 +1,14 @@ +package com.iker.tinyrpc.net.rpc.protocol; + + +public interface RpcProtocol { + + void setMsgReq(String msgReq); + + String getMsgReq(); + + int getMsgReqLen(); + + Object getObject(); + +} diff --git a/src/main/java/com/iker/tinyrpc/codec/TinyPBDecoder.java b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBDecoder.java similarity index 86% rename from src/main/java/com/iker/tinyrpc/codec/TinyPBDecoder.java rename to src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBDecoder.java index c063d2d..d047f93 100644 --- a/src/main/java/com/iker/tinyrpc/codec/TinyPBDecoder.java +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBDecoder.java @@ -1,7 +1,6 @@ -package com.iker.tinyrpc.codec; +package com.iker.tinyrpc.net.rpc.protocol.tinypb; -import com.iker.tinyrpc.protocol.TinyPBProtocol; -import com.iker.tinyrpc.util.TinyPBErrorCode; +import com.iker.tinyrpc.util.TinyRpcErrorCode; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -26,7 +25,7 @@ public class TinyPBDecoder extends ByteToMessageDecoder { */ @Override @SneakyThrows(IndexOutOfBoundsException.class) - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { + public void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int from = in.readerIndex(); log.info("begin to do TinyPBDecoder.decode"); while (in.isReadable()) { @@ -48,13 +47,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int end = start + packageLen - 1; if (end >= in.writerIndex()) { - log.debug(String.format("read less bytes than packageLen [%d]", packageLen)); + log.info(String.format("read less bytes than packageLen [%d]", packageLen)); from = start + 1; continue; } if (in.getByte(end) != TinyPBProtocol.getPbEnd()) { - log.debug("read end index is not PbEnd(0x03)"); + log.info("read end index is not PbEnd(0x03)"); from = start + 1; continue; } @@ -69,7 +68,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { if (packageLen < TinyPBProtocol.getMinPkLen() || packageLen > TinyPBProtocol.getMaxPkLen()) { // a bad package, directly drop it - request.setErrCode(TinyPBErrorCode.ERROR_FAILED_DECODE.ordinal()); + request.setErrCode(TinyRpcErrorCode.ERROR_FAILED_DECODE.ordinal()); request.setErrInfo(String.format("read pkLen [%d] out of range", packageLen)); out.add(request); continue; @@ -82,7 +81,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int msgReqIndex = msgReqLenIndex + 4; if (msgReqIndex + msgReqLen >= in.writerIndex()) { - request.setErrCode(TinyPBErrorCode.ERROR_FAILED_DECODE.ordinal()); + request.setErrCode(TinyRpcErrorCode.ERROR_FAILED_DECODE.ordinal()); request.setErrInfo(String.format("read msgReqLen [%d] out of range", msgReqLen)); out.add(request); continue; @@ -96,7 +95,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int serviceNameIndex = serviceNameLenIndex + 4; if (serviceNameIndex + serviceNameLen >= in.writerIndex()) { - request.setErrCode(TinyPBErrorCode.ERROR_FAILED_DECODE.ordinal()); + request.setErrCode(TinyRpcErrorCode.ERROR_FAILED_DECODE.ordinal()); request.setErrInfo(String.format("read serviceNameLen [%d] out of range", serviceNameLen)); out.add(request); continue; @@ -114,11 +113,12 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { request.setErrInfo(String.valueOf(in.getCharSequence(errInfoIndex, errInfoLen, CharsetUtil.UTF_8))); int pbDataIndex = errInfoIndex + errInfoLen; - request.setPbData(String.valueOf(in.getCharSequence(pbDataIndex, end - pbDataIndex - 4, CharsetUtil.UTF_8))); + request.setPbData(String.valueOf(in.getCharSequence(pbDataIndex, end - pbDataIndex - 4, CharsetUtil.ISO_8859_1))); int checkSumIndex = end - 4; request.setCheckSum(in.getInt(checkSumIndex)); out.add(request); + log.info(String.format("success decode a TinyPBProtocol of msgReq[%s]", request.getMsgReq())); } log.info("end TinyPBDecoder.decode"); } diff --git a/src/main/java/com/iker/tinyrpc/codec/TinyPBEncoder.java b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBEncoder.java similarity index 81% rename from src/main/java/com/iker/tinyrpc/codec/TinyPBEncoder.java rename to src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBEncoder.java index 4c61642..f3095fa 100644 --- a/src/main/java/com/iker/tinyrpc/codec/TinyPBEncoder.java +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBEncoder.java @@ -1,13 +1,12 @@ -package com.iker.tinyrpc.codec; +package com.iker.tinyrpc.net.rpc.protocol.tinypb; -import com.iker.tinyrpc.protocol.TinyPBProtocol; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.util.CharsetUtil; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; @Slf4j @ChannelHandler.Sharable @@ -25,7 +24,7 @@ public class TinyPBEncoder extends MessageToByteEncoder { @Override @SneakyThrows(IndexOutOfBoundsException.class) protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { - log.debug("begin to do TinyPBEncoder.encode"); + log.info("begin to do TinyPBEncoder.encode"); TinyPBProtocol protocol = (TinyPBProtocol) msg; if (protocol == null) { log.error("object convert to TinyPBProtocol get null"); @@ -43,10 +42,11 @@ protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) { out.writeInt(protocol.getErrCode()); out.writeInt(protocol.getErrInfoLen()); out.writeBytes(protocol.getErrInfo().getBytes()); - out.writeBytes(protocol.getPbData().getBytes()); + out.writeBytes(protocol.getPbData().getBytes(CharsetUtil.ISO_8859_1)); out.writeInt(protocol.getCheckSum()); out.writeByte(TinyPBProtocol.getPbEnd()); // set end flag - log.debug("end TinyPBEncoder.encode"); + log.info(String.format("success encode a package of msgReq[%s], total bytes size[%d]", protocol.getMsgReq(), out.readableBytes())); + log.info("end TinyPBEncoder.encode"); } } diff --git a/src/main/java/com/iker/tinyrpc/protocol/TinyPBProtocol.java b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBProtocol.java similarity index 62% rename from src/main/java/com/iker/tinyrpc/protocol/TinyPBProtocol.java rename to src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBProtocol.java index 899282e..26b0984 100644 --- a/src/main/java/com/iker/tinyrpc/protocol/TinyPBProtocol.java +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBProtocol.java @@ -1,9 +1,11 @@ -package com.iker.tinyrpc.protocol; +package com.iker.tinyrpc.net.rpc.protocol.tinypb; +import com.google.protobuf.Message; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; import lombok.Getter; import lombok.Setter; -public class TinyPBProtocol extends AbstractProtocol { +public class TinyPBProtocol implements RpcProtocol { @Getter private static final byte pbStart = 0x02; @@ -17,31 +19,17 @@ public class TinyPBProtocol extends AbstractProtocol { @Getter private static final int maxPkLen = 65536; // max length of a TinyPB protocol package. - @Getter @Setter private int pkLen; // length of all package - @Getter - private int msgReqLen; // length of msgReq - - @Getter - private String msgReq; // identify a request or response - - public void setMsgReq(String msgReq) { - this.msgReq = msgReq; - msgReqLen = msgReq.length(); - } + private String msgReq = ""; // length of service name @Getter private int serviceNameLen; // length of service name @Getter - private String serviceName; // service full name, like QueryService.query_name + private String serviceName = ""; // service full name, like QueryService.query_name - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - serviceNameLen = serviceName.length(); - } @Getter @Setter @@ -51,7 +39,7 @@ public void setServiceName(String serviceName) { private int errInfoLen; // length of error info @Getter - private String errInfo; // error detail info of Rpc, empty -- when call rpc success + private String errInfo = ""; // error detail info of Rpc, empty -- when call rpc success public void setErrInfo(String errInfo) { this.errInfo = errInfo; @@ -60,14 +48,44 @@ public void setErrInfo(String errInfo) { @Getter @Setter - private String pbData; // protobuf message object serialized bytes + private String pbData = ""; // protobuf message object serialized bytes @Getter @Setter private int checkSum; + public int getPkLen() { + resetPackageLen(); + return pkLen; + } + + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + serviceNameLen = serviceName.length(); + } + public void resetPackageLen() { pkLen = msgReq.length() + serviceName.length() + errInfo.length() + pbData.length() + TinyPBProtocol.getMinPkLen(); } + @Override + public void setMsgReq(String msgReq) { + this.msgReq = msgReq; + } + + @Override + public String getMsgReq() { + return msgReq; + } + + @Override + public int getMsgReqLen() { + return msgReq.length(); + } + + @Override + public Message getObject() { + return null; + } } diff --git a/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBRpcDispatcher.java b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBRpcDispatcher.java new file mode 100644 index 0000000..ee5507c --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/net/rpc/protocol/tinypb/TinyPBRpcDispatcher.java @@ -0,0 +1,116 @@ +package com.iker.tinyrpc.net.rpc.protocol.tinypb; + +import com.google.protobuf.*; +import com.iker.tinyrpc.net.rpc.RpcDispatcher; +import com.iker.tinyrpc.net.rpc.protobuf.ProtobufRpcServiceFactory; +import com.iker.tinyrpc.net.rpc.protobuf.TinyRpcController; +import com.iker.tinyrpc.net.rpc.protocol.RpcProtocol; +import com.iker.tinyrpc.util.SpringContextUtil; +import com.iker.tinyrpc.util.TinyRpcErrorCode; +import com.iker.tinyrpc.util.TinyRpcSystemException; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFutureListener; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.nio.charset.StandardCharsets; +import java.util.Optional; + + + +@Component +@Slf4j +public class TinyPBRpcDispatcher implements RpcDispatcher { + + @Override + public void dispatch(RpcProtocol protocol, Channel channel) throws RuntimeException { + TinyPBProtocol tinyPBProtocol = Optional.ofNullable((TinyPBProtocol) protocol).orElseThrow( + () -> { + throw new TinyRpcSystemException("get null object of TinyPBProtocol"); + } + ); + String msgReq = tinyPBProtocol.getMsgReq(); + + TinyPBProtocol replyProtocol = new TinyPBProtocol(); + replyProtocol.setMsgReq(tinyPBProtocol.getMsgReq()); + replyProtocol.setServiceName(tinyPBProtocol.getServiceName()); + + try { + + log.info(String.format("[%s] begin to dispatch rpc request", msgReq)); + + String[] result = new String[2]; + parseMethodFullName(tinyPBProtocol.getServiceName(), result); + String serviceName = result[0]; + String methodName = result[1]; + log.info(String.format("[%s] get serviceName [%s], get methodName [%s] from rpc request", msgReq, serviceName, methodName)); + + // get RPC protobuf service object + Service service = (Service) SpringContextUtil.getBean(ProtobufRpcServiceFactory.class).getService(serviceName).orElseThrow( + () -> { + throw new TinyRpcSystemException(TinyRpcErrorCode.ERROR_SERVICE_NOT_FOUND, String.format("[%s] not found service name of [%s]", msgReq, serviceName)); + } + ); + + // find method by methodName + Descriptors.MethodDescriptor methodDescriptor = Optional.ofNullable(service.getDescriptorForType().findMethodByName(methodName)).orElseThrow( + () -> { + throw new TinyRpcSystemException(TinyRpcErrorCode.ERROR_METHOD_NOT_FOUND, String.format("[%s] not found method name of [%s]", msgReq, methodName)); + } + ); + + TinyRpcController rpcController = new TinyRpcController(); + rpcController.setMsgReq(tinyPBProtocol.getMsgReq()); + rpcController.setMethodFullName(tinyPBProtocol.getServiceName()); + rpcController.setMethodName(methodName); + + Message request = service.getRequestPrototype(methodDescriptor).newBuilderForType().mergeFrom(ByteString.copyFrom(tinyPBProtocol.getPbData(), StandardCharsets.ISO_8859_1)).build(); + service.callMethod(methodDescriptor, rpcController, request, parameter -> { + + String pbData = String.valueOf(Optional.ofNullable(parameter).orElseThrow(() -> { + throw new TinyRpcSystemException(String.format("[%s] msgReq execute method failed, get null response, controller error info[%s]", msgReq, rpcController.errorText())); + }).toByteString()); + + // rpc method run success, now reply to client + replyProtocol.setPbData(pbData); + replyResponse(replyProtocol, channel); + }); + + } catch (TinyRpcSystemException e) { + // if occur some exception, you should reply to client, to tell error details + log.error(String.format("[%s] dispatcher catch TinyRpcSystemException, error code[%d], error info[%s]", msgReq, e.getErrorCode().ordinal(), e.getErrorInfo())); + log.error("Exception: ", e); + replyProtocol.setErrCode(e.getErrorCode().ordinal()); + replyProtocol.setErrInfo(e.getErrorInfo()); + replyResponse(replyProtocol, channel); + + } catch (InvalidProtocolBufferException e) { + log.error(String.format("[%s] dispatcher catch InvalidProtocolBufferException", msgReq)); + log.error("Exception: ", e); + } + + } + + private void replyResponse(TinyPBProtocol replyProtocol, Channel channel) { + channel.writeAndFlush(replyProtocol, channel.newPromise().addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + log.info(String.format("[%s] success write rpc response success of", replyProtocol.getMsgReq())); + } else { + log.error(String.format("[%s] failed write rpc response", replyProtocol.getMsgReq())); + log.error("Exception " , future.cause()); + } + })); + } + + private void parseMethodFullName(String methodFullName, String[] result) { + char split = '.'; + int i = methodFullName.indexOf(split); + if (i == -1 || i != methodFullName.lastIndexOf(split)) { + throw new TinyRpcSystemException(TinyRpcErrorCode.ERROR_PARSE_SERVICE_NAME, + String.format("parse serviceName error of [%s]", methodFullName)); + } + result[0] = methodFullName.substring(0, i); + result[1] = methodFullName.substring(i + 1); + } +} diff --git a/src/main/java/com/iker/tinyrpc/protocol/AbstractProtocol.java b/src/main/java/com/iker/tinyrpc/protocol/AbstractProtocol.java deleted file mode 100644 index b8c852c..0000000 --- a/src/main/java/com/iker/tinyrpc/protocol/AbstractProtocol.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.iker.tinyrpc.protocol; - -public class AbstractProtocol { -} diff --git a/src/main/java/com/iker/tinyrpc/util/SpringContextUtil.java b/src/main/java/com/iker/tinyrpc/util/SpringContextUtil.java index 843b8a0..f1f93c1 100644 --- a/src/main/java/com/iker/tinyrpc/util/SpringContextUtil.java +++ b/src/main/java/com/iker/tinyrpc/util/SpringContextUtil.java @@ -2,15 +2,29 @@ import lombok.Getter; import lombok.Setter; -import org.springframework.context.ApplicationContext; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringContextUtil { @Getter @Setter - private static ApplicationContext applicationContext; + private static AnnotationConfigApplicationContext applicationContext; + + public static T getBean(Class type) { + return applicationContext.getBean(type); + } public static T getBean(String name, Class type) { return applicationContext.getBean(name, type); } + + public static Object getBean(String name) { + return applicationContext.getBean(name); + } + + public static DefaultListableBeanFactory getBeanFactory() { + return (DefaultListableBeanFactory) applicationContext.getBeanFactory(); + } } diff --git a/src/main/java/com/iker/tinyrpc/util/TinyPBErrorCode.java b/src/main/java/com/iker/tinyrpc/util/TinyPBErrorCode.java deleted file mode 100644 index 23e7d7c..0000000 --- a/src/main/java/com/iker/tinyrpc/util/TinyPBErrorCode.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.iker.tinyrpc.util; - -public enum TinyPBErrorCode { - ERROR_FAILED_CONNECT(10000002), // failed to connection peer host - ERROR_FAILED_GET_REPLY(10000002), // failed to get server reply - ERROR_FAILED_DESERIALIZE(10000003), // deserialize failed - ERROR_FAILED_SERIALIZE(10000004), // serialize failed - ERROR_FAILED_ENCODE(10000005), // encode failed - ERROR_FAILED_DECODE(10000006), // decode failed - ERROR_RPC_CALL_TIMEOUT(10000007), // call rpc timeout - ERROR_SERVICE_NOT_FOUND(10000008), // not found service name - ERROR_METHOD_NOT_FOUND(10000009), // not found method - ERROR_PARSE_SERVICE_NAME(10000010), // not found service name - ERROR_NOT_SET_ASYNC_PRE_CALL(10000011), // you didn't set some necessary param before call async rpc - ERROR_CONNECT_SYS_ERR(10000012); // connect sys error - - TinyPBErrorCode(int i) { - - } -} diff --git a/src/main/java/com/iker/tinyrpc/util/TinyRpcErrorCode.java b/src/main/java/com/iker/tinyrpc/util/TinyRpcErrorCode.java new file mode 100644 index 0000000..904e1c7 --- /dev/null +++ b/src/main/java/com/iker/tinyrpc/util/TinyRpcErrorCode.java @@ -0,0 +1,69 @@ +package com.iker.tinyrpc.util; + +public enum TinyRpcErrorCode { + /** + * unknown error + */ + ERROR_UNKNOWN(10000000), + + /** + * failed to connection peer host + */ + ERROR_FAILED_CONNECT(10000001), + + /** + * failed to get server reply + */ + ERROR_FAILED_GET_REPLY(10000002), + /** + * deserialize protobuf data failed + */ + ERROR_FAILED_DESERIALIZE(10000003), + /** + * serialize protobuf data failed + */ + ERROR_FAILED_SERIALIZE(10000004), + /** + * encode failed + */ + ERROR_FAILED_ENCODE(10000005), + /** + * decode failed + */ + ERROR_FAILED_DECODE(10000006), + /** + * call rpc timeout + */ + ERROR_RPC_CALL_TIMEOUT(10000007), + /** + * not found service name + */ + ERROR_SERVICE_NOT_FOUND(10000008), + /** + * not found method name + */ + ERROR_METHOD_NOT_FOUND(10000009), + /** + * parse service full name error + */ + ERROR_PARSE_SERVICE_NAME(10000010), + + /** + * connect peer addr sys error + */ + ERROR_CONNECT_SYS_ERR(10000012), + + /** + * execute rpc method failed, get null response + */ + ERROR_EXECUTE_RPC_METHOD(10000013), + + /** + * send message to peer error + */ + ERROR_SEND_MESSAGE(10000004); + + TinyRpcErrorCode(int i) { + + } +} diff --git a/src/main/java/com/iker/tinyrpc/util/TinyRpcSystemException.java b/src/main/java/com/iker/tinyrpc/util/TinyRpcSystemException.java index 5d12ba1..4d91d1b 100644 --- a/src/main/java/com/iker/tinyrpc/util/TinyRpcSystemException.java +++ b/src/main/java/com/iker/tinyrpc/util/TinyRpcSystemException.java @@ -1,16 +1,30 @@ package com.iker.tinyrpc.util; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class TinyRpcSystemException extends RuntimeException { - public TinyRpcSystemException(TinyPBErrorCode errorFailedDecode) { - super("Error code: " + errorFailedDecode.name()); - } + @Getter + @Setter + private TinyRpcErrorCode errorCode; + + @Getter + @Setter + private String errorInfo; + - public TinyRpcSystemException(TinyPBErrorCode errorFailedDecode, String s) { + public TinyRpcSystemException(TinyRpcErrorCode errorFailedDecode, String s) { super("Error code: " + errorFailedDecode.name() + ", Error info: " + s); + setErrorCode(errorFailedDecode); + setErrorInfo(s); + log.error("Error code: " + errorFailedDecode.name() + ", Error info: " + s); } public TinyRpcSystemException(String s) { super("Error info: " + s); + setErrorInfo(s); } } diff --git a/src/test/java/com/iker/tinyrpc/net/QueryServiceHandler.java b/src/test/java/com/iker/tinyrpc/net/QueryServiceHandler.java new file mode 100644 index 0000000..d8f692b --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/net/QueryServiceHandler.java @@ -0,0 +1,29 @@ +package com.iker.tinyrpc.net; + +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcController; +import com.iker.tinyrpc.annotation.TinyPBService; +import com.iker.tinyrpc.proto.queryAgeReq; +import com.iker.tinyrpc.proto.queryAgeRes; +import com.iker.tinyrpc.proto.queryNameReq; +import com.iker.tinyrpc.proto.queryNameRes; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@TinyPBService +public class QueryServiceHandler extends com.iker.tinyrpc.proto.QueryService { + + @Override + public void queryName(RpcController controller, queryNameReq request, RpcCallback done) { + queryNameRes response = queryNameRes.newBuilder().setName("tinyrpc-test-queryNameMethod").setId(8888).build(); + log.info("call method queryName successful"); + done.run(response); + } + + @Override + public void queryAge(RpcController controller, queryAgeReq request, RpcCallback done) { + queryAgeRes response = queryAgeRes.newBuilder().setAge(9999).setId(8888).build(); + log.info("call method queryAge successful"); + done.run(response); + } +}; \ No newline at end of file diff --git a/src/test/java/com/iker/tinyrpc/net/TcpClientTest.java b/src/test/java/com/iker/tinyrpc/net/TcpClientTest.java index 7bc00d8..8207750 100644 --- a/src/test/java/com/iker/tinyrpc/net/TcpClientTest.java +++ b/src/test/java/com/iker/tinyrpc/net/TcpClientTest.java @@ -1,6 +1,13 @@ package com.iker.tinyrpc.net; -import com.iker.tinyrpc.protocol.TinyPBProtocol; +import com.iker.tinyrpc.net.rpc.protobuf.DefaultRpcCallback; +import com.iker.tinyrpc.net.rpc.protobuf.TinyRpcAsyncChannel; +import com.iker.tinyrpc.net.rpc.protobuf.TinyRpcController; +import com.iker.tinyrpc.net.rpc.protobuf.TinyRpcSyncChannel; +import com.iker.tinyrpc.proto.QueryService; +import com.iker.tinyrpc.proto.queryNameReq; +import com.iker.tinyrpc.proto.queryNameRes; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBProtocol; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import lombok.extern.slf4j.Slf4j; @@ -9,11 +16,9 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import javax.annotation.Resource; - +import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress; - -import static org.junit.jupiter.api.Assertions.*; +import java.util.concurrent.ExecutionException; @SpringBootTest @Slf4j @@ -29,21 +34,26 @@ void tearDown() { TcpClient genTcpClient() { EventLoopGroup eventLoopGroup = new NioEventLoopGroup(1); - TcpClient tcpClient = new TcpClient(eventLoopGroup); - tcpClient.connect(new InetSocketAddress("0.0.0.0", 12345)); + TcpClient tcpClient = new TcpClient(new InetSocketAddress("0.0.0.0", 12345), eventLoopGroup); + tcpClient.connect(); return tcpClient; } TinyPBProtocol genTinyPBProtocol() { + queryNameReq request = queryNameReq.newBuilder().setReqNo(999).setId(1).build(); + TinyPBProtocol protocol = new TinyPBProtocol(); - protocol.setPbData("test"); + try { + protocol.setPbData(request.toByteString().toString("ISO-8859-1")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + }; String msgReq = "1234567890"; protocol.setMsgReq(msgReq); - protocol.setErrCode(0); - protocol.setErrInfo(""); - String serviceName = "TestService.query"; + String serviceName = "QueryService.query_name"; protocol.setServiceName(serviceName); protocol.resetPackageLen(); + log.info(String.format("package length is %d", protocol.getPkLen())); return protocol; } @@ -53,9 +63,33 @@ void connect() { } @Test - void sendMessage() { + void sendMessage() throws InterruptedException { TcpClient tcpClient = genTcpClient(); TinyPBProtocol protocol = genTinyPBProtocol(); tcpClient.sendMessage(protocol); + + Thread.sleep(100000); + } + + @Test + void callMethod() { + queryNameReq request = queryNameReq.newBuilder().setReqNo(999).setId(1).build(); + TinyRpcAsyncChannel tinyRpcSyncChannel = new TinyRpcAsyncChannel(new InetSocketAddress("0.0.0.0", 12345)); + TinyRpcController rpcController = new TinyRpcController(); + + + log.info("request info : {}", request); + QueryService.newStub(tinyRpcSyncChannel).queryName(rpcController, request, (res)-> { + log.info("get response {}", res.toString()); + }); + + try { + tinyRpcSyncChannel.sync(); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file diff --git a/src/test/java/com/iker/tinyrpc/net/TcpServerTest.java b/src/test/java/com/iker/tinyrpc/net/TcpServerTest.java index f9d670e..2cd4724 100644 --- a/src/test/java/com/iker/tinyrpc/net/TcpServerTest.java +++ b/src/test/java/com/iker/tinyrpc/net/TcpServerTest.java @@ -1,12 +1,12 @@ package com.iker.tinyrpc.net; +import com.iker.tinyrpc.util.SpringContextUtil; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import javax.annotation.Resource; import java.net.InetSocketAddress; @SpringBootTest @@ -21,13 +21,17 @@ void setUp() { void tearDown() { } + @Test - void bind() { + void start() { try { - TcpServer tcpServer = new TcpServer(); - tcpServer.start(new InetSocketAddress(12345)); - log.debug("bind success"); - } catch (InterruptedException e) { + TcpServer tcpServer = SpringContextUtil.getBean(TcpServer.class); + tcpServer.initMainLoopGroup(1); + tcpServer.initWorkerLoopGroup(4); + tcpServer.setLocalAddress(new InetSocketAddress(12345)); + tcpServer.start(); + + } catch (Exception e) { throw new RuntimeException(e); } } diff --git a/src/test/java/com/iker/tinyrpc/codec/TinyPBDecoderTest.java b/src/test/java/com/iker/tinyrpc/net/codec/TinyPBDecoderTest.java similarity index 58% rename from src/test/java/com/iker/tinyrpc/codec/TinyPBDecoderTest.java rename to src/test/java/com/iker/tinyrpc/net/codec/TinyPBDecoderTest.java index 09910fb..65980fe 100644 --- a/src/test/java/com/iker/tinyrpc/codec/TinyPBDecoderTest.java +++ b/src/test/java/com/iker/tinyrpc/net/codec/TinyPBDecoderTest.java @@ -1,11 +1,18 @@ -package com.iker.tinyrpc.codec; +package com.iker.tinyrpc.net.codec; -import com.iker.tinyrpc.protocol.TinyPBProtocol; +import com.google.protobuf.InvalidProtocolBufferException; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBDecoder; +import com.iker.tinyrpc.proto.queryAgeReq; +import com.iker.tinyrpc.proto.queryNameReq; +import com.iker.tinyrpc.net.rpc.protocol.tinypb.TinyPBProtocol; import io.netty.buffer.ByteBuf; +import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; @@ -37,7 +44,7 @@ public ByteBuf getByteBuf(TinyPBProtocol protocol) { byteBuf.writeInt(protocol.getErrCode()); byteBuf.writeInt(protocol.getErrInfoLen()); byteBuf.writeBytes(protocol.getErrInfo().getBytes()); - byteBuf.writeBytes(protocol.getPbData().getBytes()); + byteBuf.writeBytes(protocol.getPbData().getBytes(CharsetUtil.ISO_8859_1)); byteBuf.writeInt(0); byteBuf.writeByte(TinyPBProtocol.getPbEnd()); // set end flag @@ -50,30 +57,41 @@ public ByteBuf getByteBuf(TinyPBProtocol protocol) { @Test - void decode() { - TinyPBDecoder decoder = new TinyPBDecoder(); + void decode() throws InvalidProtocolBufferException, UnsupportedEncodingException { + queryNameReq request = queryNameReq.newBuilder().setReqNo(999).setId(1).build(); TinyPBProtocol protocol = new TinyPBProtocol(); - protocol.setPbData("test"); + try { + protocol.setPbData(request.toByteString().toString("ISO-8859-1")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + }; String msgReq = "1234567890"; protocol.setMsgReq(msgReq); - protocol.setErrCode(0); - protocol.setErrInfo(""); - String serviceName = "TestService.query"; + String serviceName = "QueryService.query_name"; protocol.setServiceName(serviceName); protocol.resetPackageLen(); + log.info(String.format("package length is %d", protocol.getPkLen())); ByteBuf byteBuf = getByteBuf(protocol); List out = new ArrayList<>(); + + TinyPBDecoder decoder = new TinyPBDecoder(); decoder.decode(null, byteBuf, out); - log.debug("get out list size " + out.size()); + log.info("get out list size " + out.size()); for (Object a: out) { TinyPBProtocol tinyPBProtocol = (TinyPBProtocol)a; if (tinyPBProtocol != null) { - log.debug(String.format("get TinyPBProtocol, packageLen[%d], msgReq[%s], serviceName[%s], errCode[%d], errInfo[%s], pbData[%s], checkSum[%d]", + log.info(String.format("get TinyPBProtocol, packageLen[%d], msgReq[%s], serviceName[%s], errCode[%d], errInfo[%s],, checkSum[%d]", tinyPBProtocol.getPkLen(), tinyPBProtocol.getMsgReq(), tinyPBProtocol.getServiceName(), tinyPBProtocol.getErrCode(), - tinyPBProtocol.getErrInfo(), tinyPBProtocol.getPbData(), tinyPBProtocol.getCheckSum())); + tinyPBProtocol.getErrInfo(), tinyPBProtocol.getCheckSum())); + try { + queryAgeReq.parseFrom(protocol.getPbData().getBytes()); + queryAgeReq.parseFrom(tinyPBProtocol.getPbData().getBytes()); + } catch (InvalidProtocolBufferException e) { + throw new RuntimeException(e); + } } } diff --git a/src/test/java/com/iker/tinyrpc/proto/QueryService.java b/src/test/java/com/iker/tinyrpc/proto/QueryService.java new file mode 100644 index 0000000..2bec934 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/QueryService.java @@ -0,0 +1,324 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +/** + * Protobuf service {@code QueryService} + */ +public abstract class QueryService + implements com.google.protobuf.Service { + protected QueryService() {} + + public interface Interface { + /** + *
+     * rpc method name
+     * 
+ * + * rpc query_name(.queryNameReq) returns (.queryNameRes); + */ + public abstract void queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request, + com.google.protobuf.RpcCallback done); + + /** + *
+     * rpc method name
+     * 
+ * + * rpc query_age(.queryAgeReq) returns (.queryAgeRes); + */ + public abstract void queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request, + com.google.protobuf.RpcCallback done); + + } + + public static com.google.protobuf.Service newReflectiveService( + final Interface impl) { + return new QueryService() { + @java.lang.Override + public void queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request, + com.google.protobuf.RpcCallback done) { + impl.queryName(controller, request, done); + } + + @java.lang.Override + public void queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request, + com.google.protobuf.RpcCallback done) { + impl.queryAge(controller, request, done); + } + + }; + } + + public static com.google.protobuf.BlockingService + newReflectiveBlockingService(final BlockingInterface impl) { + return new com.google.protobuf.BlockingService() { + public final com.google.protobuf.Descriptors.ServiceDescriptor + getDescriptorForType() { + return getDescriptor(); + } + + public final com.google.protobuf.Message callBlockingMethod( + com.google.protobuf.Descriptors.MethodDescriptor method, + com.google.protobuf.RpcController controller, + com.google.protobuf.Message request) + throws com.google.protobuf.ServiceException { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.callBlockingMethod() given method descriptor for " + + "wrong service type."); + } + switch(method.getIndex()) { + case 0: + return impl.queryName(controller, (com.iker.tinyrpc.proto.queryNameReq)request); + case 1: + return impl.queryAge(controller, (com.iker.tinyrpc.proto.queryAgeReq)request); + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + public final com.google.protobuf.Message + getRequestPrototype( + com.google.protobuf.Descriptors.MethodDescriptor method) { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.getRequestPrototype() given method " + + "descriptor for wrong service type."); + } + switch(method.getIndex()) { + case 0: + return com.iker.tinyrpc.proto.queryNameReq.getDefaultInstance(); + case 1: + return com.iker.tinyrpc.proto.queryAgeReq.getDefaultInstance(); + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + public final com.google.protobuf.Message + getResponsePrototype( + com.google.protobuf.Descriptors.MethodDescriptor method) { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.getResponsePrototype() given method " + + "descriptor for wrong service type."); + } + switch(method.getIndex()) { + case 0: + return com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance(); + case 1: + return com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance(); + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + }; + } + + /** + *
+   * rpc method name
+   * 
+ * + * rpc query_name(.queryNameReq) returns (.queryNameRes); + */ + public abstract void queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request, + com.google.protobuf.RpcCallback done); + + /** + *
+   * rpc method name
+   * 
+ * + * rpc query_age(.queryAgeReq) returns (.queryAgeRes); + */ + public abstract void queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request, + com.google.protobuf.RpcCallback done); + + public static final + com.google.protobuf.Descriptors.ServiceDescriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.getDescriptor().getServices().get(0); + } + public final com.google.protobuf.Descriptors.ServiceDescriptor + getDescriptorForType() { + return getDescriptor(); + } + + public final void callMethod( + com.google.protobuf.Descriptors.MethodDescriptor method, + com.google.protobuf.RpcController controller, + com.google.protobuf.Message request, + com.google.protobuf.RpcCallback< + com.google.protobuf.Message> done) { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.callMethod() given method descriptor for wrong " + + "service type."); + } + switch(method.getIndex()) { + case 0: + this.queryName(controller, (com.iker.tinyrpc.proto.queryNameReq)request, + com.google.protobuf.RpcUtil.specializeCallback( + done)); + return; + case 1: + this.queryAge(controller, (com.iker.tinyrpc.proto.queryAgeReq)request, + com.google.protobuf.RpcUtil.specializeCallback( + done)); + return; + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + public final com.google.protobuf.Message + getRequestPrototype( + com.google.protobuf.Descriptors.MethodDescriptor method) { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.getRequestPrototype() given method " + + "descriptor for wrong service type."); + } + switch(method.getIndex()) { + case 0: + return com.iker.tinyrpc.proto.queryNameReq.getDefaultInstance(); + case 1: + return com.iker.tinyrpc.proto.queryAgeReq.getDefaultInstance(); + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + public final com.google.protobuf.Message + getResponsePrototype( + com.google.protobuf.Descriptors.MethodDescriptor method) { + if (method.getService() != getDescriptor()) { + throw new java.lang.IllegalArgumentException( + "Service.getResponsePrototype() given method " + + "descriptor for wrong service type."); + } + switch(method.getIndex()) { + case 0: + return com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance(); + case 1: + return com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance(); + default: + throw new java.lang.AssertionError("Can't get here."); + } + } + + public static Stub newStub( + com.google.protobuf.RpcChannel channel) { + return new Stub(channel); + } + + public static final class Stub extends com.iker.tinyrpc.proto.QueryService implements Interface { + private Stub(com.google.protobuf.RpcChannel channel) { + this.channel = channel; + } + + private final com.google.protobuf.RpcChannel channel; + + public com.google.protobuf.RpcChannel getChannel() { + return channel; + } + + public void queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request, + com.google.protobuf.RpcCallback done) { + channel.callMethod( + getDescriptor().getMethods().get(0), + controller, + request, + com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance(), + com.google.protobuf.RpcUtil.generalizeCallback( + done, + com.iker.tinyrpc.proto.queryNameRes.class, + com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance())); + } + + public void queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request, + com.google.protobuf.RpcCallback done) { + channel.callMethod( + getDescriptor().getMethods().get(1), + controller, + request, + com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance(), + com.google.protobuf.RpcUtil.generalizeCallback( + done, + com.iker.tinyrpc.proto.queryAgeRes.class, + com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance())); + } + } + + public static BlockingInterface newBlockingStub( + com.google.protobuf.BlockingRpcChannel channel) { + return new BlockingStub(channel); + } + + public interface BlockingInterface { + public com.iker.tinyrpc.proto.queryNameRes queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request) + throws com.google.protobuf.ServiceException; + + public com.iker.tinyrpc.proto.queryAgeRes queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request) + throws com.google.protobuf.ServiceException; + } + + private static final class BlockingStub implements BlockingInterface { + private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) { + this.channel = channel; + } + + private final com.google.protobuf.BlockingRpcChannel channel; + + public com.iker.tinyrpc.proto.queryNameRes queryName( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryNameReq request) + throws com.google.protobuf.ServiceException { + return (com.iker.tinyrpc.proto.queryNameRes) channel.callBlockingMethod( + getDescriptor().getMethods().get(0), + controller, + request, + com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance()); + } + + + public com.iker.tinyrpc.proto.queryAgeRes queryAge( + com.google.protobuf.RpcController controller, + com.iker.tinyrpc.proto.queryAgeReq request) + throws com.google.protobuf.ServiceException { + return (com.iker.tinyrpc.proto.queryAgeRes) channel.callBlockingMethod( + getDescriptor().getMethods().get(1), + controller, + request, + com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance()); + } + + } + + // @@protoc_insertion_point(class_scope:QueryService) +} + diff --git a/src/test/java/com/iker/tinyrpc/proto/TestTinypbServer.java b/src/test/java/com/iker/tinyrpc/proto/TestTinypbServer.java new file mode 100644 index 0000000..8eea6e6 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/TestTinypbServer.java @@ -0,0 +1,90 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +public final class TestTinypbServer { + private TestTinypbServer() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + static final com.google.protobuf.Descriptors.Descriptor + internal_static_queryAgeReq_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_queryAgeReq_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_queryAgeRes_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_queryAgeRes_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_queryNameReq_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_queryNameReq_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_queryNameRes_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_queryNameRes_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\030test_tinypb_server.proto\")\n\013queryAgeRe" + + "q\022\016\n\006req_no\030\001 \001(\005\022\n\n\002id\030\002 \001(\005\"Z\n\013queryAg" + + "eRes\022\020\n\010ret_code\030\001 \001(\005\022\020\n\010res_info\030\002 \001(\t" + + "\022\016\n\006req_no\030\003 \001(\005\022\n\n\002id\030\004 \001(\005\022\013\n\003age\030\005 \001(" + + "\005\"8\n\014queryNameReq\022\016\n\006req_no\030\001 \001(\005\022\n\n\002id\030" + + "\002 \001(\005\022\014\n\004type\030\003 \001(\005\"\\\n\014queryNameRes\022\020\n\010r" + + "et_code\030\001 \001(\005\022\020\n\010res_info\030\002 \001(\t\022\016\n\006req_n" + + "o\030\003 \001(\005\022\n\n\002id\030\004 \001(\005\022\014\n\004name\030\005 \001(\t2c\n\014Que" + + "ryService\022*\n\nquery_name\022\r.queryNameReq\032\r" + + ".queryNameRes\022\'\n\tquery_age\022\014.queryAgeReq" + + "\032\014.queryAgeResB \n\026com.iker.tinyrpc.proto" + + "P\001\200\001\001\210\001\001b\006proto3" + }; + descriptor = com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }); + internal_static_queryAgeReq_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_queryAgeReq_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_queryAgeReq_descriptor, + new java.lang.String[] { "ReqNo", "Id", }); + internal_static_queryAgeRes_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_queryAgeRes_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_queryAgeRes_descriptor, + new java.lang.String[] { "RetCode", "ResInfo", "ReqNo", "Id", "Age", }); + internal_static_queryNameReq_descriptor = + getDescriptor().getMessageTypes().get(2); + internal_static_queryNameReq_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_queryNameReq_descriptor, + new java.lang.String[] { "ReqNo", "Id", "Type", }); + internal_static_queryNameRes_descriptor = + getDescriptor().getMessageTypes().get(3); + internal_static_queryNameRes_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_queryNameRes_descriptor, + new java.lang.String[] { "RetCode", "ResInfo", "ReqNo", "Id", "Name", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/src/test/java/com/iker/tinyrpc/proto/queryAgeReq.java b/src/test/java/com/iker/tinyrpc/proto/queryAgeReq.java new file mode 100644 index 0000000..ea75be8 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryAgeReq.java @@ -0,0 +1,547 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +/** + * Protobuf type {@code queryAgeReq} + */ +public final class queryAgeReq extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:queryAgeReq) + queryAgeReqOrBuilder { +private static final long serialVersionUID = 0L; + // Use queryAgeReq.newBuilder() to construct. + private queryAgeReq(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private queryAgeReq() { + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new queryAgeReq(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private queryAgeReq( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 8: { + + reqNo_ = input.readInt32(); + break; + } + case 16: { + + id_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeReq_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeReq_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryAgeReq.class, com.iker.tinyrpc.proto.queryAgeReq.Builder.class); + } + + public static final int REQ_NO_FIELD_NUMBER = 1; + private int reqNo_; + /** + * int32 req_no = 1; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + + public static final int ID_FIELD_NUMBER = 2; + private int id_; + /** + * int32 id = 2; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (reqNo_ != 0) { + output.writeInt32(1, reqNo_); + } + if (id_ != 0) { + output.writeInt32(2, id_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (reqNo_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, reqNo_); + } + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(2, id_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof com.iker.tinyrpc.proto.queryAgeReq)) { + return super.equals(obj); + } + com.iker.tinyrpc.proto.queryAgeReq other = (com.iker.tinyrpc.proto.queryAgeReq) obj; + + if (getReqNo() + != other.getReqNo()) return false; + if (getId() + != other.getId()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + REQ_NO_FIELD_NUMBER; + hash = (53 * hash) + getReqNo(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeReq parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.iker.tinyrpc.proto.queryAgeReq prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code queryAgeReq} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:queryAgeReq) + com.iker.tinyrpc.proto.queryAgeReqOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeReq_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeReq_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryAgeReq.class, com.iker.tinyrpc.proto.queryAgeReq.Builder.class); + } + + // Construct using com.iker.tinyrpc.proto.queryAgeReq.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + reqNo_ = 0; + + id_ = 0; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeReq_descriptor; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeReq getDefaultInstanceForType() { + return com.iker.tinyrpc.proto.queryAgeReq.getDefaultInstance(); + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeReq build() { + com.iker.tinyrpc.proto.queryAgeReq result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeReq buildPartial() { + com.iker.tinyrpc.proto.queryAgeReq result = new com.iker.tinyrpc.proto.queryAgeReq(this); + result.reqNo_ = reqNo_; + result.id_ = id_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.iker.tinyrpc.proto.queryAgeReq) { + return mergeFrom((com.iker.tinyrpc.proto.queryAgeReq)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.iker.tinyrpc.proto.queryAgeReq other) { + if (other == com.iker.tinyrpc.proto.queryAgeReq.getDefaultInstance()) return this; + if (other.getReqNo() != 0) { + setReqNo(other.getReqNo()); + } + if (other.getId() != 0) { + setId(other.getId()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.iker.tinyrpc.proto.queryAgeReq parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.iker.tinyrpc.proto.queryAgeReq) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int reqNo_ ; + /** + * int32 req_no = 1; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + /** + * int32 req_no = 1; + * @param value The reqNo to set. + * @return This builder for chaining. + */ + public Builder setReqNo(int value) { + + reqNo_ = value; + onChanged(); + return this; + } + /** + * int32 req_no = 1; + * @return This builder for chaining. + */ + public Builder clearReqNo() { + + reqNo_ = 0; + onChanged(); + return this; + } + + private int id_ ; + /** + * int32 id = 2; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + /** + * int32 id = 2; + * @param value The id to set. + * @return This builder for chaining. + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 2; + * @return This builder for chaining. + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:queryAgeReq) + } + + // @@protoc_insertion_point(class_scope:queryAgeReq) + private static final com.iker.tinyrpc.proto.queryAgeReq DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.iker.tinyrpc.proto.queryAgeReq(); + } + + public static com.iker.tinyrpc.proto.queryAgeReq getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public queryAgeReq parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new queryAgeReq(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeReq getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/test/java/com/iker/tinyrpc/proto/queryAgeReqOrBuilder.java b/src/test/java/com/iker/tinyrpc/proto/queryAgeReqOrBuilder.java new file mode 100644 index 0000000..8f9948a --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryAgeReqOrBuilder.java @@ -0,0 +1,21 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +public interface queryAgeReqOrBuilder extends + // @@protoc_insertion_point(interface_extends:queryAgeReq) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 req_no = 1; + * @return The reqNo. + */ + int getReqNo(); + + /** + * int32 id = 2; + * @return The id. + */ + int getId(); +} diff --git a/src/test/java/com/iker/tinyrpc/proto/queryAgeRes.java b/src/test/java/com/iker/tinyrpc/proto/queryAgeRes.java new file mode 100644 index 0000000..906286a --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryAgeRes.java @@ -0,0 +1,813 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +/** + * Protobuf type {@code queryAgeRes} + */ +public final class queryAgeRes extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:queryAgeRes) + queryAgeResOrBuilder { +private static final long serialVersionUID = 0L; + // Use queryAgeRes.newBuilder() to construct. + private queryAgeRes(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private queryAgeRes() { + resInfo_ = ""; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new queryAgeRes(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private queryAgeRes( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 8: { + + retCode_ = input.readInt32(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + resInfo_ = s; + break; + } + case 24: { + + reqNo_ = input.readInt32(); + break; + } + case 32: { + + id_ = input.readInt32(); + break; + } + case 40: { + + age_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeRes_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeRes_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryAgeRes.class, com.iker.tinyrpc.proto.queryAgeRes.Builder.class); + } + + public static final int RET_CODE_FIELD_NUMBER = 1; + private int retCode_; + /** + * int32 ret_code = 1; + * @return The retCode. + */ + @java.lang.Override + public int getRetCode() { + return retCode_; + } + + public static final int RES_INFO_FIELD_NUMBER = 2; + private volatile java.lang.Object resInfo_; + /** + * string res_info = 2; + * @return The resInfo. + */ + @java.lang.Override + public java.lang.String getResInfo() { + java.lang.Object ref = resInfo_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + resInfo_ = s; + return s; + } + } + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getResInfoBytes() { + java.lang.Object ref = resInfo_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + resInfo_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int REQ_NO_FIELD_NUMBER = 3; + private int reqNo_; + /** + * int32 req_no = 3; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + + public static final int ID_FIELD_NUMBER = 4; + private int id_; + /** + * int32 id = 4; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + + public static final int AGE_FIELD_NUMBER = 5; + private int age_; + /** + * int32 age = 5; + * @return The age. + */ + @java.lang.Override + public int getAge() { + return age_; + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (retCode_ != 0) { + output.writeInt32(1, retCode_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(resInfo_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, resInfo_); + } + if (reqNo_ != 0) { + output.writeInt32(3, reqNo_); + } + if (id_ != 0) { + output.writeInt32(4, id_); + } + if (age_ != 0) { + output.writeInt32(5, age_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (retCode_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, retCode_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(resInfo_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, resInfo_); + } + if (reqNo_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(3, reqNo_); + } + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(4, id_); + } + if (age_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(5, age_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof com.iker.tinyrpc.proto.queryAgeRes)) { + return super.equals(obj); + } + com.iker.tinyrpc.proto.queryAgeRes other = (com.iker.tinyrpc.proto.queryAgeRes) obj; + + if (getRetCode() + != other.getRetCode()) return false; + if (!getResInfo() + .equals(other.getResInfo())) return false; + if (getReqNo() + != other.getReqNo()) return false; + if (getId() + != other.getId()) return false; + if (getAge() + != other.getAge()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + RET_CODE_FIELD_NUMBER; + hash = (53 * hash) + getRetCode(); + hash = (37 * hash) + RES_INFO_FIELD_NUMBER; + hash = (53 * hash) + getResInfo().hashCode(); + hash = (37 * hash) + REQ_NO_FIELD_NUMBER; + hash = (53 * hash) + getReqNo(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (37 * hash) + AGE_FIELD_NUMBER; + hash = (53 * hash) + getAge(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryAgeRes parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.iker.tinyrpc.proto.queryAgeRes prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code queryAgeRes} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:queryAgeRes) + com.iker.tinyrpc.proto.queryAgeResOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeRes_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeRes_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryAgeRes.class, com.iker.tinyrpc.proto.queryAgeRes.Builder.class); + } + + // Construct using com.iker.tinyrpc.proto.queryAgeRes.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + retCode_ = 0; + + resInfo_ = ""; + + reqNo_ = 0; + + id_ = 0; + + age_ = 0; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryAgeRes_descriptor; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeRes getDefaultInstanceForType() { + return com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance(); + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeRes build() { + com.iker.tinyrpc.proto.queryAgeRes result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeRes buildPartial() { + com.iker.tinyrpc.proto.queryAgeRes result = new com.iker.tinyrpc.proto.queryAgeRes(this); + result.retCode_ = retCode_; + result.resInfo_ = resInfo_; + result.reqNo_ = reqNo_; + result.id_ = id_; + result.age_ = age_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.iker.tinyrpc.proto.queryAgeRes) { + return mergeFrom((com.iker.tinyrpc.proto.queryAgeRes)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.iker.tinyrpc.proto.queryAgeRes other) { + if (other == com.iker.tinyrpc.proto.queryAgeRes.getDefaultInstance()) return this; + if (other.getRetCode() != 0) { + setRetCode(other.getRetCode()); + } + if (!other.getResInfo().isEmpty()) { + resInfo_ = other.resInfo_; + onChanged(); + } + if (other.getReqNo() != 0) { + setReqNo(other.getReqNo()); + } + if (other.getId() != 0) { + setId(other.getId()); + } + if (other.getAge() != 0) { + setAge(other.getAge()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.iker.tinyrpc.proto.queryAgeRes parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.iker.tinyrpc.proto.queryAgeRes) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int retCode_ ; + /** + * int32 ret_code = 1; + * @return The retCode. + */ + @java.lang.Override + public int getRetCode() { + return retCode_; + } + /** + * int32 ret_code = 1; + * @param value The retCode to set. + * @return This builder for chaining. + */ + public Builder setRetCode(int value) { + + retCode_ = value; + onChanged(); + return this; + } + /** + * int32 ret_code = 1; + * @return This builder for chaining. + */ + public Builder clearRetCode() { + + retCode_ = 0; + onChanged(); + return this; + } + + private java.lang.Object resInfo_ = ""; + /** + * string res_info = 2; + * @return The resInfo. + */ + public java.lang.String getResInfo() { + java.lang.Object ref = resInfo_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + resInfo_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + public com.google.protobuf.ByteString + getResInfoBytes() { + java.lang.Object ref = resInfo_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + resInfo_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string res_info = 2; + * @param value The resInfo to set. + * @return This builder for chaining. + */ + public Builder setResInfo( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + resInfo_ = value; + onChanged(); + return this; + } + /** + * string res_info = 2; + * @return This builder for chaining. + */ + public Builder clearResInfo() { + + resInfo_ = getDefaultInstance().getResInfo(); + onChanged(); + return this; + } + /** + * string res_info = 2; + * @param value The bytes for resInfo to set. + * @return This builder for chaining. + */ + public Builder setResInfoBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + resInfo_ = value; + onChanged(); + return this; + } + + private int reqNo_ ; + /** + * int32 req_no = 3; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + /** + * int32 req_no = 3; + * @param value The reqNo to set. + * @return This builder for chaining. + */ + public Builder setReqNo(int value) { + + reqNo_ = value; + onChanged(); + return this; + } + /** + * int32 req_no = 3; + * @return This builder for chaining. + */ + public Builder clearReqNo() { + + reqNo_ = 0; + onChanged(); + return this; + } + + private int id_ ; + /** + * int32 id = 4; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + /** + * int32 id = 4; + * @param value The id to set. + * @return This builder for chaining. + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 4; + * @return This builder for chaining. + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private int age_ ; + /** + * int32 age = 5; + * @return The age. + */ + @java.lang.Override + public int getAge() { + return age_; + } + /** + * int32 age = 5; + * @param value The age to set. + * @return This builder for chaining. + */ + public Builder setAge(int value) { + + age_ = value; + onChanged(); + return this; + } + /** + * int32 age = 5; + * @return This builder for chaining. + */ + public Builder clearAge() { + + age_ = 0; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:queryAgeRes) + } + + // @@protoc_insertion_point(class_scope:queryAgeRes) + private static final com.iker.tinyrpc.proto.queryAgeRes DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.iker.tinyrpc.proto.queryAgeRes(); + } + + public static com.iker.tinyrpc.proto.queryAgeRes getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public queryAgeRes parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new queryAgeRes(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryAgeRes getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/test/java/com/iker/tinyrpc/proto/queryAgeResOrBuilder.java b/src/test/java/com/iker/tinyrpc/proto/queryAgeResOrBuilder.java new file mode 100644 index 0000000..ca9525d --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryAgeResOrBuilder.java @@ -0,0 +1,45 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +public interface queryAgeResOrBuilder extends + // @@protoc_insertion_point(interface_extends:queryAgeRes) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 ret_code = 1; + * @return The retCode. + */ + int getRetCode(); + + /** + * string res_info = 2; + * @return The resInfo. + */ + java.lang.String getResInfo(); + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + com.google.protobuf.ByteString + getResInfoBytes(); + + /** + * int32 req_no = 3; + * @return The reqNo. + */ + int getReqNo(); + + /** + * int32 id = 4; + * @return The id. + */ + int getId(); + + /** + * int32 age = 5; + * @return The age. + */ + int getAge(); +} diff --git a/src/test/java/com/iker/tinyrpc/proto/queryNameReq.java b/src/test/java/com/iker/tinyrpc/proto/queryNameReq.java new file mode 100644 index 0000000..2d388af --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryNameReq.java @@ -0,0 +1,611 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +/** + * Protobuf type {@code queryNameReq} + */ +public final class queryNameReq extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:queryNameReq) + queryNameReqOrBuilder { +private static final long serialVersionUID = 0L; + // Use queryNameReq.newBuilder() to construct. + private queryNameReq(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private queryNameReq() { + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new queryNameReq(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private queryNameReq( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 8: { + + reqNo_ = input.readInt32(); + break; + } + case 16: { + + id_ = input.readInt32(); + break; + } + case 24: { + + type_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameReq_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameReq_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryNameReq.class, com.iker.tinyrpc.proto.queryNameReq.Builder.class); + } + + public static final int REQ_NO_FIELD_NUMBER = 1; + private int reqNo_; + /** + * int32 req_no = 1; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + + public static final int ID_FIELD_NUMBER = 2; + private int id_; + /** + * int32 id = 2; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + + public static final int TYPE_FIELD_NUMBER = 3; + private int type_; + /** + * int32 type = 3; + * @return The type. + */ + @java.lang.Override + public int getType() { + return type_; + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (reqNo_ != 0) { + output.writeInt32(1, reqNo_); + } + if (id_ != 0) { + output.writeInt32(2, id_); + } + if (type_ != 0) { + output.writeInt32(3, type_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (reqNo_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, reqNo_); + } + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(2, id_); + } + if (type_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(3, type_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof com.iker.tinyrpc.proto.queryNameReq)) { + return super.equals(obj); + } + com.iker.tinyrpc.proto.queryNameReq other = (com.iker.tinyrpc.proto.queryNameReq) obj; + + if (getReqNo() + != other.getReqNo()) return false; + if (getId() + != other.getId()) return false; + if (getType() + != other.getType()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + REQ_NO_FIELD_NUMBER; + hash = (53 * hash) + getReqNo(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (37 * hash) + TYPE_FIELD_NUMBER; + hash = (53 * hash) + getType(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameReq parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameReq parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameReq parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.iker.tinyrpc.proto.queryNameReq prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code queryNameReq} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:queryNameReq) + com.iker.tinyrpc.proto.queryNameReqOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameReq_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameReq_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryNameReq.class, com.iker.tinyrpc.proto.queryNameReq.Builder.class); + } + + // Construct using com.iker.tinyrpc.proto.queryNameReq.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + reqNo_ = 0; + + id_ = 0; + + type_ = 0; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameReq_descriptor; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameReq getDefaultInstanceForType() { + return com.iker.tinyrpc.proto.queryNameReq.getDefaultInstance(); + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameReq build() { + com.iker.tinyrpc.proto.queryNameReq result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameReq buildPartial() { + com.iker.tinyrpc.proto.queryNameReq result = new com.iker.tinyrpc.proto.queryNameReq(this); + result.reqNo_ = reqNo_; + result.id_ = id_; + result.type_ = type_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.iker.tinyrpc.proto.queryNameReq) { + return mergeFrom((com.iker.tinyrpc.proto.queryNameReq)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.iker.tinyrpc.proto.queryNameReq other) { + if (other == com.iker.tinyrpc.proto.queryNameReq.getDefaultInstance()) return this; + if (other.getReqNo() != 0) { + setReqNo(other.getReqNo()); + } + if (other.getId() != 0) { + setId(other.getId()); + } + if (other.getType() != 0) { + setType(other.getType()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.iker.tinyrpc.proto.queryNameReq parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.iker.tinyrpc.proto.queryNameReq) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int reqNo_ ; + /** + * int32 req_no = 1; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + /** + * int32 req_no = 1; + * @param value The reqNo to set. + * @return This builder for chaining. + */ + public Builder setReqNo(int value) { + + reqNo_ = value; + onChanged(); + return this; + } + /** + * int32 req_no = 1; + * @return This builder for chaining. + */ + public Builder clearReqNo() { + + reqNo_ = 0; + onChanged(); + return this; + } + + private int id_ ; + /** + * int32 id = 2; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + /** + * int32 id = 2; + * @param value The id to set. + * @return This builder for chaining. + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 2; + * @return This builder for chaining. + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private int type_ ; + /** + * int32 type = 3; + * @return The type. + */ + @java.lang.Override + public int getType() { + return type_; + } + /** + * int32 type = 3; + * @param value The type to set. + * @return This builder for chaining. + */ + public Builder setType(int value) { + + type_ = value; + onChanged(); + return this; + } + /** + * int32 type = 3; + * @return This builder for chaining. + */ + public Builder clearType() { + + type_ = 0; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:queryNameReq) + } + + // @@protoc_insertion_point(class_scope:queryNameReq) + private static final com.iker.tinyrpc.proto.queryNameReq DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.iker.tinyrpc.proto.queryNameReq(); + } + + public static com.iker.tinyrpc.proto.queryNameReq getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public queryNameReq parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new queryNameReq(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameReq getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/test/java/com/iker/tinyrpc/proto/queryNameReqOrBuilder.java b/src/test/java/com/iker/tinyrpc/proto/queryNameReqOrBuilder.java new file mode 100644 index 0000000..2eb3d84 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryNameReqOrBuilder.java @@ -0,0 +1,27 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +public interface queryNameReqOrBuilder extends + // @@protoc_insertion_point(interface_extends:queryNameReq) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 req_no = 1; + * @return The reqNo. + */ + int getReqNo(); + + /** + * int32 id = 2; + * @return The id. + */ + int getId(); + + /** + * int32 type = 3; + * @return The type. + */ + int getType(); +} diff --git a/src/test/java/com/iker/tinyrpc/proto/queryNameRes.java b/src/test/java/com/iker/tinyrpc/proto/queryNameRes.java new file mode 100644 index 0000000..cc52618 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryNameRes.java @@ -0,0 +1,887 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +/** + * Protobuf type {@code queryNameRes} + */ +public final class queryNameRes extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:queryNameRes) + queryNameResOrBuilder { +private static final long serialVersionUID = 0L; + // Use queryNameRes.newBuilder() to construct. + private queryNameRes(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private queryNameRes() { + resInfo_ = ""; + name_ = ""; + } + + @java.lang.Override + @SuppressWarnings({"unused"}) + protected java.lang.Object newInstance( + UnusedPrivateParameter unused) { + return new queryNameRes(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private queryNameRes( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 8: { + + retCode_ = input.readInt32(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + resInfo_ = s; + break; + } + case 24: { + + reqNo_ = input.readInt32(); + break; + } + case 32: { + + id_ = input.readInt32(); + break; + } + case 42: { + java.lang.String s = input.readStringRequireUtf8(); + + name_ = s; + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameRes_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameRes_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryNameRes.class, com.iker.tinyrpc.proto.queryNameRes.Builder.class); + } + + public static final int RET_CODE_FIELD_NUMBER = 1; + private int retCode_; + /** + * int32 ret_code = 1; + * @return The retCode. + */ + @java.lang.Override + public int getRetCode() { + return retCode_; + } + + public static final int RES_INFO_FIELD_NUMBER = 2; + private volatile java.lang.Object resInfo_; + /** + * string res_info = 2; + * @return The resInfo. + */ + @java.lang.Override + public java.lang.String getResInfo() { + java.lang.Object ref = resInfo_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + resInfo_ = s; + return s; + } + } + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getResInfoBytes() { + java.lang.Object ref = resInfo_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + resInfo_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int REQ_NO_FIELD_NUMBER = 3; + private int reqNo_; + /** + * int32 req_no = 3; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + + public static final int ID_FIELD_NUMBER = 4; + private int id_; + /** + * int32 id = 4; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + + public static final int NAME_FIELD_NUMBER = 5; + private volatile java.lang.Object name_; + /** + * string name = 5; + * @return The name. + */ + @java.lang.Override + public java.lang.String getName() { + java.lang.Object ref = name_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + name_ = s; + return s; + } + } + /** + * string name = 5; + * @return The bytes for name. + */ + @java.lang.Override + public com.google.protobuf.ByteString + getNameBytes() { + java.lang.Object ref = name_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (retCode_ != 0) { + output.writeInt32(1, retCode_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(resInfo_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, resInfo_); + } + if (reqNo_ != 0) { + output.writeInt32(3, reqNo_); + } + if (id_ != 0) { + output.writeInt32(4, id_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 5, name_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (retCode_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, retCode_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(resInfo_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, resInfo_); + } + if (reqNo_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(3, reqNo_); + } + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(4, id_); + } + if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(5, name_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof com.iker.tinyrpc.proto.queryNameRes)) { + return super.equals(obj); + } + com.iker.tinyrpc.proto.queryNameRes other = (com.iker.tinyrpc.proto.queryNameRes) obj; + + if (getRetCode() + != other.getRetCode()) return false; + if (!getResInfo() + .equals(other.getResInfo())) return false; + if (getReqNo() + != other.getReqNo()) return false; + if (getId() + != other.getId()) return false; + if (!getName() + .equals(other.getName())) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + RET_CODE_FIELD_NUMBER; + hash = (53 * hash) + getRetCode(); + hash = (37 * hash) + RES_INFO_FIELD_NUMBER; + hash = (53 * hash) + getResInfo().hashCode(); + hash = (37 * hash) + REQ_NO_FIELD_NUMBER; + hash = (53 * hash) + getReqNo(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (37 * hash) + NAME_FIELD_NUMBER; + hash = (53 * hash) + getName().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameRes parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameRes parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static com.iker.tinyrpc.proto.queryNameRes parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.iker.tinyrpc.proto.queryNameRes prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code queryNameRes} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:queryNameRes) + com.iker.tinyrpc.proto.queryNameResOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameRes_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameRes_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.iker.tinyrpc.proto.queryNameRes.class, com.iker.tinyrpc.proto.queryNameRes.Builder.class); + } + + // Construct using com.iker.tinyrpc.proto.queryNameRes.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @java.lang.Override + public Builder clear() { + super.clear(); + retCode_ = 0; + + resInfo_ = ""; + + reqNo_ = 0; + + id_ = 0; + + name_ = ""; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.iker.tinyrpc.proto.TestTinypbServer.internal_static_queryNameRes_descriptor; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameRes getDefaultInstanceForType() { + return com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance(); + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameRes build() { + com.iker.tinyrpc.proto.queryNameRes result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameRes buildPartial() { + com.iker.tinyrpc.proto.queryNameRes result = new com.iker.tinyrpc.proto.queryNameRes(this); + result.retCode_ = retCode_; + result.resInfo_ = resInfo_; + result.reqNo_ = reqNo_; + result.id_ = id_; + result.name_ = name_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return super.clone(); + } + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.setField(field, value); + } + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return super.setRepeatedField(field, index, value); + } + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return super.addRepeatedField(field, value); + } + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.iker.tinyrpc.proto.queryNameRes) { + return mergeFrom((com.iker.tinyrpc.proto.queryNameRes)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.iker.tinyrpc.proto.queryNameRes other) { + if (other == com.iker.tinyrpc.proto.queryNameRes.getDefaultInstance()) return this; + if (other.getRetCode() != 0) { + setRetCode(other.getRetCode()); + } + if (!other.getResInfo().isEmpty()) { + resInfo_ = other.resInfo_; + onChanged(); + } + if (other.getReqNo() != 0) { + setReqNo(other.getReqNo()); + } + if (other.getId() != 0) { + setId(other.getId()); + } + if (!other.getName().isEmpty()) { + name_ = other.name_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.iker.tinyrpc.proto.queryNameRes parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.iker.tinyrpc.proto.queryNameRes) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int retCode_ ; + /** + * int32 ret_code = 1; + * @return The retCode. + */ + @java.lang.Override + public int getRetCode() { + return retCode_; + } + /** + * int32 ret_code = 1; + * @param value The retCode to set. + * @return This builder for chaining. + */ + public Builder setRetCode(int value) { + + retCode_ = value; + onChanged(); + return this; + } + /** + * int32 ret_code = 1; + * @return This builder for chaining. + */ + public Builder clearRetCode() { + + retCode_ = 0; + onChanged(); + return this; + } + + private java.lang.Object resInfo_ = ""; + /** + * string res_info = 2; + * @return The resInfo. + */ + public java.lang.String getResInfo() { + java.lang.Object ref = resInfo_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + resInfo_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + public com.google.protobuf.ByteString + getResInfoBytes() { + java.lang.Object ref = resInfo_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + resInfo_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string res_info = 2; + * @param value The resInfo to set. + * @return This builder for chaining. + */ + public Builder setResInfo( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + resInfo_ = value; + onChanged(); + return this; + } + /** + * string res_info = 2; + * @return This builder for chaining. + */ + public Builder clearResInfo() { + + resInfo_ = getDefaultInstance().getResInfo(); + onChanged(); + return this; + } + /** + * string res_info = 2; + * @param value The bytes for resInfo to set. + * @return This builder for chaining. + */ + public Builder setResInfoBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + resInfo_ = value; + onChanged(); + return this; + } + + private int reqNo_ ; + /** + * int32 req_no = 3; + * @return The reqNo. + */ + @java.lang.Override + public int getReqNo() { + return reqNo_; + } + /** + * int32 req_no = 3; + * @param value The reqNo to set. + * @return This builder for chaining. + */ + public Builder setReqNo(int value) { + + reqNo_ = value; + onChanged(); + return this; + } + /** + * int32 req_no = 3; + * @return This builder for chaining. + */ + public Builder clearReqNo() { + + reqNo_ = 0; + onChanged(); + return this; + } + + private int id_ ; + /** + * int32 id = 4; + * @return The id. + */ + @java.lang.Override + public int getId() { + return id_; + } + /** + * int32 id = 4; + * @param value The id to set. + * @return This builder for chaining. + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 4; + * @return This builder for chaining. + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private java.lang.Object name_ = ""; + /** + * string name = 5; + * @return The name. + */ + public java.lang.String getName() { + java.lang.Object ref = name_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + name_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * string name = 5; + * @return The bytes for name. + */ + public com.google.protobuf.ByteString + getNameBytes() { + java.lang.Object ref = name_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string name = 5; + * @param value The name to set. + * @return This builder for chaining. + */ + public Builder setName( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + name_ = value; + onChanged(); + return this; + } + /** + * string name = 5; + * @return This builder for chaining. + */ + public Builder clearName() { + + name_ = getDefaultInstance().getName(); + onChanged(); + return this; + } + /** + * string name = 5; + * @param value The bytes for name to set. + * @return This builder for chaining. + */ + public Builder setNameBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + name_ = value; + onChanged(); + return this; + } + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:queryNameRes) + } + + // @@protoc_insertion_point(class_scope:queryNameRes) + private static final com.iker.tinyrpc.proto.queryNameRes DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.iker.tinyrpc.proto.queryNameRes(); + } + + public static com.iker.tinyrpc.proto.queryNameRes getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public queryNameRes parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new queryNameRes(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public com.iker.tinyrpc.proto.queryNameRes getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/src/test/java/com/iker/tinyrpc/proto/queryNameResOrBuilder.java b/src/test/java/com/iker/tinyrpc/proto/queryNameResOrBuilder.java new file mode 100644 index 0000000..a68205b --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/queryNameResOrBuilder.java @@ -0,0 +1,51 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: test_tinypb_server.proto + +package com.iker.tinyrpc.proto; + +public interface queryNameResOrBuilder extends + // @@protoc_insertion_point(interface_extends:queryNameRes) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 ret_code = 1; + * @return The retCode. + */ + int getRetCode(); + + /** + * string res_info = 2; + * @return The resInfo. + */ + java.lang.String getResInfo(); + /** + * string res_info = 2; + * @return The bytes for resInfo. + */ + com.google.protobuf.ByteString + getResInfoBytes(); + + /** + * int32 req_no = 3; + * @return The reqNo. + */ + int getReqNo(); + + /** + * int32 id = 4; + * @return The id. + */ + int getId(); + + /** + * string name = 5; + * @return The name. + */ + java.lang.String getName(); + /** + * string name = 5; + * @return The bytes for name. + */ + com.google.protobuf.ByteString + getNameBytes(); +} diff --git a/src/test/java/com/iker/tinyrpc/proto/test_tinypb_server.proto b/src/test/java/com/iker/tinyrpc/proto/test_tinypb_server.proto new file mode 100644 index 0000000..e059049 --- /dev/null +++ b/src/test/java/com/iker/tinyrpc/proto/test_tinypb_server.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; +option cc_generic_services = true; + +option java_package = "com.iker.tinyrpc.proto"; +option java_generic_services = true; +option java_multiple_files = true; + + +message queryAgeReq { + int32 req_no = 1; + int32 id = 2; +} + +message queryAgeRes { + int32 ret_code = 1; + string res_info = 2; + int32 req_no = 3; + int32 id = 4; + int32 age = 5; +} + +message queryNameReq { + int32 req_no = 1; + int32 id = 2; + int32 type = 3; +} + +message queryNameRes { + int32 ret_code = 1; + string res_info = 2; + int32 req_no = 3; + int32 id = 4; + string name = 5; +} + + +service QueryService { + // rpc method name + rpc query_name(queryNameReq) returns (queryNameRes); + + // rpc method name + rpc query_age(queryAgeReq) returns (queryAgeRes); +} \ No newline at end of file