SSim C++ API documentation (v. 1.7.4)

tp.cc

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

00001 // 
00002 // tp.cc
00003 //
00004 #include <iostream>
00005 
00006 #include <siena/ssim.h>
00007 #include <siena/tprocess.h>
00008 
00009 //
00010 // this is a simple sequential process class.  Producer holds the
00011 // process id of a neighbor process.
00012 //
00013 class Producer : public ssim::TProcess {
00014 public:
00015     Producer(ssim::ProcessId x): my_neighbor(x) {}
00016 
00017     virtual void main();
00018 
00019 private:
00020     ssim::ProcessId my_neighbor;
00021 };
00022 
00023 class Consumer : public ssim::TProcess {
00024 public:
00025     virtual void main();
00026 };
00027 
00028 //
00029 // a simple event class. 
00030 //
00031 class E : public ssim::Event {
00032 public:
00033     int x;
00034 
00035     E(int n): x(n) {};
00036 };
00037 
00038 //
00039 // this is the code executed by the Producer
00040 //
00041 void Producer::main() {
00042     std::cout << "Producer started" << std::endl;
00043     for(int i = 0; i < 10; ++i) {
00044         //
00045         // the producer sleeps for some time here (100 + i time units)
00046         //
00047         ssim::Sim::self_signal_event(NULL, 100 + i);
00048         std::cout << "Producer waiting for an event" << std::endl;
00049         //
00050         // the event we wait for here is the timeout we just set
00051         //
00052         wait_for_event();
00053         //
00054         // when we wake up after the timeout, we signal an event E to
00055         // our neighbor Producer
00056         //
00057         std::cout << "Producer signaling value " << i << std::endl;
00058         ssim::Sim::signal_event(my_neighbor, new E(i));
00059     }
00060 }
00061 
00062 //
00063 // this is the code executed by the Consumer
00064 //
00065 void Consumer::main() {
00066     std::cout << "Consumer started" << std::endl;
00067     for(;;) {
00068         //
00069         // the Consumer simply waits for incoming events, and prints
00070         // their value
00071         //
00072         std::cout << "Consumer waiting for an event" << std::endl;
00073         const E * x = (const E *)wait_for_event();
00074         std::cout << "Consumer: at " << ssim::Sim::clock() << ": " 
00075                   << x->x << std::endl;
00076     }
00077 }
00078 
00079 //
00080 // the main function here sets up the simulation.  This simulation
00081 // consists of one Producer "connected" to a Consumer.
00082 //
00083 int main(int argc, char * argv[]) {
00084     std::cout << "Program started" << std::endl;
00085     ssim::ProcessId qid = ssim::Sim::create_process(new Consumer());
00086     ssim::Sim::create_process(new Producer(qid));
00087     std::cout << "Simulation started" << std::endl;
00088     ssim::Sim::run_simulation();
00089     std::cout << "Simulation ended" << std::endl;
00090     //
00091     // sure, I should delete p and q, but this is just an example...
00092     //
00093 }