interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/apps/tutorials/vostut2.cc

Go to the documentation of this file.
00001 /* Second VOS tutorial.  Introduction to Vobjects.
00002 
00003    This tutorial covers:
00004    - Setting a default access control
00005    - Creating a Vobject on a site
00006    - Reference counting in VOS
00007    - Simple Vobject methods
00008 
00009    This file (vostut2.cc) is released into the public domain.  No
00010    restrictions are placed on its use, distribution or inclusion into
00011    other works.
00012 */
00013 
00014 #include <vos/vos/vos.hh>
00015 
00016 using namespace VUtil;
00017 using namespace VOS;
00018 
00019 #ifdef WIN32
00020 #define sleep _sleep
00021 #endif
00022 
00023 int main(int argc, char** argv)
00024 {
00025     std::cout << "VOS Tutorial 2\n\n";
00026 
00027     Site localsite;
00028     localsite.addSiteExtension(new LocalVipSiteExtension());
00029 
00030     /* Remote users are denied access to objects by default unless a Vobject's 
00031        access control policy gives them permission.  Here, we set the default 
00032        policy for new objects on this site to "accept-all" for demonstration 
00033        purposes.  Access control policies and access control lists are be 
00034        covered in detail in tutorialXXX.
00035      */
00036     localsite.setDefaultPolicy("core:accept-all");
00037 
00038     /* The fundamental abstraction in VOS is the "Vobject" (pronounced
00039        Vee-Object).  A Vobject is an entity which can exchange
00040        messages with other Vobjects.  Every Vobject is associated with
00041        a site, and the site provides the means by which the Vobject
00042        may communicate over a network using standards such as TCP/IP
00043        sockets.  A Vobject is created by using the createVobject()
00044        method (or variants createVobjectA() and createVobjectT()) 
00045        and assigning the returned object to a vRef<> template (explained below).
00046     */
00047     vRef<Vobject> primus = localsite.createVobjectA("primus");
00048 
00049     /* -- About Reference Counting --
00050 
00051        Memory management in VOS uses a technique known as Reference
00052        Counting.  The idea is simple: an object stores the number of
00053        times it's refered to (via a C++ pointer or reference, the two
00054        are essentially interchangeable) in other parts of the program.
00055        When an object is constructed, its reference is initially 1.
00056        As additional pointers to the object are created, the reference
00057        count is increased, and as those pointers go away the count is
00058        decreased.  When the count reaches 0, this means the object is
00059        no longer refered to anywhere in the program and can be safely
00060        deleted without leaving any dangling pointers.
00061 
00062        To simplify reference counting you can use the smart pointer
00063        class vRef<>.  It is a template class whose purpose is simply
00064        to ensure that a reference which you have acquired is released
00065        when you are done with it.  The way it works is very simple:
00066        the template holds a pointer to the object.  When the vRef
00067        object is destroyed (usually by going out of scope) it calls
00068        release() on the pointer it holds, thereby decreasing the reference
00069        count.  By using operator overloading, the use of the smart pointer 
00070        class is nearly identical to as if it were a normal raw pointer.  
00071        Methods in VOS that return reference counted objects generally 
00072        return them wrapped in a vRef<> for safety.
00073 
00074      */
00075 
00076     /* Now, let's look at a few simple methods you can use on Vobjects: */
00077 
00078     /* Get the Vobject's site name.  This is the name it was created
00079        with, and is unique on its site.
00080     */
00081     std::cout << "Vobject site name is: " << primus->getSiteName() << "\n";
00082 
00083     /* Get the URL you can use to refer to this Vobject.  It should
00084        consist of the hostname of your site and the vobject's unique
00085        name.
00086     */
00087     std::cout << "Vobject URL is: " << primus->getURLstr() << "\n";
00088 
00089     /* Get the site the Vobject is associated with, which is our
00090        localsite object.  Sites are a subclass of Vobjects, so you can
00091        use the entire Vobject API (described in this and following
00092        tutorials) with sites.
00093     */
00094     vRef<Site> site = primus->getSite();
00095     std::cout << "Vobject site is: " << site->getURLstr() << "\n";
00096     std::cout << "Our local site is: " << localsite.getURLstr() << "\n";
00097 
00098     /* Now, if you access this site with a VOS browser such as Mesh or
00099        Metalurgy you will see a single vobject named "primus".
00100     */
00101     std::cout << "Going into idle loop...\n";
00102     while(true) sleep(1000);
00103 }