Re: [protobuf] Choosing between multiple protobuf messages

2013-09-20 Thread Feng Xiao
You should send the type of the message along with the actual message data
and use it to tell what type is the message in the receiving side.
Or you can define and send such a message:
message FooOrBarOrLogon {
  optional Foo foo_value = 1;
  optional Bar bar_value = 2;
  optional Logon logon_value = 3;
}

On Tue, Sep 17, 2013 at 11:08 PM, Sverre Moe  wrote:

> Having multiple proto files with multiple messages within each.
>
> Standing with a String read from a Socket. My processor class gets a
> String from a socket connection.
> This String I need to convert to a Message and forward it to a listener.
>
> I need to create a message withouth knowing which message I have.
>
> Must be a better way than this?
> [CODE]
> String text;
> byte[] data = text.getBytes();
>
> Message message = null;
> try {
>message = Systems.Foo.parseFrom(data);
> } catch (InvalidProtocolBufferException e) {
>
> }
>
> try {
> message = Systems.Bar.parseFrom(data);
> } catch (InvalidProtocolBufferException e) {
>
> }
>
> try {
> message = Users.Logon.parseFrom(data);
> } catch (InvalidProtocolBufferException e) {
>
> }
>
> return message;
> [/CODE]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


[protobuf] Choosing between multiple protobuf messages

2013-09-20 Thread Sverre Moe
Having multiple proto files with multiple messages within each. 

Standing with a String read from a Socket. My processor class gets a String 
from a socket connection. 
This String I need to convert to a Message and forward it to a listener.

I need to create a message withouth knowing which message I have.

Must be a better way than this?
[CODE]
String text;
byte[] data = text.getBytes();

Message message = null;
try {
   message = Systems.Foo.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Systems.Bar.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Users.Logon.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

return message;
[/CODE]

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


[protobuf] Choosing between multiple protobuf messages

2013-09-20 Thread Sverre Moe
Having multiple proto files with multiple messages within each.
I am using Java and protobuf-java version 2.5.0

My processor class gets a String from a socket connection. This string I
need to convert to a Message and forward it to a listener.

I need to create a message without knowing which message type/class I have.

Must be a better way than this?
[CODE]
String text;
byte[] data = chunk.getBytes();

Message message = null;
try {
message = Systems.Foo.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Systems.Bar.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Users.Logon.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

return Message;


[/CODE]




This could be a double/triple post. I posted this some time ago twice
directly on https://groups.google.com/forum/#!forum/protobuf, but it did
not appear on the group list. So this time I try sending the email myself.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


[protobuf] Choosing between multiple protobuf messages

2013-09-20 Thread Sverre Moe
Having multiple proto files with multiple messages within each. 
I am using Java and protobuf-java version 2.5.0

My processor class gets a String from a socket connection. This string I 
need to convert to a Message and forward it to a listener.

I need to create a message without knowing which message type/class I have.

Must be a better way than this? 
[CODE]
String text;
byte[] data = chunk.getBytes();

Message message = null;
try {
message = Systems.Foo.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Systems.Bar.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

try {
message = Users.Logon.parseFrom(data);
} catch (InvalidProtocolBufferException e) {

}

return Message;
[/CODE]


This could be a double post. I posted this some time ago, but it did not 
appear on the group list.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.