simple '.proto' structure

116 views
Skip to first unread message

MohanR

unread,
Feb 9, 2012, 6:33:50 AM2/9/12
to Protocol Buffers
Hi,
I tried to write and read a simple '.proto' structure but
when I read it back I get an exception. Is there something wrong with
the way I read ?


Thanks.

--------------proto-------------

package message;

message Load {

enum LoadType {
HIGH = 0;
MEDIUM = 1;
LOW = 2;
}
message LoadBalance {
optional LoadType type = 2 [default = MEDIUM];
}
optional LoadBalance loadbalancer = 1;
}

-------------Write-----------

Message.Load message =
Message.Load.newBuilder().setLoadbalancer(

Message.Load.LoadBalance.newBuilder().setType( Message.Load.LoadType.HIGH)).build();

------------Read----------

Message.Load message = Message.Load.parseFrom( value );

-----------Exception--------

InvalidProtocolBufferExceptionProtocol message contained an invalid
tag (zero).
com.google.protobuf.InvalidProtocolBufferException: Protocol message
contained an invalid tag (zero).
at
com.google.protobuf.InvalidProtocolBufferException.invalidTag(InvalidProtocolBufferException.java:
68)
at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:
108)
at message.Message$Load$Builder.mergeFrom(Message.java:723)

Henner Zeller

unread,
Feb 9, 2012, 5:51:09 PM2/9/12
to MohanR, Protocol Buffers
On Thu, Feb 9, 2012 at 03:33, MohanR <radhakris...@gmail.com> wrote:
> Hi,
>         I tried to write and read a simple '.proto' structure but
> when I read it back I get an exception. Is there something wrong with
> the way I read ?
>
>
> Thanks.
>
> --------------proto-------------
>
> package message;
>
> message Load {
>
>  enum LoadType {
>    HIGH = 0;
>    MEDIUM = 1;
>    LOW = 2;
>  }
>  message LoadBalance {
>    optional LoadType type = 2 [default = MEDIUM];
>  }
>  optional LoadBalance loadbalancer = 1;
> }
>
> -------------Write-----------
>
>        Message.Load message =
> Message.Load.newBuilder().setLoadbalancer(
>
> Message.Load.LoadBalance.newBuilder().setType( Message.Load.LoadType.HIGH)).build();

uhm, where do you set the value ? I'd expected something like
SerializeToString()


>
> ------------Read----------
>
>            Message.Load message = Message.Load.parseFrom( value );
>
> -----------Exception--------
>
> InvalidProtocolBufferExceptionProtocol message contained an invalid
> tag (zero).
> com.google.protobuf.InvalidProtocolBufferException: Protocol message
> contained an invalid tag (zero).
>        at
> com.google.protobuf.InvalidProtocolBufferException.invalidTag(InvalidProtocolBufferException.java:
> 68)
>        at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:
> 108)
>        at message.Message$Load$Builder.mergeFrom(Message.java:723)
>

> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To post to this group, send email to prot...@googlegroups.com.
> To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
>

MohanR

unread,
Feb 10, 2012, 1:06:49 AM2/10/12
to Protocol Buffers
I though this code sets the 'enum' value.

Message.Load.newBuilder().setLoadbalancer(
Message.Load.LoadBalance.newBuilder().setType( Message.Load.LoadType.HIGH)).build();

Mohan

MohanR

unread,
Feb 21, 2012, 2:54:57 AM2/21/12
to Protocol Buffers
I mean that zookeeper is used to persist.

public void send() throws IOException, InterruptedException,
KeeperException {

if( null != zkc ){
ZooKeeper zk = zkc.getZookeeper();
zk.create( root + "/protocolbuffer",
getData(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL
);
}
}

private byte[] getData() throws InvalidProtocolBufferException {
Message.Load message = Message.Load.newBuilder().setType(

( Message.Load.LoadType.HIGH)).build();
Message.Load message1 =
Message.Load.parseFrom( message.toByteArray() );
System.out.println( "Distributed value is [" +
message1.getType().toString() +"]");
return message.toByteArray();
}


I get a non-empty byte array back. But there is an exception.

Message.Load message = Message.Load.parseFrom( value );

Mohan
Reply all
Reply to author
Forward
0 new messages