xorp

transaction.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/transaction.hh,v 1.14 2008/10/02 21:57:36 bms Exp $
00023 
00024 #ifndef __LIBXORP_TRANSACTION_HH__
00025 #define __LIBXORP_TRANSACTION_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 #include "libxorp/eventloop.hh"
00029 #include "libxorp/ref_ptr.hh"
00030 
00031 
00032 
00033 
00034 
00047 class TransactionOperation {
00048 public:
00052     virtual ~TransactionOperation() {}
00053 
00059     virtual bool dispatch() = 0;
00060 
00064     virtual string str() const = 0;
00065 };
00066 
00075 class TransactionManager {
00076 public:
00077     typedef ref_ptr<TransactionOperation> Operation;
00078 
00092     TransactionManager(EventLoop& e,
00093                uint32_t timeout_ms = 0,
00094                uint32_t max_pending = 10) : 
00095         _e(e), _timeout_ms(timeout_ms), _max_pending(max_pending), _next_tid(0)
00096     {
00097     }
00098 
00102     virtual ~TransactionManager() {}
00103 
00112     bool start(uint32_t& new_tid);
00113 
00120     bool commit(uint32_t tid);
00121 
00128     bool abort(uint32_t tid);
00129 
00137     virtual bool add(uint32_t tid, const Operation& op);
00138 
00149     bool retrieve_size(uint32_t tid, uint32_t& count) const;
00150 
00161     uint32_t timeout_ms() const     { return _timeout_ms; }
00162 
00168     uint32_t max_pending() const { return _max_pending; }
00169 
00175     uint32_t pending() const    { return _transactions.size(); }
00176 
00177 protected:
00178 
00185     virtual void pre_commit(uint32_t tid);
00186 
00192     virtual void post_commit(uint32_t tid);
00193 
00204     virtual void operation_result(bool success, 
00205                   const TransactionOperation& op);
00206 
00217     bool flush(uint32_t tid);
00218     
00219 protected:
00226     class Transaction {
00227     public:
00228     typedef list<Operation> OperationList;
00229     
00230     Transaction(TransactionManager& mgr, const XorpTimer& timeout_timer)
00231         : _mgr(&mgr), _timeout_timer(timeout_timer), _op_count(0)
00232     {}
00233 
00234     Transaction(TransactionManager& mgr) : _mgr(&mgr), _op_count(0)
00235     {}
00236     Transaction() { _mgr = NULL; }
00237 
00238     Transaction& operator=(const Transaction& rhs) {
00239         _mgr = rhs._mgr;
00240         _ops = rhs._ops;
00241         _timeout_timer = rhs._timeout_timer;
00242         _op_count = rhs._op_count;
00243         return *this;
00244     }
00245 
00247     void add(const Operation& op);
00248 
00250     void commit();
00251 
00253     void flush();
00254     
00256     void defer_timeout();
00257 
00259     void cancel_timeout();
00260     
00262     OperationList& operations() { return _ops; }
00263 
00265     uint32_t size() const { return _op_count; }
00266     
00267     private:
00268     TransactionManager* _mgr;
00269     OperationList       _ops;
00270     XorpTimer       _timeout_timer;
00271     uint32_t        _op_count;
00272     };
00273 
00274 private:
00276     void timeout(uint32_t tid);
00277 
00279     void crank_tid();
00280 
00281 private:
00282     typedef map<uint32_t, Transaction> TransactionDB;
00283 
00284     EventLoop& _e;
00285     TransactionDB _transactions;
00286     uint32_t _timeout_ms;
00287     uint32_t _max_pending;
00288     uint32_t _next_tid;
00289     
00290     friend class Transaction; // for Transaction to call operation_result()
00291 };
00292 
00293 #endif // __LIBXORP_TRANSACTION_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations