package lava.net.psyc.packages;
import java.util.Enumeration;
import java.util.Vector;
import lava.net.common.UNL;
import lava.net.common.VariableModifier;
import lava.net.psyc.PSYCMessageCenter;
import lava.net.psyc.PSYCPackage;
/**
*
**/
public class Skeleton implements PSYCPackage
{
private class NameAliasHandle
{
/**
*
**/
public String name = null;
/**
*
**/
public String[] aliases = null;
/**
*
**/
public boolean handle = false;
/**
*
**/
public NameAliasHandle(String name, String[] aliases, boolean handle)
{
this.name = name;
this.aliases = aliases;
this.handle = handle;
}
}
/**
* The name of the package.
* Default: The package has no name.
**/
private String PackageName = null;
/**
* The major version of the package.
* Default: The major version is 1.
**/
private int PackageMajorVersion = 1;
/**
* The minor version of the package.
* Default: The minor version is 0.
**/
private int PackageMinorVersion = 0;
/**
* Array describing the methods the package knows including
* their aliases.
**/
private Vector PackageMethods = new Vector();
/**
* Array describing the variables the package knows including
* their aliases.
**/
private Vector PackageVariables = new Vector();
/**
* The PSYCMessageCenter the package is connected to.
* This is initialized by the registerCenter method, which
* is called within the initialization process.
**/
protected PSYCMessageCenter center = null;
/**
*
**/
protected void setPackageName(String name)
{
if(name != null)
PackageName = name;
}
/**
*
**/
protected void setPackageVersion(int major, int minor)
{
if(major > 0)
PackageMajorVersion = major;
if(minor > 0)
PackageMinorVersion = minor;
}
/**
*
**/
protected void addPackageMethod(String name, String[] aliases, //
boolean handle)
{
if(name != null)
PackageMethods.addElement(new NameAliasHandle(name,aliases,handle));
}
/**
*
**/
protected void addPackageVariable(String name, String[] aliases, //
boolean getMessaged)
{
if(name != null)
PackageVariables.addElement(new NameAliasHandle(name,aliases,//
getMessaged));
}
/**
* Returns the name of the package. The name should not
* contain blank characters. The method should return
* null or an empty String if there is no package name
* that should be provided.
* Default: Returns null, if there is no PackageName given,
* else it returns the name and the version of the package.
**/
public String getPackageName()
{
if(PackageName == null)
return null;
return PackageName + PSYCMessageCenter.VERSION_DELIMITER + //
PackageMajorVersion + PSYCMessageCenter.MINOR_VERSION_DELIMITER + //
PackageMinorVersion;
}
/**
* Returns, if the application behind the package can
* use the package.
* Default: The application cannot use the package.
* Override this in subclasses if needed.
**/
public boolean uses()
{
return true;
}
/**
* Returns, if the application behind the package UNDERSTANDS
* the package. Normally one should find this out, if the
* application installs its own callback class.
* Default: The application cannot understand the package.
* Override this in subclasses if needed.
**/
public boolean understands()
{
return false;
}
/**
* Returns, if the package wants to get messaged about errors
* on connections.
* Default: The does not want errors.
* Override this in subclasses if needed.
**/
public boolean wantsErrors()
{
return false;
}
/**
* Returns all methods the package wants to handle, it may
* return null or an empty Array, if it doesn't want to handle
* any method.
* Default: Return a list of methods described in PackageMethods.
**/
public String[] getMethods()
{
synchronized(PackageMethods)
{
int count = 0;
for(Enumeration e = PackageMethods.elements();e.hasMoreElements();)
if(((NameAliasHandle)e.nextElement()).handle)
++count;
if(count > 0)
{
String[] ret = new String[count];
int i = 0;
for(Enumeration e = PackageMethods.elements();//
e.hasMoreElements();)
{
NameAliasHandle method = (NameAliasHandle)e.nextElement();
if(method.handle)
ret[i++] = method.name;
}
return ret;
}
else
return null;
}
}
/**
* Returns all variables the package wants to get messaged
* if they were changed.
* Default: Return a list of methods described in PackageVariables.
**/
public String[] getVariables()
{
synchronized(PackageVariables)
{
int count = 0;
for(Enumeration e = PackageVariables.elements();//
e.hasMoreElements();)
if(((NameAliasHandle)e.nextElement()).handle)
++count;
if(count > 0)
{
String[] ret = new String[count];
int i = 0;
for(Enumeration e = PackageVariables.elements();//
e.hasMoreElements();)
{
NameAliasHandle variable = (NameAliasHandle)e.nextElement();
if(variable.handle)
ret[i++] = variable.name;
}
return ret;
}
else
return null;
}
}
/**
*
**/
private void initializeAliases(PSYCMessageCenter center)
{
int i;
Enumeration e;
for(e = PackageMethods.elements();e.hasMoreElements();)
{
NameAliasHandle method = (NameAliasHandle)e.nextElement();
if(method.aliases != null)
for(i = 0;i < method.aliases.length;++i)
center.addMethodAlias(method.aliases[i],method.name);
}
for(e = PackageVariables.elements();e.hasMoreElements();)
{
NameAliasHandle variable = (NameAliasHandle)e.nextElement();
if(variable.aliases != null)
for(i = 0;i < variable.aliases.length;++i)
center.addVariableAlias(variable.aliases[i],variable.name);
}
}
/**
* Called to tell the package it's responsible PSYCMessageCenter.
* This method will be called sometimes while the message center
* initializes the package but before the package must do some work.
**/
public void registerCenter(PSYCMessageCenter center)
{
this.center = center;
initializeAliases(center);
}
/**
* Called, if a method, the package wants to handle, were called.
* Default: It does nothing.
* Override this in subclasses if needed.
**/
public void received(UNL source, String method, String body)
{
}
/**
* Called, if a variable, the package wants to get messaged for,
* were changed.
* Default: It does nothing.
* Override this in subclasses if needed.
**/
public void variableChanged(UNL source, VariableModifier modifier)
{
}
/**
* Called, if a connection will be resetted.
* Default: It does nothing.
* Override this in subclasses if needed.
**/
public void reset(UNL source)
{
}
}