public CMessage sendAndWait(CMessage msg) throws TimeOutException { if (msg != null) { msg.setFrom(getCubeAgent().getUri()); msg.setReplyTo(getCubeAgent().getUri()); /*if (msg.getTo() != null && !msg.getTo().contains("/ext/")) msg.setTo(msg.getTo() + "/ext/" + getExtensionFactory().getExtensionId());*/ // String to = msg.getTo(); msg.setCorrelation(++correlation); waitingCorrelation = msg.getCorrelation(); // System.out.println(msg.toString()); try { this.waitingMessage = null; this.getCubeAgent().getCommunicator().sendMessage(msg); } catch (Exception e) { // this.getCubeAgent().getLogger().warning("The Plugin could not send a message to " + to + // "!"); } try { long initialTime = System.currentTimeMillis(); long currentTime = initialTime; long waitingTime = TIMEOUT; synchronized (csplock) { while (((currentTime < (initialTime + TIMEOUT)) && waitingTime > 1) && (this.waitingMessage == null)) { csplock.wait(waitingTime); currentTime = System.currentTimeMillis(); waitingTime = waitingTime - (currentTime - initialTime); } } } catch (InterruptedException e) { // this.getCubeAgent().getLogger().warning("The Plugin waits for a response message from " + // to + " but no answer! timeout excedded!"); } return this.waitingMessage; } else { return null; } }
protected void handleMessage(CMessage msg) throws Exception { if (msg != null) { if (msg.getBody() != null) { if (msg.getBody().toString().equalsIgnoreCase("getPropertyValue")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); String p = null; if (uuid != null && name != null) { p = getPropertyValue(uuid.toString(), name.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); resmsg.setBody(p); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("addProperty")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); Object value = msg.getHeader("value"); boolean p = false; if (uuid != null && name != null && value != null) { p = addProperty(uuid.toString(), name.toString(), value.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); if (p == true) resmsg.setBody("true"); else resmsg.setBody("false"); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("getReferencedElements")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); String resultat = ""; if (uuid != null && name != null) { List<String> res = getReferencedElements(uuid.toString(), name.toString()); for (String r : res) { resultat += this.agent.getRuntimeModelController().getAgentOfElement(r) + "###" + r + ","; } } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); resmsg.setBody(resultat); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("addReferencedElement")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); Object onlyone = msg.getHeader("onlyone"); Object refuuid = msg.getHeader("refuuid"); boolean p = false; if (uuid != null && name != null && refuuid != null) { this.agent.addExternalElement(refuuid.toString(), msg.getFrom().toString()); p = addReferencedElement( uuid.toString(), name.toString(), onlyone.toString().equalsIgnoreCase("true") ? true : false, refuuid.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); if (p == true) resmsg.setBody("true"); else resmsg.setBody("false"); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("removeReferencedElement")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); Object refuuid = msg.getHeader("refuuid"); boolean p = false; if (uuid != null && name != null && refuuid != null) { p = removeReferencedElement(uuid.toString(), name.toString(), refuuid.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); if (p == true) resmsg.setBody("true"); else resmsg.setBody("false"); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("hasReferencedElement")) { Object uuid = msg.getHeader("uuid"); Object name = msg.getHeader("name"); Object refuuid = msg.getHeader("refuuid"); boolean p = false; if (uuid != null && name != null && refuuid != null) { p = hasReferencedElement(uuid.toString(), name.toString(), refuuid.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setObject(msg.getObject()); if (p == true) resmsg.setBody("true"); else resmsg.setBody("false"); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (msg.getBody().toString().equalsIgnoreCase("getRemoteElement")) { Object uuid = msg.getHeader("uuid"); ManagedElement me = null; if (uuid != null) { me = getLocalElement(uuid.toString()); } CMessage resmsg = new CMessage(); resmsg.setTo(msg.getFrom()); resmsg.setCorrelation(msg.getCorrelation()); resmsg.setAttachement(me); resmsg.setObject(msg.getObject()); try { getCubeAgent().getCommunicator().sendMessage(resmsg); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }