Sirikata
libcore/include/sirikata/core/transfer/TransferRequest.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 #ifndef _SIRIKATA_CORE_TRANSFER_REQUEST_HPP_
00006 #define _SIRIKATA_CORE_TRANSFER_REQUEST_HPP_
00007 
00008 #include <sirikata/core/util/Platform.hpp>
00009 #include <sirikata/core/transfer/Defs.hpp>
00010 #include <sirikata/core/transfer/TransferData.hpp>
00011 #include "RemoteFileMetadata.hpp"
00012 #include "URI.hpp"
00013 #include <sirikata/core/transfer/OAuthParams.hpp>
00014 
00015 namespace Sirikata {
00016 namespace Transfer {
00017 
00018 class TransferRequest;
00019 typedef std::tr1::shared_ptr<TransferRequest> TransferRequestPtr;
00020 
00021 /*
00022  * Base class for a request going into the transfer pool to set
00023  * the priority of a request
00024  */
00025 class SIRIKATA_EXPORT TransferRequest {
00026 
00027 public:
00028     typedef std::tr1::function<void()> ExecuteFinished;
00029 
00030     //Return the priority of the request
00031     inline Priority getPriority() const {
00032         return mPriority;
00033     }
00034 
00035     //Returns true if this request is for deletion
00036     inline bool isDeletionRequest() const {
00037         return mDeletionRequest;
00038     }
00039 
00046     virtual const std::string& getIdentifier() const = 0;
00047 
00048     inline const std::string& getClientID() const {
00049         return mClientID;
00050     }
00051 
00052     virtual void execute(std::tr1::shared_ptr<TransferRequest> req, ExecuteFinished cb) = 0;
00053 
00054     virtual void notifyCaller(TransferRequestPtr me, TransferRequestPtr from) = 0;
00055 
00056     virtual ~TransferRequest() {}
00057 
00058     friend class TransferPool;
00059 
00060 protected:
00061     inline void setClientID(const std::string& clientID) {
00062         mClientID = clientID;
00063     }
00064 
00065     //Change the priority of the request
00066     inline void setPriority(Priority p) {
00067         mPriority = p;
00068     }
00069 
00070     //Change the priority of the request
00071     inline void setDeletion() {
00072         mDeletionRequest = true;
00073     }
00074 
00075     Priority mPriority;
00076     std::string mClientID;
00077     bool mDeletionRequest;
00078 
00079 };
00080 
00081 class MetadataRequest;
00082 typedef std::tr1::shared_ptr<MetadataRequest> MetadataRequestPtr;
00083 /*
00084  * Handles requests for metadata of a file when all you have is the URI
00085  */
00086 class SIRIKATA_EXPORT MetadataRequest: public TransferRequest {
00087 
00088 public:
00089     typedef std::tr1::function<void(
00090             std::tr1::shared_ptr<MetadataRequest> request,
00091             std::tr1::shared_ptr<RemoteFileMetadata> response)> MetadataCallback;
00092 
00093     MetadataRequest(const URI &uri, Priority priority, MetadataCallback cb) :
00094      mURI(uri), mCallback(cb) {
00095         mPriority = priority;
00096         mDeletionRequest = false;
00097         mID = uri.toString();
00098     }
00099 
00100     inline const std::string &getIdentifier() const {
00101         return mID;
00102     }
00103 
00104     inline const URI& getURI() {
00105         return mURI;
00106     }
00107 
00108     void execute(std::tr1::shared_ptr<TransferRequest> req, ExecuteFinished cb);
00109 
00110     inline void notifyCaller(TransferRequestPtr me, TransferRequestPtr from) {
00111         std::tr1::shared_ptr<MetadataRequest> meC =
00112             std::tr1::static_pointer_cast<MetadataRequest, TransferRequest>(me);
00113         std::tr1::shared_ptr<MetadataRequest> fromC =
00114             std::tr1::static_pointer_cast<MetadataRequest, TransferRequest>(from);
00115         mCallback(meC, fromC->mRemoteFileMetadata);
00116     }
00117     inline void notifyCaller(MetadataRequestPtr me, TransferRequestPtr from, RemoteFileMetadataPtr data) {
00118         std::tr1::shared_ptr<MetadataRequest> fromC =
00119                 std::tr1::static_pointer_cast<MetadataRequest, TransferRequest>(from);
00120         mCallback(me, data);
00121     }
00122 
00123     inline bool operator==(const MetadataRequest& other) const {
00124         return mID == other.mID;
00125     }
00126     inline bool operator<(const MetadataRequest& other) const {
00127         return mID < other.mID;
00128     }
00129 
00130     virtual ~MetadataRequest() {}
00131 
00132 protected:
00133 
00134     const URI mURI;
00135     std::string mID;
00136     MetadataCallback mCallback;
00137     std::tr1::shared_ptr<RemoteFileMetadata> mRemoteFileMetadata;
00138 
00139     MetadataRequest(const URI &uri, Priority priority) :
00140         mURI(uri) {
00141         mPriority = priority;
00142         mDeletionRequest = false;
00143 
00144         mID = uri.toString();
00145     }
00146 
00147 
00148     inline void execute_finished(std::tr1::shared_ptr<RemoteFileMetadata> response, ExecuteFinished cb) {
00149         mRemoteFileMetadata = response;
00150         cb();
00151         SILOG(transfer, detailed, "done MetadataRequest execute_finished");
00152     }
00153 
00154 };
00155 
00156 /*
00157  * Handles requests for the data associated with a file:chunk pair
00158  */
00159 class SIRIKATA_EXPORT DirectChunkRequest : public TransferRequest {
00160 
00161 public:
00162     typedef std::tr1::function<void(
00163             std::tr1::shared_ptr<DirectChunkRequest> request,
00164             std::tr1::shared_ptr<const DenseData> response)> DirectChunkCallback;
00165 
00166     DirectChunkRequest(const Chunk &chunk, Priority priority, DirectChunkCallback cb)
00167             : mChunk(std::tr1::shared_ptr<Chunk>(new Chunk(chunk))),
00168             mCallback(cb)
00169             {
00170                 mPriority = priority;
00171                 mDeletionRequest = false;
00172                 mID = chunk.toString();
00173             }
00174 
00175     inline const Chunk& getChunk() {
00176         return *mChunk;
00177     }
00178 
00179     inline const std::string &getIdentifier() const {
00180         return mID;
00181     }
00182 
00183     void execute(std::tr1::shared_ptr<TransferRequest> req, ExecuteFinished cb);
00184 
00185     void execute_finished(std::tr1::shared_ptr<const DenseData> response, ExecuteFinished cb);
00186 
00187     void notifyCaller(TransferRequestPtr me, TransferRequestPtr from);
00188     void notifyCaller(TransferRequestPtr me, TransferRequestPtr from, DenseDataPtr data);
00189 
00190 protected:
00191     std::string mID;
00192     std::tr1::shared_ptr<Chunk> mChunk;
00193     DirectChunkCallback mCallback;
00194     std::tr1::shared_ptr<const DenseData> mDenseData;
00195 };
00196 
00197 typedef std::tr1::shared_ptr<DirectChunkRequest> DirectChunkRequestPtr;
00198 
00199 /*
00200  * Handles requests for the data associated with a file:chunk pair
00201  */
00202 class SIRIKATA_EXPORT ChunkRequest : public MetadataRequest {
00203 
00204 public:
00205     typedef std::tr1::function<void(
00206             std::tr1::shared_ptr<ChunkRequest> request,
00207             std::tr1::shared_ptr<const DenseData> response)> ChunkCallback;
00208 
00209     ChunkRequest(const URI &uri, const RemoteFileMetadata &metadata, const Chunk &chunk,
00210             Priority priority, ChunkCallback cb)
00211             : MetadataRequest(uri, priority),
00212             mMetadata(std::tr1::shared_ptr<RemoteFileMetadata>(new RemoteFileMetadata(metadata))),
00213             mChunk(std::tr1::shared_ptr<Chunk>(new Chunk(chunk))),
00214             mCallback(cb)
00215             {
00216                 mDeletionRequest = false;
00217                 mID = chunk.getHash().toString();
00218             }
00219 
00220     inline const RemoteFileMetadata& getMetadata() {
00221         return *mMetadata;
00222     }
00223 
00224     inline const Chunk& getChunk() {
00225         return *mChunk;
00226     }
00227 
00228     void execute(std::tr1::shared_ptr<TransferRequest> req, ExecuteFinished cb);
00229 
00230     void execute_finished(std::tr1::shared_ptr<const DenseData> response, ExecuteFinished cb);
00231 
00232     void notifyCaller(TransferRequestPtr me, TransferRequestPtr from);
00233     void notifyCaller(TransferRequestPtr me, TransferRequestPtr from, DenseDataPtr data);
00234 
00235 protected:
00236     std::tr1::shared_ptr<RemoteFileMetadata> mMetadata;
00237     std::tr1::shared_ptr<Chunk> mChunk;
00238     std::tr1::shared_ptr<const DenseData> mDenseData;
00239     ChunkCallback mCallback;
00240 };
00241 
00242 typedef std::tr1::shared_ptr<ChunkRequest> ChunkRequestPtr;
00243 
00244 
00245 
00246 
00247 
00248 class UploadRequest;
00249 typedef std::tr1::shared_ptr<UploadRequest> UploadRequestPtr;
00253 class SIRIKATA_EXPORT UploadRequest : public TransferRequest {
00254   public:
00255     typedef std::map<String, String> StringMap;
00256 
00257     typedef std::tr1::function<void(
00258         UploadRequestPtr request,
00259         const Transfer::URI& path)> UploadCallback;
00260 
00261     UploadRequest(OAuthParamsPtr oauth,
00262         const StringMap& files, const String& path, const StringMap& params,
00263         Priority priority,
00264         UploadCallback cb)
00265         : mOAuth(oauth),
00266           mFiles(files),
00267           mPath(path),
00268           mParams(params),
00269           mCB(cb)
00270     {
00271     }
00272     virtual ~UploadRequest() {}
00273 
00274     // TransferRequest Interface
00275     virtual const std::string& getIdentifier() const;
00276     virtual void execute(TransferRequestPtr req, ExecuteFinished cb);
00277     virtual void notifyCaller(TransferRequestPtr me, TransferRequestPtr from);
00278 
00279     OAuthParamsPtr oauth() { return mOAuth; }
00280     const StringMap& files() { return mFiles; }
00281     const String& path() { return mPath; }
00282     const StringMap& params() { return mParams; }
00283 
00284   private:
00285 
00286     void execute_finished(Transfer::URI uploaded_path, ExecuteFinished cb);
00287 
00288     const OAuthParamsPtr mOAuth;
00289     const StringMap mFiles; // Filename -> data
00290     const String mPath; // Requested location of asset,
00291                         // e.g. apiupload/filename results in
00292                         // /username/apiupload/filename
00293     const StringMap mParams; // Optional params, e.g. title = 'Building'
00294 
00295     // The URI the file was actually uploaded to, or empty if it failed
00296     Transfer::URI mUploadedPath;
00297 
00298     UploadCallback mCB;
00299 };
00300 
00301 } // namespace Transfer
00302 } // namespace Sirikata
00303 
00304 #endif //_SIRIKATA_CORE_TRANSFER_REQUEST_HPP_