00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _RESOURCELISTS_H_
00031 #define _RESOURCELISTS_H_
00032
00033
00034
00035
00036 #include <list>
00037
00038
00039 #include "accessnumber.h"
00040 #include "e164resource.h"
00041 #include "mutex.h"
00042 #include "ipaddress.h"
00043 #include "osbid.h"
00044 #include "resource.h"
00045 #include "stringutil.h"
00046 #include "trunkresource.h"
00047
00048
00049
00050 namespace OSB_DB {
00051 class Session;
00052 }
00053
00054
00055
00056 namespace OSB_LIB {
00057
00058
00059
00060
00061
00062
00077 template<typename R>
00078 class ResourceList {
00079 public:
00081 typedef std::list<R> List;
00083 typedef ReadGuard<R*> Rg;
00085 typedef WriteGuard<R*> Wg;
00087 typedef RwGuard ListRg;
00089 typedef RwGuard ListWg;
00091 enum GuardStatus {
00093 success = 0,
00094 lockFailed = 1,
00095 objectNotFound = 2
00096 };
00097
00098 public:
00104 explicit ResourceList(bool useLocks);
00105
00106 public:
00113 List resources_;
00114
00115 public:
00117 bool useLocks() const;
00118
00132 R* find(
00133 const Id<Resource>& id,
00134 bool doThrow
00135 );
00136
00150 const R* find(
00151 const Id<Resource>& id,
00152 bool doThrow
00153 ) const;
00154
00156
00157
00170 Rg readLock(
00171 const Id<Resource>& id,
00172 bool doThrow
00173 ) const;
00187 Wg writeLock(
00188 const Id<Resource>& id,
00189 bool doThrow
00190 );
00197 ListRg readLock() const;
00204 ListWg writeLock();
00206
00218 bool insert(const R& r);
00228 bool replace(const R& r);
00235 bool replace(const ResourceList<R>& src);
00245 bool erase(const Id<Resource>& id);
00251 bool clear();
00252
00259 void sortByResNum(bool desc = false);
00260
00261 private:
00263 bool useLocks_;
00265 mutable RwMutex mutex_;
00266
00267 private:
00278 typename List::iterator findIter(const Id<Resource>& id);
00279
00288 void eraseUnlocked(const Id<Resource>& id);
00289
00295 void replaceUnlocked(const R& r);
00296
00304 static bool lessResNum(
00305 const R& lhs,
00306 const R& rhs
00307 );
00308
00316 static bool greaterResNum(
00317 const R& lhs,
00318 const R& rhs
00319 );
00320 };
00321
00322
00323
00327 class ResourceLists {
00328 public:
00330
00331
00332 typedef ResourceList<AccessNumber> AnList;
00334 typedef ResourceList<E164Resource> E164List;
00336 typedef ResourceList<Ip4Address> Ip4List;
00338 typedef ResourceList<IpGroup> IpGroupList;
00340 typedef ResourceList<TrunkResource> TrList;
00342
00343 public:
00352 explicit ResourceLists(bool useLocks);
00353
00354 public:
00356 AnList accessNumbers_;
00358 E164List e164s_;
00360 Ip4List ip4s_;
00362 IpGroupList ipGroups_;
00364 TrList trunkResources_;
00365
00367
00368
00384 static bool fetchAn(
00385 const OSB_DB::Session& session,
00386 AnList& dest,
00387 bool availOnly = false,
00388 const Id<Network>& nwId = Id<Network>()
00389 );
00403 bool fetchAn(
00404 const OSB_DB::Session& session,
00405 bool availOnly = false,
00406 const Id<Network>& nwId = Id<Network>()
00407 );
00409
00411
00412
00426 static bool fetchE164(
00427 const OSB_DB::Session& session,
00428 E164List& dest,
00429 Resource::Status status,
00430 const std::string& pattern,
00431 size_t maxAdd
00432 );
00449 bool fetchE164(
00450 const OSB_DB::Session& session,
00451 Resource::Status status,
00452 const std::string& pattern,
00453 size_t maxAdd
00454 );
00456
00458
00459
00475 static bool fetchIp4(
00476 const OSB_DB::Session& session,
00477 Ip4List& dest,
00478 bool availOnly = false,
00479 const Id<Network>& nwId = Id<Network>()
00480 );
00494 bool fetchIp4(
00495 const OSB_DB::Session& session,
00496 bool availOnly = false,
00497 const Id<Network>& nwId = Id<Network>()
00498 );
00506 void getAssignableIp4s(
00507 const OSB_DB::Session& session,
00508 AssignableIp4s& dest
00509 );
00511
00513
00514
00530 static bool fetchIpg(
00531 const OSB_DB::Session& session,
00532 IpGroupList& dest,
00533 bool availOnly = false,
00534 const Id<Network>& nwId = Id<Network>()
00535 );
00549 bool fetchIpg(
00550 const OSB_DB::Session& session,
00551 bool availOnly = false,
00552 const Id<Network>& nwId = Id<Network>()
00553 );
00555
00557
00558
00574 static bool fetchTr(
00575 const OSB_DB::Session& session,
00576 TrList& dest,
00577 bool availOnly = false,
00578 const Id<Network>& nwId = Id<Network>()
00579 );
00593 bool fetchTr(
00594 const OSB_DB::Session& session,
00595 bool availOnly = false,
00596 const Id<Network>& nwId = Id<Network>()
00597 );
00599 };
00600
00601
00602
00603
00604
00605
00606 template<typename R>
00607 ResourceList<R>::ResourceList(bool useLocks)
00608 : useLocks_(useLocks)
00609 {
00610
00611 }
00612
00613 template<typename R>
00614 bool ResourceList<R>::useLocks() const
00615 {
00616 return useLocks_;
00617 }
00618
00619 template<typename R>
00620 typename ResourceList<R>::List::iterator
00621 ResourceList<R>::findIter(const Id<Resource>& id)
00622 {
00623 typedef typename List::iterator Iter;
00624 bool found = false;
00625 Iter rc = resources_.begin();
00626 Iter e = resources_.end();
00627 while (!found && rc != e) {
00628 if (id == rc->oid()) found = true;
00629 else ++rc;
00630 }
00631 return rc;
00632 }
00633
00634 template<typename R>
00635 R* ResourceList<R>::find(
00636 const Id<Resource>& id,
00637 bool doThrow
00638 )
00639 {
00640 R* rc = 0;
00641 typename List::iterator i = findIter(id);
00642 if (i != resources_.end()) {
00643 rc = &(*i);
00644 }
00645 else if (doThrow) {
00646 throw DefException(712) << id;
00647 }
00648 return rc;
00649 }
00650
00651 template<typename R>
00652 const R* ResourceList<R>::find(
00653 const Id<Resource>& id,
00654 bool doThrow
00655 ) const
00656 {
00657 return const_cast<ResourceList*>(this)->find(id, doThrow);
00658 }
00659
00660 template<typename R>
00661 typename ResourceList<R>::Rg
00662 ResourceList<R>::readLock(
00663 const Id<Resource>& id,
00664 bool doThrow
00665 ) const
00666 {
00667
00668 RwMutex* mutex = 0;
00669 if (useLocks_) {
00670 if (0 != mutex_.readLock()) return Rg(lockFailed);
00671 mutex = &mutex_;
00672 }
00673
00674
00675 Rg rc(0, true, mutex);
00676
00677
00678 const R* ptr = find(id, doThrow);
00679
00680 if (0 != ptr) {
00681 rc = Rg(ptr, rc.release(), mutex);
00682 }
00683 else {
00684 rc.status_ = objectNotFound;
00685 rc.unlock();
00686 }
00687
00688 return rc;
00689 }
00690
00691 template<typename R>
00692 typename ResourceList<R>::Wg
00693 ResourceList<R>::writeLock(
00694 const Id<Resource>& id,
00695 bool doThrow
00696 )
00697 {
00698
00699 RwMutex* mutex = 0;
00700 if (useLocks_) {
00701 if (0 != mutex_.writeLock()) return Wg(lockFailed);
00702 mutex = &mutex_;
00703 }
00704
00705
00706 Wg rc(0, true, mutex);
00707
00708
00709 R* ptr = find(id, doThrow);
00710
00711 if (0 != ptr) {
00712 rc = Wg(ptr, rc.release(), mutex);
00713 }
00714 else {
00715 rc.status_ = objectNotFound;
00716 rc.unlock();
00717 }
00718
00719 return rc;
00720 }
00721
00722 template<typename R>
00723 typename ResourceList<R>::ListRg
00724 ResourceList<R>::readLock() const
00725 {
00726
00727 if (!useLocks_) return ListRg();
00728
00729
00730 if (useLocks_) {
00731 if (0 != mutex_.readLock()) return ListRg(false);
00732 }
00733
00734
00735 return ListRg(true, &mutex_, &RwMutex::readUnlock);
00736 }
00737
00738 template<typename R>
00739 typename ResourceList<R>::ListWg
00740 ResourceList<R>::writeLock()
00741 {
00742
00743 if (!useLocks_) return ListWg();
00744
00745
00746 if (useLocks_) {
00747 if (0 != mutex_.writeLock()) return ListWg(false);
00748 }
00749
00750
00751 return ListWg(true, &mutex_, &RwMutex::writeUnlock);
00752 }
00753
00754 template<typename R>
00755 bool ResourceList<R>::insert(const R& r)
00756 {
00757 ListWg guard = writeLock();
00758 if (!guard.locked()) return false;
00759 bool rc = false;
00760 if (0 == find(r.oid(), false)) {
00761 resources_.push_back(r);
00762 rc = true;
00763 }
00764 return rc;
00765 }
00766
00767 template<typename R>
00768 bool ResourceList<R>::replace(const ResourceList<R>& src)
00769 {
00770 typedef typename List::const_iterator Iter;
00771
00772 ListWg guard = writeLock();
00773 if (!guard.locked()) return false;
00774 Iter i = src.resources_.begin();
00775 Iter e = src.resources_.end();
00776 for (; i != e; ++i) replaceUnlocked(*i);
00777 return true;
00778 }
00779
00780 template<typename R>
00781 bool ResourceList<R>::replace(const R& r)
00782 {
00783 ListWg guard = writeLock();
00784 if (!guard.locked()) return false;
00785 replaceUnlocked(r);
00786 return true;
00787 }
00788
00789 template<typename R>
00790 void ResourceList<R>::replaceUnlocked(const R& r)
00791 {
00792 R* ptr = find(r.oid(), false);
00793 if (ptr != &r) {
00794 if (0 != ptr) eraseUnlocked(r.oid());
00795 resources_.push_back(r);
00796 }
00797 }
00798
00799 template<typename R>
00800 bool ResourceList<R>::erase(const Id<Resource>& id)
00801 {
00802 ListWg guard = writeLock();
00803 if (!guard.locked()) return false;
00804 eraseUnlocked(id);
00805 return true;
00806 }
00807
00808 template<typename R>
00809 void ResourceList<R>::eraseUnlocked(const Id<Resource>& id)
00810 {
00811 typename List::iterator i = findIter(id);
00812 if (i != resources_.end()) resources_.erase(i);
00813 }
00814
00815 template<typename R>
00816 bool ResourceList<R>::clear()
00817 {
00818 ListWg guard = writeLock();
00819 if (!guard.locked()) return false;
00820 resources_.clear();
00821 return true;
00822 }
00823
00824 template<typename R>
00825 bool ResourceList<R>::lessResNum(const R& lhs, const R& rhs)
00826 {
00827 const std::string& lname = lhs.resourceNumber();
00828 const std::string& rname = rhs.resourceNumber();
00829 return LessStr<>(true)(lname, rname);
00830 }
00831
00832 template<typename R>
00833 bool ResourceList<R>::greaterResNum(const R& lhs, const R& rhs)
00834 {
00835 const std::string& lname = lhs.resourceNumber();
00836 const std::string& rname = rhs.resourceNumber();
00837 return GreaterStr<>(true)(lname, rname);
00838 }
00839
00840 template<typename R>
00841 void ResourceList<R>::sortByResNum(bool desc)
00842 {
00843 if (desc) resources_.sort(greaterResNum);
00844 else resources_.sort(lessResNum);
00845 }
00846 }
00847 #endif // #ifndef _RESOURCELISTS_H_