srv-object.cpp

Go to the documentation of this file.
00001 /*
00002  * srv-object.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * Helpful routines for managing and communicating physics state back down
00031  * to the client.  See srv-physics.h
00032  */
00033 
00034 // includes -------------------------------------------------------------------
00035 #include "srv-object.h"         // always include our own header first!
00036 
00037 
00038 
00039 namespace aesop {
00040 
00041 // interface destructors
00042 ServerObject::~ServerObject(void) throw() { }
00043 
00044 
00045 // our component name
00046 static const char * s_cName                     = "_server";
00047 
00048 
00049 ////////////////////////////////////////////////////////////////////////////////
00050 //
00051 //      static helper methods
00052 //
00053 ////////////////////////////////////////////////////////////////////////////////
00054 
00055 
00056 ////////////////////////////////////////////////////////////////////////////////
00057 //
00058 //      SrvObj -- class that implements the ServerObject interface
00059 //
00060 ////////////////////////////////////////////////////////////////////////////////
00061 
00062 class SrvObj : public ServerObject {
00063 public:
00064         // destructor ----------------------------------------------------------
00065         ~SrvObj(void) throw() { }
00066 
00067         // aesop::ComponentData class interface methods ------------------------
00068         void dump(void) const throw();
00069 
00070         // aesop::ServerObject class interface methods -------------------------
00071         void setAnimationState(IN conn_id_t ownerId,
00072                                 IN const char * state);
00073         const char * getAnimationState(IN conn_id_t recipient) const;
00074 
00075         // factory methods -----------------------------------------------------
00076         static smart_ptr<ServerObject> create(void);
00077 
00078 private:
00079         // constructor ---------------------------------------------------------
00080         SrvObj(void) throw() { }
00081 
00082         // private helper methods ----------------------------------------------
00083         void initialize(void);
00084 
00085         // private member data -------------------------------------------------
00086         std::string             m_animState;
00087         conn_id_t               m_animOwner;
00088 };
00089 
00090 
00091 
00092 ////////////////////////////////////////////////////////////////////////////////
00093 //
00094 //      SrvObj -- aesop::ComponentData class interface methods
00095 //
00096 ////////////////////////////////////////////////////////////////////////////////
00097 
00098 ////////////////////////////////////////////////////////////////////////////////
00099 //
00100 //      SrvObj -- aesop::ServerObject class interface methods
00101 //
00102 ////////////////////////////////////////////////////////////////////////////////
00103 
00104 void
00105 SrvObj::setAnimationState
00106 (
00107 IN conn_id_t ownerId,
00108 IN const char * state
00109 )
00110 {
00111         // ASSERT(ownerId) -- can be null
00112         ASSERT(state, "can be empty but not null");
00113 
00114         m_animOwner = ownerId;
00115         m_animState = state;
00116 }
00117 
00118 
00119 
00120 const char *
00121 SrvObj::getAnimationState
00122 (
00123 IN conn_id_t recipient
00124 )
00125 const
00126 {
00127         if (recipient == m_animOwner)
00128                 return NULL;    // owner doesn't get their own state back
00129         return m_animState.c_str();
00130 }
00131 
00132 
00133 
00134 ////////////////////////////////////////////////////////////////////////////////
00135 //
00136 //      SrvObj -- public static methods (factories)
00137 //
00138 ////////////////////////////////////////////////////////////////////////////////
00139 
00140 smart_ptr<ServerObject>
00141 SrvObj::create
00142 (
00143 void
00144 )
00145 {
00146         smart_ptr<SrvObj> local = new SrvObj;
00147         ASSERT(local, "out of memory");
00148 
00149         local->initialize();
00150 
00151         return local;
00152 }
00153 
00154 
00155 
00156 ////////////////////////////////////////////////////////////////////////////////
00157 //
00158 //      SrvObj -- private helper methods
00159 //
00160 ////////////////////////////////////////////////////////////////////////////////
00161 
00162 void
00163 SrvObj::initialize
00164 (
00165 void
00166 )
00167 {
00168         m_animOwner = 0;
00169 }
00170 
00171 
00172 
00173 ////////////////////////////////////////////////////////////////////////////////
00174 //
00175 //      public API
00176 //
00177 ////////////////////////////////////////////////////////////////////////////////
00178 
00179 smart_ptr<ServerObject>
00180 getServerObject
00181 (
00182 IN smart_ptr<Instance>& instance
00183 )
00184 {
00185         ASSERT(instance, "null");
00186 
00187         smart_ptr<ComponentData> cd = instance->getComponentData(s_cName);
00188         if (cd) {
00189                 smart_ptr<ServerObject> so = cd;
00190                 ASSERT_THROW(so, "server component data cannot be upcast?");
00191                 return so;
00192         }
00193 
00194         // no component data registered yet!
00195         DPRINTF("Creating server component data on the fly...");
00196         smart_ptr<ServerObject> so = SrvObj::create();
00197         ASSERT(so, "null");
00198 
00199         cd = so;
00200         ASSERT(cd, "failed to downcast?");
00201         instance->setComponentData(s_cName, cd);
00202         return so;
00203 }
00204 
00205 
00206 
00207 };      // aesop namespace
00208