# Design and Analysis of Adaptive Hierarchical Low-Power Long-Range Networks
## 4 Software Implementation
### Adaptive Sub-Network Organization  
Following Wiselib's design principles for code re-usability, we implement our algorithm in a modular way based on [22]. We separate the modules that implement the selection of the coordinator, the formation of the sub-network and the communication and routing of messages to ease the development and their operation in the final implementation. These three components are then orchestrated by a core-component that allow for information exchange. All components provide clean interfaces to easily communicate with each other, without heavy information exchanges. Coordinator Selection (CS).The first module is related to the sub-network coordinator selection process. The coordinator selection mechanism is implemented as a single, stand-alone, software component. Join Decision (JD).The second module is responsible for deciding which sub-network the device decides to join. This component handles the join request from elected coordinators and once it decides to join a sub-network replies with a confirmation message. The decision is based on the distance of the coordinator and the ID of the device as described in Section 2.3. Iterator (IT).The third module is related to the routing of data once the organization of the sub-networks is complete. This module is responsible for categorizing and storing nearby devices into those that have already joined the sub-network, devices that have not joined yet and devices that have joined another sub-network. Collected information is maintained in _membership tables_ by the IT module. This module monitors the device's sub-network and updates the _membership tables_ based on observed changes to the local topology. The three modules above are used by the main module which is called the Core Component (CC). It is the kernel of the implementation that controls and coordinates all other components so that sub-networks are properly formed and maintained. The CC provides a public interface for other algorithms to take advantage of the resulting network organization. In the following, the life-cycle of CC during the formation of a new sub-network is presented. 1. CS is invoked to decide on the role of the device. Based on the received messages, it decides to be elected as a coordinator or chooses to remain a simple member. 2. If the device is a coordinator: JD it sends an invitation message to nearby devices for them to join the sub-network. 3. Upon receiving an invitation message, CC isolates the message's payload and passes it to [PERSON]. If [PERSON] decides to join, a confirmation message is sent to the coordinator and the IT is notified of the address in order for it to be saved as the device's _coordinator_. If [PERSON] decides not to join, a deny message is generated from JD and passed to the sender of the invitation. 4. If a deny message is received, its payload is passed to JD to be examined, in case the sub-network's conditions are of interest and the IT is notified in order to keep track of which devices have joined the sub-network and which have not. 5. Once all devices have replied, the membership tables are filled in the IT, the process of sub-network formation is completed and routing of data can begin. [PERSON] Implementation Details  
In Wiselib, the interface of each module is called a concept. We present here the _Wiselib_ concepts for each one of four basic modules[PERSON] The design goal of the concept is provided with clear interfaces so that the implementation can be easily used by other algorithms with minimum effort. Core Component (CC ) Concept.The CC concept takes as template parameters a set of components types such as the Radio and Timer that are needed for sending messages and registering events. The most important parameters are the types for the CS, JD and IT which the CC will use for the sub-networking algorithm[PERSON] The first method that initializes the module also provides instances of the components that the module will use.