planet.generic.commonapi
Class NetworkImpl

java.lang.Object
  extended byplanet.generic.commonapi.NetworkImpl
All Implemented Interfaces:
Network, java.io.Serializable

public class NetworkImpl
extends java.lang.Object
implements Network

This implementation of Network interface pretends to abstract any network, based with the actual commonapi, of course. This not consider the time (number of steps) for simulation. Only stabilize the network at each join, leave, ... Operationals modes for this network implementation:

  1. Accessing directly to an instance of this class, and adding nodes, leaving them (or part of them), etc. without time certainty. In some cases it is usefull. In this case, events cannot be treated.
  2. Generating a new instance of NetworkSimulator and invoking ONLY AND ONLY IF the NetworkSimulator methods, with time certainty and treatment of events.

Author:
Jordi Pujol
See Also:
Serialized Form

Nested Class Summary
 class NetworkImpl.NetworkIterator
          This class implements the java.util.Iterator interface and is backed up by TreeMap that contains all existing nodes on the network.
 
Constructor Summary
NetworkImpl(NodeFactory nodeFactory)
          Initialize the network with: No nodes.
 
Method Summary
 boolean existNodeWithId(Id id)
          Inform if exist on the network one node with Id id.
 void failNodes(Id[] nodes)
          Runs the stabilization process after each fail node.
 int getProximity(Id nodeA, Id nodeB)
          Always returns 1 (one).
 Application getRandomApplication(java.lang.String appId)
          Gets a reference of some application on any node of the network.
 Node getRandomNode(java.util.Random r)
          Returns a randomly selected node of actual network.
 int getSimulatedSteps()
          Gets the actual number of simulated steps.
 java.lang.String getTopology()
          Gets the actual topology of the underlying network.
 java.util.Iterator iterator()
          Builds an Iterator to iterate over all nodes of the network.
 void joinNode(Node node)
          Add the node to the actual network.
 void joinNode(Node node, Id bootstrap)
          Adds node to the network, using the node with Id bootstrap to enter it.
 int joinNodes(int size)
          Add size nodes to the actual network.
 int joinNodes(int size, Id[] bootstrap)
          Add size nodes to the actual network.
 void leaveNodes(Id[] nodes)
          Leave theese nodes for the network.
protected  int[] makeRandomIndexes(int max)
          Returns an array of indexes in range (0..number of nodes) to use to identify which nodes to select.
 void prettyPrintNodes()
          Shows only the owner Id, the successor and predecessor for each Node.
 void printNodes()
          Shows for each Node its printNode().
protected  boolean process()
          Process all nodes one step at this network.
 int registerApplication(Id[] nodes)
          Register the Application specified at properties file to the specified nodes by theirs Ids.
 void registerApplicationAll()
          Register the Application app to all nodes existing at the underlying network.
 void registerApplicationRandom(int nodes)
          Register the Application app to radomly at the number of Nodes nodes.
 int run(int steps)
          Simulate a total number steps steps.
protected  void send(Queue messages)
          Takes all messages of queue and send to all destination nodes.
protected  void sendMessages()
          Sends all message pending to be delivered by all nodes.
 boolean simulate()
          Simulate one step.
 int size()
          Returns the actual number of nodes to the network.
 int stabilize()
          Stabilize the network.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkImpl

public NetworkImpl(NodeFactory nodeFactory)
Initialize the network with:
  1. No nodes.
  2. Specified NodeFactory as parameter.
  3. Topology specified by NodeFactory.getTopology()

Parameters:
nodeFactory - NodeFactory implementation to use to build new nodes with default attributes (specified by properties file).
Method Detail

joinNode

public void joinNode(Node node)
              throws InitializationException
Add the node to the actual network. Its bootstrap can be:
  1. node: If there aren't nodes to the network.
  2. Any node: Any node in the network if just exists nodes to the network.
Runs the stabilization process after joins the node.

