/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/client/Future.h

00001 /*
00002  *
00003  * Licensed to the Apache Software Foundation (ASF) under one
00004  * or more contributor license agreements.  See the NOTICE file
00005  * distributed with this work for additional information
00006  * regarding copyright ownership.  The ASF licenses this file
00007  * to you under the Apache License, Version 2.0 (the
00008  * "License"); you may not use this file except in compliance
00009  * with the License.  You may obtain a copy of the License at
00010  * 
00011  *   http://www.apache.org/licenses/LICENSE-2.0
00012  * 
00013  * Unless required by applicable law or agreed to in writing,
00014  * software distributed under the License is distributed on an
00015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00016  * KIND, either express or implied.  See the License for the
00017  * specific language governing permissions and limitations
00018  * under the License.
00019  *
00020  */
00021 
00022 #ifndef _Future_
00023 #define _Future_
00024 
00025 #include <boost/bind.hpp>
00026 #include <boost/shared_ptr.hpp>
00027 #include "qpid/Exception.h"
00028 #include "qpid/framing/SequenceNumber.h"
00029 #include "qpid/framing/StructHelper.h"
00030 #include "FutureCompletion.h"
00031 #include "FutureResponse.h"
00032 #include "FutureResult.h"
00033 #include "SessionCore.h"
00034 
00035 namespace qpid {
00036 namespace client {
00037 
00038 class Future : private framing::StructHelper
00039 {
00040     framing::SequenceNumber command;
00041     boost::shared_ptr<FutureResponse> response;
00042     boost::shared_ptr<FutureResult> result;
00043     bool complete;
00044 
00045 public:
00046     Future() : complete(false) {}    
00047     Future(const framing::SequenceNumber& id) : command(id), complete(false) {}    
00048 
00049     void sync(SessionCore& session)
00050     {
00051         if (!isComplete(session)) {
00052             session.getExecution().syncTo(command);
00053             wait(session);
00054         }
00055     }
00056 
00057     void wait(SessionCore& session)
00058     {
00059         if (!isComplete(session)) {
00060             FutureCompletion callback;
00061             session.getExecution().getCompletionTracker().listenForCompletion(
00062                 command,                                                     
00063                 boost::bind(&FutureCompletion::completed, &callback)
00064             );
00065             callback.waitForCompletion();
00066             session.assertOpen();
00067             complete = true;
00068         }
00069     }
00070 
00071     framing::AMQMethodBody* getResponse(SessionCore& session) 
00072     {
00073         if (response) {
00074             session.getExecution().getCompletionTracker().listenForCompletion(
00075                 command,                                                     
00076                 boost::bind(&FutureResponse::completed, response)
00077             );            
00078             return response->getResponse(session);
00079         } else {
00080             throw Exception("Response not expected");
00081         }
00082     }
00083 
00084     template <class T> void decodeResult(T& value, SessionCore& session) 
00085     {
00086         if (result) {
00087             decode(value, result->getResult(session));
00088         } else {
00089             throw Exception("Result not expected");
00090         }
00091     }
00092 
00093     bool isComplete(SessionCore& session) {
00094         return complete || session.getExecution().isComplete(command);
00095     }
00096 
00097     bool isCompleteUpTo(SessionCore& session) {
00098         return complete || session.getExecution().isCompleteUpTo(command);
00099     }
00100 
00101     void setCommandId(const framing::SequenceNumber& id) { command = id; }
00102     void setFutureResponse(boost::shared_ptr<FutureResponse> r) { response = r; }
00103     void setFutureResult(boost::shared_ptr<FutureResult> r) { result = r; }
00104 };
00105 
00106 }}
00107 
00108 #endif

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7