Nodenet Problem Set Javadocs

nodenet
Class Node

java.lang.Object
  extended bynodenet.Node
All Implemented Interfaces:
NodeNetElement, Resettable, java.lang.Runnable, java.io.Serializable

public class Node
extends java.lang.Object
implements NodeNetElement, Resettable, java.io.Serializable

This class implements NodeNetElement. It is responsible for the GUI and maintains its own thread. It relies on a class that implements NodeBehavior to do anything. It repeatedly calls that class's act method from within a while(true) {} loop.

Note: the following methods are synchronized to prevent addition or subtraction of a channel during NodeBehavior.transmitPacket(InputChannelVector, OutputChannelVector)

This is a change from earlier versions of nodeNet where students could have written code that never returned from the transmitPacket call (formerly known as act).

Version:
$Id: Node.java,v 1.15 2004/01/14 21:43:17 gus Exp $
Author:
Todd C. Parnell, tparnell@ai.mit.edu, Patrick G. Heck, gus.heck@olin.edu
See Also:
NodeNetElement, NodeBehavior, Serialized Form

Field Summary
static int GUISIZE
          Diameter of the circle used to paint nodes.
static java.awt.Color HIGHLIGHT_COLOR
           
 
Constructor Summary
Node(java.lang.Class nbc, int x, int y, java.lang.String name, boolean showName, boolean enabled)
          Build a node setting various properties.
Node(NodeBehavior nb)
           
 
Method Summary
 void addInputChannel(InputChannel c)
          Adds a new InputChannel.
 void addOutputChannel(OutputChannel c)
          Adds a new OutputChannel.
 void configure()
          Display a configuratin dialog to the user and record changes made to the configuration via this dialog.
 boolean contains(int x, int y)
          Determine if this element contains the specified point.
 boolean contains(java.awt.Point p)
          Determine if this element contains the specified point.
 void destroy()
          Destroy the Node.
 boolean equals(java.lang.Object o)
           
protected  void finalize()
           
static java.util.Enumeration getBehaviors()
          Gets an Enumeration of all currently known NodeBehaviors.
static java.awt.Color getColorFromBehavior(java.lang.String behavior)
          Returns the current Color associated with the given NodeBehavior.
 NodeConfigurationBean getConfig()
           
 int getMyID()
          Getter for property myID.
 java.lang.String getName()
          Get the name of this instance.
 java.awt.Point getPos()
           
 boolean isDestroyed()
          Test to see if this element has been destroyed.
 boolean isEditable()
           
 boolean isEnabled()
          Determine if this element is enabled.
 boolean isSelected()
          Determine the selection stae of this element.
 void notifyOfDestruction(NodeNetElement bse)
          Notify this that a NodeNetElement has been destroyed.
 void paint(java.awt.Graphics g)
          Paint ourself to a graphics context.
 void removeInputChannel(InputChannel c)
          Removes an InputChannel.
 void removeOutputChannel(OutputChannel c)
          Removes an OutputChannel.
 void reset()
          Return all properties and other state to the initial state of the object so long as such changes do not alter the network structure.
 void run()
          Repeatedly calls the transmitPacket method for the associated NodeBehavior.
 void setEnabled(boolean b)
          Set the enabled property.
 void setName(java.lang.String n)
          Set the name of this instance.
 void setPos(int a, int b)
           
 void setPos(java.awt.Point p)
           
 void setSelected(boolean newState)
          Notify this object that it should render itself as selected or unselected.
 void start()
          Starts the node running.
 void stop()
          Stops the node.
 java.lang.String templateXML()
          Generate an xml element representing a template of this node.
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GUISIZE

public static final int GUISIZE
Diameter of the circle used to paint nodes.

See Also:
Constant Field Values

HIGHLIGHT_COLOR

public static final java.awt.Color HIGHLIGHT_COLOR
Constructor Detail

Node

public Node(NodeBehavior nb)

Node

public Node(java.lang.Class nbc,
            int x,
            int y,
            java.lang.String name,
            boolean showName,
            boolean enabled)
Build a node setting various properties. This is used when recreating a network from a saved template. ID can't be set so a map of saved ID to created nodes must be kept during parsing so that channels can find their nodes.

Method Detail

getColorFromBehavior

public static java.awt.Color getColorFromBehavior(java.lang.String behavior)
Returns the current Color associated with the given NodeBehavior. If a mapping does not currently exist, a new one is created.

Parameters:
behavior - the NodeBehavior whose Color will be returned

getBehaviors

public static java.util.Enumeration getBehaviors()
Gets an Enumeration of all currently known NodeBehaviors.


setPos

public void setPos(java.awt.Point p)

setPos

public void setPos(int a,
                   int b)

getPos

public java.awt.Point getPos()

setSelected

public void setSelected(boolean newState)
Description copied from interface: NodeNetElement
Notify this object that it should render itself as selected or unselected.

Specified by:
setSelected in interface NodeNetElement
Parameters:
newState - the new selection state

isSelected

public boolean isSelected()
Description copied from interface: NodeNetElement
Determine the selection stae of this element.

Specified by:
isSelected in interface NodeNetElement
Returns:
the current selection state

setEnabled

public void setEnabled(boolean b)
Description copied from interface: NodeNetElement
Set the enabled property. This property determines whether or not the element participates in the simulation. Elements that are disabled should never have any behavior beyond the fact that they exist. Enabled elements interact with other elements by generating, absorbing or transmitting packets.

