Now you can download a copy of these docs so you can use them offline! Download now
NetworkQueue.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2011. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "NetworkTables/NetworkQueue.h" 00008 00009 #include "NetworkTables/Entry.h" 00010 #include "NetworkTables/Key.h" 00011 #include "NetworkTables/Confirmation.h" 00012 #include "NetworkTables/Denial.h" 00013 #include "NetworkTables/TransactionStart.h" 00014 #include "NetworkTables/TransactionEnd.h" 00015 #include "Synchronized.h" 00016 00017 namespace NetworkTables 00018 { 00019 00020 NetworkQueue::NetworkQueue() : 00021 m_dataLock(NULL), 00022 m_inTransaction(false) 00023 { 00024 m_dataLock = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE); 00025 } 00026 00027 NetworkQueue::~NetworkQueue() 00028 { 00029 semDelete(m_dataLock); 00030 } 00031 00032 void NetworkQueue::Offer(TransactionStart *value) 00033 { 00034 Synchronized sync(m_dataLock); 00035 m_inTransaction = true; 00036 m_dataQueue.push_back(DataQueue_t::value_type(value, false)); 00037 } 00038 00039 void NetworkQueue::Offer(TransactionEnd *value) 00040 { 00041 Synchronized sync(m_dataLock); 00042 m_inTransaction = false; 00043 m_dataQueue.push_back(DataQueue_t::value_type(value, false)); 00044 } 00045 00046 void NetworkQueue::Offer(Data *value, bool needsDelete) 00047 { 00048 Synchronized sync(m_dataLock); 00049 if (value->IsEntry()) 00050 { 00051 if (m_inTransaction) 00052 { 00053 m_dataQueue.push_back(DataQueue_t::value_type(value, needsDelete)); 00054 } 00055 else 00056 { 00057 DataHash_t::iterator found = m_latestDataHash.find(((Entry *)value)->GetId()); 00058 if (found != m_latestDataHash.end()) 00059 { 00060 // Replace the old value for this key with a new one 00061 // If this used to be auto_ptr'd, then delete it 00062 // (This should never happen, right?) 00063 if (found->second->second) 00064 delete found->second->first; 00065 found->second->first = value; 00066 } 00067 else 00068 { 00069 // Add a new entry to the queue 00070 m_dataQueue.push_back(DataQueue_t::value_type(value, needsDelete)); 00071 m_latestDataHash.insert(DataHash_t::value_type(((Entry *)m_dataQueue.back().first)->GetId(), m_dataQueue.end() - 1)); 00072 } 00073 } 00074 } 00075 else 00076 { 00077 // TODO: Combine Confirmations 00078 // TODO: Combine Denials 00079 m_dataQueue.push_back(DataQueue_t::value_type(value, needsDelete)); 00080 } 00081 } 00082 00083 void NetworkQueue::Offer(std::auto_ptr<Data> value) 00084 { 00085 // Indicate that we released this from an auto_ptr 00086 Offer(value.release(), true); 00087 } 00088 00089 bool NetworkQueue::IsEmpty() 00090 { 00091 Synchronized sync(m_dataLock); 00092 return m_dataQueue.empty(); 00093 } 00094 00095 bool NetworkQueue::ContainsKey(Key *key) 00096 { 00097 Synchronized sync(m_dataLock); 00098 DataHash_t::iterator found = m_latestDataHash.find(key->GetId()); 00099 return key != NULL && found != m_latestDataHash.end(); 00100 } 00101 00102 // @return the data and if it came from an auto_ptr 00103 std::pair<Data *, bool> NetworkQueue::Poll() 00104 { 00105 Synchronized sync(m_dataLock); 00106 if (IsEmpty()) 00107 return DataQueue_t::value_type(NULL, false); 00108 DataQueue_t::value_type data = m_dataQueue.front(); 00109 if (data.first->IsEntry()) 00110 { 00111 m_latestDataHash.erase(((Entry *)data.first)->GetId()); 00112 } 00113 m_dataQueue.pop_front(); 00114 return data; 00115 } 00116 00117 void NetworkQueue::Clear() 00118 { 00119 Synchronized sync(m_dataLock); 00120 m_dataQueue.clear(); 00121 m_latestDataHash.clear(); 00122 } 00123 00124 Data *NetworkQueue::Peek() 00125 { 00126 Synchronized sync(m_dataLock); 00127 if (IsEmpty()) 00128 return NULL; 00129 return m_dataQueue.front().first; 00130 } 00131 00132 } // namespace 00133
Generated on Thu Jan 12 2012 22:35:21 for WPILibC++ by
1.7.1