peer2me.session
Class Session

java.lang.Object
  extended by peer2me.session.Session

public class Session
extends java.lang.Object

This class binds togethers most of the framework and acts as a global access point. Initiates the Network, creates a Service, and sets the default Group. Uses Singelton. getInstance(...) will be used only one time. After that getInstance() should be used.

Author:
Tommy Bjoernsgaard

Method Summary
 Group createGroup(java.lang.String name)
          This method creates a new Group and adds it to the session.
 Group createGroup(java.lang.String name, Node[] nodes)
          This method creates a Group and adds it to the session.
 Node[] getAllNodes()
          Gets all the discovered nodes
 Node[] getAllNodes(Group group)
          Gets alle the nodes in the given group.
 Group getGroup(Node node)
          Gets a Group based on a Node.
 Group getGroup(java.lang.String name)
          Gets a Group with the corresponding name.
 Group getGroupAt(int index)
          Gets the Group at the given index in the collection of groups in session.
 Group[] getGroups()
          Gets all the groups
static Session getInstance()
          Must only be used after getInstance(...)
static Session getInstance(Framework framework, Group group, Service service, Network network, boolean activatePing)
          This should be used only one time, and sets a default Group, the Network and the Service.
 javax.bluetooth.LocalDevice getLocalDevice()
          Gets the LocalDevice
 Node getLocalNode()
          Gets the local Node
 Network getNetwork()
          Gets a referecnce to Network
 Node getNode(int index, Group group)
          Gets the Node at the given index in the given Group.
 Node getNode(java.lang.String address)
          Gets a Node based on an address
 Service getService()
          Gets a reference to Service
 void initialize()
          Initializes the network.
 void messagePartReceived(java.lang.String messageID, int part, int total)
          This method is called for every messagepart that is received, giving the messagepartnumber of the total number of messages.
 void messageReceived(Message message)
          This method is called whenever a Message is received.
 void moveNode(Node node, Group from, Group to)
          Moves a Node from one Group to another.
 void nodeFound(NetworkNode networkNode)
          This method is called whenever a new device running Peer2Me is found.
 void nodeLost(Node node)
          This mehtod is called whenever a Node is lost or abscent.
 void removeGroup(int index)
          Removes the Group at the given index.
 void removeGroup(java.lang.String name)
          Removes the Group with the given name.
 void removeNode(int index, Group group)
          Removes a Node with the given index from a Group.
 void removeNode(Node node, Group group)
          Removes a Node from a Group.
 void search()
          Performes a search for devices and services.
 void searchCompleted()
          This method is called when a search has been completed
 void sendMessage(Message message)
          Passes on a Message to Network.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Session getInstance(Framework framework,
                                  Group group,
                                  Service service,
                                  Network network,
                                  boolean activatePing)
This should be used only one time, and sets a default Group, the Network and the Service. Will also set local properties as localDevice and localNode.

Parameters:
framework - a reference to framework.
group - a group which is set as default
service - the service provided by framework
network - the network provided by framework
activatePing - sets if ping-functionality should be used
Returns:
Returns an instance of Session

getInstance

public static Session getInstance()
Must only be used after getInstance(...) has be called one time. Acts as a global access point to Session

Returns:
a reference to Session

initialize

public void initialize()
Initializes the network.


search

public void search()
            throws FrameworkNotInitializedException
Performes a search for devices and services. initialize must be called before trying to search.

Throws:
FrameworkNotInitializedException

nodeFound

public void nodeFound(NetworkNode networkNode)
This method is called whenever a new device running Peer2Me is found. NetworkNode is a representative for this device. It creates a new Node and sends it up to Framework. The new node will also be added to the default group.

Parameters:
networkNode - the networkNode that is found

nodeLost

public void nodeLost(Node node)
This mehtod is called whenever a Node is lost or abscent. It is then removed from its Group.

Parameters:
node - the node which is lost or abscent

searchCompleted

public void searchCompleted()
This method is called when a search has been completed


getNetwork

