How to add a root node
PainlessMesh can (optionally) use a root node. In this case you would designate one node as a root using the
setRoot() function. Mesh formation can further be sped up by making the other nodes aware that this mesh contains a root node using the
setContainsRoot() function. Adding a root node is highly recommended if you end up using a bridge, and/or have a large mesh, because it solves the problem/challenge explained below. Be careful though never to add more than one root node to a mesh.
Why is a root node useful?
An important limitation during mesh formation is that each node can only connect to one AP at a time. As a result a mesh will generally only have one node available that can connect to other nodes.
Due to this constraint, there can be a problem forming a complete mesh when it is spread out. For example, if we have nodes A to D, where A can reach B, B can reach C etc., but A can't reach C and D can't reach B, we could get B connecting to A and C connecting to D, but then A couldn't connect to C or D (because they are too far away). Similarly D wouldn't be able to connect to A or B and no complete mesh will be formed. Without the root node this is solved by the fact that B (and C) will keep scanning and will (randomly) decide to disconnect and connect to the other mesh. After, for example, B connects to C, now A will be alone and can connect to B, resulting in a full mesh. As mentioned this is done randomly, because there is no way a node can know if connecting to the other sub mesh will be beneficial for the mesh as a whole or detrimental.
This works, although due to the random nature, it can be slow, especially if many more nodes are involved. In this case it can happen that one node decides to leave one submesh and connect to the other, but then a bit later another node leaves the second submesh to connect to the first, in essence undoing the previous change. An additional complication can be when A is an AP only node (e.g. a bridge node). In that case A can't reconnect to B, after B connects to C. So instead B needs to switch back to A and then C needs to decide to connect to B, then D would connect to C, finally resulting in a full mesh.
The above difficulties are overcome by setting a root node. For example, in the above example the bridge would be a root node and as a result B would never decide to disconnect from this node. Similarly, when C connects to B it will also be rooted in place and wouldn't try to restructure the network.