# A Novel Routing Scheme for Creating Opportunistic Context-Virtual Networks in IoT Scenarios +
## 5 SACAR Algorithm Description
### SACAR Specific Algorithms  
Four different versions of the SACAR-based algorithm are proposed (Figure 3) taking into account different contextual information for delivering a message (i.e., they are in contact, they share some interests, or they have previously been in the range of the destination node),namely (i) SACAR-In Range (SACAR-IR), (ii) SACAR-Opportunistic Context Virtual Networks (SACAR-OCVN), (iii) SACAR-Historical Contacts (SACAR-HC), and (iv) SACAR-Hybrid. Next, the main insights of each version are described:  
* **SACAR-IR** (Figure 3a): When a message is sent by a node, before the message is created and sent (Step 1 in the figure), this algorithm first checks if the destination node is directly connected with the sender and no previous messages have been exchanged between them. If there is a connection and the destination node has some skills meeting the goals of the source node, the sender creates the message and sends it. The pseudocode of this algorithm is detailed in Algorithm 3. This algorithm has been implemented on The One Simulator [34], as is detailed in the next section. In order to implement it, this router extends the _ActiveRouter_ class in order to redefine the method _createNewMessage_ where the aforementioned requirements are checked. If the destination node is not located in the reachable area of the sender, SACAR-IR dynamically seeks alternative nodes that can process and adapt the environment to the sender's preferences/goals (Step 2 in Figure 3a), checking again the defined requirements. ```
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_{i}^{x,y_{i}}\in\mathcal{N}_{i}\)do
9:if\(d_{i}^{x,y_{i}}\_previousConnections(d_{j}^{x,y_{j}})\
eq true\)then
10:if\(d_{i}^{x,y_{i}}\_isConnectedTo(d_{j}^{x,y_{j}})\) and \((G_{i})\cap(S_{j})\
eq\emptyset\)then
11:if\(d_{i}^{x,y_{i}}\_getBufferOccupancy\
eq full\)then
12:\(m_{i}\leftarrow\{G_{i},S_{i}\}\)\(\triangleright\) The message is created
13:\(sendMessage(m_{i},d_{j}^{x,y_{j}})\)
14:endif
15:endif
16:endfor
17:endif
18:endif
19:endfor
20:while\(t<T\)
```  
**Algorithm 3** SACAR-IR pseudocode. * **SACAR-OCVN** (Figure 3b): This algorithm covers the situations where there is no direct connection between the sender and the destination node. Therefore, it implements the store-carry-and-forward technique to reach the destination node. When the profile has to be sent, if there is a direct connection between the sender and the receiver, the message is just created and sent. If there is no direct connection (Step 1 in the figure) with the destination node, the sender identifies the neighbor nodes sharing some common interests/goals. Then, the message is created and sent to the identified nodes (Step 2), creating the OCVN to send the message. Those nodes not sharing some of the goals are discarded. Therefore, they are not overloaded, and their resources are not wasted (Step 3 in Figure 3b), being excluded from the OCVN. Algorithm 4 presents the pseudocode of this routing protocol. In summary, the set of connected possible candidates to redistribute the messages and create the OCVN is checked and modified in order to fulfill one of the following requirements: the intermediate nodes share interests with the sender and, finally, one of the intermediate nodes is connected with the destination node. The implementation of this algorithm in The One Simulator mainly redefines the method _TryAllMessagesToAllConnections_ to implement the aforementioned requirements.