Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "WakeupEvent.h"
- #include "Simulator.h"
- #include "Node.h"
- #include <iostream>
- #include <chrono>
- #include <random>
- Node::Node(Simulator * sim, int id) {
- _simulator = sim;
- _id=id;
- }
- float Node::getCrtTime() {
- if (_simulator != nullptr)
- return _simulator->getCrtTime();
- else
- return 0;
- }
- void Node::sleep(boost::coroutines2::coroutine<void>::push_type &contextSwitch, float deltat) {
- // create the WakeupEvent
- if (deltat <= 0) // in case the deltat is invalid, do nothing
- return;
- if (_simulator == nullptr) // in case Node is not bound to a simulator, do nothing
- return;
- Event * wu = new WakeupEvent(_id, _simulator, getCrtTime()+deltat);
- _simulator->insertEvent(wu);
- // we now need to sleep so control flow switches to the simulator.
- // When Simulator processes the packet and calls this node again,
- // execution will resume from here.
- contextSwitch();
- }
- // the CSMA algorithm should go in here
- // This is the MAIN function for the code that runs on the nodes of the network
- void Node::operator()(boost::coroutines2::coroutine<void>::push_type &contextSwitch) {
- std::cout << "Node " << _id << " alive\n";
- unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
- std::minstd_rand0 generator (seed); // minstd_rand0 is a standard linear_congruential_engine
- // run 5 iterations where we sleep a random amount of time units between 1 and 100.
- for (int i=0; i<5; i++) {
- // generate the random number
- float sleeptime = generator() % 100;
- std::cout << "Node " << _id << " crt time " << getCrtTime() << " sleeps for "
- << sleeptime << std::endl;
- sleep(contextSwitch, sleeptime);
- std::cout << "Wakeup! Node " << _id << " crt time " << getCrtTime() << std::endl;
- }
- std::cout << "Node " << _id << " crt time " << getCrtTime() << " says good-bye!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement