Main Page · Modules · All Classes · Class Hierarchy
MAPacketCache.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MAPacketCache.hpp"
23 
24 #include <MCBinaryData.hpp>
25 #include <MCLog.hpp>
26 
27 MAPacketCache::MAPacketCache(unsigned int storage_size_limit) : CurrentStorageSize(0),
28  StorageSizeLimit(storage_size_limit < 100000 ? 100000 : storage_size_limit)
29 {
30 }
31 
32 
33 MAPacketCache::~MAPacketCache()
34 {
35  Release();
36 }
37 
38 
39 void MAPacketCache::AddPacket(int index, MCBinaryData& packet)
40 {
41  const auto Iter = Packets.find(index);
42 
43  if (Iter != Packets.end())
44  {
45  CurrentStorageSize -= Iter->second->GetSize();
46  delete Iter->second;
47  Iter->second = &packet;
48  } else {
49  Packets[index] = &packet;
50  }
51  CurrentStorageSize += packet.GetSize();
52  // Note: It is a race condition to add a new item with the lowest index and
53  // delete it instantly if the storage is full. -> The packet IDs must increase and
54  // not decrease so it is not a real case.
55  while (CurrentStorageSize > StorageSizeLimit)
56  {
57  auto Iter2 = Packets.begin();
58 
59  CurrentStorageSize -= Iter2->second->GetSize();
60  delete Iter2->second;
61  Packets.erase(Iter2);
62  }
63  Add(index, packet);
64 }
65 
66 
67 MCBinaryData* MAPacketCache::GetPacket(int index)
68 {
69  if (index < Packets.begin()->first || index > Packets.rbegin()->first)
70  {
71  MC_WARNING("Packet index out of range (%d -> min. %d max. %d)", index, Packets.begin()->first,
72  Packets.rbegin()->first);
73  return nullptr;
74  }
75  return Packets[index];
76 }
77 
78 
79 int MAPacketCache::GetCurrentStorageSize() const
80 {
81  return CurrentStorageSize;
82 }
83 
84 
85 bool MAPacketCache::UploadPacket(int index)
86 {
87  MCBinaryData* Packet = GetPacket(index);
88 
89 // MC_LOG("Upload - id: %d size: %d\n", index, Packet->GetSize());
90  if (!Packet)
91  {
92  return false;
93  }
94  return Upload(index, *Packet);
95 }
96 
97 
98 void MAPacketCache::Release()
99 {
100  for (auto& packet : Packets)
101  {
102  delete packet.second;
103  }
104  Packets.clear();
105  CurrentStorageSize = 0;
106 }
Binary data class.
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
Indexer< T > index(T &t)
Iterator magic for for loops to get the index while iterating through.
Definition: MCDefs.hpp:146
int GetSize() const
Get binary data size.