Skip to content

Commit

Permalink
pw_rpc: Create PacketByteFactory
Browse files Browse the repository at this point in the history
Bug: b/360174359

Test: EXEMPT just wrapping generated code

Change-Id: I16222ca66741cd306910db30bf7fba8bb1c38505
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/230011
Commit-Queue: Kieran Cyphus <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Reviewed-by: Wyatt Hepler <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
Kieran Cyphus authored and CQ Bot Account committed Aug 19, 2024
1 parent 306aa50 commit f788ed4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions pw_rpc/java/main/dev/pigweed/pw_rpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RPC_CLIENT_SOURCES = [
"InvalidRpcStateException.java",
"Method.java",
"MethodClient.java",
"PacketByteFactory.java",
"Packets.java",
"PendingRpc.java",
"RpcError.java",
Expand Down
75 changes: 75 additions & 0 deletions pw_rpc/java/main/dev/pigweed/pw_rpc/PacketByteFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package dev.pigweed.pw_rpc;

import com.google.protobuf.MessageLite;
import dev.pigweed.pw_rpc.internal.Packet.PacketType;
import dev.pigweed.pw_rpc.internal.Packet.RpcPacket;

/** Util class for creating request / response packets for testing. */
public final class PacketByteFactory {
private PacketByteFactory() {}

/**
* Creates encoded request packet bytes.
*
* <p>There is no parameter for {@link Status} since request packets always have a status of
* {@link Status#OK}.
*
* @param channelId the channel ID for the packet
* @param serviceName the service name for the packet
* @param methodName the method name for the packet
* @param payload the payload for the packet
* @return the encoded packet bytes
*/
public static byte[] requestBytes(
int channelId, String serviceName, String methodName, MessageLite payload) {
return packetBytes(channelId, Status.OK, serviceName, methodName, payload, PacketType.REQUEST);
}

/**
* Creates encoded response packet bytes.
*
* @param channelId the channel ID for the packet
* @param status the status for the packet
* @param serviceName the service name for the packet
* @param methodName the method name for the packet
* @param payload the payload for the packet
* @return the encoded packet bytes
*/
public static byte[] responseBytes(
int channelId, Status status, String serviceName, String methodName, MessageLite payload) {
return packetBytes(channelId, status, serviceName, methodName, payload, PacketType.RESPONSE);
}

private static byte[] packetBytes(int channelId,
Status status,
String serviceName,
String methodName,
MessageLite payload,
PacketType type) {
RpcPacket.Builder builder = RpcPacket.newBuilder()
.setStatus(status.code())
.setType(type)
.setChannelId(channelId)
.setServiceId(Ids.calculate(serviceName))
.setMethodId(Ids.calculate(methodName));

if (payload != null) {
builder.setPayload(payload.toByteString());
}
return builder.build().toByteArray();
}
}

0 comments on commit f788ed4

Please sign in to comment.