In general, the minimum parameter will be a port number for the interpreter to receive communications on (although the interpreter will likely have a default). For example, the IButton2Name interpreter, which converts between an iButton id and a name, has the following parameter list: [port] . The [...] means the parameter is optional.
An application or context component can communicate with the context interpreter using the BaseObject class. It can only ask the interpreter to interpret some context.
When talking to an interpreter, or any context component, you must have the hostname, port number, and id of the interpreter. The id for an interpreter is the classname without the preceding 'I'. For example, to communicate with an instance of IIButton2Name, the id is "IButton2Name".
public DataObject askInterpreter(String remoteHost, int remotePort, String remoteId, AttributeNameValues data)The data parameter contains the context data to be interpreted. Example code showing how to use the IIButton2Name interpreter running on the localhost on port 8888:
AttributeNameValues data = new AttributeNameValues();
data.addAttributeNameValue(IIButton2Name.IBUTTONID, "ABCDEF");
DataObject interpreted = askInterpreter("localhost",8888,IIButton2Name.CLASSNAME,data);
AttributeNameValues newData = new AttributeNameValues(interpreted);
System.out.println("new data is: "+newData);
When you want to create a new interpreter, there are a set of steps that you must take:
/** * Name of interpreter */ public static final String CLASSNAME = "IButton2Name";
Example code from the IIButton2Name class follows:
/**
* Constructor that creates the interpreter at the given port. It sets
* the id of the this interpreter to CLASSNAME.
*/
public IIButton2Name(int port) {
super(port); // call constructor
setId(CLASSNAME); // set id
hash.put("16AC850600000044", ANIND); // set up for interpretation
hash.put("166F3C060000003E", SALBER);
hash.put("16F78206000000C3", ABOWD);
hash.put("161B4206000000B5", KHAI);
hash.put("1681400600000048", BROTHERT);
hash.put("16A640060000007B", JMANKOFF);
hash.put("16C78708000000E9", RJO);
hash.put("16D78C08000000C2", DNGUYEN);
hash.put("16748D080000000A", MGP);
hash.put("165D8E0800000064", FUTAKAWA);
hash.put("16148B0800000055", ISHIGURO);
hash.put("16A58E08000000B7", BAS);
hash.put("16CF8F0800000076",KENT);
hash.put("16ED870800000090",VISHAL);
}
Example code from the IIButton2Name class follows:
/**
* Sets the incoming attributes for the interpreter. It has only
* one: IBUTTONID.
*
* @return the incoming attributes for this interpreter
*/
protected Attributes setInAttributes() {
Attributes atts = new Attributes();
atts.addAttribute(IBUTTONID);
return atts;
}
Example code from the IIButton2Name class follows:
/**
* Sets the outgoing attributes for the interpreter. It has only
* one: USERNAME.
*
* @return the outgoing attributes for this interpreter
*/
protected Attributes setOutAttributes() {
Attributes atts = new Attributes();
atts.addAttribute(USERNAME);
return atts;
}
/**
* This method performs the actual interpretation of this component.
* It takes an iButton id and returns a user name.
*
* @param data AttributeNameValues containing data to be interpreted
* @return AttributeNameValues object containing the interpreted data
*/
protected AttributeNameValues interpretData(AttributeNameValues data) {
String buttonid = (String)data.getAttributeNameValue(IBUTTONID).getValue();
String name = (String)hash.get(buttonid);
if (name == null) {
return null;
}
AttributeNameValues atts = new AttributeNameValues();
atts.addAttributeNameValue(USERNAME,name);
return atts;
}
Back to the Context
Servers section.
Forward to the Helper
Classes section.
Up to the Context
Components section.