Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions CodenameOne/src/com/codename1/annotations/grpc/GrpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.annotations.grpc;

Expand All @@ -15,11 +28,14 @@
import java.lang.annotation.Target;

/// Marks an interface as a gRPC client that the build-time annotation
/// processor wires up to a generated gRPC-Web implementation. Each
/// processor wires up to a generated implementation: gRPC-Web in an
/// application, native gRPC over HTTP/2 in a backend. Each
/// abstract method must carry an [Rpc] annotation naming the gRPC
/// method, and at most one non-callback parameter -- the request
/// message, a `@ProtoMessage`-annotated POJO. The final parameter is
/// an `OnComplete<Response<ResponseMessage>>` callback.
/// message, a `@ProtoMessage`-annotated POJO. In an application the
/// final parameter is an `OnComplete<Response<ResponseMessage>>`
/// callback; in a backend methods are blocking and may stream (see the
/// backend's `com.codename1.backend.grpc` package).
///
/// The fully qualified service path defaults to
/// `<value()>/<methodName>` -- for example
Expand All @@ -28,12 +44,14 @@
/// interface-level default.
///
/// The processor emits a `<SimpleName>Impl` class in generated-sources
/// and registers it with [com.codename1.io.grpc.GrpcClients] so the
/// and registers it with the runtime's client registry
/// (`com.codename1.io.grpc.GrpcClients` in an application) so the
/// interface's `static T of(String baseUrl)` factory can return an
/// instance without the project source referencing the impl directly.
/// Mirrors [com.codename1.annotations.rest.RestClient].
/// Mirrors `com.codename1.annotations.rest.RestClient`.
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
@com.codename1.impl.SharedWithBackend
public @interface GrpcClient {
/// Fully qualified gRPC service path (without leading slash), e.g.
/// `helloworld.Greeter`. Combined with each method's [Rpc#value()]
Expand Down
14 changes: 14 additions & 0 deletions CodenameOne/src/com/codename1/annotations/grpc/ProtoEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.annotations.grpc;

Expand All @@ -27,5 +40,6 @@
/// the field is absent.
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
@com.codename1.impl.SharedWithBackend
public @interface ProtoEnum {
}
14 changes: 14 additions & 0 deletions CodenameOne/src/com/codename1/annotations/grpc/ProtoField.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.annotations.grpc;

Expand All @@ -28,6 +41,7 @@
/// `fixed32` / `fixed64` in proto3).
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
@com.codename1.impl.SharedWithBackend
public @interface ProtoField {
/// Protobuf field tag (positive integer, unique per message).
int tag();
Expand Down
16 changes: 15 additions & 1 deletion CodenameOne/src/com/codename1/annotations/grpc/ProtoMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.annotations.grpc;

Expand All @@ -29,10 +42,11 @@
/// `@ProtoEnum`-marked enums, or `java.util.List` of any of the
/// above (interpreted as `repeated` in proto3).
///
/// Mirrors the design of [com.codename1.annotations.Mapped] for JSON
/// Mirrors the design of `com.codename1.annotations.Mapped` for JSON
/// projection; you can carry both annotations on the same class to
/// support JSON and protobuf wire formats off the same POJO.
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
@com.codename1.impl.SharedWithBackend
public @interface ProtoMessage {
}
22 changes: 19 additions & 3 deletions CodenameOne/src/com/codename1/annotations/grpc/Rpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.annotations.grpc;

Expand All @@ -14,17 +27,20 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/// Binds a [GrpcClient] interface method to a gRPC unary RPC. The
/// Binds a method to a gRPC RPC: a [GrpcClient] interface method in an
/// application or a backend, or a method of a backend `@GrpcService`. The
/// `value` is the gRPC method name (e.g. `SayHello`) and is combined
/// with the interface-level [GrpcClient#value()] to form
/// `/<service>/<method>`. The optional [#service()] overrides the
/// interface-level service path for a single method (useful when an
/// interface aggregates calls into multiple services).
///
/// Streaming RPCs are not supported in this release -- only unary
/// (single request, single response).
/// Applications call unary RPCs only (gRPC-Web carries no client
/// streaming). A backend supports all four shapes; it infers the shape
/// from the method signature.
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@com.codename1.impl.SharedWithBackend
public @interface Rpc {
/// The gRPC method name as declared in the `.proto` `rpc` line,
/// e.g. `SayHello`. Joined to the service path via `/`.
Expand Down
15 changes: 14 additions & 1 deletion CodenameOne/src/com/codename1/annotations/grpc/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
/// gRPC-Web client annotations. [GrpcClient] marks an interface as a
/// gRPC client; [Rpc] binds each method to a unary RPC.
/// gRPC client; [Rpc] binds each method to an RPC.
/// [ProtoMessage] marks a POJO as a protobuf message;
/// [ProtoField] tags each field; [ProtoEnum] marks an enum as
/// protobuf-encoded.
Expand Down
14 changes: 14 additions & 0 deletions CodenameOne/src/com/codename1/io/grpc/ProtoCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.grpc;

Expand All @@ -19,6 +32,7 @@
///
/// Implementations must be stateless -- the same codec instance is
/// shared across concurrent requests.
@com.codename1.impl.SharedWithBackend
public interface ProtoCodec<T> {

/// Writes `value`'s populated fields to `out` in the protobuf
Expand Down
16 changes: 15 additions & 1 deletion CodenameOne/src/com/codename1/io/grpc/ProtoCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.grpc;

Expand All @@ -19,7 +32,8 @@
/// impls and per-class codecs call [#lookup(Class)] to resolve
/// nested-message types at runtime.
///
/// Mirrors [com.codename1.io.rest.RestClients].
/// Mirrors `com.codename1.io.rest.RestClients`.
@com.codename1.impl.SharedWithBackend
public final class ProtoCodecs {

private static final Map<Class<?>, ProtoCodec<?>> REGISTRY = new HashMap<Class<?>, ProtoCodec<?>>();
Expand Down
14 changes: 14 additions & 0 deletions CodenameOne/src/com/codename1/io/grpc/ProtoReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.grpc;

Expand All @@ -23,6 +36,7 @@
/// field number, and call the appropriate `readXxx` method. Unknown
/// fields are passed to [#skipField(int)] -- the same recovery
/// behaviour `protoc`-generated code exhibits.
@com.codename1.impl.SharedWithBackend
public final class ProtoReader {

private final byte[] buf;
Expand Down
14 changes: 14 additions & 0 deletions CodenameOne/src/com/codename1/io/grpc/ProtoWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.grpc;

Expand All @@ -28,6 +41,7 @@
/// | 1 | I64 | fixed64, sfixed64, double |
/// | 2 | LEN | string, bytes, embedded messages, packed repeated |
/// | 5 | I32 | fixed32, sfixed32, float |
@com.codename1.impl.SharedWithBackend
public final class ProtoWriter {

public static final int WIRE_VARINT = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codenameone.developerguide.backend.grpc;

import com.codename1.annotations.grpc.Rpc;
import com.codename1.backend.annotations.GrpcService;
import com.codename1.backend.grpc.StreamObserver;
import java.util.Iterator;

// tag::backend-grpc-service[]
@GrpcService("demo.Greeter")
public class Greeter {
@Rpc("SayHello") // unary
public HelloReply sayHello(HelloRequest request) {
return reply("Hello " + request.name);
}

@Rpc("Countdown") // server streaming
public void countdown(HelloRequest request, StreamObserver<HelloReply> out) {
for (int i = 3; i > 0; i--) {
out.onNext(reply(request.name + " " + i));
}
}

@Rpc("Collect") // client streaming
public HelloReply collect(Iterator<HelloRequest> requests) {
StringBuilder names = new StringBuilder();
while (requests.hasNext()) {
names.append(requests.next().name).append(' ');
}
return reply(names.toString().trim());
}

@Rpc("Chat") // bidirectional streaming
public void chat(Iterator<HelloRequest> in, StreamObserver<HelloReply> out) {
while (in.hasNext()) {
out.onNext(reply("Hi " + in.next().name));
}
}
// end::backend-grpc-service[]

private static HelloReply reply(String text) {
HelloReply r = new HelloReply();
r.name = text;
return r;
}
// tag::backend-grpc-service[]
}
// end::backend-grpc-service[]
Loading
Loading