How to Implement Network Data Synchronization in NS2

To implement the Network Data Synchronization within NS2 that has needs to encompass simulating how data is maintained consistent over several nodes. It is specifically vital in the mobile ad hoc networks (MANETs), distributed systems, sensor networks, or peer-to-peer (P2P) networks in which the nodes may have various copies of data which require to be synchronized periodically or on-demand. Given below is a key procedure on how to perform and execute it in the simulation NS2:

Key Components of Data Synchronization:

  1. Data Versions: These nodes are maintain kinds of data, while synchronization, and they exchange up-to-date to make sure that data consistency.
  2. Synchronization Trigger: It can be activated periodically, on-demand, or while nodes identify the data inconsistency.
  3. Data Conflict Resolution: If the nodes are have conflicting data, a resolution mechanism is required to make sure consistency.
  4. Node Mobility (Optional): In the mobile networks which the nodes may synchronize data while they arrive in the communication range.

Steps to Implement Data Synchronization in NS2

  1. Define Nodes and Data: Every node has its individual version of the data, then those versions are synchronized while communication.
  2. Implement Synchronization Protocol: To make a procedure for identifying the outdated data and interchanging updates among the nodes.
  3. Conflict Resolution: Execute a conflict resolution mechanism to manage cases in which nodes have various versions of data.
  4. Optional Mobility: For mobile networks that to replicate the nodes moving in and out of communication range, activating synchronization.

Example: Simulating Data Synchronization in NS2

The given instance that replicates the two nodes are synchronizing their data. One node has an outdated version of the data, and the other node distributes its updated form while the synchronization.

Step 1: Define Nodes and Data

# Define the simulator object

set ns [new Simulator]

# Define trace and nam files for output

set tracefile [open out.tr w]

set namfile [open out.nam w]

$ns trace-all $tracefile

$ns namtrace-all $namfile

# Define a ‘finish’ procedure to end the simulation and visualize in NAM

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Set up the topography for the simulation area

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Define the wireless channel

set chan [new Channel/WirelessChannel]

# Configure wireless nodes (representing nodes with data to synchronize)

$ns node-config -adhocRouting DSDV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channel $chan

# Create two nodes: node1 and node2

set node1 [$ns node]

set node2 [$ns node]

# Set node positions for NAM visualization

$node1 set X_ 100; $node1 set Y_ 100; $node1 set Z_ 0

$node2 set X_ 900; $node2 set Y_ 900; $node2 set Z_ 0

# Create a duplex wireless link between the two nodes

$ns duplex-link $node1 $node2 2Mb 10ms DropTail

# Define the initial version of the data at both nodes

set data_version_node1 1   ;# Outdated version at node1

set data_version_node2 2   ;# Updated version at node2

Step 2: Implement Data Synchronization Protocol

# Procedure for node1 to initiate data synchronization with node2

proc initiate_sync {node1 node2} {

global data_version_node1 data_version_node

puts “Node $node1 initiating data synchronization with node $node2…”

# Check if node1’s data version is outdated compared to node2

if { $data_version_node1 < $data_version_node2 } {

puts “Node $node1 has outdated data (version $data_version_node1). Synchronizing…”

synchronize_data $node1 $node2

} else {

puts “Node $node1 already has the latest data (version $data_version_node1).”

}

}

# Procedure to synchronize data between two nodes

proc synchronize_data {node1 node2} {

global data_version_node1 data_version_node2

# Node2 shares its data with node1

set data_version_node1 $data_version_node2

puts “Node $node1 updated its data to version $data_version_node1.”

}

# Step 3: Schedule Data Synchronization

$ns at 1.0 “initiate_sync $node1 $node2”

Step 3: Finish the Simulation

# Schedule the end of the simulation

$ns at 5.0 “finish”

# Run the simulation

$ns run

  1. Explanation of the Script
  • Data Versions: Node1 begins with an out-of-date version of the data (version 1), whereas node2 has an up-to-date version (version 2).
  • Synchronization Protocol: The initiate_sync protocol verifies if node1 has an outdated version of the data likened to the node2. If it requests the synchronize_data protocol to update node1’s data to the newest version.
  • Simple Synchronization: In this instance, replicates a simple synchronization procedure in which node1 updates its data according to the more current data held by node2.
  1. Run the Simulation

We can save the script as data_sync.tcl and run it using:

ns data_sync.tcl

It will produce a trace file (out.tr) and a NAM file (out.nam). The trace file records the packet transmissions, and the NAM file permits to envision the synchronization procedure.

  1. Visualize the Simulation in NAM

To envision the simulation in NAM, use the below command:

nam out.nam

In the NAM, we can monitor how the two nodes are swapping data to synchronize their content.

  1. Advanced Data Synchronization Features
  • Periodic Synchronization: Execute the periodic synchronization in which the nodes are periodically verify if they want to synchronize data.

Example:

proc periodic_sync {node1 node2 interval} {

initiate_sync $node1 $node2

# Schedule the next synchronization

global ns

$ns at [expr [$ns now] + $interval] “periodic_sync $node1 $node2 $interval”

}

 

# Start periodic synchronization every 5 seconds

$ns at 1.0 “periodic_sync $node1 $node2 5.0”

  • Conflict Resolution: To execute a conflict resolution mechanism for situations in which both nodes have various updates.

Example:

proc resolve_conflict {node1 node2} {

global data_version_node1 data_version_node2

# Simple resolution: Use the higher version number

if { $data_version_node1 > $data_version_node2 } {

set data_version_node2 $data_version_node1

puts “Node $node2 updated to version $data_version_node2 (from node1).”

} else {

set data_version_node1 $data_version_node2

puts “Node $node1 updated to version $data_version_node1 (from node2).”

}

}

# Call conflict resolution after synchronization

$ns at 2.0 “resolve_conflict $node1 $node2”

  • Content-based Synchronization: Rather than the version numbers that nodes can be synchronized depending on the particular content identifiers or hashes to make sure data consistency.
  • Node Mobility: For these mobile networks that replicate the nodes are moving in and out of range, and activating synchronization while they arrive into communication range.

Example:

# Move node1 and node2 into range for synchronization

$ns at 0.0 “$node1 setdest 500 500 10.0”

$ns at 0.0 “$node2 setdest 500 500 5.0”

In this set up, you can obtain the knowledge regarding how to define the nodes and data, how to visualize the simulation using NAM that helps to execute the procedure for Data synchronization using the NS2. More materials will be offered in another manual, if required.

ns2project.com assists you in the implementation process by delivering results in Network Data Synchronization using the ns2 tool. Please share your project details with us for optimal support; we specialize in mobile ad hoc networks (MANETs), distributed systems, sensor networks, and peer-to-peer (P2P) networks.