|
libose
|
discusses the bundle as a stack
An OSC bundle contains a header followed by zero or more bundle elements. A bundle element may be a bundle, or a message.
The elements of a bundle are ordered, with the first element being the one in closest proximity to the header. Throughout this library, the bundle is thought of as a stack, that grows away from the header, i.e. the top of the stack is the element furthest from the header.
Since bundle elements themselves may contain many things—more bundle elements in the case of bundles, or typed data like numbers, strings, etc. in messages—they too may each be thought of as stacks that grow away from their header or address.
To add a new message to a bundle, you push it onto the end:
This example creates a new bundle from an array of bytes, and adds two messages to to the bundle, one with address /hello, which will be the first message closest to the header, and one with address /list, which will be the last message, furthest from the header.
Messages can also be built up incrementally by first creating a message with only an address, and then pushing items into the bundle (as messages), and then further pushing them onto the message.
Here, each item is pushed into the bundle and then pushed onto the end of the message we are creating. The important thing to note is that typed items such as integers cannot exist in a bundle on their own, since a bundle can only contain bundle elements (i.e. bundles and messages). So, when we push an int into the bundle, we are actually creating a message that contains that int and has an empty address. Then, when we call ose_push(), the value of that message gets concatenated onto the end of the message below it.
Evaluation of the elements of a bundle is done using the OSE virtual machine. The VM consists of multiple bundles that serve each play a role in its operation. The simplest description of its operation is that it moves elements from the input bundle to the stack bundle; bundles found in the input bundle are moved as-is to the stack, while messages are destructured, each item becoming its own message on the stack.
Given an input bundle with two messages:
after running the VM, the stack will look like this:
The input bundle is read in reverse order—each element is popped off the end. The items of each message, however, are read starting from the item closest to the address, and ending with the address itself as a string. See OSE Virtual Machine and the various examples for more info about how to use the VM as an OSC server.