-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
pw_rpc/java/main/dev/pigweed/pw_rpc/PacketByteFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |