SSim C++ API documentation (v. 1.7.5)

tp.cc

This simplistic example illustrates the use of sequential processes, as defined by the Tprocess class.

// 
// tp.cc
//
#include <iostream>

#include <siena/ssim.h>
#include <siena/tprocess.h>

//
// this is a simple sequential process class.  Producer holds the
// process id of a neighbor process.
//
class Producer : public ssim::TProcess {
public:
    Producer(ssim::ProcessId x): my_neighbor(x) {}

    virtual void main();

private:
    ssim::ProcessId my_neighbor;
};

class Consumer : public ssim::TProcess {
public:
    virtual void main();
};

//
// a simple event class. 
//
class E : public ssim::Event {
public:
    int x;

    E(int n): x(n) {};
};

//
// this is the code executed by the Producer
//
void Producer::main() {
    std::cout << "Producer started" << std::endl;
    for(int i = 0; i < 10; ++i) {
        //
        // the producer sleeps for some time here (100 + i time units)
        //
        ssim::Sim::self_signal_event(NULL, 100 + i);
        std::cout << "Producer waiting for an event" << std::endl;
        //
        // the event we wait for here is the timeout we just set
        //
        wait_for_event();
        //
        // when we wake up after the timeout, we signal an event E to
        // our neighbor Producer
        //
        std::cout << "Producer signaling value " << i << std::endl;
        ssim::Sim::signal_event(my_neighbor, new E(i));
    }
}

//
// this is the code executed by the Consumer
//
void Consumer::main() {
    std::cout << "Consumer started" << std::endl;
    for(;;) {
        //
        // the Consumer simply waits for incoming events, and prints
        // their value
        //
        std::cout << "Consumer waiting for an event" << std::endl;
        const E * x = (const E *)wait_for_event();
        std::cout << "Consumer: at " << ssim::Sim::clock() << ": " 
                  << x->x << std::endl;
    }
}

//
// the main function here sets up the simulation.  This simulation
// consists of one Producer "connected" to a Consumer.
//
int main(int argc, char * argv[]) {
    std::cout << "Program started" << std::endl;
    ssim::ProcessId qid = ssim::Sim::create_process(new Consumer());
    ssim::Sim::create_process(new Producer(qid));
    std::cout << "Simulation started" << std::endl;
    ssim::Sim::run_simulation();
    std::cout << "Simulation ended" << std::endl;
    //
    // sure, I should delete p and q, but this is just an example...
    //
}