2

我已经编写了一个代理并添加到节点 1 以使用 RemoteGetParamReq 获取节点 2 的物理参数(传播速度、节点能量等),该方法在代理 startup() 方法中工作。如何根据需要在代理 processRequest() 方法中使用 RemoteGetParamReq当节点 1 触发 DatagramReq 时获取远程节点 2 的参数值,以便我可以获得最新的参数值。

class MyRemoteParam extends UnetAgent {
AgentID phy,rmt
RemoteGetParamReq req
Message rsp

@Override
   protected void setup() {
    super.setup()
    register(Services.PHYSICAL);
        register(Services.DATAGRAM);
   }

void startup() {

    phy = agentForService Services.PHYSICAL
    rmt= agentForService Services.REMOTE
    req = new RemoteGetParamReq();
    req.setRecipient(rmt);
    req.setRemoteAgentID(phy);
    req.setTo(2);
    req.get(PhysicalParam.propagationSpeed);
    req.get(PhysicalParam.timestampedTxDelay);
    req.get(MyEnergyParameters.init_energy);
    rsp = phy.request(req, 2000);
    System.out.println "Node 2 propogation speed:"+rsp.get(PhysicalParam.propagationSpeed)
    System.out.println "Node 2 energy:"+rsp.get(MyEnergyParameters.init_energy)


}

 @Override
  Message processRequest(Message msg) { 
  if (msg instanceof DatagramReq) {

    req = new RemoteGetParamReq();  
    req.setRecipient(rmt);
    req.setRemoteAgentID(phy);
    req.setTo(2);
    req.get(PhysicalParam.propagationSpeed);
    req.get(PhysicalParam.timestampedTxDelay);
    req.get(MyEnergyParameters.init_energy);
    rsp = phy.request(req, 2000);
    System.out.println "Node 2 propogation         speed:"+rsp.get(PhysicalParam.propagationSpeed)
    System.out.println "Node 2     energy:"+rsp.get(MyEnergyParameters.init_energy) 

    return new Message(msg, Performative.AGREE)
  }
  return null
 }//end of processrequest

}//end of MyRemoteParam class
4

1 回答 1

1

processRequest()方法应该完成以响应请求者,因此在其中进行参数请求并等待不是一个好主意。但是,您可以通过添加如下内容来触发对参数的异步请求OneShotBehavior

  @Override
  Message processRequest(Message msg) {
    if (msg instanceof DatagramReq) {
      add(new OneShotBehavior() {
        @Override
        public void action() {
          req = new RemoteGetParamReq();  
          req.setRecipient(rmt);
          req.setRemoteAgentID(phy);
          req.setTo(2);
          req.get(PhysicalParam.propagationSpeed);
          req.get(PhysicalParam.timestampedTxDelay);
          req.get(MyEnergyParameters.init_energy);
          rsp = phy.request(req, 2000);
          System.out.println "Node 2 propogation speed:"+rsp.get(PhysicalParam.propagationSpeed)
          System.out.println "Node 2 energy:"+rsp.get(MyEnergyParameters.init_energy)
        } // action
      } // one shot behavior
      return new Message(msg, Performative.AGREE)
    }
    return null
  } // process request

旁注:DatagramReq可能不是触发此操作的正确请求,因为它要求您的代理发送数据报。您可能希望为此目的定义您自己的适当命名的请求,以获得良好的编程风格。

于 2019-07-20T06:40:29.327 回答