aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/network/rpcservice.cpp
blob: 6e7c73b38eec3f4bb75fc23a28fac87265db858e (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "rpcservice.h"
#include "rpcnetwork.h"

namespace mbus {

RPCService::RPCService(const Mirror &mirror,
                       const string &pattern) :
    _mirror(mirror),
    _pattern(pattern),
    _addressIdx(random()),
    _addressGen(0),
    _addressList()
{ }

RPCService::~RPCService() {}

RPCServiceAddress::UP
RPCService::resolve()
{
    if (_pattern.find("tcp/") == 0) {
        size_t pos = _pattern.find_last_of('/');
        if (pos != string::npos && pos < _pattern.size() - 1) {
            RPCServiceAddress::UP ret(new RPCServiceAddress(
                            _pattern,
                            _pattern.substr(0, pos)));
            if (!ret->isMalformed()) {
                return ret;
            }
        }
    } else {
        if (_addressGen != _mirror.updates()) {
            _addressGen = _mirror.updates();
            _addressList = _mirror.lookup(_pattern);
        }
        if (!_addressList.empty()) {
            _addressIdx = (_addressIdx + 1) % _addressList.size();
            const AddressList::value_type &entry = _addressList[_addressIdx];
            return RPCServiceAddress::UP(new RPCServiceAddress(
                            entry.first,
                            entry.second));
        }
    }
    return RPCServiceAddress::UP();
}


} // namespace mbus