public Network getNetwork()
Gets a referecnce to Network

Returns:
a reference to network

getService

public Service getService()
Gets a reference to Service

Returns:
the reference to service

getLocalDevice

public javax.bluetooth.LocalDevice getLocalDevice()
Gets the LocalDevice

Returns:
the localdevice object

getLocalNode

public Node getLocalNode()
Gets the local Node

Returns:
the node

messageReceived

public void messageReceived(Message message)
This method is called whenever a Message is received.

Parameters:
message - the message which is received

messagePartReceived

public void messagePartReceived(java.lang.String messageID,
                                int part,
                                int total)
This method is called for every messagepart that is received, giving the messagepartnumber of the total number of messages.

Parameters:
part -
total -

createGroup

public Group createGroup(java.lang.String name)
This method creates a new Group and adds it to the session.

Parameters:
name - name of the group
Returns:
the group just created

createGroup

public Group createGroup(java.lang.String name,
                         Node[] nodes)
This method creates a Group and adds it to the session. Adds also an array of nodes directly.

Parameters:
name - the name of the group
nodes - the nodes added upon creation of the group
Returns:
the group just created

getGroups

public Group[] getGroups()
Gets all the groups

Returns:
an array of the groups

getGroup

public Group getGroup(java.lang.String name)
               throws GroupNotFoundException
Gets a Group with the corresponding name. Throws an exception if the group is not found.

Parameters:
name - the name of the group
Returns:
the group
Throws:
GroupNotFoundException

getGroup

public Group getGroup(Node node)
               throws GroupNotFoundException
Gets a Group based on a Node. It will get the group the node is a member of.

Parameters:
node - the node in the group
Returns:
the group with the node
Throws:
GroupNotFoundException

getGroupAt

public Group getGroupAt(int index)
                 throws GroupNotFoundException
Gets the Group at the given index in the collection of groups in session. Throws an exception if the group is not found.

Parameters:
index - the index of the group
Returns:
the group found at the index
Throws:
GroupNotFoundException

removeGroup

public void removeGroup(int index)
                 throws GroupNotFoundException
Removes the Group at the given index. Will also remove the nodes in the group if not empty. Throws an exception if the group is not found.

Parameters:
index - the index of the group
Throws:
GroupNotFoundException

removeGroup

public void removeGroup(java.lang.String name)
                 throws GroupNotFoundException
Removes the Group with the given name. Will also remove the nodes in the group if not empty. Throws an exception if the group is not found.

Parameters:
name - the name of the group
Throws:
GroupNotFoundException

getAllNodes

public Node[] getAllNodes()
Gets all the discovered nodes

Returns:
an array of the nodes

getAllNodes

public Node[] getAllNodes(Group group)
Gets alle the nodes in the given group.

Parameters:
group - the group of the nodes
Returns:
an array of nodes

getNode

public Node getNode(java.lang.String address)
Gets a Node based on an address

Parameters:
address - the address of the node
Returns:
the node with the given address

getNode

public Node getNode(int index,
                    Group group)
             throws NodeNotFoundException
Gets the Node at the given index in the given Group. Throws an exception if the node is not found.

Parameters:
index - the index of the node
group - the group of the node
Returns:
the node
Throws:
NodeNotFoundException

moveNode

public void moveNode(Node node,
                     Group from,
                     Group to)
Moves a Node from one Group to another. Will also send a notification to all other nodes about the movement.

Parameters:
node - the node to move
from - the group the node lies in
to - the new group of the node

removeNode

public void removeNode(int index,
                       Group group)
                throws NodeNotFoundException
Removes a Node with the given index from a Group. Throws an exception if the node is not found.

Parameters:
index - the index of the node
group - the group of node
Throws:
NodeNotFoundException

removeNode

public void removeNode(Node node,
                       Group group)
Removes a Node from a Group.

Parameters:
node - the node to remove
group - the group of the node

sendMessage

public void sendMessage(Message message)
Passes on a Message to Network. Used when sending messages.

Parameters:
message - the message to be sent.