5

I came across this blog post, which I found awesome and enlightening, showing how to do fixed-length framing of string messages sent to an Akka IO socket server. I have been working with an open source library I found here called ScalaBuff, which creates a nice thin layer on top of protocol buffer objects.

The trouble I'm having is in adapting the blog author's (couldn't find a link to contact him directly) code to take the length (4-byte sequence) and then the protobuf byte array. I can worry about figuring out which message is on the wire later, right now I just want to get the code to work with one sample message.

My issue is that I am having trouble converting the Akka IO code from pulling akka ByteStrings into being able to send and pull the raw bytes from the protobuf message. This is a symptom of my lack of familiarity with socket servers using Akka IO. I can get to and from the byte representation of my protobuf object (a Zombie Sighting), but I just can't get the sample from the blog to work on byte arrays instead of strings.

If anybody has some advice, some sample code, or some input on how to get from point A (the blog post mentioned above) to point B (an Akka IO socket client that sends a protobuf message to an Akka IO socket server.. I think I have the client working.. maybe), that would be awesome.

Kevin Hoffman
  • 5,154
  • 4
  • 31
  • 33
  • Turns out my problem had nothing to do with Akka IO or even Scala. When I was sending, I was sending what amounted to a "toString" representation of the protobuf byte array rather than the actual byte array converted properly to string. – Kevin Hoffman Feb 02 '13 at 12:37

1 Answers1

0

Try this:

val myByteArray = myByteString.toArray // converts to an Array[Byte]
val myMessage = MyMessage.defaultInstance.mergeFrom(myByteArray)

MyMessage is the ScalaBuff-compiler-generated class using your MyMessage.proto template. Note that the Google protobuf library has a separate ByteString class, make sure you don't mix the two.

UPDATE: Kevin's problem has been solved, basically the problem was the he was using the toString method of an Array[Byte] instead of wrapping the byte array in a new String(), which correctly converts the byte array to a String, to be used in a "%s".format call.

  • I have seen and used the toArray and mergeFrom code before. My problem is I don't know how to strip the byte framing stuff from the code sample in that blog. I can't figure out how to tell when one message stops and the other starts if I'm just reading from a socket stream... Any chance you could show me what it looks like reading and writing these arrays over Akka IO sockets? – Kevin Hoffman Feb 02 '13 at 01:23
  • Have you tried using the `mergeDelimitedFrom(InputStream)` and `writeDelimitedTo(OutputStream)` methods? The `writeDelimitedTo` method basically adds a delimiter between messages so the `mergeDelimitedFrom` method knows when how to read them properly. – Sandro Gržičić Feb 02 '13 at 01:25
  • Maybe the [Akka IO tutorial](http://doc.akka.io/docs/akka/2.0/scala/io.html) would help you? Also try going to #scala or #akka on the Freenode IRC channel. Sadly, I don't have much Akka 2.0 IO experience myself. – Sandro Gržičić Feb 02 '13 at 01:36
  • Yeah I've been looking at that tutorial.. and all the code is for 2.1, e.g. the methods like putLong and putShort don't even exist on the ByteStringBuilder they use ... – Kevin Hoffman Feb 02 '13 at 01:42
  • BTW, not sure if it's helpful, but instead of directly using Akka, I'm actually using Spray (with an Akka backend) with a REST API for my application server (which communicates with Android clients using protobuf messages). Maybe you should try it, if it suits your purpose. It's very simple. – Sandro Gržičić Feb 02 '13 at 01:58
  • I have managed to send a length prefix frame from the client to the server. The server picks up a 45-byte message, which is the "getSerializedSize" of the protobuf message. Everything works until I execute 'val theMessage = ZombieSighting.defaultInstance.mergeFrom(theBytes)' .. this line halts execution and sbt stops giving me output after this. – Kevin Hoffman Feb 02 '13 at 02:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23820/discussion-between-sandro-grzicic-and-kevin-hoffman) – Sandro Gržičić Feb 02 '13 at 02:22