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

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

Go to the documentation of this file.
00001 /* Fifth VOS tutorial.  Introduction to Metaobjects.
00002 
00003    This tutorial file covers:
00004    - Accessing remote metaobjects
00005    - The meta_cast<> template
00006 
00007    This file (vostut5client.cc) is released into the public domain.  No
00008    restrictions are placed on its use, distribution or inclusion into
00009    other works.
00010 */
00011 
00012 // You must run this program after running vostut5server.
00013 
00014 #include <vos/vos/vos.hh>
00015 
00016 using namespace VUtil;
00017 using namespace VOS;
00018 
00019 void listTypes(Vobject* vob);
00020 
00021 int main(int argc, char** argv)
00022 {
00023     std::cout << "VOS Tutorial 5 Client\n\n";
00024 
00025     Site localsite;
00026     localsite.addSiteExtension(new LocalVipSiteExtension());
00027 
00028     std::string siteurl;
00029     if(argc > 1) siteurl = argv[1];
00030     else siteurl = "vip://localhost:4231";
00031 
00032     // The purpose of this client part of vostut5 is simply to
00033     // demonstrate that the use of meta_cast<> and show that API for
00034     // Property are the same whether you accessing local or remote
00035     // vobjects.
00036 
00037     try {
00038         vRef<Vobject> position = Vobject::findObjectFromRoot(siteurl + "/planet/position");
00039 
00040         listTypes(position);
00041 
00042         /*
00043           To extract the Property class interface from the position
00044           vobject, we use the template method meta_cast<>.  Given a
00045           Vobject or MetaObject, meta_cast<> returns the desired c++
00046           interface to that vobject if available, or NULL if not.
00047           Think of it as an elaborate typecast.
00048         */
00049         vRef<Property> positionproperty = meta_cast<Property>(position);
00050 
00051         std::cout << "vostut5client: The property value of " << positionproperty->getURLstr()
00052                   << " is now " << positionproperty->read() << std::endl;
00053 
00054     } catch(std::runtime_error& e) {
00055         std::cout << "Error!  " << e.what() << "\n";
00056     }
00057 }
00058 
00059 void listTypes(Vobject* vob)
00060 {
00061     std::cout << "Types for " << vob->getURLstr() << ": ";
00062     for(TypeSetIterator types = vob->getTypes(); types.hasMore(); types++)
00063     {
00064         if(!types.atStart()) std::cout << ", ";
00065         std::cout << (*types);
00066     }
00067     std::cout << "\n";
00068 }
00069