Specified by:
setEnabled in interface NodeNetElement
Parameters:
b - the new state of the enabled property

isEnabled

public boolean isEnabled()
Description copied from interface: NodeNetElement
Determine if this element is enabled.

Specified by:
isEnabled in interface NodeNetElement
Returns:
the current state of the enabled property.
See Also:
NodeNetElement.setEnabled(boolean)

getConfig

public NodeConfigurationBean getConfig()

setName

public void setName(java.lang.String n)
Description copied from interface: NodeNetElement
Set the name of this instance. Names may or may not be unique, and may or may not play a role in the functionality of the class.

Specified by:
setName in interface NodeNetElement
Parameters:
n - The name to use for this element.

getName

public java.lang.String getName()
Description copied from interface: NodeNetElement
Get the name of this instance.

Specified by:
getName in interface NodeNetElement
Returns:
then name of this element
See Also:
NodeNetElement.setName(String)

contains

public boolean contains(java.awt.Point p)
Description copied from interface: NodeNetElement
Determine if this element contains the specified point.

Specified by:
contains in interface NodeNetElement
Parameters:
p - the point to test
Returns:
true if the point is contained, false otherwise.

contains

public boolean contains(int x,
                        int y)
Description copied from interface: NodeNetElement
Determine if this element contains the specified point.

Specified by:
contains in interface NodeNetElement
Parameters:
x - the x coordinate of the point to test
y - the y coordinate of the point to test
Returns:
true if the point is contained, false otherwise.

isEditable

public boolean isEditable()

configure

public void configure()
Description copied from interface: NodeNetElement
Display a configuratin dialog to the user and record changes made to the configuration via this dialog.

Specified by:
configure in interface NodeNetElement

addInputChannel

public void addInputChannel(InputChannel c)
Adds a new InputChannel. If the specified InputChannel already exists (as determined by the equals method), it is not added again. This condition is silently ignored.

Parameters:
c - the InputChannel to connect

removeInputChannel

public void removeInputChannel(InputChannel c)
Removes an InputChannel. If the specified InputChannel is not a current InputChannel for this Node (as determined by the equals method), this method does nothing.

Parameters:
c - the InputChannel to disconnect

addOutputChannel

public void addOutputChannel(OutputChannel c)
Adds a new OutputChannel. If the specified OutputChannel already exists (as determined by the equals method), it is not added again. This condition is silently ignored.

Parameters:
c - the OutputChannel to connect

removeOutputChannel

public void removeOutputChannel(OutputChannel c)
Removes an OutputChannel. If the specified OutputChannel is not a current OutputChannel for this Node (as determined by the equals method), this method does nothing.

Parameters:
c - the OutputChannel to disconnect

destroy

public void destroy()
Destroy the Node. destroy() should be called whenever a Node will no longer be used. When destroyed, Nodes notify all connected input and output channels of the impending destruction and remove refrences to those channels. Typically, this will force their destruction as well. Note: Node.finalize() automatically calls destroy().

Specified by:
destroy in interface NodeNetElement

isDestroyed

public boolean isDestroyed()
Description copied from interface: NodeNetElement
Test to see if this element has been destroyed.

Specified by:
isDestroyed in interface NodeNetElement
Returns:
true if the object is destroyed false otherwise.

getMyID

public int getMyID()
Getter for property myID.

Returns:
Value of property myID.

templateXML

public java.lang.String templateXML()
Generate an xml element representing a template of this node. Active state such as packet counts should not be preserved. Only information that is needed to recreate a network in it's pristine state (i.e. before the user starts the simulation) should be included.

Returns:
String An xml representiation of the node.

notifyOfDestruction

public void notifyOfDestruction(NodeNetElement bse)
Notify this that a NodeNetElement has been destroyed. Typically, this method will be called by an input or output channel connected to the node. Once notified of destruction, all references to the element are removed.

Specified by:
notifyOfDestruction in interface NodeNetElement
Parameters:
bse - the element that is being destroyed

paint

public void paint(java.awt.Graphics g)
Description copied from interface: NodeNetElement
Paint ourself to a graphics context.

Specified by:
paint in interface NodeNetElement
Parameters:
g - The graphics context on which to render ourself

start

public void start()
Starts the node running. Once running, a node will repeatedly call the act method for it's associated NodeBehavior.

Specified by:
start in interface NodeNetElement

stop

public void stop()
Stops the node.

Specified by:
stop in interface NodeNetElement

run

public void run()
Repeatedly calls the transmitPacket method for the associated NodeBehavior. Note: This method should never be called directly. Instead, use the start() method.

Specified by:
run in interface java.lang.Runnable

equals

public boolean equals(java.lang.Object o)

finalize

protected void finalize()
                 throws java.io.IOException
Throws:
java.io.IOException

reset

public void reset()
Description copied from interface: Resettable
Return all properties and other state to the initial state of the object so long as such changes do not alter the network structure. For example channels should drop all contained packets, but should not forget what nodes they are attached to. NodeBehaviors should implement this if they hold onto objects read from the channel after transmitPacket is called, otherwise resetting the network will leave some packets held in the nodes.

Specified by:
reset in interface Resettable

Nodenet Problem Set Javadocs