summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/AdapterCache.cpp
blob: 6622ab6c182baf37237535b22ee4095d9a509e7a (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceGrid/AdapterCache.h>
#include <IceGrid/NodeSessionI.h>
#include <IceGrid/ServerCache.h>

using namespace std;
using namespace IceGrid;

AdapterEntryPtr
AdapterCache::get(const string& id, bool create) const
{
    Lock sync(*this);
    AdapterCache& self = const_cast<AdapterCache&>(*this);
    AdapterEntryPtr entry = self.getImpl(id, create);
    if(!entry)
    {
	AdapterNotExistException ex;
	ex.id = id;
	throw ex;
    }
    return entry;
}

AdapterEntry::AdapterEntry(Cache<string, AdapterEntry>& cache, const std::string& id) : 
    _cache(cache),
    _id(id),
    _replicated(false)
{
}

void
AdapterEntry::enableReplication(LoadBalancingPolicy policy)
{
    Lock sync(*this);
    _replicated = true;
    _loadBalancing = policy;
}

void
AdapterEntry::disableReplication()
{
    _replicated = false;
}

void
AdapterEntry::addServer(const ServerEntryPtr& entry)
{
    Lock sync(*this);
    assert(_replicated || _servers.empty());
    _servers.insert(make_pair(entry->getName(), entry));
}

void
AdapterEntry::removeServer(const ServerEntryPtr& entry)
{
    bool remove = false;
    {
	Lock sync(*this);
	_servers.erase(entry->getName());
	remove = _servers.empty();
    }
    if(remove)
    {
	_cache.remove(_id);
    }    
}

AdapterPrx
AdapterEntry::getProxy(const string& serverId) const
{
    Lock sync(*this);
    if(!_replicated)
    {
	return _servers.begin()->second->getAdapter(_id);
    }
    else
    {
	//
	// TODO: implement load balancing strategies. This is also not really correct to use the first adapter 
	// if the server id is empty this could  allow a server to always override the first server endpoints.
	//
	map<string, ServerEntryPtr>::const_iterator p = serverId.empty() ? _servers.begin() : _servers.find(serverId);
	if(p == _servers.end())
	{
	    AdapterNotExistException ex;
	    ex.id = _id;
	    throw ex;
	}
	return p->second->getAdapter(_id);
    }
}

bool
AdapterEntry::canRemove()
{
    Lock sync(*this);
    return _servers.empty();
}