2

I want to implement energy model in unetstack, I know it's theory but don't know how to implement it in the unetstack as I'm still learning it. Please provide the steps involved in it. Basic code skeleton will also be helpful.

Expected Output : I expect the output that every node after transmitting/receiving packets, it prints out the remaining energy.

Sourabh
  • 79
  • 8

1 Answers1

1

The most natural place to keep track of energy would be in the PHYSICAL (phy) agent. Assuming you are using the HalfDuplexModem phy in a UnetSim, I'd subclass it and monitor the TxFrameNtf and RxFrameNtf by overriding the send() method. I'd then add relevant energy attribute to keep track of the energy usage.

Example Groovy code:

import org.arl.fjage.Message
import org.arl.unet.sim.HalfDuplexModem

class MyHalfDuplexModem extends HalfDuplexModem {

  float energy = 1000   // change to whatever initial energy your node has

  @Override
  boolean send(Message m) {
    if (m instanceof TxFrameNtf) energy -= 10  // change according to your model
    if (m instanceof RxFrameNtf) energy -= 1   // change according to your model
    return super.send(m)
  }

}

Finally, in the simulation DSL, you can replace the default HalfDuplexModem by your customized version:

modem.model = MyHalfDuplexModem
Mandar Chitre
  • 2,110
  • 6
  • 14
  • yes sir I'm using HalfDuplexModem. and I also followed your method. I have added 2 lines to print the energy after sending/receiving data in the if conditions after reducing energy, but the question is I'm not getting those on the shell when I tried "phy << new DatagramReq(to: 2, data: [1,2,3])". what can I do print energy after sending data? I haven't changed inital energy yet. And i'm not getting idea how to use "send" cmd on shell – Sourabh Jun 11 '19 at 05:04
  • Printing is not the recommended way of logging. You should use a Java logger to log to the log file. See https://stackoverflow.com/questions/5950557/good-examples-using-java-util-logging for examples. If you log using the Java logger, you should find the log entries in the log-0.txt file in the logs folder of UnetSim. – Mandar Chitre Jun 12 '19 at 06:49
  • ok sir, I will do it. But how to use "send" that's overriden, I mean to say how to invoke it ? – Sourabh Jun 13 '19 at 04:20
  • `send` is called everytime a message is sent by an agent. You can write your own send to monitor the messages and pass to the parent `send` by calling `super.send(msg)`. – Mandar Chitre Jul 01 '19 at 19:15