# Querying on Federated Sensor Networks
## Appendix A  
```
At each gateway x; forallthe gateways y in FSNdo ify is a neighbor of xthen \(D_{x}\)(x,y) = c(x,y); else \(D_{x}\)(x,y) = \(\infty\);  end if foreachneighbor gateway zdo \(D_{x}\)(z,y) = \(\infty\);  end for Send distance vector \(D_{x}\) to all neighbor gateways; repeat ifDistance vector \(D_{z}\) is received from a neighbor gateway zthen foreachgateway y in FSNdo \(D_{x}\)(x,y) = min{\(D_{x}\)(x,y), c(x,z) + \(D_{z}\)(z,y)};  end if ifthere is link cost change in \(D_{x}\)then  Send distance vector \(D_{x}\) to all neighbor gateways;  end if until forever;
```  
**Algorithm A1**Distance-Vector Algorithm  
```
Extract message info; ifmessage is a beacon message from a sensor nodethen  Update sensor member info at the gateway;  end if elseifmessage is a beacon message from a gatewaythen  Update gateway routing table;  end if elseifmessage is a query messagethen  Update query info at the gateway; ifquery destination is broadcast addressthen  Send query message to all neighbor gateways and member sensor nodes; else  Send message to next node on the destination path;  end if elseifmessage is an event messagethen  Update event info at the gateway;  Send message to next node(s);  end if
```  
**Algorithm B1**Processing New Message Algorithm at a Gateway  
```
Extract message info; ifmessage is a beacon message from a sensor nodethen  Update sensor member info at the gateway;  end if else ifmessage is a beacon message from a gatewaythen  Update gateway routing table;  end if else ifmessage is a query messagethen  Update query



## Appendix C

```
Extract message info; ifmessage is a beacon messagethen  Update gateway info at the sensor; end if elseifmessage is a query messagethen  Update query info at the sensor; ifself is an event agentthen  Send event data to the requesting node(s); else  Send query message to next node(s);  end if elseifmessage is an event messagethen  Update event info at the sensor; ifself is an query agentthen  Query is resolved; else  Send event message to next node(s);  end if  end if
```

**Algorithm 1**Processing New Message Algorithm at a Sensor Node

## References

* _[PERSON] et al. (2013)_ [PERSON]; [PERSON] [PERSON]; [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON] Deployment and evaluation of a wireless sensor network for methane leak detection. Sens. Actuators A Phys. **2013**, _202_, 217-225. * _[PERSON] et al. (2013)_ [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON] Parksense: A smartphone based sensing system for on-street parking. In Proceedings of the 19 th annual International Conference on Mobile Computing & Networking, Miami, FL, USA, 30 September-4 October 2013; pp. 75-86. * _[PERSON] (2015)_ [PERSON]; [PERSON] Wireless networks in underground mines. Industrial Wireless Sensor Networks: Monitoring, Control and Automation; Woodhead Publishing: Cambridge, UK, 2015; p. 107. * _[PERSON] and [PERSON] (2013)_ [PERSON]; [PERSON] A survey on in-network querying and tracking services for wireless sensor networks. Ad Hoc Netw. **2013**, _11_, 596-610. * _[PERSON] and [PERSON] (2002)_ [PERSON]; [PERSON] Rumor routing algorthim for sensor networks. In Proceedings of the 1 st ACM International Workshop on Wireless Sensor Networks and Applications (WSNA '02), Atlanta, GA, USA, 28 September 2002; pp. 22-31. * [PERSON] and [PERSON] (2007) [PERSON]; [PERSON] Distributed quad-tree for spatial querying in wireless sensor networks. In Proceedings of the 2007 IEEE International Conference on Communications, Glasgow, UK, 24-28 June 2007; pp. 3325-3332. * _[PERSON] et al. (2002)_ [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] [PERSON] Ght: A geographic hash table for data-centric storage. In Proceedings of the 1 st ACM international Workshop on Wireless Sensor Networks and Applications (WSNA '02), Atlanta, GA, USA, 28 September 2002; pp. 78-87. * _[PERSON] et al. (2000)_ [PERSON]; [PERSON]; [PERSON] Directed diffusion: A scalable and robust communication paradigm for sensor networks.