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

/home/tetron/hack/vos/apps/tutorials/vostut7hello.hh

Go to the documentation of this file.
00001 /* Seventh VOS tutorial.  Creating your own MetaObjects
00002 
00003    This tutorial file covers:
00004    - Subclassing from MetaObject
00005    - Essential methods to override
00006    - Defining specialized Local and Remote MetaObjects
00007 
00008    This file (vostut7hello.hh) is released into the public domain.  No
00009    restrictions are placed on its use, distribution or inclusion into
00010    other works.
00011 */
00012 
00013 #ifndef _VOSTUT7_HH_
00014 #define _VOSTUT7_HH_
00015 
00016 #include <vos/vos/vos.hh>
00017 
00018 /* In this tutorial we demonstrate how to create a metaobject which
00019    implements an API for saying hello to things.  The method that we
00020    will implement will be simple:
00021 
00022    std::string hello(const std::string& s)
00023 
00024    This method will accept a greeting and return a string with the
00025    Vobject's reply.
00026 */
00027 
00028 // XXX
00029 //IMPORT_METAOBJECT_FACTORIES(Hello)
00030 
00031 /* First define the "Hello" class.  As a MetaObject extension, it must
00032    inherit from the "MetaObject" class.  This class will provides an
00033    abstract interface for the "hello" method we want to support.
00034    Because the action of this method depends on whether we are
00035    accessing a local or remote object. the actual implementation of
00036    this method will be done in further subclasses.
00037  */
00038 class Hello : public VOS::MetaObject
00039 {
00040 protected:
00041 
00042     // There should only ever be one constructor, and it should take
00043     // one parameter.
00044 
00045     Hello(VOS::VobjectBase* superobject);
00046 
00047 
00048 public:
00049     // The hello method.  By having "Hello" be an abstract class an
00050     // application do not need to know whether the vobject is local or
00051     // remote, simply that it has the "Hello" interface.
00052 
00053     virtual std::string hello(const std::string& s) = 0;
00054 
00055 
00056     // This returns the VOS type string for this specific extension,
00057     // in this case "tutorial:hello".  This is a vobject type, not the
00058     // C++ class name.
00059 
00060     virtual const std::string getVOSType();
00061 };
00062 
00063 
00064 // This class implements Hello for remote Vobjects.  It is essentially
00065 // a "stand in" or "stub" and its purpose is to translate C++ method
00066 // calls into the actual network commands that acomplish the desired
00067 // task, in this case to say hello to the Vobject in question.
00068 
00069 class RemoteHello : public Hello
00070 {
00071 protected:
00072 
00073     // There should only ever be one constructor, and it should take
00074     // one parameter.  An application never calls the constructor
00075     // directly, instead remote metaobjects are instantiated by VOS
00076     // automatically when new vobjects are encountered.
00077 
00078     RemoteHello(VOS::VobjectBase* superobject);
00079 
00080 public:
00081 
00082     // This won't implement the "hello" logic, but rather will create
00083     // and send a "tutorial:hello" message to the remote site.
00084 
00085     virtual std::string hello(const std::string& s);
00086 
00087 
00088     // This method will be used to instantiate a new Hello metaobject
00089     // when needed.  We can't use the constructor directly because
00090     // constructors are not normal functions or methods.
00091 
00092     static VOS::MetaObject* new_RemoteHello(VOS::VobjectBase* superobject, const std::string& type);
00093 };
00094 
00095 
00096 // This class implements Hello for local Vobjects.  This means that it
00097 // implements the hello() method and responds to incoming
00098 // "tutorial:hello" messages
00099 
00100 class LocalHello : public virtual Hello
00101 {
00102 protected:
00103 
00104     // There should only ever be one constructor, and it should take
00105     // one parameter.  An application never calls the constructor
00106     // directly, but rather will use Site::creatMetaObject().
00107 
00108     LocalHello(VOS::VobjectBase* superobject);
00109 
00110 public:
00111 
00112     // This implements the actual logic for answering a "hello".
00113 
00114     virtual std::string hello(const std::string& s);
00115 
00116 
00117     // This method is called when a "tutorial:hello" message is
00118     // delivered to this Vobject.  See vostut7hello.cc for details.
00119 
00120     void handleHello(VOS::Message* m);
00121 
00122     /* This method will be used to instantiate a new Hello metaobject
00123        when needed.  We can't use the constructor directly because
00124        constructors are not normal functions or methods.
00125     */
00126     static VOS::MetaObject* new_LocalHello(VOS::VobjectBase* superobject, const std::string& type);
00127 };
00128 
00129 #endif