-
Notifications
You must be signed in to change notification settings - Fork 0
Hw34 grpc #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Hw34 grpc #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import com.google.protobuf.gradle.generateProtoTasks | ||
| import com.google.protobuf.gradle.ofSourceSet | ||
| import com.google.protobuf.gradle.id | ||
| import com.google.protobuf.gradle.plugins | ||
| import com.google.protobuf.gradle.protobuf | ||
| import com.google.protobuf.gradle.protoc | ||
|
|
||
| plugins { | ||
| id("idea") | ||
| id("com.google.protobuf") | ||
| } | ||
|
|
||
| val errorProneAnnotations: String by project | ||
| val tomcatAnnotationsApi: String by project | ||
|
|
||
| dependencies { | ||
| implementation("io.grpc:grpc-netty") | ||
| implementation("io.grpc:grpc-protobuf") | ||
| implementation("io.grpc:grpc-stub") | ||
| implementation("com.google.protobuf:protobuf-java") | ||
| implementation("com.google.errorprone:error_prone_annotations:$errorProneAnnotations") | ||
|
|
||
| implementation("org.apache.tomcat:annotations-api:$tomcatAnnotationsApi") | ||
|
|
||
| implementation ("ch.qos.logback:logback-classic") | ||
| } | ||
|
|
||
| val protoSrcDir = "$projectDir/build/generated" | ||
|
|
||
| idea { | ||
| module { | ||
| sourceDirs = sourceDirs.plus(file(protoSrcDir)) | ||
| } | ||
| } | ||
|
|
||
| sourceSets { | ||
| main { | ||
| proto { | ||
| srcDir(protoSrcDir) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protobuf { | ||
| generatedFilesBaseDir = protoSrcDir | ||
|
|
||
| protoc { | ||
| artifact = "com.google.protobuf:protoc:3.19.4" | ||
| } | ||
|
|
||
| generateProtoTasks { | ||
| ofSourceSet("main") | ||
| } | ||
| } | ||
|
|
||
| afterEvaluate { | ||
| tasks { | ||
| getByName("generateProto").dependsOn(processResources) | ||
| } | ||
| } | ||
|
|
||
| protobuf { | ||
| generatedFilesBaseDir = protoSrcDir | ||
|
|
||
| protoc { | ||
| artifact = "com.google.protobuf:protoc:3.19.4" | ||
| } | ||
| plugins { | ||
| id("grpc") { | ||
| artifact = "io.grpc:protoc-gen-grpc-java:1.56.1" | ||
| } | ||
| } | ||
|
|
||
| generateProtoTasks { | ||
| ofSourceSet("main").forEach { | ||
| it.plugins { | ||
| id("grpc") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| afterEvaluate { | ||
| tasks { | ||
| getByName("generateProto").dependsOn(processResources) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ru.otus.protobuf; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @SuppressWarnings({"squid:S2142"}) | ||
| public class NumServiceImpl extends NumServiceGrpc.NumServiceImplBase { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(NumServiceImpl.class); | ||
|
|
||
| private static final long NEXT_NUM_DELAY_MILLIS = 2_000; | ||
|
|
||
| @Override | ||
| public void getNumbers(final GetNumsRequest request, final StreamObserver<GetNumResponse> responseObserver) { | ||
|
|
||
| logger.info("Received request: firstValue={}, lastValue={}", request.getFirstNum(), request.getLastNum()); | ||
|
|
||
| try { | ||
| for (long i = request.getFirstNum() + 1; i <= request.getLastNum(); i++) { | ||
| GetNumResponse response = GetNumResponse.newBuilder().setNum(i).build(); | ||
| responseObserver.onNext(response); | ||
|
|
||
| logger.info("Sent value: {}", i); | ||
|
|
||
| Thread.sleep(NEXT_NUM_DELAY_MILLIS); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| logger.error("Interrupted while streaming", e); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
|
|
||
| responseObserver.onCompleted(); | ||
| logger.info("Stream completed"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ru.otus.protobuf; | ||
|
|
||
| import io.grpc.ManagedChannelBuilder; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class NumsClient { | ||
| private static final Logger logger = LoggerFactory.getLogger(NumsClient.class); | ||
| private static final String SERVER_HOST = "localhost"; | ||
| private static final int SERVER_PORT = 8190; | ||
|
|
||
| private static final NumsClientStreamObserver streamObserver = new NumsClientStreamObserver(); | ||
| private static AtomicLong lastServerValue = new AtomicLong(); | ||
|
|
||
| private static final long NEXT_NUM_DELAY_MILLIS = 1_000; | ||
|
|
||
| public static void main(String[] args) { | ||
| logger.info("Starting client"); | ||
|
|
||
| startObservingServerValues(); | ||
|
|
||
| long currentValue = 0; | ||
| for (int i = 0; i <= 50; i++) { | ||
| try { | ||
| currentValue = calculateNextValue(currentValue); | ||
|
|
||
| logger.info("currentValue: {}", currentValue); | ||
| Thread.sleep(NEXT_NUM_DELAY_MILLIS); | ||
|
|
||
| } catch (InterruptedException e) { | ||
| logger.error("Interrupted while running", e); | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static long calculateNextValue(final long currentValue) { | ||
| long serverValue = streamObserver.getLastValue(); | ||
| logger.info("serverValue: {}", currentValue); | ||
|
|
||
| long previousServerValue = lastServerValue.getAndSet(serverValue); | ||
|
|
||
| if (serverValue != previousServerValue) { | ||
| return currentValue + serverValue + 1; | ||
| } else { | ||
| return currentValue + 1; | ||
| } | ||
| } | ||
|
|
||
| private static void startObservingServerValues() { | ||
| GetNumsRequest request = | ||
| GetNumsRequest.newBuilder().setFirstNum(0).setLastNum(30).build(); | ||
|
|
||
| var channel = ManagedChannelBuilder.forAddress(SERVER_HOST, SERVER_PORT) | ||
| .usePlaintext() | ||
| .build(); | ||
|
|
||
| NumServiceGrpc.newStub(channel).getNumbers(request, streamObserver); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.otus.protobuf; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class NumsClientStreamObserver implements StreamObserver<GetNumResponse> { | ||
| private static final Logger logger = LoggerFactory.getLogger(NumsClientStreamObserver.class); | ||
| private long lastValue; | ||
|
|
||
| @Override | ||
| public void onNext(GetNumResponse numberResponse) { | ||
| lastValue = numberResponse.getNum(); | ||
| logger.info("new value: {}", lastValue); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
| logger.error("Error occurred: ", throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCompleted() { | ||
| logger.info("request completed"); | ||
| } | ||
|
|
||
| public long getLastValue() { | ||
| return lastValue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. одного synchronized не достаточно. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. как реализуется требование ДЗ ?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++
Если точнее - https://github.com/manfe513/java-otus/pull/15/files#diff-e3224650e63e135729d99535f01823245d730cd579fdd3aaf32b30305bb4bcd8R44 Вот на примере выдачи:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не вижу в коде добавленный synchnonized
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не запушилось по-видимому не углядел |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ru.otus.protobuf; | ||
|
|
||
| import io.grpc.ServerBuilder; | ||
| import java.io.IOException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class NumsServer { | ||
| private static final Logger logger = LoggerFactory.getLogger(NumsServer.class); | ||
| public static final int SERVER_PORT = 8190; | ||
|
|
||
| public static void main(String[] args) throws IOException, InterruptedException { | ||
|
|
||
| var server = ServerBuilder.forPort(SERVER_PORT) | ||
| .addService(new NumServiceImpl()) | ||
| .build() | ||
| .start(); | ||
|
|
||
| logger.info("server waiting for client connections..."); | ||
| server.awaitTermination(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package ru.otus.protobuf; | ||
|
|
||
| option java_multiple_files = true; | ||
|
|
||
| message GetNumsRequest { | ||
| int64 firstNum = 1; | ||
| int64 lastNum = 2; | ||
| } | ||
|
|
||
| message GetNumResponse { | ||
| int64 num = 1; | ||
| } | ||
|
|
||
| service NumService { | ||
| rpc getNumbers(GetNumsRequest) returns (stream GetNumResponse); | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут проще можно:
currentValue + lastServerValue.getAndSet(0) + 1;