/** * This method ensures that the incoming attributes are correct and calls interpretData(). It * returns the interpreted results. * * @param data Incoming interpret request * @param error Incoming error, if any * @return interpreted results * @see #interpretData(AttributeNameValues) */ public DataObject callInterpreter(DataObject data, String error) { Vector v = new Vector(); DataObject result = new DataObject(INTERPRET_REPLY, v); Attributes dataToInterpret = null; Error err = new Error(error); if (err.getError() == null) { dataToInterpret = new Attributes(data); if (dataToInterpret == null) { err.setError(Error.MISSING_PARAMETER_ERROR); } else if (!canHandle(dataToInterpret)) { err.setError(Error.INVALID_ATTRIBUTE_ERROR); } } if (err.getError() == null) { Attributes interpreted = interpretData(dataToInterpret); if (interpreted != null) { v.addElement(interpreted.toDataObject()); err.setError(Error.NO_ERROR); } else { err.setError(Error.INVALID_DATA_ERROR); } } v.addElement(err.toDataObject()); return result; }
/** * This method stores the attribute name values, both in persistent storage and in local storage. * * @param atts AttributeNameValues to store */ public void storeAttributeNameValues(Attributes atts) { store(atts); AttributeNameValue time = atts.getAttributeNameValue(TIMESTAMP); long timestamp = 0; if (time != null) { Long l = new Long((String) time.getValue()); if (l != null) timestamp = l.longValue(); else { System.out.println("ERROR: in <storeAttributeNameValues> timestamp null"); } } for (int i = 0; i < atts.numAttributes(); i++) { AttributeNameValue attNew = (AttributeNameValue) atts.getAttributeAt(i); String attName = attNew.getName(); Object o = attributesTimes.get(attName); long storedTime = -1; if (o != null) storedTime = ((Long) o).longValue(); else System.out.println("ERROR: in <storeAttributeNameValues> storedTime null"); if (storedTime != -1 && storedTime <= timestamp) { AttributeNameValue attOld = attributesCache.getAttributeNameValue(attName); attOld.setValue(attNew.getValue()); } } }
/** * Constructor that creates a BaseObject with the given port and sets the incoming and outgoing * attributes. * * @param port Port number to create the BaseObject on * @see context.arch.BaseObject */ public Interpreter(int port) { super(port); debugprintln(DEBUG, "Interpreter construction after super () port=" + port); inAttributes = setInAttributes(); inAttributeTypes = inAttributes.toTypesHashtable(); outAttributes = setOutAttributes(); outAttributeTypes = outAttributes.toTypesHashtable(); }
/** * This method checks the list of incoming attributes to ensure that the interpreter can handle * these attributes. * * @param inAtts List of incoming attributes to check * @return whether the list of attributes is valid */ private boolean canHandle(Attributes inAtts) { for (int i = 0; i < inAtts.numAttributes(); i++) { Attribute inAtt = inAtts.getAttributeAt(i); if (!isInAttribute(inAtt.getName())) { return false; } } return true; }
/** * This method is called to send a the results of an asynchronous service execution to a * requesting component. * * @param input Object containing information on the requesting component * @param atts AttributeNameValues containing the service results * @return result of sending the service results * @see CommunicationsHandler#userRequest(DataObject,String,String,int) */ protected DataObject sendServiceResult(ServiceInput input, Attributes atts) { Vector v = new Vector(); v.addElement(new DataObject(Constants.ID, input.getId())); input.setInput(null); v.addElement(input.toDataObject()); v.addElement(atts.toDataObject()); DataObject result = new DataObject(SERVICE_RESULT, v); try { return comm.userRequest( result, SERVICE_RESULT, input.getHostname(), Integer.parseInt(input.getPort())); } catch (DecodeException de) { System.out.println("Service sendServiceResult() Decode: " + de); } catch (EncodeException ee) { System.out.println("Service sendServiceResult() Encode: " + ee); } catch (InvalidDecoderException ide) { System.out.println("Service sendServiceResult() InvalidDecoder: " + ide); } catch (InvalidEncoderException iee) { System.out.println("Service sendServiceResult() InvalidEncoder: " + iee); } catch (InvalidProtocolException ipe) { System.out.println("Service sendServiceResult() InvalidProtocol: " + ipe); } catch (ProtocolException pe) { System.out.println("Service sendServiceResult() Protocol: " + pe); } catch (IOException io) { System.out.println("Service sendServiceResult() IOException: " + io); } return null; }
/** * This method is called to aggregate the list of constant attributes that the widgets relevant to * this server provide. This should be called after a constructor sets the widget handles to use * or after setWidgets() has been called. * * @return AttributeNameValues the server constant attributes */ protected Attributes initConstantAttributes() { // this protects us against the Widget constructor // that calls us too early (we havent' got the widgets yet) // it's good practice anyway if (widgets == null) { return null; } Attributes atts = new Attributes(); for (int i = 0; i < widgets.size(); i++) { WidgetHandle handle = widgets.getWidgetHandleAt(i); DataObject widgetAtts = getWidgetConstantAttributes(handle.getHostName(), handle.getPort(), handle.getId()); String error = new Error(widgetAtts).getError(); if (error != null) { if (error.equals(Error.NO_ERROR)) { Attributes wAtts = new Attributes(widgetAtts); for (int j = 0; j < wAtts.numAttributes(); j++) { AttributeNameValue wAtt = (AttributeNameValue) wAtts.getAttributeAt(j); if (atts.getAttributeNameValue(wAtt.getName()) == null) { atts.addAttributeNameValue(wAtt); } } } } } atts.addAttributes(setServerConstantAttributes()); return atts; }
/** * Basic constructor that creates a function description object from a DataObject. The dataObject * is expected to have a <FUNCTION> tag as the top level. * * @param data DataObject containing function description info */ public FunctionDescription(DataObject data) { DataObject nameObj = data.getDataObject(FUNCTION_NAME); name = nameObj.getValue(); DataObject descriptionObj = data.getDataObject(FUNCTION_DESCRIPTION); description = descriptionObj.getValue(); attributes = Attributes.fromDataObject(data.getDataObject(Attributes.ATTRIBUTES)); DataObject timingObj = data.getDataObject(FUNCTION_SYNCHRONICITY); synchronicity = timingObj.getValue(); }
/** * This method converts the service function info to a DataObject * * @return FunctionDescription object converted to a <FUNCTION> DataObject */ public DataObject toDataObject() { DataObjects v = new DataObjects(); v.addElement(new DataObject(FUNCTION_NAME, name)); v.addElement(new DataObject(FUNCTION_DESCRIPTION, description)); if (attributes != null) { v.addElement(attributes.toDataObject()); } v.addElement(new DataObject(FUNCTION_SYNCHRONICITY, synchronicity)); return new DataObject(FUNCTION, v); }
/** * This method converts the subscriber info to a DataObject * * @return Subscriber object converted to a <SUBSCRIBER> DataObject */ public DataObject toDataObject() { Vector v = new Vector(); v.addElement(new DataObject(SUBSCRIBER_ID, id)); v.addElement(new DataObject(HOSTNAME, host)); v.addElement(new DataObject(PORT, Integer.toString(port))); v.addElement(new DataObject(CALLBACK_NAME, callback)); v.addElement(new DataObject(CALLBACK_TAG, tag)); v.addElement(conditions.toDataObject()); v.addElement(attributes.toDataObject()); return new DataObject(SUBSCRIBER, v); }
/** * This method sets up the server for use. This includes getting the attributes and services * information from relevant widgets. * * <p>Modification made by Agathe * * @see #setAttributes() * @see #setServices() */ protected void serverSetup() { attributes = initAttributes(); constantAttributes = initConstantAttributes(); // added by Agathe attributesCache = new Attributes(); attributeTypes = attributes.toTypesHashtable(); // constantAttributeTypes = constantAttributes.toTypesHashtable(); // Added by agathe // ?? for (int i = 0; i < attributes.numAttributes(); i++) { Attribute att = attributes.getAttributeAt(i); attributesCache.addAttributeNameValue(att.getName(), att.getSubAttributes(), att.getType()); } attributesTimes = new Hashtable(); Long long1 = new Long(0); for (Enumeration e = attributeTypes.keys(); e.hasMoreElements(); ) { attributesTimes.put(e.nextElement(), long1); } if (storage != null) { storage.setAttributes(attributes, attributeTypes); // storage.setConstantAttributes(constantAttributes,cosntantAttributeTypes); // Added by // agathe } services = initServices(); }
/** * This method runs a query on a widget, asking for either it's latest acquired data (QUERY) or * asking for the widget to acquire and return new data (UPDATE_AND_QUERY). Currently, it deals * with QUERY and UPDATE_AND_QUERY in exactly the same way. * * @param query DataObject containing the query request * @param update Whether or not to acquire new data * @param error String containing the incoming error value * @return DataObject containing the reply to the query */ protected DataObject queryWidget(DataObject query, boolean update, String error) { System.out.println(query); DataObject result = null; Vector v = new Vector(); if (update) { result = new DataObject(UPDATE_AND_QUERY_REPLY, v); } else { result = new DataObject(QUERY_REPLY, v); } Attributes atts = new Attributes(query); Error err = new Error(error); if (err.getError() == null) { if (atts == null) { err.setError(Error.MISSING_PARAMETER_ERROR); } else if (!canHandle(atts)) { err.setError(Error.INVALID_ATTRIBUTE_ERROR); } } if (err.getError() == null) { Attributes subset = attributesCache.getSubset(atts); if (subset.numAttributes() == 0) { err.setError(Error.INVALID_DATA_ERROR); } else { v.addElement(subset.toDataObject()); if (subset.numAttributes() >= atts.numAttributes()) { err.setError(Error.NO_ERROR); } else { err.setError(Error.INCOMPLETE_DATA_ERROR); } } } v.addElement(err.toDataObject()); return result; }
/** * This method converts the Callback object to a DataObject * * @return Callback object converted to a <CALLBACK> DataObject */ public DataObject toDataObject() { DataObjects v = new DataObjects(); v.addElement(new DataObject(CALLBACK_NAME, name)); v.addElement(attributes.toDataObject()); return new DataObject(CALLBACK, v); }
/** * Constructor that takes a DataObject holding the callback info. The expected tag of the * DataObject is <CALLBACK> * * @param data DataObject containing the callback info */ public Callback(DataObject data) { DataObject nameObj = data.getDataObject(CALLBACK_NAME); this.name = nameObj.getValue(); this.attributes = Attributes.fromDataObject(data); }
/** * Adds an outgoing attribute * * @param name Name of the attribute to set * @param type Type of the attribute */ protected void setOutAttribute(String name, String type) { outAttributeTypes.put(name, type); outAttributes.addAttribute(name, type); }
/** * Adds an incoming attribute * * @param name Name of the attribute to set * @param type Type of the attribute */ protected void setInAttribute(String name, String type) { inAttributeTypes.put(name, type); inAttributes.addAttribute(name, type); }