From cb5e96d0106ed968640c11f7a0162d7ce25d4eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 27 Mar 2012 21:50:01 +0200 Subject: Test classes for sending protocol buffers. --- build_protobufs.sh | 4 + src/main/java/com/orbekk/protobuf/Rpc.java | 1085 ++++++++++++++++++++ src/main/java/com/orbekk/protobuf/Rpc.proto | 15 + .../com/orbekk/protobuf/SimpleProtobufClient.java | 29 + .../com/orbekk/protobuf/SimpleProtobufServer.java | 71 ++ 5 files changed, 1204 insertions(+) create mode 100755 build_protobufs.sh create mode 100644 src/main/java/com/orbekk/protobuf/Rpc.java create mode 100644 src/main/java/com/orbekk/protobuf/Rpc.proto create mode 100644 src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java create mode 100644 src/main/java/com/orbekk/protobuf/SimpleProtobufServer.java diff --git a/build_protobufs.sh b/build_protobufs.sh new file mode 100755 index 0000000..59136c6 --- /dev/null +++ b/build_protobufs.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +find src/main/java -name '*.proto' -exec \ + protoc {} --java_out=src/main/java \; diff --git a/src/main/java/com/orbekk/protobuf/Rpc.java b/src/main/java/com/orbekk/protobuf/Rpc.java new file mode 100644 index 0000000..2748329 --- /dev/null +++ b/src/main/java/com/orbekk/protobuf/Rpc.java @@ -0,0 +1,1085 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: src/main/java/com/orbekk/protobuf/Rpc.proto + +package com.orbekk.protobuf; + +public final class Rpc { + private Rpc() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + } + public interface RequestOrBuilder + extends com.google.protobuf.MessageOrBuilder { + + // optional string full_service_name = 1; + boolean hasFullServiceName(); + String getFullServiceName(); + + // optional string method_name = 2; + boolean hasMethodName(); + String getMethodName(); + + // optional bytes request_proto = 3; + boolean hasRequestProto(); + com.google.protobuf.ByteString getRequestProto(); + } + public static final class Request extends + com.google.protobuf.GeneratedMessage + implements RequestOrBuilder { + // Use Request.newBuilder() to construct. + private Request(Builder builder) { + super(builder); + } + private Request(boolean noInit) {} + + private static final Request defaultInstance; + public static Request getDefaultInstance() { + return defaultInstance; + } + + public Request getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Request_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Request_fieldAccessorTable; + } + + private int bitField0_; + // optional string full_service_name = 1; + public static final int FULL_SERVICE_NAME_FIELD_NUMBER = 1; + private Object fullServiceName_; + public boolean hasFullServiceName() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + public String getFullServiceName() { + Object ref = fullServiceName_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + if (com.google.protobuf.Internal.isValidUtf8(bs)) { + fullServiceName_ = s; + } + return s; + } + } + private com.google.protobuf.ByteString getFullServiceNameBytes() { + Object ref = fullServiceName_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8((String) ref); + fullServiceName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + // optional string method_name = 2; + public static final int METHOD_NAME_FIELD_NUMBER = 2; + private Object methodName_; + public boolean hasMethodName() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + public String getMethodName() { + Object ref = methodName_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + if (com.google.protobuf.Internal.isValidUtf8(bs)) { + methodName_ = s; + } + return s; + } + } + private com.google.protobuf.ByteString getMethodNameBytes() { + Object ref = methodName_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8((String) ref); + methodName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + // optional bytes request_proto = 3; + public static final int REQUEST_PROTO_FIELD_NUMBER = 3; + private com.google.protobuf.ByteString requestProto_; + public boolean hasRequestProto() { + return ((bitField0_ & 0x00000004) == 0x00000004); + } + public com.google.protobuf.ByteString getRequestProto() { + return requestProto_; + } + + private void initFields() { + fullServiceName_ = ""; + methodName_ = ""; + requestProto_ = com.google.protobuf.ByteString.EMPTY; + } + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized != -1) return isInitialized == 1; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (((bitField0_ & 0x00000001) == 0x00000001)) { + output.writeBytes(1, getFullServiceNameBytes()); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + output.writeBytes(2, getMethodNameBytes()); + } + if (((bitField0_ & 0x00000004) == 0x00000004)) { + output.writeBytes(3, requestProto_); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(1, getFullServiceNameBytes()); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(2, getMethodNameBytes()); + } + if (((bitField0_ & 0x00000004) == 0x00000004)) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(3, requestProto_); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + @java.lang.Override + protected Object writeReplace() throws java.io.ObjectStreamException { + return super.writeReplace(); + } + + public static com.orbekk.protobuf.Rpc.Request parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static com.orbekk.protobuf.Rpc.Request parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input, extensionRegistry)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static com.orbekk.protobuf.Rpc.Request parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Request parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(com.orbekk.protobuf.Rpc.Request prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder + implements com.orbekk.protobuf.Rpc.RequestOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Request_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Request_fieldAccessorTable; + } + + // Construct using com.orbekk.protobuf.Rpc.Request.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder(BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + } + } + private static Builder create() { + return new Builder(); + } + + public Builder clear() { + super.clear(); + fullServiceName_ = ""; + bitField0_ = (bitField0_ & ~0x00000001); + methodName_ = ""; + bitField0_ = (bitField0_ & ~0x00000002); + requestProto_ = com.google.protobuf.ByteString.EMPTY; + bitField0_ = (bitField0_ & ~0x00000004); + return this; + } + + public Builder clone() { + return create().mergeFrom(buildPartial()); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.orbekk.protobuf.Rpc.Request.getDescriptor(); + } + + public com.orbekk.protobuf.Rpc.Request getDefaultInstanceForType() { + return com.orbekk.protobuf.Rpc.Request.getDefaultInstance(); + } + + public com.orbekk.protobuf.Rpc.Request build() { + com.orbekk.protobuf.Rpc.Request result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + private com.orbekk.protobuf.Rpc.Request buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + com.orbekk.protobuf.Rpc.Request result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return result; + } + + public com.orbekk.protobuf.Rpc.Request buildPartial() { + com.orbekk.protobuf.Rpc.Request result = new com.orbekk.protobuf.Rpc.Request(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((from_bitField0_ & 0x00000001) == 0x00000001)) { + to_bitField0_ |= 0x00000001; + } + result.fullServiceName_ = fullServiceName_; + if (((from_bitField0_ & 0x00000002) == 0x00000002)) { + to_bitField0_ |= 0x00000002; + } + result.methodName_ = methodName_; + if (((from_bitField0_ & 0x00000004) == 0x00000004)) { + to_bitField0_ |= 0x00000004; + } + result.requestProto_ = requestProto_; + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.orbekk.protobuf.Rpc.Request) { + return mergeFrom((com.orbekk.protobuf.Rpc.Request)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.orbekk.protobuf.Rpc.Request other) { + if (other == com.orbekk.protobuf.Rpc.Request.getDefaultInstance()) return this; + if (other.hasFullServiceName()) { + setFullServiceName(other.getFullServiceName()); + } + if (other.hasMethodName()) { + setMethodName(other.getMethodName()); + } + if (other.hasRequestProto()) { + setRequestProto(other.getRequestProto()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder( + this.getUnknownFields()); + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + this.setUnknownFields(unknownFields.build()); + onChanged(); + return this; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + this.setUnknownFields(unknownFields.build()); + onChanged(); + return this; + } + break; + } + case 10: { + bitField0_ |= 0x00000001; + fullServiceName_ = input.readBytes(); + break; + } + case 18: { + bitField0_ |= 0x00000002; + methodName_ = input.readBytes(); + break; + } + case 26: { + bitField0_ |= 0x00000004; + requestProto_ = input.readBytes(); + break; + } + } + } + } + + private int bitField0_; + + // optional string full_service_name = 1; + private Object fullServiceName_ = ""; + public boolean hasFullServiceName() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + public String getFullServiceName() { + Object ref = fullServiceName_; + if (!(ref instanceof String)) { + String s = ((com.google.protobuf.ByteString) ref).toStringUtf8(); + fullServiceName_ = s; + return s; + } else { + return (String) ref; + } + } + public Builder setFullServiceName(String value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000001; + fullServiceName_ = value; + onChanged(); + return this; + } + public Builder clearFullServiceName() { + bitField0_ = (bitField0_ & ~0x00000001); + fullServiceName_ = getDefaultInstance().getFullServiceName(); + onChanged(); + return this; + } + void setFullServiceName(com.google.protobuf.ByteString value) { + bitField0_ |= 0x00000001; + fullServiceName_ = value; + onChanged(); + } + + // optional string method_name = 2; + private Object methodName_ = ""; + public boolean hasMethodName() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + public String getMethodName() { + Object ref = methodName_; + if (!(ref instanceof String)) { + String s = ((com.google.protobuf.ByteString) ref).toStringUtf8(); + methodName_ = s; + return s; + } else { + return (String) ref; + } + } + public Builder setMethodName(String value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000002; + methodName_ = value; + onChanged(); + return this; + } + public Builder clearMethodName() { + bitField0_ = (bitField0_ & ~0x00000002); + methodName_ = getDefaultInstance().getMethodName(); + onChanged(); + return this; + } + void setMethodName(com.google.protobuf.ByteString value) { + bitField0_ |= 0x00000002; + methodName_ = value; + onChanged(); + } + + // optional bytes request_proto = 3; + private com.google.protobuf.ByteString requestProto_ = com.google.protobuf.ByteString.EMPTY; + public boolean hasRequestProto() { + return ((bitField0_ & 0x00000004) == 0x00000004); + } + public com.google.protobuf.ByteString getRequestProto() { + return requestProto_; + } + public Builder setRequestProto(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000004; + requestProto_ = value; + onChanged(); + return this; + } + public Builder clearRequestProto() { + bitField0_ = (bitField0_ & ~0x00000004); + requestProto_ = getDefaultInstance().getRequestProto(); + onChanged(); + return this; + } + + // @@protoc_insertion_point(builder_scope:com.orbekk.protobuf.Request) + } + + static { + defaultInstance = new Request(true); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:com.orbekk.protobuf.Request) + } + + public interface ResponseOrBuilder + extends com.google.protobuf.MessageOrBuilder { + + // optional bytes response_proto = 1; + boolean hasResponseProto(); + com.google.protobuf.ByteString getResponseProto(); + + // optional int32 error = 2 [default = 0]; + boolean hasError(); + int getError(); + + // optional string error_message = 3; + boolean hasErrorMessage(); + String getErrorMessage(); + } + public static final class Response extends + com.google.protobuf.GeneratedMessage + implements ResponseOrBuilder { + // Use Response.newBuilder() to construct. + private Response(Builder builder) { + super(builder); + } + private Response(boolean noInit) {} + + private static final Response defaultInstance; + public static Response getDefaultInstance() { + return defaultInstance; + } + + public Response getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Response_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Response_fieldAccessorTable; + } + + private int bitField0_; + // optional bytes response_proto = 1; + public static final int RESPONSE_PROTO_FIELD_NUMBER = 1; + private com.google.protobuf.ByteString responseProto_; + public boolean hasResponseProto() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + public com.google.protobuf.ByteString getResponseProto() { + return responseProto_; + } + + // optional int32 error = 2 [default = 0]; + public static final int ERROR_FIELD_NUMBER = 2; + private int error_; + public boolean hasError() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + public int getError() { + return error_; + } + + // optional string error_message = 3; + public static final int ERROR_MESSAGE_FIELD_NUMBER = 3; + private Object errorMessage_; + public boolean hasErrorMessage() { + return ((bitField0_ & 0x00000004) == 0x00000004); + } + public String getErrorMessage() { + Object ref = errorMessage_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + if (com.google.protobuf.Internal.isValidUtf8(bs)) { + errorMessage_ = s; + } + return s; + } + } + private com.google.protobuf.ByteString getErrorMessageBytes() { + Object ref = errorMessage_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8((String) ref); + errorMessage_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private void initFields() { + responseProto_ = com.google.protobuf.ByteString.EMPTY; + error_ = 0; + errorMessage_ = ""; + } + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized != -1) return isInitialized == 1; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (((bitField0_ & 0x00000001) == 0x00000001)) { + output.writeBytes(1, responseProto_); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + output.writeInt32(2, error_); + } + if (((bitField0_ & 0x00000004) == 0x00000004)) { + output.writeBytes(3, getErrorMessageBytes()); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(1, responseProto_); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(2, error_); + } + if (((bitField0_ & 0x00000004) == 0x00000004)) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(3, getErrorMessageBytes()); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + @java.lang.Override + protected Object writeReplace() throws java.io.ObjectStreamException { + return super.writeReplace(); + } + + public static com.orbekk.protobuf.Rpc.Response parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static com.orbekk.protobuf.Rpc.Response parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input, extensionRegistry)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static com.orbekk.protobuf.Rpc.Response parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static com.orbekk.protobuf.Rpc.Response parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(com.orbekk.protobuf.Rpc.Response prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder + implements com.orbekk.protobuf.Rpc.ResponseOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Response_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.orbekk.protobuf.Rpc.internal_static_com_orbekk_protobuf_Response_fieldAccessorTable; + } + + // Construct using com.orbekk.protobuf.Rpc.Response.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder(BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + } + } + private static Builder create() { + return new Builder(); + } + + public Builder clear() { + super.clear(); + responseProto_ = com.google.protobuf.ByteString.EMPTY; + bitField0_ = (bitField0_ & ~0x00000001); + error_ = 0; + bitField0_ = (bitField0_ & ~0x00000002); + errorMessage_ = ""; + bitField0_ = (bitField0_ & ~0x00000004); + return this; + } + + public Builder clone() { + return create().mergeFrom(buildPartial()); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.orbekk.protobuf.Rpc.Response.getDescriptor(); + } + + public com.orbekk.protobuf.Rpc.Response getDefaultInstanceForType() { + return com.orbekk.protobuf.Rpc.Response.getDefaultInstance(); + } + + public com.orbekk.protobuf.Rpc.Response build() { + com.orbekk.protobuf.Rpc.Response result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + private com.orbekk.protobuf.Rpc.Response buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + com.orbekk.protobuf.Rpc.Response result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return result; + } + + public com.orbekk.protobuf.Rpc.Response buildPartial() { + com.orbekk.protobuf.Rpc.Response result = new com.orbekk.protobuf.Rpc.Response(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((from_bitField0_ & 0x00000001) == 0x00000001)) { + to_bitField0_ |= 0x00000001; + } + result.responseProto_ = responseProto_; + if (((from_bitField0_ & 0x00000002) == 0x00000002)) { + to_bitField0_ |= 0x00000002; + } + result.error_ = error_; + if (((from_bitField0_ & 0x00000004) == 0x00000004)) { + to_bitField0_ |= 0x00000004; + } + result.errorMessage_ = errorMessage_; + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.orbekk.protobuf.Rpc.Response) { + return mergeFrom((com.orbekk.protobuf.Rpc.Response)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.orbekk.protobuf.Rpc.Response other) { + if (other == com.orbekk.protobuf.Rpc.Response.getDefaultInstance()) return this; + if (other.hasResponseProto()) { + setResponseProto(other.getResponseProto()); + } + if (other.hasError()) { + setError(other.getError()); + } + if (other.hasErrorMessage()) { + setErrorMessage(other.getErrorMessage()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder( + this.getUnknownFields()); + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + this.setUnknownFields(unknownFields.build()); + onChanged(); + return this; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + this.setUnknownFields(unknownFields.build()); + onChanged(); + return this; + } + break; + } + case 10: { + bitField0_ |= 0x00000001; + responseProto_ = input.readBytes(); + break; + } + case 16: { + bitField0_ |= 0x00000002; + error_ = input.readInt32(); + break; + } + case 26: { + bitField0_ |= 0x00000004; + errorMessage_ = input.readBytes(); + break; + } + } + } + } + + private int bitField0_; + + // optional bytes response_proto = 1; + private com.google.protobuf.ByteString responseProto_ = com.google.protobuf.ByteString.EMPTY; + public boolean hasResponseProto() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + public com.google.protobuf.ByteString getResponseProto() { + return responseProto_; + } + public Builder setResponseProto(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000001; + responseProto_ = value; + onChanged(); + return this; + } + public Builder clearResponseProto() { + bitField0_ = (bitField0_ & ~0x00000001); + responseProto_ = getDefaultInstance().getResponseProto(); + onChanged(); + return this; + } + + // optional int32 error = 2 [default = 0]; + private int error_ ; + public boolean hasError() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + public int getError() { + return error_; + } + public Builder setError(int value) { + bitField0_ |= 0x00000002; + error_ = value; + onChanged(); + return this; + } + public Builder clearError() { + bitField0_ = (bitField0_ & ~0x00000002); + error_ = 0; + onChanged(); + return this; + } + + // optional string error_message = 3; + private Object errorMessage_ = ""; + public boolean hasErrorMessage() { + return ((bitField0_ & 0x00000004) == 0x00000004); + } + public String getErrorMessage() { + Object ref = errorMessage_; + if (!(ref instanceof String)) { + String s = ((com.google.protobuf.ByteString) ref).toStringUtf8(); + errorMessage_ = s; + return s; + } else { + return (String) ref; + } + } + public Builder setErrorMessage(String value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000004; + errorMessage_ = value; + onChanged(); + return this; + } + public Builder clearErrorMessage() { + bitField0_ = (bitField0_ & ~0x00000004); + errorMessage_ = getDefaultInstance().getErrorMessage(); + onChanged(); + return this; + } + void setErrorMessage(com.google.protobuf.ByteString value) { + bitField0_ |= 0x00000004; + errorMessage_ = value; + onChanged(); + } + + // @@protoc_insertion_point(builder_scope:com.orbekk.protobuf.Response) + } + + static { + defaultInstance = new Response(true); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:com.orbekk.protobuf.Response) + } + + private static com.google.protobuf.Descriptors.Descriptor + internal_static_com_orbekk_protobuf_Request_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_com_orbekk_protobuf_Request_fieldAccessorTable; + private static com.google.protobuf.Descriptors.Descriptor + internal_static_com_orbekk_protobuf_Response_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_com_orbekk_protobuf_Response_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n+src/main/java/com/orbekk/protobuf/Rpc." + + "proto\022\023com.orbekk.protobuf\"P\n\007Request\022\031\n" + + "\021full_service_name\030\001 \001(\t\022\023\n\013method_name\030" + + "\002 \001(\t\022\025\n\rrequest_proto\030\003 \001(\014\"K\n\010Response" + + "\022\026\n\016response_proto\030\001 \001(\014\022\020\n\005error\030\002 \001(\005:" + + "\0010\022\025\n\rerror_message\030\003 \001(\t" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + internal_static_com_orbekk_protobuf_Request_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_com_orbekk_protobuf_Request_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_com_orbekk_protobuf_Request_descriptor, + new java.lang.String[] { "FullServiceName", "MethodName", "RequestProto", }, + com.orbekk.protobuf.Rpc.Request.class, + com.orbekk.protobuf.Rpc.Request.Builder.class); + internal_static_com_orbekk_protobuf_Response_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_com_orbekk_protobuf_Response_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_com_orbekk_protobuf_Response_descriptor, + new java.lang.String[] { "ResponseProto", "Error", "ErrorMessage", }, + com.orbekk.protobuf.Rpc.Response.class, + com.orbekk.protobuf.Rpc.Response.Builder.class); + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/src/main/java/com/orbekk/protobuf/Rpc.proto b/src/main/java/com/orbekk/protobuf/Rpc.proto new file mode 100644 index 0000000..49e60ea --- /dev/null +++ b/src/main/java/com/orbekk/protobuf/Rpc.proto @@ -0,0 +1,15 @@ +package com.orbekk.protobuf; + +// Next tag: 4 +message Request { + optional string full_service_name = 1; + optional string method_name = 2; + optional bytes request_proto = 3; +} + +// Next tag: 4 +message Response { + optional bytes response_proto = 1; + optional int32 error = 2 [default = 0]; + optional string error_message = 3; +} diff --git a/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java b/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java new file mode 100644 index 0000000..eabb516 --- /dev/null +++ b/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java @@ -0,0 +1,29 @@ +package com.orbekk.protobuf; + +import java.net.Socket; +import java.net.UnknownHostException; +import java.io.IOException; + +public class SimpleProtobufClient { + public void run() { + try { + Socket socket = new Socket("localhost", 10000); + Rpc.Request r1 = Rpc.Request.newBuilder() + .setFullServiceName("Service1") + .build(); + Rpc.Request r2 = Rpc.Request.newBuilder() + .setFullServiceName("Service2") + .build(); + r1.writeDelimitedTo(socket.getOutputStream()); + r2.writeDelimitedTo(socket.getOutputStream()); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new SimpleProtobufClient().run(); + } +} diff --git a/src/main/java/com/orbekk/protobuf/SimpleProtobufServer.java b/src/main/java/com/orbekk/protobuf/SimpleProtobufServer.java new file mode 100644 index 0000000..57afae9 --- /dev/null +++ b/src/main/java/com/orbekk/protobuf/SimpleProtobufServer.java @@ -0,0 +1,71 @@ +package com.orbekk.protobuf; + +import java.util.logging.Level; +import java.util.logging.Logger; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +public class SimpleProtobufServer extends Thread { + private static Logger logger = Logger.getLogger( + SimpleProtobufServer.class.getName()); + ServerSocket serverSocket; + + public static SimpleProtobufServer create(int port) { + try { + InetSocketAddress address = new InetSocketAddress(port); + ServerSocket serverSocket = new ServerSocket(); + serverSocket.setReuseAddress(true); + serverSocket.bind(address); + return new SimpleProtobufServer(serverSocket); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not create server. ", e); + return null; + } + } + + public SimpleProtobufServer(ServerSocket serverSocket) { + this.serverSocket = serverSocket; + } + + private void handleConnection(final Socket connection) { + new Thread(new Runnable() { + @Override public void run() { + try { + Rpc.Request r1 = Rpc.Request.parseDelimitedFrom( + connection.getInputStream()); + Rpc.Request r2 = Rpc.Request.parseDelimitedFrom( + connection.getInputStream()); + System.out.println(r1); + System.out.println(r2); + } catch (IOException e) { + logger.info("Closed connection: " + connection); + } finally { + try { + connection.close(); + } catch (IOException e) { + } + } + } + }).start(); + } + + public void run() { + logger.info("Running server on port " + serverSocket.getLocalPort()); + while (!serverSocket.isClosed()) { + try { + Socket connection = serverSocket.accept(); + handleConnection(connection); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not establish connection. ", + e); + } + } + } + + public static void main(String[] args) { + SimpleProtobufServer.create(10000).start(); + } +} -- cgit v1.2.3