## 5 Proposed Locality-Routing Algorithm  
A missing feature in conventional DHTs is the ability to perform complex queries, such as a lookup operations containing regular expressions. One solution to the above problem is to perform the broadcasting in all the nodes (Full Flooding) or only in some of them, which are a part of the DHT (Limited Flooding). These solutions are currently used to many peer to peer applications. In Reference [78] a structured flooding algorithm is applied to the Chord architecture. In this flooding scheme, Full-Flooding (Full_F), when a node receives a lookup query, it forwards it to all the nodes of its finger table and these nodes recursively retransmit the lookup query to all their respective nodes. The flooding stops after TTL = 4 hops. In this scheme the lookup message will visit each node at least one time to obtain data retrieval. However, in this model locality is not considered. Typically, different criteria can be adopted to provide locality by creating logical groups that form communities in order to achieve common objectives and goals [83]. This approach is common in limited flooding solutions. With the objective to compare the performance of the flooding algorithms with the one of the proposed solutions, we have adapted the conventional flooding algorithms, by adding the application layer and some specific features of microgrids. In the case of microgrids, these logical groups are often categorized based on their nodes functionality (loads, generators, storage system, etc.) so that the exchange of messages outside a certain community is not taken into account and thus doesn't consume communication resources [19]. Therefore, the basic idea of the proposed algorithm is to add network locality to DHTs based on the type of DERs connected to the microgrid and also enabling data retrieval. In this way, to provide locality aware in DHTs systems, the nodes' finger tables of the traditional Chord model have been modified in order to embed DER functionality and keep the neighborhood of locality in the ID space (Chord space-key is not considered). In this scheme, when a node joins the network, it first identifies the kind of DER that has been associated to it, which may be a critical load (CL), a semi-critical load (SCL), a non-critical load (NCL), a distributed generator (DG) or a storage system (SS). After that,Limited Flooding (Limited_F) is applied. In Limited Flooding a node initiates the search for a specific functionality of the DER node by sending the query to all its neighbors that have the searched prefix. When a node receives the query message, it continues the flooding search until the message time to live (TTL) threshold (in this case TTL = 4). Therefore the lookup message will visit each searched kind of node at least one time to achieve data retrieval. However, with the main idea of reducing network latencies and unnecessary network traffic, besides the proposed locality finger table, a new Locality-Routing Algorithm (Proposed_LRA) has been developed. The locality-routing algorithm helps know the number of devices and their type (the searched prefix depends on the kind of device: CL, SS, DG, etc.) that are connected to the microgrid to provide an efficient management of the microgrid resources. Figure 5 shows the flowchart of the LRA algorithm. When the lookup process of the peer-to-peer nodes starts, two threads are generated, either for ingoing or outgoing lookups petitions. The outgoing lookup process is started by a node of the network in order to find the searched prefix or kind of device. For that, a lookup message must be created. The searching message has three main fields: (i) IP_node_source that contains the IP of the node which starts the lookup process; (ii) DER prefix that contains the searched prefix nodes and (iii) Match_Node_List, which is generated as the message progresses in the network and contains a list with the matched nodes.