Skip to content

Commit

Permalink
[RSDK-8312] Collect and send API and SDK version in metadata (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Jul 30, 2024
1 parent d43d99f commit e20e23f
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/update-protos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Set API Version
run: |
echo ${{ github.event.client_payload.tag }} > api_version.lock
- name: Generate buf
run: make buf

Expand Down
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
.PHONY: buf
buf:
rm -rf core/sdk/src/main/gen
rm -rf core/sdk/src/main/resources/protofds
mkdir -p core/sdk/src/main/resources/protofds
buf build --as-file-descriptor-set=true -o core/sdk/src/main/resources/protofds/viam.api buf.build/viamrobotics/api
buf generate --template buf.gen.yaml buf.build/viamrobotics/api
buf build --as-file-descriptor-set=true -o core/sdk/src/main/resources/protofds/viam.api buf.build/viamrobotics/api:$$(cat api_version.lock)
buf generate --template buf.gen.yaml buf.build/viamrobotics/api:$$(cat api_version.lock)
buf generate --template buf.gen.yaml buf.build/viamrobotics/goutils
buf generate --template buf.gen.yaml buf.build/googleapis/googleapis

.PHONY: run_client
run_client:
./gradlew runExample1Client --args="localhost:8080"

.PHONY: run_client_webrtc_auth
run_client_webrtc_auth:
./gradlew runExample2Client

.PHONY: run_client_grpc_auth
run_client_grpc_auth:
./gradlew runExample3Client

.PHONY: run_server
run_server:
./gradlew runExample1Server --args="8080"
1 change: 1 addition & 0 deletions api_version.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.1.327
6 changes: 3 additions & 3 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: v1
version: v2
plugins:
- plugin: buf.build/protocolbuffers/java:v25.2
- remote: buf.build/protocolbuffers/java:v27.2
out: core/sdk/src/main/gen
opt: lite
- plugin: buf.build/grpc/java:v1.61.1
- remote: buf.build/grpc/java:v1.65.1
out: core/sdk/src/main/gen
opt: lite
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ subprojects {
ext {
min_api = 29
target_api = 34
api_version = "unreleased"
File file = rootProject.file("api_version.lock")
file.withReader { reader -> api_version = reader.readLine() }
}

def pomConfig = {
Expand Down
20 changes: 20 additions & 0 deletions core/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,23 @@ repositories {
kotlin {
jvmToolchain(11)
}

abstract class CreateVersionFileTask extends DefaultTask {
@TaskAction
def action() {
File root = project.file("src/main/kotlin/com/viam/sdk/core/util/VersionInfo.kt")
root.withWriter {
writer -> {
writer.writeLine("package com.viam.sdk.core.util\n")
writer.writeLine("const val SDK_VERSION = \"v$project.version\"")
writer.writeLine("const val API_VERSION = \"$project.api_version\"")
}
}
}
}

tasks.register("createVersionsFile", CreateVersionFileTask)

afterEvaluate {
tasks.compileJava.dependsOn 'createVersionsFile'
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.viam.sdk.core.rpc;

import io.grpc.CallCredentials;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.*;
import io.grpc.stub.MetadataUtils;

import java.util.Optional;

import com.viam.sdk.core.util.Metadata;

public class BasicManagedChannel extends Channel {

private final ManagedChannel channel;
private final io.grpc.Channel channel;
private final CallCredentials callCreds;

public BasicManagedChannel(final ManagedChannel channel) {
this(channel, null);
}

public BasicManagedChannel(final ManagedChannel channel, final CallCredentials callCreds) {
this.channel = channel;
ClientInterceptor versionInterceptor = MetadataUtils.newAttachHeadersInterceptor(Metadata.getVERSION_METADATA());
this.channel = ClientInterceptors.intercept(channel, versionInterceptor);
this.callCreds = callCreds;
}

Expand Down
5 changes: 1 addition & 4 deletions core/sdk/src/main/java/com/viam/sdk/core/rpc/Dialer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import com.viam.sdk.core.webrtc.PeerConnection;
import com.viam.sdk.core.webrtc.PeerConnectionFactory;
import com.viam.sdk.core.webrtc.SessionDescription;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.*;
import io.grpc.stub.MetadataUtils;
import java.io.Closeable;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private static Metadata toGRPCMetadata(final Grpc.Metadata metadata) {

@Override
public void start(final Listener<ResponseT> responseListener, Metadata headers) {
headers.merge(com.viam.sdk.core.util.Metadata.getVERSION_METADATA());

this.responseListener = responseListener;
final String method = "/" + this.methodDescriptor.getFullMethodName();
final Grpc.RequestHeaders requestHeaders = Grpc.RequestHeaders.newBuilder()
Expand Down Expand Up @@ -136,7 +138,7 @@ public void sendMessage(RequestT message) {
}

private void writeMessage(boolean eos, List<Byte> msgBytes) {
if (msgBytes == null || msgBytes.size() == 0) {
if (msgBytes == null || msgBytes.isEmpty()) {
final Grpc.PacketMessage packet = Grpc.PacketMessage.newBuilder()
.setEom(true)
.build();
Expand All @@ -149,12 +151,12 @@ private void writeMessage(boolean eos, List<Byte> msgBytes) {
return;
}

while (msgBytes.size() != 0) {
while (!msgBytes.isEmpty()) {
final int amountToSend = Math.min(msgBytes.size(), MAX_REQUEST_MESSAGE_PACKET_DATA_SIZE);
final Grpc.PacketMessage.Builder packetBuilder = Grpc.PacketMessage.newBuilder();
packetBuilder.setData(ByteString.copyFrom(Bytes.toArray(msgBytes.subList(0, amountToSend))));
msgBytes = msgBytes.subList(amountToSend, msgBytes.size());
if (msgBytes.size() == 0) {
if (msgBytes.isEmpty()) {
packetBuilder.setEom(true);
}
final Grpc.RequestMessage requestMessage = Grpc.RequestMessage.newBuilder()
Expand Down
2 changes: 0 additions & 2 deletions core/sdk/src/main/kotlin/com/viam/sdk/core/util/Media.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@file:JvmName("MediaUtils")

package com.viam.sdk.core.util

import com.viam.component.camera.v1.Camera.Format
Expand Down Expand Up @@ -153,7 +152,6 @@ private fun getDimensionsFromPNG(image: Iterable<Byte>): ImageDimensions {
val width = header.getInt(4)
val height = header.getInt(8)
return ImageDimensions(width, height)

}

private fun getDimensionsFromViamRGBA(image: Iterable<Byte>): ImageDimensions {
Expand Down
14 changes: 14 additions & 0 deletions core/sdk/src/main/kotlin/com/viam/sdk/core/util/Metadata.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@file:JvmName("Metadata")
package com.viam.sdk.core.util

import io.grpc.Metadata

/**
* Metadata containing client and API versions
*/
val VERSION_METADATA: Metadata
get() {
val md = Metadata()
md.put(Metadata.Key.of("viam_client", Metadata.ASCII_STRING_MARSHALLER), "java;$SDK_VERSION;$API_VERSION")
return md;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.viam.sdk.core.util

const val SDK_VERSION = "v0.0.5"
const val API_VERSION = "v0.1.327"
19 changes: 19 additions & 0 deletions core/sdk/src/test/kotlin/com/viam/sdk/core/util/MetadataTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.viam.sdk.core.util

import io.grpc.Metadata
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

class MetadataTest {

@Test
fun getVersionMetadata() {
val metadata = VERSION_METADATA
val versionString = metadata.get(Metadata.Key.of("viam_client", Metadata.ASCII_STRING_MARSHALLER))
assertNotNull(versionString)
val components = versionString!!.split(";")
assertEquals(components[0], "java")
assertTrue(components[1].matches("v[0-9]+\\.[0-9]+\\.[0-9]+".toRegex()))
assertTrue(components[2].matches("(v[0-9]+\\.[0-9]+\\.[0-9]+)|unreleased".toRegex()))
}
}

0 comments on commit e20e23f

Please sign in to comment.