planet.simulate
Class Simulator

java.lang.Object
  extended byplanet.simulate.Simulator
All Implemented Interfaces:
ISimulator, java.io.Serializable

public class Simulator
extends java.lang.Object
implements ISimulator, java.io.Serializable

The simulator is the main class. It controls the events to execute them at the opportune moment, controls the nodes, while they join, go away, they fail, and handles to the messages and the message queues

Author:
Pedro García, Carles Pairot, Ruben Mondejar
See Also:
Serialized Form

Constructor Summary
Simulator(IScheduler timer)
          Constructor, create a new simulation with a determinate event scheduler
Simulator(IScheduler timer, boolean load)
          Constructor, create a new simulation with a determinate event scheduler and with indicated argument, will load a old state of simulation
 
Method Summary
 Node addNode(Id id, Id bootstrap)
          Create and joins a new node to the simulated network.
 void addNode(Node node, Id bootstrap)
          Joins a new node to the simulated network.
 java.util.HashMap getNodes()
          Returns the HashMap structure of the all nodes
 Node getRandomNode(java.util.Random r)
          Selects a node by a random number
 int getSizeRing()
          Return the size of the network
 void parseEvents(java.util.Vector events)
          Prepares all events to simulate from a events vector
 void parseFail(IEvent aEvent, java.util.Random r)
          Prepares the fail event to simulate from a event interface
 void parseJoin(IEvent aEvent, java.util.Random r)
          Prepares the join event to simulate from a event interface
 void parseLeave(IEvent aEvent, java.util.Random r)
          Prepares the leave event to simulate from a event interface
 void parseLookup(IEvent aEvent, java.util.Random r)
          Prepares the lookup event to simulate from a event interface
 void printNodes()
           
 void removeNodes()
          Removes the ready nodes to delete
 void run(int steps)
          Executes the events at the opportune moment, controls the nodes, while they join, go away, they fail,and handles to the messages and the message queues
 void saveState()
          Serialize the actual state.
 void send(Queue messages)
          Takes all messages of queue and send to all destination nodes
 boolean simulate()
          Executes step by step the events at the opportune moment, controls the nodes, while they join, go away, they fail,and handles to the messages and the message queues.
 void stop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Simulator

public Simulator(IScheduler timer)
          throws InitializationException
Constructor, create a new simulation with a determinate event scheduler

Parameters:
timer - Scheduler with the all events to simulate
See Also:
IScheduler

Simulator

public Simulator(IScheduler timer,
                 boolean load)
          throws java.io.IOException,
                 java.io.FileNotFoundException,
                 java.lang.ClassNotFoundException,
                 InitializationException
Constructor, create a new simulation with a determinate event scheduler and with indicated argument, will load a old state of simulation

Parameters:
timer - Scheduler with the all events to simulate
load - if true, load a old state
See Also:
IScheduler
Method Detail

run

public void run(int steps)
         throws InitializationException
Executes the events at the opportune moment, controls the nodes, while they join, go away, they fail,and handles to the messages and the message queues

Specified by:
run in interface ISimulator
Parameters:
steps - number of steps that the simulation lasts
Throws:
InitializationException

saveState

public void saveState()
Serialize the actual state.


simulate

public boolean simulate()
                 throws InitializationException
Executes step by step the events at the opportune moment, controls the nodes, while they join, go away, they fail,and handles to the messages and the message queues. It stops when the events over and the stabilization is been completed.

Returns:
finish return true if the simulation is been finished
Throws:
InitializationException

addNode

public void addNode(Node node,
                    Id bootstrap)
Joins a new node to the simulated network.

Parameters:
node - New node to add to the network.
bootstrap - Id of the any node in the network

printNodes

public void printNodes()

addNode

public Node addNode(Id id,
                    Id bootstrap)
             throws InitializationException
Create and joins a new node to the simulated network.

Parameters:
id - Id of the new node
bootstrap - Id of the any node in the network
Returns:
node a reference to new node
Throws:
InitializationException

removeNodes

public void removeNodes()
Removes the ready nodes to delete


parseEvents

public void parseEvents(java.util.Vector events)
                 throws InitializationException
Prepares all events to simulate from a events vector

Parameters:
events - Vector of events
Throws:
InitializationException

parseJoin

public void parseJoin(IEvent aEvent,
                      java.util.Random r)
               throws InitializationException
Prepares the join event to simulate from a event interface

Throws:
InitializationException
See Also:
IEvent

parseLeave

public void parseLeave(IEvent aEvent,
                       java.util.Random r)
Prepares the leave event to simulate from a event interface

See Also:
IEvent

parseLookup

public void parseLookup(IEvent aEvent,
                        java.util.Random r)
Prepares the lookup event to simulate from a event interface

See Also:
IEvent

parseFail

public void parseFail(IEvent aEvent,
                      java.util.Random r)
Prepares the fail event to simulate from a event interface

See Also:
IEvent

stop

public void stop()
Specified by:
stop in interface ISimulator

send

public void send(Queue messages)
Takes all messages of queue and send to all destination nodes

See Also:
Queue

getRandomNode

public Node getRandomNode(java.util.Random r)
Selects a node by a random number

Parameters:
r - Random number
Returns:
n chosen Node

getSizeRing

public int getSizeRing()
Return the size of the network

Returns:
size int of the number of nodes

getNodes

public java.util.HashMap getNodes()
Returns the HashMap structure of the all nodes

Returns:
size int of number of nodes