Skip to content

Commit

Permalink
update version
Browse files Browse the repository at this point in the history
  • Loading branch information
姑苏 committed Jan 7, 2021
1 parent 36969a9 commit a20ca7f
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion common/common.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
1 change: 0 additions & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down
19 changes: 0 additions & 19 deletions common/src/main/java/rpc/common/RpcResponse.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* @date 2021/1/6
*/
@Data
public class NettyClient extends ChannelInboundHandlerAdapter implements Callable {
public class RpcClient extends ChannelInboundHandlerAdapter implements Callable {

private ChannelHandlerContext context;
private String result;
private RpcRequest request;
private String serverHost;
private int serverPort;

public NettyClient(String serverHost, int serverPort) {
public RpcClient(String serverHost, int serverPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import java.lang.reflect.Method;
import rpc.common.RpcDecoder;
import rpc.common.RpcEncoder.JSONRpcSerializer;
import rpc.common.RpcRequest;
import rpc.common.RpcResponse;

/**
* @author razertory
* @date 2021/1/6
*/
public class NettyServer {
public class RpcServer {

public void init(String host, int port) throws Exception {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
Expand All @@ -42,7 +42,7 @@ protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
@Override
public void channelRead(ChannelHandlerContext ctx, Object o)
throws Exception {
ctx.writeAndFlush("foo");
ctx.writeAndFlush(invoke(o));
}
});
}
Expand All @@ -51,15 +51,23 @@ public void channelRead(ChannelHandlerContext ctx, Object o)
System.out.println("started server on port: " + port);
}

private RpcResponse invoke(Object o) {
RpcResponse rpcResponse = new RpcResponse();
if (o instanceof RpcRequest) {
rpcResponse.setRequestId(((RpcRequest) o).getRequestId());
private Object invoke(Object o) throws Exception{
if (!(o instanceof RpcRequest)) {
throw new Exception("fuck");
}
return rpcResponse;
RpcRequest rpcRequest = (RpcRequest) o;
Object serverObj = this;
Class<?> serverClass = serverObj.getClass();
String methodName = rpcRequest.getMethodName();
Class<?>[] parameterTypes = rpcRequest.getParameterTypes();
Method method = serverClass.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
Object[] parameters = rpcRequest.getParameters();
Object ret = method.invoke(serverObj, parameters);
return ret;
}

public static void main(String[] args) throws Exception {
new NettyServer().init("", new Integer(args[0]));
new RpcServer().init("", new Integer(args[0]));
}
}
14 changes: 7 additions & 7 deletions common/src/main/java/rpc/proxy/RpcProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import rpc.common.RpcEncoder;
import rpc.common.RpcRequest;
import rpc.common.RpcEncoder.JSONRpcSerializer;
import rpc.io.NettyClient;
import rpc.io.RpcClient;

/**
* @author razertory
Expand All @@ -33,10 +33,10 @@ public class RpcProxy {
private ExecutorService executorService = Executors
.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

private NettyClient nettyClient;
private RpcClient rpcClient;

public Object createProxy(final Class<?> serviceClass, String serverHost, int serverPort) {
nettyClient = new NettyClient(serverHost, serverPort);
rpcClient = new RpcClient(serverHost, serverPort);
return Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),

Expand All @@ -50,9 +50,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
request.setMethodName(method.getName());
request.setParameterTypes(method.getParameterTypes());
request.setParameters(args);
nettyClient.setRequest(request);
rpcClient.setRequest(request);
bind();
Object result = executorService.submit(nettyClient).get();
Object result = executorService.submit(rpcClient).get();
return result;
}
});
Expand All @@ -70,9 +70,9 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addFirst(new RpcEncoder(RpcRequest.class, new JSONRpcSerializer()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(nettyClient);
pipeline.addLast(rpcClient);
}
});
bootstrap.connect(nettyClient.getServerHost(), nettyClient.getServerPort()).sync();
bootstrap.connect(rpcClient.getServerHost(), rpcClient.getServerPort()).sync();
}
}
11 changes: 11 additions & 0 deletions common/src/main/java/util/LogUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util;

/**
* @author gusu
* @date 2021/1/7
*/
public class LogUtil {
public static void log(Object s) {
System.out.println(s);
}
}
8 changes: 5 additions & 3 deletions common/src/test/java/rpc/proxy/RpcProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import org.junit.Assert;
import org.junit.Test;
import rpc.io.NettyServer;
import rpc.io.RpcServer;

public class RpcProxyTest {

private String host = "127.0.0.1";
private int port = 10001;

@Test
public void createProxy() throws Exception {
new NettyServer().init(host, port);
SampleService sampleService = (SampleService) new RpcProxy().createProxy(SampleService.class, host, port);
new SampleServiceImpl().init(host, port);
SampleService sampleService = (SampleService) new RpcProxy()
.createProxy(SampleService.class, host, port);
Assert.assertEquals(new SampleServiceImpl().foo(""), sampleService.foo(""));
}
}
4 changes: 3 additions & 1 deletion common/src/test/java/rpc/proxy/SampleServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

package rpc.proxy;

import rpc.io.RpcServer;

/**
* @author razertory
* @date 2021/1/6
*/
public class SampleServiceImpl implements SampleService{
public class SampleServiceImpl extends RpcServer implements SampleService {

public String foo(String greet) {
return "foo" + greet;
Expand Down
2 changes: 1 addition & 1 deletion mapreduce/mapreduce.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<artifactId>MIT6.824-Java</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<modules>
<module>common</module>
<module>mapreduce</module>
Expand Down
2 changes: 1 addition & 1 deletion raft/raft.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
2 changes: 1 addition & 1 deletion raftkv/raftkv.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down

0 comments on commit a20ca7f

Please sign in to comment.