Specified by:
joinNode in interface Network
Parameters:
node - Node to add to the network.
Throws:
InitializationException
See Also:
Network.joinNode(planet.commonapi.Node)

joinNode

public void joinNode(Node node,
                     Id bootstrap)
              throws InitializationException
Adds node to the network, using the node with Id bootstrap to enter it. Runs the stabilization process after joins this node.

Specified by:
joinNode in interface Network
Parameters:
node - Node to add to the network.
bootstrap - Id of the node to use as bootstrap for node.
Throws:
InitializationException - if the bootstrap not exists in the network.
See Also:
Network.joinNode(planet.commonapi.Node, planet.commonapi.Id)

joinNodes

public int joinNodes(int size)
              throws InitializationException
Add size nodes to the actual network. Their class and their Ids depends of the actual configuration of different factories. As bootstrap for those nodes are used any of the existing nodes or the firt of them if exists no nodes. Runs the stabilization process for each node.

Specified by:
joinNodes in interface Network
Parameters:
size - Number of nodes to add to the network.
Returns:
A number of actual simulated steps after join size nodes.
Throws:
InitializationException - if occur some problem with building nodes.
See Also:
Network.joinNodes(int)

joinNodes

public int joinNodes(int size,
                     Id[] bootstrap)
              throws InitializationException
Add size nodes to the actual network. Their class and their Ids depends of the actual configuration of different factories. As bootstrap for those nodes are used the Ids that appears at bootstrap. If size > bootstrap.length, new nodes are distributed radomly for them.

Specified by:
joinNodes in interface Network
Parameters:
size - Number of nodes to add to the network.
bootstrap - Ids of nodes to use as bootstrap for new nodes.
Returns:
A number of actual simulated steps after join size nodes.
Throws:
InitializationException - if occur some problem with building nodes.
See Also:
Network.joinNodes(int, planet.commonapi.Id[])

leaveNodes

public void leaveNodes(Id[] nodes)
                throws InitializationException
Leave theese nodes for the network. If an exception is thrown, it method ensures that the existing Ids has been leaved. Runs its stabilization process at the end of all leaves.

Specified by:
leaveNodes in interface Network
Parameters:
nodes - Ids for nodes to leave.
Throws:
InitializationException - if exists some Id in the array as parameter that not exists in the network. The message includes which are of them.
See Also:
Network.leaveNodes(planet.commonapi.Id[])

failNodes

public void failNodes(Id[] nodes)
               throws InitializationException
Runs the stabilization process after each fail node.

Specified by:
failNodes in interface Network
Parameters:
nodes - Node's Ids that must leave the ring.
Throws:
InitializationException
See Also:
Network.failNodes(planet.commonapi.Id[])

registerApplicationAll

public void registerApplicationAll()
                            throws InitializationException
Register the Application app to all nodes existing at the underlying network.

Specified by:
registerApplicationAll in interface Network
Throws:
InitializationException
See Also:
planet.commonapi.Network#registerApplicationAll(planet.commonapi.Application)

registerApplication

public int registerApplication(Id[] nodes)
                        throws InitializationException
Register the Application specified at properties file to the specified nodes by theirs Ids.

Specified by:
registerApplication in interface Network
Parameters:
nodes - Ids of those nodes to register Application app.
Returns:
The number of Nodes with the Application registered. If all that's ok, the value returned would have be the same that nodes.length.
Throws:
InitializationException
See Also:
planet.commonapi.Network#registerApplication(planet.commonapi.Application, planet.commonapi.Id[])

registerApplicationRandom

public void registerApplicationRandom(int nodes)
                               throws InitializationException
Register the Application app to radomly at the number of Nodes nodes. If nodes is equals or greater than number of nodes in the network, his effects are the same of registerApplicationAll(Aplication).

