## 6 Proposal of a Layered Communication Architecture and an Application Protocol for MG's
### Application Layer  
The application layer is responsible for the processing and data analysis, as well as event management and supervision of the microgrid status. In addition, a specific microgrid communication protocol has been developed and implemented at the application layer for transmitting data among peers. Figure 7 shows an overview of the implemented Microgrid communication packet structure. The message header contains the type of message (ToM), the id device address, and data parameters and values. As Figure 7 shows, there are five ToMs that each peer of the network can send to the others. The function and the size of each ToM are also described. Figure 8 shows four different architectures that can be used to implement the proposed algorithms. Depending on the used architecture, the performance of the network varies. The four evaluated architectures are: (i) Full-flooding-Architecture (Full_F*); (ii) Limited-flooding-Architecture (Limited_F*); (iii) Simple Query-Response (simple QR) and (iv) Embedded Query-Response (Embedded QR). Figure 8: Different approaches with a specific DHT peer algorithm over of the proposed communication architecture. Figure 7: Developed Packet Frame Structure. In the approaches shown in Figure 8a-c, any peer of the network can launch the primitive QUERY about a specific kind of device to know their operational parameters. The difference between a normal QUERY and a primitive QUERY is that the second one is directly and only sent to the matched nodes. These nodes reply to the source node without transmitting the query through the ring. When the query is launched, the lookup process starts (according to the algorithm executed) and find the nodes of the network that are of the queried type. Then, the resources management is performed. Once the node receives the responses from the different peers, the data exchanges using periodic request-responses queries are executed. These queries occur between queried nodes through the application layer. In Figure 8d the application layer is embedded into the P2P layer, so that both data exchanges and lookup process are launched simultaneously. To better understand the differences between simple and embedded query response architectures, Figure 9 illustrates the sequence diagram of both architectures. Flooding architectures have not been considered because they are not based on the chosen DHT protocol. As it can be seen in Figure 9a, a peer sends out a lookup message to the LRA finger table neighbors that match the searched prefix. Thus the neighbor peers propagate the lookup message among the matched nodes that are listed in their LRA finger tables. The lookup message is passed around the ring following this procedure. When the lookup message reaches the source node, it has the information about all the matched nodes. Once it has this information, it is able to send the primitive query directly to the matched nodes without passing through other peers. Figure 9b, shows that the primitive query message is embedded in the lookup message. As a result, both the number of messages that need to be sent and the latency of the system are significantly reduced. Figure 10 shows the used pseudo-code to embed the application layer into the DHT-P2P one. Figure 9: Time-space diagram showing the lookup and query-response processes for the (a) simple and the (b) embedded communication architectures. Figure 10 shows how the application layer has been implemented in the P2P layer. The _Manager_ module manages the overall operations of each node in the P2P network. The peer is initialized by providing a port to listen for incoming connections (serverport), a host address (serverhost) and a node identifier (node). The _handlers_ is a class that contains the different peers' functions. Using threads the _mainloop_ of a peer begins up a socket that listens for incoming connections from other peers. When an incoming connection is accepted, the server will have a new socket object used to send and receive data on the connection.