The simulator strives to abstract from most of the details in network programming and to ease the development of algorithm simulations. The simulator core is self-contained in a Java module dev.oxoo2a.sim4da. The distributed algorithm simulation is controlled by an instance of class Simulator:
Simulator s = new Simulator(n_nodes);
for (int id=0; id<n_nodes; id++) {
Node n = new ApplicationNode(id);
s.attachNode(id,n);
}
s.runSimulation(duration);By instantiating a Simulator object, a network of n_nodes nodes is created. For each node id between [n,n_nodes) the code for the given id must be attached to the simulator. This ApplicationNode is derived from the abstract class Node which provides all the required functionailty for implementing the algorithm simulation (see below). Finally, the simulation can be executed for duration seconds.
Extending class Node enables the implementation of the intended distributed algorithm by implementing the method main:
public void main () {
...
}Inside main, the following methods are available:
myId: returns theidof the given nodenumberOfNodes(): returns the total number of nodes in the networksendUnicast(recv_id,String message): sending a raw stringmessageto noderecv_idsendUnicast(recv_id,Message message): sending aHashMap-like message encoded in JSON to noderecv_idsendBroadcast(String message): sending a raw stringmessageto all nodes (except the sender itself)sendBroadcast(Message message): sending aHashMap-like message encoded in JSON to all nodes (except the sender itself)stillSimulating(): returnstruewhile the duration for the simulation is not exceededreceive(): blocks until a message is received by the node; an object of typeNetwork.Messageis returned, which storessender_id,receiver_id, send mode (unicast or broadcast), and thepayloadas a string. If aMessageis expected, this payload must be deserialized from JSON back into an object of typeMessageby callingMessage.fromJson(payload).