Specified by:
registerApplicationRandom in interface Network
Parameters:
nodes - Number of nodes to radomly select to register the Application app.
Throws:
InitializationException
See Also:
planet.commonapi.Network#registerApplicationRandom(planet.commonapi.Application, int)

makeRandomIndexes

protected int[] makeRandomIndexes(int max)
Returns an array of indexes in range (0..number of nodes) to use to identify which nodes to select.

Parameters:
max - Number of indexes generate.
Returns:
An array of indexes with values at range (0..number of nodes) with a size of max elements.

getTopology

public java.lang.String getTopology()
Gets the actual topology of the underlying network.

Specified by:
getTopology in interface Network
Returns:
A String defining the actual topology of the network.
See Also:
Network.getTopology(), Topology

size

public int size()
Returns the actual number of nodes to the network.

Specified by:
size in interface Network
Returns:
The actual number of nodes to the network.
See Also:
Network.size()

printNodes

public void printNodes()
Shows for each Node its printNode().

Specified by:
printNodes in interface Network

prettyPrintNodes

public void prettyPrintNodes()
Shows only the owner Id, the successor and predecessor for each Node.

Specified by:
prettyPrintNodes in interface Network

run

public int run(int steps)
Simulate a total number steps steps.

Specified by:
run in interface Network
Parameters:
steps - Number of steps to simulate.
Returns:
Number of actual stabilization steps after run steps steps.
See Also:
Network.run(int)

simulate

public boolean simulate()
Simulate one step.

Specified by:
simulate in interface Network
Returns:
true if the simulation process must to continue for to finish the stabilization.
See Also:
Network.simulate()

process

protected boolean process()
Process all nodes one step at this network.

Returns:
true if continue the simulation. false in other case.

sendMessages

protected void sendMessages()
Sends all message pending to be delivered by all nodes.


send

protected void send(Queue messages)
Takes all messages of queue and send to all destination nodes. If any destination of any message is not found, it is returned with mode sets to Globals.ERROR.

See Also:
Queue, Globals, ERROR

getRandomNode

public Node getRandomNode(java.util.Random r)
Returns a randomly selected node of actual network.

Specified by:
getRandomNode in interface Network
Parameters:
r - Generator of random numbers.
Returns:
A node radomly selected.
See Also:
Network.getRandomNode(java.util.Random)

existNodeWithId

public boolean existNodeWithId(Id id)
Inform if exist on the network one node with Id id.

Specified by:
existNodeWithId in interface Network
Parameters:
id - Id of the node to be search.
Returns:
true if and only if exist one node with the same id.
See Also:
Network.existNodeWithId(planet.commonapi.Id)

stabilize

public int stabilize()
Stabilize the network.

Specified by:
stabilize in interface Network
Returns:
Actual number of simulated steps after stabilization.
See Also:
Network.stabilize()

getRandomApplication

public Application getRandomApplication(java.lang.String appId)
Gets a reference of some application on any node of the network.

Specified by:
getRandomApplication in interface Network
Parameters:
appId - Application identification for searching it.
Returns:
A reference of an existing application on any node or null if there is not an Application with the appId.
See Also:
Network.getRandomApplication(java.lang.String)

iterator

public java.util.Iterator iterator()
Builds an Iterator to iterate over all nodes of the network.

Specified by:
iterator in interface Network
Returns:
An Iterator for all nodes on the network.

getSimulatedSteps

public int getSimulatedSteps()
Gets the actual number of simulated steps.

Specified by:
getSimulatedSteps in interface Network
Returns:
actual number of simulated steps
See Also:
Network.getSimulatedSteps()

getProximity

public int getProximity(Id nodeA,
                        Id nodeB)
Always returns 1 (one).

Specified by:
getProximity in interface Network
Parameters:
nodeA - First node to evaluate.
nodeB - Second node to evaluate.
Returns:
The distance between theese two nodes (in this implementation, always one).
See Also:
Network.getProximity(planet.commonapi.Id, planet.commonapi.Id)