My take on network messages

Warning: I do not have any Blinks yet, so everything on this post is about completely untested code. All I can talk about it is that it compiles (I compile, therefore I am). :slight_smile:

I want to implement a specific game using Blinks and I would need some type of network flood algorithm to use so I can do network validation and synchronization. @jbobrow pointed me to the code by @gapMindful (that helped a lot, specially considering I do not have any Blinks to test). In he end, I thought it could be implemented in an easier way (and with a slightly simpler algorithm) so I went ahead and gave it a go. Here is the result:

If any of you with Blinks could try it and let me know if it does anything useful (again, it might not even work), that would be great.

Limitations:

  • It handles a single message going through the network at a time (it tries to simply ignore anything unexpected like 2 messages running at once).
  • Payload is currently hardcoded to being a single byte (hey, this is better than the 64 values in the ValueOnFace API :slight_smile: ). It can be extended to have more bytes as the payload but I wanted to keep it simple and small.

Here is the API:

// Prototype for functions that want to change the payload of a
// message that is being propagated. Any modifications to payload
// will be seen by upstream nodes. This can be used for, for
// example, add some type of routing information (counting hops
// is what I had in mind).
typedef void (*ForwardHandler)(byte* payload);

// Prototype for functions that want to change the payload of a
// message reply being sent back to the parent. Any modifications
// to payload will be seen by downstream nodes. The local_payload
// parameter should be used to keep track of the progress so far
// (as we potentially receive replies from multiple faces. For
// example, if we are creating a sum of something, we would have
// code similar to this:
//
// void sum(byte* payload, byte* local_payload) {
//   *local_payload += payload;
// }
typedef void (*ReplyHandler)(byte* payload, byte* local_payload);

// BroadcastMessage sends the given message to all available
// neighboors so it can be propagated through the network.
// The forward_handler callback is called whenever a new
// message being routed reaches the current node and the
// reply_handler callback is called for any replies that
// reaches the it.
bool BroadcastMessage(
  const Message& message, ForwardHandler forward_handler,
  ReplyHandler reply_handler);

// Loop updates the status of the NetworkMessageManager. If it
// returns true (this should ever only happen in the node that
// originally sent the message), then result will contain the
// actual result of the network query made. If it returns false,
// then teh result value is meaningless. This should be called
// at every loop iteration.
bool Loop(byte* result);
1 Like

There is a glaring issue with the code that I completely overlooked so do not bother trying it as it will need a considerable redesign before it does what it is supposed to. Anyway, a fellow Blinker (is this a thing?) is sending me a full set of Blinks for development so I will be able to actually see how broken my code is before trying to make it generally available. Sorry for the noise. :slight_smile:

1 Like

Ok. I fixed the glaring issue (I guess. Again, can not test). There are probably others. :slight_smile:

1 Like