/
proxy.cc
60 lines (56 loc) · 1.94 KB
/
proxy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "common.h"
#include "blockproxy.h"
#include "video.h"
#include "filesystem.h"
#include "logging.h"
using namespace std;
class Proxy : public BlockProxy
{
private:
unordered_map<vIndex, size_t> requestedVideos;
protected:
virtual void initialize();
virtual void handleMessage(cMessage* msg);
public:
Proxy() : BlockProxy() {}
virtual ~Proxy() {}
};
Define_Module(Proxy);
void Proxy::initialize()
{
BlockProxy::initialize();
}
void Proxy::handleMessage(cMessage *msg)
{
if (!strcmp(msg->getName(), "teardown")) {
Paket *p = check_and_cast<Paket *>(msg);
vIndex i = p->getIndex();
vSegment s = p->getSeg() + 1;
while (cache.exists(VideoSegment(i,s))) ++s;
if (s < requestedVideos[i]) {
queryVideoSegment(i, s);
logging(LOG_QRY, addr, centralServerIP, i, s);
} else {
requestedVideos.erase(i);
}
delete p;
} else if (!strcmp(msg->getName(), "close")) {
Paket *p = check_and_cast<Paket *>(msg);
vIndex i = p->getIndex();
if (!FileSystem::instance().exists(addr, i) &&
!requestedVideos.count(i)) {
vSegment s = p->getSeg() + 1;
for (; cache.exists(VideoSegment(i,s)); ++s);
size_t m = min(cache.maxSize()/10,
VideoCollection::instance().getVideoSegment(i));
if (s < m) {
requestedVideos.emplace(i, m);
queryVideoSegment(i, s);
logging(LOG_QRY, addr, centralServerIP, i, s);
}
}
delete p;
} else {
BlockProxy::handleMessage(msg);
}
}