# A Novel Routing Scheme for Creating Opportunistic Context-Virtual Networks in IoT Scenarios +
## 5 SACAR Algorithm Description
### SACAR Specific Algorithms
```
1:An IoT scenario: \(\mathcal{D}\), simulation time: \(T\), frequency \(\omega_{t}\)
2:do
3:for all node \(d_{i}^{x;y_{i}}\in\mathcal{D}\)do
4:\(d_{i}^{x;y_{i}}.listen()\)
5:if\(\omega_{t}\) is triggered then
6:if\(G_{i}\
eq\emptyset\)then
7:\(\mathcal{N}_{i}\gets d_{i}^{x;y_{i}}.getNeighbours()\)
8:for all neighbor node \(d_{j}^{x;y_{j}}\in\mathcal{N}_{i}\)do
9:if\((G_{i})\cap(G_{j}\cup(S_{j})\
eq\emptyset\)then\(\triangleright\) Only the nodes sharing interests are selected
10:\(m_{i}\leftarrow\{G_{i},S_{i}\}\)\(\triangleright\) The message is created
11:\(sendMessage(m_{i},d_{j}^{x;y_{j}})\)
12:endif
13:endfor
14:endif
15:endfor
16:while\(t<T\)
```  
**Algorithm 4** SACAR-OCVN pseudocode. * **SACAR-HC** (Figure 3c): This algorithm also implements the store-carry-and-forward technique in order to reach nodes out of the communication range of the sender node. However, instead of sending the message to intermediate nodes that have similar interests, the message is sent to those nodes that have been previously in contact with the destination node (an OCVN is created taking into account the contextual information about the encounters among nodes). To implement this algorithm (Step 1), each node stores a list with information about the different nodes with which it has been previously in contact. Then, if the source node wants to send a message and there is no direct connection with the destination node, it searches those neighbor nodes that have previously been, or are right now, in contact with the destination node. Finally, the message is forwarded to intermediate nodes that have been/are in contact with the target node until the message reaches its destination (Step 3 in the figure). The pseudocode of this protocol is presented in Algorithm 5. The peculiarity of SACAR-HC is that nodes store a list with the addresses of the rest of the nodes with which they have been in contact. * **SACAR-Hybrid** (Figure 3d): Finally, this version of the algorithm mixes SACAR-OCVN and SACAR-HC in order to be able to create OCVNs both when the nodes share some interests or when they have been in contact with the destination node (Step 1). In this case, the intermediate nodes are those that meet one of these conditions, increasing the size of the virtual network and, hence, the probabilities of the message to reach its destination. Given that SACAR-Hybrid combines both SACAR-OCVN and SACAR-HC, the implementation mixes these two solutions in a router implemented in The One Simulator. Concretely, the _TryAllMessagesToAllConnections_ method is redefined in order to identify the intermediate nodes meeting the defined premises (at least they have a common interest or they have been in contact with the destination node). The details of this version are shown in Algorithm 6. ```
0: An IoT scenario: \(\mathcal{D}\), simulation time: \(T\), frequency \(\omega_{t}\), encounters list: \(\mathcal{L}\)
1:do
2:for all node \(d_{i}^{x;y_{i}}\in\mathcal{D}\)do
3:\(d_{i}^{x;y_{i}}.listen()\)
4:if\(\omega_{t}\) is triggered then
5:if\(G_{i}\
eq\emptyset\)then
6:\(\mathcal{N}_{i}\gets d_{i}^{x;y_{i}}.getNeighbours()\)
7:for all neighbor node \(d_{j}^{x;y_{j}}\in\mathcal{N}_{i}\)do
8:if\(\textbf{then}_{j}^{x;y_{j}}=\text{destination node OR destination node}\in d_{j}^{x;y_{j}}\). \(\mathcal{L}\)
9:\(m_{i}\leftarrow\{G_{i},S_{i}\}\)\(\triangleright\) The message is created
10:\(sendMessage(m_{i},d_{j}^{x;y_{j}})\)
11:endif
12:endfor
13:endif
14:endif
15:endfor
16:while\(t<T\)
```  
**Algorithm 5** SACAR-HC pseudocode. Figure 3: Behavior of the defined Situational and Adaptive Context-Aware Routing (SACAR) algorithms. IR, In Range; HC, Historical Contacts.