Sirikata
liboh/plugins/js/JSObjectScriptManager.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  JSObjectScriptManager.hpp
00003  *
00004  *  Copyright (c) 2010, Ewen Cheslack-Postava
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  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
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _SIRIKATA_JS_OBJECT_SCRIPT_MANAGER_HPP_
00034 #define _SIRIKATA_JS_OBJECT_SCRIPT_MANAGER_HPP_
00035 
00036 #include "Platform.hpp"
00037 #include <sirikata/oh/ObjectScriptManager.hpp>
00038 #include <sirikata/core/options/Options.hpp>
00039 #include <sirikata/core/transfer/TransferMediator.hpp>
00040 #include <sirikata/core/transfer/ResourceDownloadTask.hpp>
00041 #include <sirikata/mesh/ModelsSystem.hpp>
00042 #include <sirikata/mesh/Filter.hpp>
00043 #include <sirikata/mesh/Visual.hpp>
00044 
00045 #include <v8.h>
00046 
00047 #define JS_SCRIPTS_DIR "js/scripts"
00048 #define JS_PLUGINS_DIR "liboh/plugins"
00049 
00050 namespace Sirikata {
00051 namespace JS {
00052 
00053 class JSObjectScript;
00054 class JSCtx;
00055 class SIRIKATA_SCRIPTING_JS_EXPORT JSObjectScriptManager : public ObjectScriptManager {
00056 public:
00057     static ObjectScriptManager* createObjectScriptManager(ObjectHostContext* ctx, const Sirikata::String& arguments);
00058 
00059     JSObjectScriptManager(ObjectHostContext* ctx, const Sirikata::String& arguments);
00060     virtual ~JSObjectScriptManager();
00061 
00062     virtual ObjectScript* createObjectScript(HostedObjectPtr ho, const String& args, const String& script);
00063     virtual void destroyObjectScript(ObjectScript* toDestroy);
00064 
00065     JSObjectScript* createHeadless(const String& args, const String& script,int32 maxres);
00066 
00067     OptionSet* getOptions() const { return mOptions; }
00068 
00069 
00070 
00071 
00072     // Mesh loading functions
00073     typedef std::tr1::function<void(Mesh::VisualPtr)> MeshLoadCallback;
00074     void loadMesh(const Transfer::URI& uri, MeshLoadCallback cb);
00075 
00076 private:
00077     ObjectHostContext* mContext;
00078     
00079     void createVisibleTemplate(JSCtx*);
00080     void createPresenceTemplate(JSCtx*);
00081     void createContextTemplate(JSCtx*);
00082     void createUtilTemplate(JSCtx*);
00083     void createJSInvokableObjectTemplate(JSCtx*);
00084     void createSystemTemplate(JSCtx*);
00085     void createTimerTemplate(JSCtx*);
00086     void createContextGlobalTemplate(JSCtx*);
00087     JSCtx* createJSCtx(HostedObjectPtr);
00088 
00089 
00090     OptionSet* mOptions;
00091 
00092     // The manager also maintains mesh data. We store it here so it is easily
00093     // shared by all the scripts, particularly important because mesh data is so
00094     // costly memory-wise.
00095     // FIXME this should be more complicated -- perhaps tracking shared_ptr's for awhile
00096     // or just maintaining an LRU.
00097     typedef std::tr1::unordered_map<Transfer::URI, Mesh::VisualWPtr, Transfer::URI::Hasher> MeshCache;
00098     MeshCache mMeshCache;
00099     // Some additional information is needed to keep track of in-progress
00100     // downloads. Note that these are only ever modified in the main thread
00101     typedef std::tr1::unordered_map<Transfer::URI, Transfer::ResourceDownloadTaskPtr, Transfer::URI::Hasher> MeshDownloads;
00102     MeshDownloads mMeshDownloads;
00103     typedef std::vector<MeshLoadCallback> MeshLoadCallbackList;
00104     typedef std::tr1::unordered_map<Transfer::URI, MeshLoadCallbackList, Transfer::URI::Hasher> WaitingMeshCallbacks;
00105     WaitingMeshCallbacks mMeshCallbacks;
00106 
00107     Transfer::TransferPoolPtr mTransferPool;
00108     // FIXME because we don't have proper multithreaded support in cppoh, we
00109     // need to allocate our own thread dedicated to parsing
00110     Network::IOService* mParsingIOService;
00111     Network::IOWork* mParsingWork;
00112     Thread* mParsingThread;
00113 
00114     ModelsSystem* mModelParser;
00115     Mesh::Filter* mModelFilter;
00116 
00117     void meshDownloaded(Transfer::ResourceDownloadTaskPtr taskptr, Transfer::TransferRequestPtr request, Transfer::DenseDataPtr data);
00118     void parseMeshWork(const Transfer::RemoteFileMetadata& metadata, const Transfer::Fingerprint& fp, Transfer::DenseDataPtr data);
00119     void meshParsed();
00120     void finishMeshDownload(const Transfer::URI& uri, Mesh::VisualPtr mesh);
00121 
00122 };
00123 
00124 } // namespace JS
00125 } // namespace Sirikata
00126 
00127 #endif //_SIRIKATA_JS_OBJECT_SCRIPT_MANAGER_HPP_