next up previous
Next: Example of Groups Management Up: NetStream: a Flexible and Previous: Versions of NetStream


A Basic Example of Utilization

In this section we provide an example of utilization of some of the more interesting features of the NetStream class. We will include basic operations as well as other more sophisticated behavior such as sending/receiving packets for use in the WAN when the programmer judges inefficient to send basic (small) objects through a long distance connection. Also, some synchronization services such as creating a barrier or a wait operation are illustrated to shown the versatility of the library.

Notice that most of the methods are invoked inside the << and >> operators (what it is called stream manipulators) for the sake of uniformity and elegancy in C++.

#include "../../netstream.hh"
int main (int argc, char** argv) {

    NetStream netstream;
    int       mypid;
    char      c;
    int       i, s, t;
    double    d;
    char      str[1000];

    NetStream::init(argc,argv); // Initialize the comm system
    mypid = netstream.my_pid(); // Notice the new invokation in v1.5

    if (mypid==0)
    {
        strcpy(str,"hello world");
        netstream << set_target(1)  << set_source(1)
              << get_target(&t) << get_source(&s)
              << my_pid(&mypid);

        netstream << barrier;       // Synchronize

        netstream << 9 << 'a' << str;
        netstream >> i >> c >> str;

        cout << "process " << mypid << ":"
             << " sends to process " << t
             << " and gets data from processs " << s << endl
             << i << endl << c << endl << str << endl << flush;

        strcpy(str,"this is sent inside a heterogeneous packet");
        netstream << pack_begin
                << str << 9.9 << 'z'
              << pack_end;
    }
    else
    {
        netstream << set_source(0)  << set_target(0)
                  << get_source(&s) << get_target(&t)
              << my_pid(&mypid);

        netstream << barrier;       // Synchronize

        netstream >> i >> c >> str;
        netstream << i << c << str; // ECHO

        netstream << wait(packed);  // Wait for a packed message
        netstream << pack_begin     // Reads the packed message
                >> str >> d >> c
              << pack_end;

        cout << "process " << mypid << ":"
             << " sends to process " << t
             << " and gets data from processs " << s << endl
             << str << endl << d << endl << c << endl << flush;
    }

    NetStream::finalize();
}



Enrique Alba 2001-11-15