├── .gitignore ├── README.md ├── src └── main │ ├── proto │ └── CSTIRpcServer.proto │ └── java │ └── com │ └── github │ └── leafee98 │ └── CSTI │ └── rpcserver │ ├── App.java │ └── rpc │ └── RPCServerImpl.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.idea/ 3 | /target/ 4 | /.vscode/ 5 | /*.iml 6 | /.settings/ 7 | /.project 8 | /.classpath 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Class Schedule To Icalendar (RPCServer) 2 | 3 | 借助于 [grpc](https://grpc.io) 实现的搭载 [class-schedule-to-icalendar-core](https://github.com/leafee98/class-schedule-to-icalendar-core) 的 RPC 服务, 将课程表到 ical 文件的过程实现到 RPC 调用 4 | 5 | ## 接口调用 6 | 7 | 详细请参见 [proto](/src/main/proto) 下的文件 8 | 9 | ## 运行 10 | 11 | 运行包含依赖的 jar 文件即可, 运行时参数如下 12 | 13 | ``` 14 | -l port to listen, default 8047 15 | ``` 16 | -------------------------------------------------------------------------------- /src/main/proto/CSTIRpcServer.proto: -------------------------------------------------------------------------------- 1 | 2 | syntax = "proto3"; 3 | 4 | option java_multiple_files = true; 5 | option java_package = "com.github.leafee98.CSTI.rpcserver.rpc"; 6 | option java_outer_classname = "CSTIRpcServerProto"; 7 | 8 | package rpcserver; 9 | 10 | service CSTIRpcServer { 11 | // generate ical with json configure 12 | rpc jsonGenerate(ConfJson) returns (ResultIcal) {} 13 | rpc tomlGenerate(ConfToml) returns (ResultIcal) {} 14 | } 15 | 16 | message ConfJson { 17 | string content = 1; 18 | } 19 | 20 | message ConfToml { 21 | string content = 1; 22 | } 23 | 24 | message ResultIcal { 25 | string content = 2; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/leafee98/CSTI/rpcserver/App.java: -------------------------------------------------------------------------------- 1 | package com.github.leafee98.CSTI.rpcserver; 2 | 3 | import java.io.IOException; 4 | 5 | import com.github.leafee98.CSTI.rpcserver.rpc.RPCServerImpl; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class App { 11 | private static final Logger logger = LoggerFactory.getLogger(App.class); 12 | 13 | private RPCServerImpl server; 14 | private int listenPort; 15 | 16 | private void initDefaultConfigure() { 17 | this.listenPort = 8047; 18 | } 19 | 20 | private void parseParameter(String[] args) { 21 | for (int i = 0; i < args.length; i++) { 22 | if (args[i].equals("-l")) { 23 | this.listenPort = Integer.parseInt(args[i + 1]); 24 | i += 1; 25 | } 26 | } 27 | } 28 | 29 | private void logConfigure() { 30 | logger.info("showing the configuration"); 31 | logger.info("configure: listenPort = {}", this.listenPort); 32 | } 33 | 34 | private void startServer() throws IOException { 35 | server = new RPCServerImpl(this.listenPort); 36 | server.start(); 37 | } 38 | 39 | private void addShutdownHook() { 40 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { 41 | // use system error stream to print log 42 | // since logger is not available in shutdown hook 43 | System.err.println("rpcserver stopping ..."); 44 | try { 45 | server.stop(); 46 | } catch (InterruptedException e) { 47 | System.err.println("error while shutting down"); 48 | e.printStackTrace(System.err); 49 | } 50 | System.err.println("rpcserver shutdown"); 51 | })); 52 | } 53 | 54 | private void blockUntilShutdown() throws InterruptedException { 55 | server.blockUntilShutdown(); 56 | } 57 | 58 | public static void main(String[] args) throws IOException, InterruptedException { 59 | App app = new App(); 60 | app.initDefaultConfigure(); 61 | app.parseParameter(args); 62 | app.logConfigure(); 63 | 64 | app.startServer(); 65 | app.addShutdownHook(); 66 | app.blockUntilShutdown(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/leafee98/CSTI/rpcserver/rpc/RPCServerImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.leafee98.CSTI.rpcserver.rpc; 2 | 3 | import com.github.leafee98.CSTI.core.bean.ScheduleObject; 4 | import com.github.leafee98.CSTI.core.bean.loader.JSONLoader; 5 | import com.github.leafee98.CSTI.core.generate.Generator; 6 | import io.grpc.Server; 7 | import io.grpc.ServerBuilder; 8 | import io.grpc.Status; 9 | import io.grpc.StatusRuntimeException; 10 | import io.grpc.protobuf.services.ProtoReflectionService; 11 | import io.grpc.stub.StreamObserver; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | 15 | import java.io.IOException; 16 | import java.util.concurrent.TimeUnit; 17 | 18 | public class RPCServerImpl { 19 | private static final Logger logger = LoggerFactory.getLogger(RPCServerImpl.class); 20 | 21 | private final Server server; 22 | 23 | public void start() throws IOException { 24 | logger.info("starting rpc server ..."); 25 | server.start(); 26 | } 27 | 28 | public void blockUntilShutdown() throws InterruptedException { 29 | if (server != null) { 30 | server.awaitTermination(); 31 | } 32 | } 33 | 34 | public void stop() throws InterruptedException { 35 | if (server != null) { 36 | server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 37 | } 38 | } 39 | 40 | public RPCServerImpl(int port) { 41 | this.server = ServerBuilder.forPort(port) 42 | .addService(new RPCService()) 43 | .addService(ProtoReflectionService.newInstance()) 44 | .build(); 45 | } 46 | 47 | private static class RPCService extends CSTIRpcServerGrpc.CSTIRpcServerImplBase { 48 | private final JSONLoader jsonLoader; 49 | 50 | public RPCService() { 51 | this.jsonLoader = new JSONLoader(); 52 | } 53 | 54 | @Override 55 | public void jsonGenerate(ConfJson request, StreamObserver responseObserver) { 56 | try { 57 | String content = request.getContent(); 58 | ResultIcal.Builder builder = ResultIcal.newBuilder(); 59 | ScheduleObject scheduleObject = jsonLoader.load(content); 60 | Generator generator = new Generator(scheduleObject); 61 | String generated = generator.generate().toString(); 62 | builder.setContent(generated); 63 | 64 | responseObserver.onNext(builder.build()); 65 | responseObserver.onCompleted(); 66 | 67 | logger.info("handle rpc jsonGenerate success"); 68 | } catch (Exception e) { 69 | responseObserver.onError(new StatusRuntimeException(Status.ABORTED.withDescription( 70 | "error while transforming, maybe the request content format wrong.\n" + 71 | "exception detail: " + e.toString()))); 72 | 73 | logger.error("handle rpc jsonGenerate failed, exception detail follow:"); 74 | logger.error(e.toString()); 75 | } 76 | } 77 | 78 | @Override 79 | public void tomlGenerate(ConfToml request, StreamObserver responseObserver) { 80 | String errMsg = "not implemented rpc tomlGenerate called, respond error"; 81 | logger.warn(errMsg); 82 | responseObserver.onError(new Exception(errMsg)); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.leafee98 8 | class-schedule-to-icalendar-rpcserver 9 | v0.3.0 10 | 11 | 12 | 11 13 | 11 14 | UTF-8 15 | 16 | 17 | 18 | 19 | jitpack.io 20 | https://jitpack.io 21 | 22 | 23 | 24 | 25 | 26 | 27 | com.github.leafee98 28 | class-schedule-to-icalendar-core 29 | v0.3.0 30 | 31 | 32 | 33 | org.slf4j 34 | slf4j-api 35 | 2.0.0-alpha1 36 | 37 | 38 | org.slf4j 39 | slf4j-jdk14 40 | 2.0.0-alpha1 41 | 42 | 43 | 44 | io.grpc 45 | grpc-services 46 | 1.35.0 47 | 48 | 49 | io.grpc 50 | grpc-netty-shaded 51 | 1.35.0 52 | 53 | 54 | io.grpc 55 | grpc-protobuf 56 | 1.35.0 57 | 58 | 59 | io.grpc 60 | grpc-stub 61 | 1.35.0 62 | 63 | 64 | org.apache.tomcat 65 | annotations-api 66 | 6.0.53 67 | provided 68 | 69 | 70 | 71 | 72 | 73 | 74 | kr.motd.maven 75 | os-maven-plugin 76 | 1.6.2 77 | 78 | 79 | 80 | 81 | 82 | org.xolstice.maven.plugins 83 | protobuf-maven-plugin 84 | 0.6.1 85 | 86 | com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier} 87 | grpc-java 88 | io.grpc:protoc-gen-grpc-java:1.35.0:exe:${os.detected.classifier} 89 | 90 | 91 | 92 | 93 | compile 94 | compile-custom 95 | 96 | 97 | 98 | 99 | 100 | 101 | maven-jar-plugin 102 | 3.2.0 103 | 104 | 105 | 106 | com.github.leafee98.CSTI.rpcserver.App 107 | 108 | 109 | 110 | 111 | 112 | 113 | maven-assembly-plugin 114 | 3.3.0 115 | 116 | 117 | 118 | com.github.leafee98.CSTI.rpcserver.App 119 | 120 | 121 | 122 | jar-with-dependencies 123 | 124 | 125 | 126 | 127 | make-assembly 128 | package 129 | 130 | single 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | --------------------------------------------------------------------------------